From 49449b90be742ab1a7c0e3d9901e357ac016b02a Mon Sep 17 00:00:00 2001 From: laurejablonski Date: Sun, 27 Apr 2025 12:42:10 +0200 Subject: [PATCH] test: questions data --- .../tests/data/questions/questions.test.ts | 85 +++++++++++-------- .../questions/questions.testdata.ts | 1 + 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/backend/tests/data/questions/questions.test.ts b/backend/tests/data/questions/questions.test.ts index 8ad2d47c..b3392860 100644 --- a/backend/tests/data/questions/questions.test.ts +++ b/backend/tests/data/questions/questions.test.ts @@ -1,45 +1,51 @@ import { beforeAll, describe, expect, it } from 'vitest'; import { setupTestApp } from '../../setup-tests'; import { QuestionRepository } from '../../../src/data/questions/question-repository'; -import { - getAssignmentRepository, - getClassRepository, - getGroupRepository, - getQuestionRepository, - getStudentRepository, -} from '../../../src/data/repositories'; -import { StudentRepository } from '../../../src/data/users/student-repository'; +import { getQuestionRepository } from '../../../src/data/repositories'; import { LearningObjectIdentifier } from '../../../src/entities/content/learning-object-identifier'; -import { Language } from '@dwengo-1/common/util/language'; import { Question } from '../../../src/entities/questions/question.entity'; -import { Class } from '../../../src/entities/classes/class.entity'; -import { Assignment } from '../../../src/entities/assignments/assignment.entity'; +import { testLearningObject03, testLearningObject05 } from '../../test_assets/content/learning-objects.testdata'; +import { getQuestion01, getQuestion02, getQuestion03, getQuestion05, getQuestion06 } from '../../test_assets/questions/questions.testdata'; +import { getNoordkaap, getTool } from '../../test_assets/users/students.testdata'; +import { getAssignment01 } from '../../test_assets/assignments/assignments.testdata'; +import { getTestGroup01 } from '../../test_assets/assignments/groups.testdata'; describe('QuestionRepository', () => { let questionRepository: QuestionRepository; - let studentRepository: StudentRepository; beforeAll(async () => { await setupTestApp(); questionRepository = getQuestionRepository(); - studentRepository = getStudentRepository(); }); it('should return all questions part of the given learning object', async () => { - const id = new LearningObjectIdentifier('id05', Language.English, 1); - const questions = await questionRepository.findAllQuestionsAboutLearningObject(id); + const q1 = getQuestion01(); + const q2 = getQuestion02(); + const q3 = getQuestion05(); + const q4 = getQuestion06(); + const loid = { + hruid: q1.learningObjectHruid, + language: q1.learningObjectLanguage, + version: q1.learningObjectVersion, + } as LearningObjectIdentifier; + const questions = await questionRepository.findAllQuestionsAboutLearningObject(loid); expect(questions).toBeTruthy(); expect(questions).toHaveLength(4); + expect(questions[0].sequenceNumber!).toBeOneOf([q1.sequenceNumber, q2.sequenceNumber, q3.sequenceNumber, q4.sequenceNumber]); + expect(questions[1].sequenceNumber!).toBeOneOf([q1.sequenceNumber, q2.sequenceNumber, q3.sequenceNumber, q4.sequenceNumber]); + expect(questions[2].sequenceNumber!).toBeOneOf([q1.sequenceNumber, q2.sequenceNumber, q3.sequenceNumber, q4.sequenceNumber]); + expect(questions[3].sequenceNumber!).toBeOneOf([q1.sequenceNumber, q2.sequenceNumber, q3.sequenceNumber, q4.sequenceNumber]); }); it('should create new question', async () => { - const id = new LearningObjectIdentifier('id03', Language.English, 1); - const student = await studentRepository.findByUsername('Noordkaap'); - - const clazz = await getClassRepository().findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9'); - const assignment = await getAssignmentRepository().findByClassAndId(clazz!, 21000); - const group = await getGroupRepository().findByAssignmentAndGroupNumber(assignment!, 21001); + const id = { + hruid: testLearningObject03.hruid, + language: testLearningObject03.language, + version: testLearningObject03.version, + } as LearningObjectIdentifier; + const student = getNoordkaap(); + const group = getTestGroup01(); await questionRepository.createQuestion({ loId: id, inGroup: group!, @@ -50,18 +56,15 @@ describe('QuestionRepository', () => { expect(question).toBeTruthy(); expect(question).toHaveLength(1); + expect(question[0].content).toBe('question?'); }); - let clazz: Class | null; - let assignment: Assignment | null; - let loId: LearningObjectIdentifier; it('should find all questions for a certain learning object and assignment', async () => { - clazz = await getClassRepository().findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9'); - assignment = await getAssignmentRepository().findByClassAndId(clazz!, 21000); - loId = { - hruid: 'id05', - language: Language.English, - version: 1, + const assignment = getAssignment01(); + const loId = { + hruid: testLearningObject05.hruid, + language: testLearningObject05.language, + version: testLearningObject05.version, }; const result = await questionRepository.findAllQuestionsAboutLearningObjectInAssignment(loId, assignment!); sortQuestions(result); @@ -84,7 +87,14 @@ describe('QuestionRepository', () => { }); it("should find only the questions for a certain learning object and assignment asked by the user's group", async () => { - const result = await questionRepository.findAllQuestionsAboutLearningObjectInAssignment(loId, assignment!, 'Tool'); + const loId = { + hruid: testLearningObject05.hruid, + language: testLearningObject05.language, + version: testLearningObject05.version, + }; + const assignment = getAssignment01(); + + const result = await questionRepository.findAllQuestionsAboutLearningObjectInAssignment(loId, assignment!, getTool().username); // (student Tool is in group #2) expect(result).toHaveLength(1); @@ -99,12 +109,17 @@ describe('QuestionRepository', () => { }); it('should not find removed question', async () => { - const id = new LearningObjectIdentifier('id04', Language.English, 1); - await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(id, 1); + const usedQuestion = getQuestion03(); + const id = { + hruid: usedQuestion.learningObjectHruid, + language: usedQuestion.learningObjectLanguage, + version: usedQuestion.learningObjectVersion, + } as LearningObjectIdentifier; + await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(id, usedQuestion.sequenceNumber!); - const question = await questionRepository.findAllQuestionsAboutLearningObject(id); + const questions = await questionRepository.findAllQuestionsAboutLearningObject(id); - expect(question).toHaveLength(0); + expect(questions).toHaveLength(0); }); }); diff --git a/backend/tests/test_assets/questions/questions.testdata.ts b/backend/tests/test_assets/questions/questions.testdata.ts index 2583ba72..b9339274 100644 --- a/backend/tests/test_assets/questions/questions.testdata.ts +++ b/backend/tests/test_assets/questions/questions.testdata.ts @@ -27,6 +27,7 @@ export function makeTestQuestions(em: EntityManager): Question[] { content: 'question', }); + //gets deleted question03 = em.create(Question, { learningObjectLanguage: testLearningObject04.language, learningObjectVersion: testLearningObject04.version,