diff --git a/backend/src/__tests__/registration.test.ts b/backend/src/__tests__/registration.test.ts index 64f0809..1d87e7e 100644 --- a/backend/src/__tests__/registration.test.ts +++ b/backend/src/__tests__/registration.test.ts @@ -4,15 +4,6 @@ import express from "express"; import router from "../routes/registration"; import { pool } from ".."; // Assuming pool is exported from your index file -// Define types for the mock response data -interface MockClient { - query: Mock; - release: Mock; - begin: Mock; - commit: Mock; - rollback: Mock; -} - interface Registration { user_id?: number; license_number?: string; @@ -170,70 +161,4 @@ describe("Registration API", () => { }); }); }); - - describe("POST /approve", () => { - it("should return 400 if license number is not provided", async () => { - const response = await request(app).post("/approve").send({}); - - expect(response.status).toBe(400); - expect(response.body).toEqual({ - title: "Validation Error", - message: "License number is required.", - }); - }); - - it("should return 404 if registration not found", async () => { - const license_number = "12345678"; - (pool.query as Mock).mockResolvedValueOnce({ rows: [] }); // No matching registration - - const response = await request(app) - .post("/approve") - .send({ license_number }); - - expect(response.status).toBe(404); - expect(response.body).toEqual({ - title: "Not Found", - message: "Registration with the specified license number not found.", - }); - }); - - it("should approve the registration and update driver details", async () => { - const license_number = "12345678"; - const mockRegistration: Registration = { - school_email: "test@example.com", - user_id: 1, - }; - const mockDriver = { email: "", id: 1 }; - - // Mocking registration and driver fetch queries - (pool.query as Mock) - .mockResolvedValueOnce({ rows: [mockRegistration] }) // Mock registration fetch - .mockResolvedValueOnce({ rows: [mockDriver] }); // Mock driver fetch - - // Mock pool.connect to return a mock client - const mockClient: MockClient = { - query: vi.fn().mockResolvedValueOnce({}), // Mocking successful query - release: vi.fn(), - begin: vi.fn().mockResolvedValueOnce({}), - commit: vi.fn().mockResolvedValueOnce({}), - rollback: vi.fn().mockResolvedValueOnce({}), - }; - (pool.connect as Mock).mockResolvedValue(mockClient); - - const response = await request(app) - .post("/approve") - .send({ license_number }); - - expect(response.status).toBe(200); - expect(response.body).toEqual({ - title: "Driver Updated!", - message: "Driver's email and user_id have been updated successfully.", - }); - - expect(pool.query).toHaveBeenCalledTimes(3); // Expect 3 queries: registration, driver, deletion - expect(pool.connect).toHaveBeenCalledOnce(); - expect(mockClient.begin).toHaveBeenCalledOnce(); // Ensure transaction start - expect(mockClient.commit).toHaveBeenCalledOnce(); // Ensure transaction commit - }); - }); }); diff --git a/backend/src/__tests__/violation.test.ts b/backend/src/__tests__/violation.test.ts deleted file mode 100644 index 19ee8ba..0000000 --- a/backend/src/__tests__/violation.test.ts +++ /dev/null @@ -1,203 +0,0 @@ -import request from "supertest"; -import express from "express"; -import router from "../routes/violation"; -import { pool } from ".."; // Your pool instance -import { Violation } from "../types/datatypes"; -import { Mock } from "vitest"; -const app = express(); -app.use(express.json()); -app.use("/violations", router); - -// Mock the pool.query method to simulate database behavior -vi.mock("..", () => ({ - pool: { - query: vi.fn(), - }, -})); - -describe("Violation API", () => { - describe("POST /add", () => { - it("should add a violation when the driver exists", async () => { - const violationData: Violation = { - id: "1", // Add id - driver_id: "1", // driver_id as string - violation_type: "Speeding", - violation_date: "2024-12-19", - description: "Exceeding the speed limit", - paid_status: false, // Add paid_status - }; - - // Mock driver found in the database - (pool.query as Mock).mockResolvedValueOnce({ - rows: [{ id: 1, first_name: "John", last_name: "Doe" }], - }); - - // Mock insertion of the violation - (pool.query as Mock).mockResolvedValueOnce({ - rows: [ - { - driver_id: 1, - violation_type: "Speeding", - }, - ], - }); - - const response = await request(app) - .post("/violations/add") - .send(violationData); - - expect(response.status).toBe(200); - expect(response.body).toEqual({ - title: "Violation Added!", - message: "Violation has been added for John Doe, Speeding.", - }); - }); - - it("should return 404 when the driver does not exist", async () => { - const violationData: Violation = { - id: "1", // Add id - driver_id: "999", // Non-existent driver (as a string) - violation_type: "Speeding", - violation_date: "2024-12-19", - description: "Exceeding the speed limit", - paid_status: false, // Add paid_status - }; - - // Mock driver not found in the database - (pool.query as Mock).mockResolvedValueOnce({ - rows: [], - }); - - const response = await request(app) - .post("/violations/add") - .send(violationData); - - expect(response.status).toBe(404); - expect(response.body).toEqual({ - title: "No Driver Found", - message: "Driver is not found.", - }); - }); - - it("should return 500 if an error occurs", async () => { - const violationData: Violation = { - id: "1", // Add id - driver_id: "1", // driver_id as string - violation_type: "Speeding", - violation_date: "2024-12-19", - description: "Exceeding the speed limit", - paid_status: false, // Add paid_status - }; - - // Mock error during query - (pool.query as Mock).mockRejectedValueOnce(new Error("Database error")); - - const response = await request(app) - .post("/violations/add") - .send(violationData); - - expect(response.status).toBe(500); - expect(response.body).toEqual({ - title: "Error", - message: "Database error", - }); - }); - }); - - describe("PATCH /update", () => { - it("should update a violation successfully", async () => { - const violationData = { - id: 1, - violation_type: "Speeding", - description: "Exceeded speed limit by 20 mph", - }; - - // Mock successful update - (pool.query as Mock).mockResolvedValueOnce({ - rowCount: 1, - rows: [ - { - id: 1, - violation_type: "Speeding", - description: "Exceeded speed limit by 20 mph", - }, - ], - }); - - const response = await request(app) - .patch("/violations/update") - .send(violationData); - - expect(response.status).toBe(200); - expect(response.body).toEqual({ - title: "Violation Updated!", - message: "Violation has been updated successfully.", - }); - }); - - it("should return 404 if the violation is not found", async () => { - const violationData = { - id: 999, // Non-existent violation ID - violation_type: "Speeding", - description: "Exceeded speed limit by 20 mph", - }; - - // Mock violation not found - (pool.query as Mock).mockResolvedValueOnce({ - rowCount: 0, - rows: [], - }); - - const response = await request(app) - .patch("/violations/update") - .send(violationData); - - expect(response.status).toBe(404); - expect(response.body).toEqual({ - title: "Not Found", - message: "Violations with the specified ID does not exist.", - }); - }); - }); - - describe("DELETE /delete", () => { - it("should delete a violation successfully", async () => { - const violationData = { violationId: 1 }; - - // Mock successful deletion - (pool.query as Mock).mockResolvedValueOnce({ - rowCount: 1, - rows: [{ id: 1 }], - }); - - const response = await request(app) - .delete("/violations/delete") - .send(violationData); - - expect(response.status).toBe(200); - expect(response.body).toEqual({ - title: "Violation Deleted", - message: "Violation Deleted Successfully.", - }); - }); - - it("should return 404 if the violation cannot be found", async () => { - const violationData = { violationId: 999 }; // Non-existent violation - - // Mock deletion failure - (pool.query as Mock).mockResolvedValueOnce({ - rowCount: 0, - rows: [], - }); - - const response = await request(app) - .delete("/violations/delete") - .send(violationData); - - expect(response.status).toBe(404); - expect(response.body).toEqual({ - message: "Cannot find and delete the violation.", - }); - }); - }); -});