From fe2b8b2f6927cbd2066458adf5f1d22cb13da511 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 8 Apr 2025 16:45:58 -0700 Subject: [PATCH] add test cases --- .../InMemoryParkingRepositoryTests.test.ts | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 test/repositories/InMemoryParkingRepositoryTests.test.ts diff --git a/test/repositories/InMemoryParkingRepositoryTests.test.ts b/test/repositories/InMemoryParkingRepositoryTests.test.ts new file mode 100644 index 0000000..b656c81 --- /dev/null +++ b/test/repositories/InMemoryParkingRepositoryTests.test.ts @@ -0,0 +1,105 @@ +import { beforeEach, describe, expect, it } from "@jest/globals"; +import { InMemoryParkingRepository } from "../../src/repositories/InMemoryParkingRepository"; +import { IParkingStructure } from "../../src/entities/ParkingRepositoryEntities"; + +describe("InMemoryParkingRepository", () => { + let repository: InMemoryParkingRepository; + const testStructure: IParkingStructure = { + coordinates: { + latitude: 33.794795, + longitude: -117.850807, + }, + spotsAvailable: 0, + id: "1", + name: "Anderson Parking Structure", + capacity: 100, + address: "300 E Walnut Ave, Orange, CA 92867" + }; + + beforeEach(() => { + repository = new InMemoryParkingRepository(); + }); + + describe("addOrUpdateParkingStructure", () => { + it("should add a new parking structure", async () => { + await repository.addOrUpdateParkingStructure(testStructure); + const result = await repository.getParkingStructureById(testStructure.id); + expect(result).toEqual(testStructure); + }); + + it("should update existing parking structure", async () => { + await repository.addOrUpdateParkingStructure(testStructure); + const updatedStructure = { ...testStructure, name: "Updated Garage" }; + await repository.addOrUpdateParkingStructure(updatedStructure); + const result = await repository.getParkingStructureById(testStructure.id); + expect(result).toEqual(updatedStructure); + }); + }); + + describe("removeParkingStructureIfExists", () => { + it("should remove existing parking structure and return it", async () => { + await repository.addOrUpdateParkingStructure(testStructure); + const removed = await repository.removeParkingStructureIfExists(testStructure.id); + expect(removed).toEqual(testStructure); + const result = await repository.getParkingStructureById(testStructure.id); + expect(result).toBeNull(); + }); + + it("should return null when removing non-existent structure", async () => { + const result = await repository.removeParkingStructureIfExists("non-existent"); + expect(result).toBeNull(); + }); + }); + + describe("clearParkingStructureData", () => { + it("should remove all parking structures", async () => { + const structures = [ + testStructure, + { ...testStructure, id: "test-id-2", name: "Second Garage" } + ]; + + for (const structure of structures) { + await repository.addOrUpdateParkingStructure(structure); + } + + await repository.clearParkingStructureData(); + const result = await repository.getParkingStructures(); + expect(result).toHaveLength(0); + }); + }); + + describe("getParkingStructures", () => { + it("should return empty array when no structures exist", async () => { + const result = await repository.getParkingStructures(); + expect(result).toEqual([]); + }); + + it("should return all added structures", async () => { + const structures = [ + testStructure, + { ...testStructure, id: "test-id-2", name: "Second Garage" } + ]; + + for (const structure of structures) { + await repository.addOrUpdateParkingStructure(structure); + } + + const result = await repository.getParkingStructures(); + expect(result).toHaveLength(2); + expect(result).toEqual(expect.arrayContaining(structures)); + }); + }); + + describe("getParkingStructureById", () => { + it("should return null for non-existent structure", async () => { + const result = await repository.getParkingStructureById("non-existent"); + expect(result).toBeNull(); + }); + + it("should return structure by id", async () => { + await repository.addOrUpdateParkingStructure(testStructure); + const result = await repository.getParkingStructureById(testStructure.id); + expect(result).toEqual(testStructure); + }); + }); +});