mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add and use generator method for system objects
This commit is contained in:
10
test/generators.ts
Normal file
10
test/generators.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ISystem } from "../src/entities/entities";
|
||||
|
||||
|
||||
export function generateMockSystems(): ISystem[] {
|
||||
return [
|
||||
{ id: "1", name: "System A" },
|
||||
{ id: "2", name: "System B" },
|
||||
{ id: "3", name: "System C" },
|
||||
];
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { beforeEach, describe, expect, test } from "@jest/globals";
|
||||
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
||||
import { generateMockSystems } from "../generators";
|
||||
import { mock } from "node:test";
|
||||
|
||||
// For repositories created in the future, reuse core testing
|
||||
// logic from here and differentiate setup (e.g. creating mocks)
|
||||
@@ -18,10 +20,7 @@ describe("UnoptimizedInMemoryRepository", () => {
|
||||
|
||||
describe("getSystems", () => {
|
||||
test("gets the systems stored in the repository", async () => {
|
||||
const mockSystems = [
|
||||
{ id: "1", name: "System A" },
|
||||
{ id: "2", name: "System B" },
|
||||
];
|
||||
const mockSystems = generateMockSystems();
|
||||
await repository.addOrUpdateSystem(mockSystems[0]);
|
||||
await repository.addOrUpdateSystem(mockSystems[1]);
|
||||
|
||||
@@ -39,11 +38,7 @@ describe("UnoptimizedInMemoryRepository", () => {
|
||||
|
||||
describe("getSystemById", () => {
|
||||
test("gets a system by the ID if it exists", async () => {
|
||||
const mockSystems = [
|
||||
{ id: "1", name: "System A" },
|
||||
{ id: "2", name: "System B" },
|
||||
{ id: "3", name: "System C" },
|
||||
];
|
||||
const mockSystems = generateMockSystems();
|
||||
for (const system of mockSystems) {
|
||||
await repository.addOrUpdateSystem(system);
|
||||
}
|
||||
@@ -313,7 +308,8 @@ describe("UnoptimizedInMemoryRepository", () => {
|
||||
|
||||
describe("addOrUpdateSystem", () => {
|
||||
test("adds a new system if nonexistent", async () => {
|
||||
const newSystem = { id: "sys1", name: "System A", millisecondsSinceEpoch: 1620000000000 };
|
||||
const mockSystems = generateMockSystems();
|
||||
const newSystem = mockSystems[0];
|
||||
|
||||
await repository.addOrUpdateSystem(newSystem);
|
||||
|
||||
@@ -322,8 +318,10 @@ describe("UnoptimizedInMemoryRepository", () => {
|
||||
});
|
||||
|
||||
test("updates an existing system if it exists", async () => {
|
||||
const existingSystem = { id: "sys1", name: "System A", millisecondsSinceEpoch: 1620000000000 };
|
||||
const updatedSystem = { id: "sys1", name: "Updated System A", millisecondsSinceEpoch: 1625000000000 };
|
||||
const mockSystems = generateMockSystems();
|
||||
const existingSystem = mockSystems[0];
|
||||
const updatedSystem = structuredClone(existingSystem);
|
||||
updatedSystem.name = "Updated System";
|
||||
|
||||
await repository.addOrUpdateSystem(existingSystem);
|
||||
await repository.addOrUpdateSystem(updatedSystem);
|
||||
@@ -445,11 +443,10 @@ describe("UnoptimizedInMemoryRepository", () => {
|
||||
|
||||
describe("clearSystemData", () => {
|
||||
test("clears all systems from the repository", async () => {
|
||||
const system1 = { id: "sys1", name: "System A" };
|
||||
const system2 = { id: "sys2", name: "System B" };
|
||||
|
||||
await repository.addOrUpdateSystem(system1);
|
||||
await repository.addOrUpdateSystem(system2);
|
||||
const mockSystems = generateMockSystems();
|
||||
for (const system of mockSystems) {
|
||||
await repository.addOrUpdateSystem(system);
|
||||
}
|
||||
|
||||
await repository.clearSystemData();
|
||||
|
||||
@@ -458,7 +455,8 @@ describe("UnoptimizedInMemoryRepository", () => {
|
||||
});
|
||||
|
||||
test("clears system data when the repository has data", async () => {
|
||||
const system = { id: "sys1", name: "System A" };
|
||||
const mockSystems = generateMockSystems();
|
||||
const system = mockSystems[0];
|
||||
await repository.addOrUpdateSystem(system);
|
||||
|
||||
await repository.clearSystemData();
|
||||
|
||||
Reference in New Issue
Block a user