2025SELab2-project-Dwengo/backend/tests/data/questions/answers.test.ts
Gerald Schmittinger 4dcd4671ca fix(backend): Workaround voor autoincrement-problemen bij SQLite
SQLite (die we voor de automatische tests gebruiken) ondersteunt geen autoincrement op kolommen die deel uitmaken van een composite primary key. Hiervoor heb ik een workaround geïmplementeerd.
2025-03-13 02:20:01 +01:00

80 lines
2.9 KiB
TypeScript

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 { 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);
const question = questions.filter((it) => it.sequenceNumber == 2)[0];
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']);
});
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];
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);
});
});