<commit_before>/*
 * Copyright (C) 2019 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
import glob from 'glob'
import gql from 'graphql-tag'

import {Assignment} from './graphqlData/Assignment'
import {Submission} from './graphqlData/Submission'

import mockGraphqlQuery from '../../shared/graphql_query_mock'

// Dynamically load and cache all of the `DefaultMocks` defined in `./graphqlData/*.js`
let _dynamicDefaultMockImports = null
async function loadDefaultMocks() {
  if (_dynamicDefaultMockImports !== null) {
    return _dynamicDefaultMockImports
  }

  const filesToImport = glob.sync('./graphqlData/**.js', {cwd: './app/jsx/assignments_2/student'})
  const defaultMocks = await Promise.all(
    filesToImport.map(async file => {
      const fileImport = await import(file)
      return fileImport.DefaultMocks || {}
    })
  )
  _dynamicDefaultMockImports = defaultMocks.filter(m => m !== undefined)
  return _dynamicDefaultMockImports
}

// Small wrapper around mockGraphqlQuery which includes our default overrides
export async function mockQuery(queryAST, overrides = [], variables = {}) {
  if (!Array.isArray(overrides)) {
    overrides = [overrides]
  }
  const defaultOverrides = await loadDefaultMocks()
  const allOverrides = [...defaultOverrides, ...overrides]
  return mockGraphqlQuery(queryAST, allOverrides, variables)
}

export async function mockAssignment(overrides = []) {
  const query = gql`
    query AssignmentQuery {
      assignment(id: "1") {
        ...Assignment
      }
    }
    ${Assignment.fragment}
  `
  const result = await mockQuery(query, overrides)
  const assignment = result.data.assignment

  // TODO: Move env out of assignment and into react context.
  assignment.env = {
    assignmentUrl: 'mocked-assignment-url',
    courseId: '1',
    currentUser: {id: '1', display_name: 'bob', avatar_url: 'awesome.avatar.url'},
    modulePrereq: null,
    moduleUrl: 'mocked-module-url'
  }
  return assignment
}

export async function mockSubmission(overrides = []) {
  const query = gql`
    query SubmissionQuery($submissionID: ID!) {
      node(id: "1") {
        ... on Submission {
          ...Submission
        }
      }
    }
    ${Submission.fragment}
  `
  if (!Array.isArray(overrides)) {
    overrides = [overrides]
  }
  overrides.push({Node: () => ({__typename: 'Submission'})})
  const result = await mockQuery(query, overrides, {submissionID: '1'})
  return result.data.node
}

export async function mockAssignmentAndSubmission(overrides = []) {
  const result = await Promise.all([mockAssignment(overrides), mockSubmission(overrides)])
  return {
    assignment: result[0],
    submission: result[1]
  }
}
<commit_msg>Cleanup mocks.js<commit_after>/*
 * Copyright (C) 2019 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
import glob from 'glob'
import gql from 'graphql-tag'

import {Assignment} from './graphqlData/Assignment'
import {Submission} from './graphqlData/Submission'

import mockGraphqlQuery from '../../shared/graphql_query_mock'

// Dynamically load and cache all of the `DefaultMocks` defined in `./graphqlData/*.js`
let _dynamicDefaultMockImports = null
async function loadDefaultMocks() {
  if (_dynamicDefaultMockImports !== null) {
    return _dynamicDefaultMockImports
  }

  const filesToImport = glob.sync('./graphqlData/**.js', {cwd: './app/jsx/assignments_2/student'})
  const defaultMocks = await Promise.all(
    filesToImport.map(async file => {
      const fileImport = await import(file)
      return fileImport.DefaultMocks || {}
    })
  )
  _dynamicDefaultMockImports = defaultMocks.filter(m => m !== undefined)
  return _dynamicDefaultMockImports
}

const SUBMISSION_QUERY = gql`
  query SubmissionQuery($submissionID: ID!) {
    submission(id: "1") {
      ...Submission
    }
  }
  ${Submission.fragment}
`

const ASSIGNMENT_QUERY = gql`
  query AssignmentQuery {
    assignment(id: "1") {
      ...Assignment
    }
  }
  ${Assignment.fragment}
`

// Small wrapper around mockGraphqlQuery which includes our default overrides
export async function mockQuery(queryAST, overrides = [], variables = {}) {
  if (!Array.isArray(overrides)) {
    overrides = [overrides]
  }
  const defaultOverrides = await loadDefaultMocks()
  const allOverrides = [...defaultOverrides, ...overrides]
  return mockGraphqlQuery(queryAST, allOverrides, variables)
}

export async function mockAssignment(overrides = []) {
  const result = await mockQuery(ASSIGNMENT_QUERY, overrides)
  const assignment = result.data.assignment

  // TODO: Move env out of assignment and into react context.
  assignment.env = {
    assignmentUrl: 'mocked-assignment-url',
    courseId: '1',
    currentUser: {id: '1', display_name: 'bob', avatar_url: 'awesome.avatar.url'},
    modulePrereq: null,
    moduleUrl: 'mocked-module-url'
  }
  return assignment
}

export async function mockSubmission(overrides = []) {
  const result = await mockQuery(SUBMISSION_QUERY, overrides, {submissionID: '1'})
  return result.data.submission
}

export async function mockAssignmentAndSubmission(overrides = []) {
  const result = await Promise.all([mockAssignment(overrides), mockSubmission(overrides)])
  return {
    assignment: result[0],
    submission: result[1]
  }
}

{'fraction_non_alphanumeric': 0.10937741387301096, 'fraction_numerical': 0.004016684690251815, 'mean_word_length': 3.8860377358490568, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2962582', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1943, 'n_words': 584}