From c96ccfbfddf68f7bfaed4f01b75fe5edaf32088c Mon Sep 17 00:00:00 2001 From: Laure Jablonski Date: Sun, 9 Mar 2025 09:43:29 +0100 Subject: [PATCH] test: testen voor answer repo staan klaar, creatie werkt niet omdat geen sequence number meegegeven kan worden --- backend/tests/data/answers.test.ts | 85 ++++++++++++++++++++++++++++++ backend/tests/setup-tests.ts | 45 ++++++++-------- 2 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 backend/tests/data/answers.test.ts diff --git a/backend/tests/data/answers.test.ts b/backend/tests/data/answers.test.ts new file mode 100644 index 00000000..1757c475 --- /dev/null +++ b/backend/tests/data/answers.test.ts @@ -0,0 +1,85 @@ +import { beforeAll, describe, expect, it } from 'vitest'; +import { setupTestApp } from '../setup-tests'; +import { AnswerRepository } from '../../src/data/questions/answer-repository'; +import { + getAnswerRepository, + getQuestionRepository, + getTeacherRepository, +} from '../../src/data/repositories'; +import { QuestionRepository } from '../../src/data/questions/question-repository'; +import { LearningObjectIdentifier } from '../../src/entities/content/learning-object-identifier'; +import { Language } from '../../src/entities/content/language'; +import { Question } from '../../src/entities/questions/question.entity'; +import { TeacherRepository } from '../../src/data/users/teacher-repository'; + +describe('AnswerRepository', () => { + let AnswerRepository: AnswerRepository; + let QuestionRepository: QuestionRepository; + let TeacherRepository: TeacherRepository; + + beforeAll(async () => { + await setupTestApp(); + AnswerRepository = getAnswerRepository(); + QuestionRepository = getQuestionRepository(); + TeacherRepository = getTeacherRepository(); + }); + + it('should find all answers to a question', async () => { + const id = new LearningObjectIdentifier('id05', Language.English, '1'); + const questions = + await QuestionRepository.findAllQuestionsAboutLearningObject(id); + let question: Question; + if (questions[0].sequenceNumber == 2) { + question = questions[0]; + } else { + question = questions[1]; + } + const answers = + await AnswerRepository.findAllAnswersToQuestion(question); + + expect(answers).toBeTruthy(); + expect(answers).toHaveLength(2); + }); + + // it('should create an answer to a question', async () => { + // const teacher = await TeacherRepository.findByUsername('FooFighters'); + // const id = new LearningObjectIdentifier('id05', Language.English, '1'); + // const questions = + // await QuestionRepository.findAllQuestionsAboutLearningObject(id); + // let question: Question; + // if (questions[0].sequenceNumber == 1) { + // question = questions[0]; + // } else { + // question = questions[1]; + // } + // await AnswerRepository.createAnswer({ + // toQuestion: question, + // author: teacher!, + // content: 'created answer', + // }); + + // const answers = + // await AnswerRepository.findAllAnswersToQuestion(question); + + // expect(answers).toBeTruthy(); + // expect(answers).toHaveLength(1); + // expect(answers[0].content).toBe('created answer'); + // }); + + it('should not find a removed answer', async () => { + const id = new LearningObjectIdentifier('id04', Language.English, '1'); + const questions = + await QuestionRepository.findAllQuestionsAboutLearningObject(id); + + await AnswerRepository.removeAnswerByQuestionAndSequenceNumber( + questions[0], + 1 + ); + + const emptyList = await AnswerRepository.findAllAnswersToQuestion( + questions[0] + ); + + expect(emptyList).toHaveLength(0); + }); +}); diff --git a/backend/tests/setup-tests.ts b/backend/tests/setup-tests.ts index 6eeda030..e6661faa 100644 --- a/backend/tests/setup-tests.ts +++ b/backend/tests/setup-tests.ts @@ -516,29 +516,29 @@ export async function setupTestApp() { content: 'question', }); - // const answer01 = em.create(Answer, { - // author: teacher01, - // toQuestion: question02, - // sequenceNumber: 1, - // timestamp: new Date(), - // content: 'answer', - // }); + const answer01 = em.create(Answer, { + author: teacher01, + toQuestion: question02, + sequenceNumber: 1, + timestamp: new Date(), + content: 'answer', + }); - // const answer02 = em.create(Answer, { - // author: teacher01, - // toQuestion: question02, - // sequenceNumber: 2, - // timestamp: new Date(), - // content: 'answer2', - // }); + const answer02 = em.create(Answer, { + author: teacher01, + toQuestion: question02, + sequenceNumber: 2, + timestamp: new Date(), + content: 'answer2', + }); - // const answer03 = em.create(Answer, { - // author: teacher02, - // toQuestion: question04, - // sequenceNumber: 1, - // timestamp: new Date(), - // content: 'answer', - // }); + const answer03 = em.create(Answer, { + author: teacher02, + toQuestion: question04, + sequenceNumber: 1, + timestamp: new Date(), + content: 'answer', + }); // const submission01 = em.create(Submission, { // learningObjectHruid: 'id03', @@ -635,5 +635,8 @@ export async function setupTestApp() { question02, question03, question04, + answer01, + answer02, + answer03, ]); }