From 3d56d2b74f0eed60072f0d5ab92ece32261124c8 Mon Sep 17 00:00:00 2001
From: Federico Hurtado <fed_home@Federicos-Mac-mini.local>
Date: Wed, 23 Oct 2024 09:52:52 -0400
Subject: [PATCH] Unit tests for peopleRepository.

---
 backend/package.json                          |  6 +-
 backend/repository/peopleRepository.js        |  5 ++
 .../test/repository/peopleRepository.test.js  | 73 +++++++++++++++++++
 3 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 backend/test/repository/peopleRepository.test.js

diff --git a/backend/package.json b/backend/package.json
index fe08ad0..4d6b96b 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -5,7 +5,7 @@
   "main": "index.js",
   "scripts": {
     "start": "nodemon index.js",
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "jest"
   },
   "author": "",
   "license": "ISC",
@@ -21,5 +21,9 @@
     "passport": "^0.7.0",
     "passport-cas": "^0.1.1",
     "sequelize": "^6.37.4"
+  },
+  "devDependencies": {
+    "jest": "^29.7.0",
+    "jest-mock": "^29.7.0"
   }
 }
diff --git a/backend/repository/peopleRepository.js b/backend/repository/peopleRepository.js
index 680b8fb..5191ddc 100644
--- a/backend/repository/peopleRepository.js
+++ b/backend/repository/peopleRepository.js
@@ -47,7 +47,12 @@ async function addPerson(person) {
     return results.insertId;
   } catch (error) {
     console.error(error);
+    return null;
   }
 }
 
+async function getPersonById() {}
+
+async function deletePersonById() {}
+
 module.exports = { addPerson };
diff --git a/backend/test/repository/peopleRepository.test.js b/backend/test/repository/peopleRepository.test.js
new file mode 100644
index 0000000..c29c280
--- /dev/null
+++ b/backend/test/repository/peopleRepository.test.js
@@ -0,0 +1,73 @@
+// Mock the database's pool and query function
+jest.mock("../../config/database/database.config", () => {
+  const queryMock = jest.fn(); // Mock query function
+  return { pool: { query: queryMock } }; // Return the mocked pool directly
+});
+
+const { pool } = require("../../config/database/database.config");
+const { addPerson } = require("../../repository/peopleRepository");
+
+/*
+Test methods for peopleRepository
+*/
+describe("Testing peopleRepository.js", () => {
+  // mock person for testing
+  const mockPerson = {
+    firstName: "John",
+    lastName: "Doe",
+    middleName: "A",
+    maidenName: null,
+    suffix: null,
+    nickName: "Johnny",
+    techAlumniChapter: "Tidewater AC",
+    classYear: "2024",
+    gradYear: "2024",
+    gradSemester: "spring",
+    gender: "male",
+  };
+
+  /*
+  Tests for the addPerson method
+  */
+  describe("addPerson Tests", () => {
+    it("Insert works with no errors", async () => {
+      // Mock the database to return the new insert ID as 1
+      const mockInsertId = 1;
+      pool.query.mockResolvedValueOnce([{ insertId: mockInsertId }]);
+
+      // call add person
+      const result = await addPerson(mockPerson);
+
+      // Verify the SQL query and parameters
+      expect(pool.query).toHaveBeenCalledWith(
+        expect.any(String), // SQL query string, can be further detailed if needed
+        [
+          "John",
+          "Doe",
+          "A",
+          null,
+          null,
+          "Johnny",
+          2, // hardcoded for now
+          "2024",
+          "2024",
+          "spring",
+          "male",
+        ]
+      );
+
+      // expect function to return the new insert id
+      expect(result).toBe(mockInsertId);
+    });
+
+    it("insert throws an error if query fails", async () => {
+      // Mock the database to throw an error
+      const mockError = new Error("Database query failed");
+      pool.query.mockRejectedValueOnce(mockError);
+
+      // call addPerson and expect it to handle the error
+      const result = await addPerson(mockPerson);
+      expect(result).toBe(null);
+    });
+  });
+});
-- 
GitLab