Added repositories, made database unit-testable.
This commit is contained in:
parent
d9303dd6e8
commit
fe1e0a7b64
15 changed files with 1672 additions and 45 deletions
33
backend/tests/data/users.test.ts
Normal file
33
backend/tests/data/users.test.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import {initializeTests} from "../testutils"
|
||||
import {Student} from "../../src/entities/users/student.entity";
|
||||
import {describe, it, expect, beforeAll} from "vitest";
|
||||
import {getRepository} from "../../src/orm";
|
||||
import {StudentRepository} from "../../src/data/users/student-repository";
|
||||
|
||||
const username = "teststudent";
|
||||
const firstName = "John";
|
||||
const lastName = "Doe";
|
||||
describe("StudentRepository", () => {
|
||||
let studentRepository: StudentRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
await initializeTests()
|
||||
studentRepository = getRepository(Student) as StudentRepository;
|
||||
});
|
||||
|
||||
it("should return the queried student after he was added", async () => {
|
||||
await studentRepository.insert(new Student(username, firstName, lastName));
|
||||
|
||||
let retrievedStudent = await studentRepository.findByUsername(username);
|
||||
expect(retrievedStudent).toBeTruthy();
|
||||
expect(retrievedStudent?.firstName).toBe(firstName);
|
||||
expect(retrievedStudent?.lastName).toBe(lastName);
|
||||
});
|
||||
|
||||
it("should no longer return the queried student after he was removed again", async () => {
|
||||
await studentRepository.deleteByUsername(username);
|
||||
|
||||
let retrievedStudent = await studentRepository.findByUsername(username);
|
||||
expect(retrievedStudent).toBeNull();
|
||||
});
|
||||
});
|
|
@ -4,6 +4,6 @@ describe("Sample test", () => {
|
|||
it("should sum to 2", () => {
|
||||
const expected = 2;
|
||||
const result = 1 + 1;
|
||||
expect(result).toBe(expected);
|
||||
expect(result).equals(expected);
|
||||
});
|
||||
})
|
||||
|
|
7
backend/tests/testutils.ts
Normal file
7
backend/tests/testutils.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import {initORM} from "../src/orm";
|
||||
import dotenv from "dotenv";
|
||||
|
||||
export async function initializeTests() {
|
||||
dotenv.config({path: ".env.test"});
|
||||
await initORM(true);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue