mirror of
https://github.com/OHTU-German-learning-website/OHTU-German-learning-website.git
synced 2026-10-12 19:13:51 +03:00
updated tests
This commit is contained in:
@@ -4,6 +4,7 @@ import { useTestDatabase } from "@/backend/test/testdb";
|
||||
import { useTestRequest } from "@/backend/test/mock-request";
|
||||
import { TestFactory } from "@/backend/test/testfactory";
|
||||
import { DB } from "@/backend/db";
|
||||
|
||||
describe("POST /api/auth/admin/exercises/free-form", () => {
|
||||
useTestDatabase();
|
||||
|
||||
@@ -12,13 +13,18 @@ describe("POST /api/auth/admin/exercises/free-form", () => {
|
||||
const { mockPost } = useTestRequest(admin);
|
||||
|
||||
const validInput = {
|
||||
question: "What is the capital of France?",
|
||||
questions: "What is the capital of France?",
|
||||
answers: [
|
||||
{
|
||||
answer: "Paris",
|
||||
feedback: "Excellent work!",
|
||||
is_correct: true,
|
||||
},
|
||||
{
|
||||
answer: "London",
|
||||
feedback: "Try again!",
|
||||
is_correct: false,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -42,14 +48,26 @@ describe("POST /api/auth/admin/exercises/free-form", () => {
|
||||
[exercise_id]
|
||||
);
|
||||
expect(freeFormExerciseResult.rows.length).toBe(1);
|
||||
expect(freeFormExerciseResult.rows[0].question).toBe(validInput.question);
|
||||
expect(freeFormExerciseResult.rows[0].question).toBe(
|
||||
validInput.questions[0].question
|
||||
);
|
||||
|
||||
const questionRows = await DB.pool(
|
||||
"SELECT * FROM free_form_questions WHERE free_form_exercise_id = $1 ORDER BY ASC",
|
||||
[freeFormExerciseResult.rows[0].id]
|
||||
);
|
||||
expect(questionRows.rows.length).toBe(1);
|
||||
expect(questionRows.rows[0].question).toBe(
|
||||
validInput.questions[0].question
|
||||
);
|
||||
// Verify that the free_form_answers were inserted
|
||||
const freeFormAnswersResult = await DB.pool(
|
||||
"SELECT * FROM free_form_answers WHERE free_form_exercise_id = $1",
|
||||
[freeFormExerciseResult.rows[0].id]
|
||||
[questionRows.rows[0].id]
|
||||
);
|
||||
expect(freeFormAnswersResult.rows.length).toBe(
|
||||
validInput.questions[0].answers.length
|
||||
);
|
||||
expect(freeFormAnswersResult.rows.length).toBe(validInput.answers.length);
|
||||
});
|
||||
|
||||
it("should return a 422 error for invalid input (missing question)", async () => {
|
||||
|
||||
@@ -17,15 +17,25 @@ describe("freeform answers", () => {
|
||||
const exercise = await TestFactory.exercise({
|
||||
category: "freeform",
|
||||
});
|
||||
|
||||
const exerciseId = exercise.id;
|
||||
const freeFormExercise = await TestFactory.freeFormExercise({
|
||||
exercise_id: exerciseId,
|
||||
question: "Test Question",
|
||||
});
|
||||
|
||||
const freeFormQuestion = await DB.pool(
|
||||
`INSERT INTO free_form_questions (free_form_exercise_id, question, question_order)
|
||||
VALUES ($1, $2, $3) RETURNING *`,
|
||||
[freeFormExercise.id, "Test Question", 1]
|
||||
);
|
||||
|
||||
const questionId = freeFormQuestion.rows[0].id;
|
||||
|
||||
// Add possible answers
|
||||
await TestFactory.freeFormAnswer({
|
||||
free_form_exercise_id: freeFormExercise.id,
|
||||
free_form_question_id: questionId,
|
||||
answer: "This is the correct answer",
|
||||
is_correct: true,
|
||||
feedback: "Great job!",
|
||||
@@ -33,6 +43,7 @@ describe("freeform answers", () => {
|
||||
|
||||
await TestFactory.freeFormAnswer({
|
||||
free_form_exercise_id: freeFormExercise.id,
|
||||
free_form_question_id: questionId,
|
||||
answer: "This is another correct answer",
|
||||
is_correct: true,
|
||||
feedback: "Well done!",
|
||||
@@ -40,22 +51,24 @@ describe("freeform answers", () => {
|
||||
|
||||
await TestFactory.freeFormAnswer({
|
||||
free_form_exercise_id: freeFormExercise.id,
|
||||
free_form_question_id: questionId,
|
||||
answer: "This is an incorrect answer",
|
||||
is_correct: false,
|
||||
feedback: "Try again",
|
||||
});
|
||||
|
||||
return freeFormExercise.id;
|
||||
return { exerciseId: freeFormExercise.id, questionId };
|
||||
}
|
||||
|
||||
it("should evaluate exact match answers correctly", async () => {
|
||||
const user = await TestFactory.user();
|
||||
const { mockPost } = useTestRequest(user);
|
||||
const exerciseId = await setupExercise();
|
||||
const { exerciseId, questionId } = await setupExercise();
|
||||
|
||||
const response = await POST(
|
||||
mockPost("/api/exercises/freeform/answers", {
|
||||
freeFormExerciseId: exerciseId,
|
||||
freeFormQuestionId: questionId,
|
||||
answer: "This is the correct answer",
|
||||
})
|
||||
);
|
||||
@@ -72,11 +85,12 @@ describe("freeform answers", () => {
|
||||
it("should evaluate similar but not exact answers", async () => {
|
||||
const user = await TestFactory.user();
|
||||
const { mockPost } = useTestRequest(user);
|
||||
const exerciseId = await setupExercise();
|
||||
const { exerciseId, questionId } = await setupExercise();
|
||||
|
||||
const response = await POST(
|
||||
mockPost("/api/exercises/freeform/answers", {
|
||||
freeFormExerciseId: exerciseId,
|
||||
freeFormQuestionId: questionId,
|
||||
answer: "This is the correct answer word",
|
||||
})
|
||||
);
|
||||
@@ -94,11 +108,12 @@ describe("freeform answers", () => {
|
||||
it("should reject completely incorrect answers", async () => {
|
||||
const user = await TestFactory.user();
|
||||
const { mockPost } = useTestRequest(user);
|
||||
const exerciseId = await setupExercise();
|
||||
const { exerciseId, questionId } = await setupExercise();
|
||||
|
||||
const response = await POST(
|
||||
mockPost("/api/exercises/freeform/answers", {
|
||||
freeFormExerciseId: exerciseId,
|
||||
freeFormQuestionId: questionId,
|
||||
answer: "Something totally different",
|
||||
})
|
||||
);
|
||||
@@ -110,14 +125,15 @@ describe("freeform answers", () => {
|
||||
expect(result.similarity).toBeLessThan(0.7);
|
||||
});
|
||||
|
||||
it("should return 400 for missing required fields", async () => {
|
||||
it("should return 422 for missing required fields", async () => {
|
||||
const user = await TestFactory.user();
|
||||
const { mockPost } = useTestRequest(user);
|
||||
|
||||
const response = await POST(
|
||||
mockPost("/api/exercises/freeform/answers", {
|
||||
freeFormExerciseId: "some-id",
|
||||
// missing answer field
|
||||
freeFormExerciseId: "123",
|
||||
// missing answer
|
||||
//missing freeFormQuestionId
|
||||
})
|
||||
);
|
||||
|
||||
@@ -131,6 +147,7 @@ describe("freeform answers", () => {
|
||||
const response = await POST(
|
||||
mockPost("/api/exercises/freeform/answers", {
|
||||
freeFormExerciseId: "-1",
|
||||
freeFormQuestionId: "-1",
|
||||
answer: "Some answer",
|
||||
})
|
||||
);
|
||||
@@ -141,23 +158,27 @@ describe("freeform answers", () => {
|
||||
it("should record user answers in the database", async () => {
|
||||
const user = await TestFactory.user();
|
||||
const { mockPost } = useTestRequest(user);
|
||||
const exerciseId = await setupExercise();
|
||||
const { exerciseId, questionId } = await setupExercise();
|
||||
|
||||
await POST(
|
||||
mockPost("/api/exercises/freeform/answers", {
|
||||
freeFormExerciseId: exerciseId,
|
||||
freeFormQuestionId: questionId,
|
||||
answer: "This is the correct answer",
|
||||
})
|
||||
);
|
||||
|
||||
const result = await DB.pool(
|
||||
`SELECT * FROM free_form_user_answers`
|
||||
// WHERE user_id = $1 AND free_form_exercise_id = $2`,
|
||||
// [user.id, exerciseId]
|
||||
`SELECT * FROM free_form_user_answers
|
||||
WHERE user_id = $1 AND free_form_exercise_id = $2`,
|
||||
[user.id, exerciseId]
|
||||
);
|
||||
expect(result.rows.length).toBe(1);
|
||||
expect(result.rows[0].answer).toBe("This is the correct answer");
|
||||
expect(result.rows[0].is_correct).toBe(true);
|
||||
expect(String(result.rows[0].free_form_question_id)).toBe(
|
||||
String(questionId)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user