From dd6b84536e7c0604fa6f15016957d4dd2efbae37 Mon Sep 17 00:00:00 2001 From: laurejablonski Date: Sun, 27 Apr 2025 13:24:26 +0200 Subject: [PATCH] test: answers data --- backend/tests/data/questions/answers.test.ts | 33 ++++++++----------- .../test_assets/questions/answers.testdata.ts | 1 + 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/backend/tests/data/questions/answers.test.ts b/backend/tests/data/questions/answers.test.ts index 80cd3034..b69765be 100644 --- a/backend/tests/data/questions/answers.test.ts +++ b/backend/tests/data/questions/answers.test.ts @@ -6,39 +6,35 @@ import { QuestionRepository } from '../../../src/data/questions/question-reposit import { LearningObjectIdentifier } from '../../../src/entities/content/learning-object-identifier'; import { Language } from '@dwengo-1/common/util/language'; import { TeacherRepository } from '../../../src/data/users/teacher-repository'; +import { getQuestion01, getQuestion02, getQuestion04, getQuestion05, getQuestion06 } from '../../test_assets/questions/questions.testdata'; +import { getAnswer01, getAnswer02, getAnswer03 } from '../../test_assets/questions/answers.testdata'; +import { getFooFighters } from '../../test_assets/users/teachers.testdata'; +import { testLearningObject05 } from '../../test_assets/content/learning-objects.testdata'; 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); - - const question = questions.find((it) => it.sequenceNumber === 2); + const question = getQuestion02(); + const a1 = getAnswer01(); + const a2 = getAnswer02(); const answers = await answerRepository.findAllAnswersToQuestion(question!); expect(answers).toBeTruthy(); expect(answers).toHaveLength(2); - expect(answers[0].content).toBeOneOf(['answer', 'answer2']); - expect(answers[1].content).toBeOneOf(['answer', 'answer2']); + expect(answers[0].content).toBeOneOf([a1.content, a2.content]); + expect(answers[1].content).toBeOneOf([a1.content, a2.content]); }); 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); - - const question = questions[0]; + const teacher = getFooFighters(); + const question = getQuestion01(); await answerRepository.createAnswer({ toQuestion: question, @@ -54,12 +50,11 @@ describe('AnswerRepository', () => { }); it('should not find a removed answer', async () => { - const id = new LearningObjectIdentifier('id04', Language.English, 1); - const questions = await questionRepository.findAllQuestionsAboutLearningObject(id); + const deleted = getAnswer03(); - await answerRepository.removeAnswerByQuestionAndSequenceNumber(questions[0], 1); + await answerRepository.removeAnswerByQuestionAndSequenceNumber(deleted.toQuestion, deleted.sequenceNumber!); - const emptyList = await answerRepository.findAllAnswersToQuestion(questions[0]); + const emptyList = await answerRepository.findAllAnswersToQuestion(deleted.toQuestion); expect(emptyList).toHaveLength(0); }); diff --git a/backend/tests/test_assets/questions/answers.testdata.ts b/backend/tests/test_assets/questions/answers.testdata.ts index b5a5d360..904ba338 100644 --- a/backend/tests/test_assets/questions/answers.testdata.ts +++ b/backend/tests/test_assets/questions/answers.testdata.ts @@ -20,6 +20,7 @@ export function makeTestAnswers(em: EntityManager): Answer[] { content: 'answer2', }); + // gets deleted answer03 = em.create(Answer, { author: getLimpBizkit(), toQuestion: getQuestion04(),