test: answers data

This commit is contained in:
laurejablonski 2025-04-27 13:24:26 +02:00
parent 49449b90be
commit dd6b84536e
2 changed files with 15 additions and 19 deletions

View file

@ -6,39 +6,35 @@ import { QuestionRepository } from '../../../src/data/questions/question-reposit
import { LearningObjectIdentifier } from '../../../src/entities/content/learning-object-identifier'; import { LearningObjectIdentifier } from '../../../src/entities/content/learning-object-identifier';
import { Language } from '@dwengo-1/common/util/language'; import { Language } from '@dwengo-1/common/util/language';
import { TeacherRepository } from '../../../src/data/users/teacher-repository'; 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', () => { describe('AnswerRepository', () => {
let answerRepository: AnswerRepository; let answerRepository: AnswerRepository;
let questionRepository: QuestionRepository;
let teacherRepository: TeacherRepository;
beforeAll(async () => { beforeAll(async () => {
await setupTestApp(); await setupTestApp();
answerRepository = getAnswerRepository(); answerRepository = getAnswerRepository();
questionRepository = getQuestionRepository();
teacherRepository = getTeacherRepository();
}); });
it('should find all answers to a question', async () => { it('should find all answers to a question', async () => {
const id = new LearningObjectIdentifier('id05', Language.English, 1); const question = getQuestion02();
const questions = await questionRepository.findAllQuestionsAboutLearningObject(id); const a1 = getAnswer01();
const a2 = getAnswer02();
const question = questions.find((it) => it.sequenceNumber === 2);
const answers = await answerRepository.findAllAnswersToQuestion(question!); const answers = await answerRepository.findAllAnswersToQuestion(question!);
expect(answers).toBeTruthy(); expect(answers).toBeTruthy();
expect(answers).toHaveLength(2); expect(answers).toHaveLength(2);
expect(answers[0].content).toBeOneOf(['answer', 'answer2']); expect(answers[0].content).toBeOneOf([a1.content, a2.content]);
expect(answers[1].content).toBeOneOf(['answer', 'answer2']); expect(answers[1].content).toBeOneOf([a1.content, a2.content]);
}); });
it('should create an answer to a question', async () => { it('should create an answer to a question', async () => {
const teacher = await teacherRepository.findByUsername('FooFighters'); const teacher = getFooFighters();
const id = new LearningObjectIdentifier('id05', Language.English, 1); const question = getQuestion01();
const questions = await questionRepository.findAllQuestionsAboutLearningObject(id);
const question = questions[0];
await answerRepository.createAnswer({ await answerRepository.createAnswer({
toQuestion: question, toQuestion: question,
@ -54,12 +50,11 @@ describe('AnswerRepository', () => {
}); });
it('should not find a removed answer', async () => { it('should not find a removed answer', async () => {
const id = new LearningObjectIdentifier('id04', Language.English, 1); const deleted = getAnswer03();
const questions = await questionRepository.findAllQuestionsAboutLearningObject(id);
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); expect(emptyList).toHaveLength(0);
}); });

View file

@ -20,6 +20,7 @@ export function makeTestAnswers(em: EntityManager): Answer[] {
content: 'answer2', content: 'answer2',
}); });
// gets deleted
answer03 = em.create(Answer, { answer03 = em.create(Answer, {
author: getLimpBizkit(), author: getLimpBizkit(),
toQuestion: getQuestion04(), toQuestion: getQuestion04(),