Skip to content

Commit

Permalink
Merge pull request #187 from theopensystemslab/oz/hpi-year-update
Browse files Browse the repository at this point in the history
feat: update column name to hpi1999
  • Loading branch information
zz-hh-aa authored Dec 18, 2024
2 parents 92006d4 + 7095fb7 commit ff3c966
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
23 changes: 11 additions & 12 deletions app/data/hpiRepo.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// __tests__/hpiRepo.test.ts
import { hpi2000Repo } from "./hpiRepo"; // Adjust the import according to your file structure
import prisma from "./db"; // Your Prisma setup file
import { hpi1999Repo } from "./hpiRepo";
import prisma from "./db";

jest.mock("./db", () => ({
hPI: {
aggregate: jest.fn(),
},
}));

describe("hpi2000Repo", () => {
describe("hpi1999Repo", () => {
afterEach(() => {
jest.clearAllMocks();
});
Expand All @@ -19,11 +18,11 @@ describe("hpi2000Repo", () => {

// Mock the Prisma client response
(prisma.hPI.aggregate as jest.Mock).mockResolvedValueOnce({
_avg: { hpi2000: mockAverageHpi },
_avg: { hpi1999: mockAverageHpi },
});

// Call the function
const result = await hpi2000Repo.getHPIByITL3(itl3);
const result = await hpi1999Repo.getHPIByITL3(itl3);

// Assertions
expect(result).toBe(mockAverageHpi);
Expand All @@ -34,7 +33,7 @@ describe("hpi2000Repo", () => {
},
},
_avg: {
hpi2000: true,
hpi1999: true,
},
});
});
Expand All @@ -44,12 +43,12 @@ describe("hpi2000Repo", () => {

// Mock rejection of the Prisma client
(prisma.hPI.aggregate as jest.Mock).mockResolvedValueOnce({
_avg: { hpi2000: null },
_avg: { hpi1999: null },
});

// Call the function and expect an error
await expect(hpi2000Repo.getHPIByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find hpi2000 for itl3 ${itl3}`
await expect(hpi1999Repo.getHPIByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find hpi1999 for itl3 ${itl3}`
);
});

Expand All @@ -62,8 +61,8 @@ describe("hpi2000Repo", () => {
);

// Call the function and expect an error
await expect(hpi2000Repo.getHPIByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find hpi2000 for itl3 ${itl3}`
await expect(hpi1999Repo.getHPIByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find hpi1999 for itl3 ${itl3}`
);
});
});
10 changes: 5 additions & 5 deletions app/data/hpiRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import prisma from "./db";
const getHPIByITL3 = async (itl3: string): Promise<number> => {
try {
const {
_avg: { hpi2000: averageHpi },
_avg: { hpi1999: averageHpi },
} = await prisma.hPI.aggregate({
// TODO: Should there be a relationship on ITL3?
where: {
Expand All @@ -12,21 +12,21 @@ const getHPIByITL3 = async (itl3: string): Promise<number> => {
},
},
_avg: {
hpi2000: true,
hpi1999: true,
},
});

// Check if the average HPI is null and throw an error
if (averageHpi === null) {
throw new Error(`Data error: Unable to find hpi2000 for itl3 ${itl3}`);
throw new Error(`Data error: Unable to find hpi1999 for itl3 ${itl3}`);
}

return averageHpi;
} catch (error) {
throw Error(`Data error: Unable to find hpi2000 for itl3 ${itl3}`);
throw Error(`Data error: Unable to find hpi1999 for itl3 ${itl3}`);
}
};

export const hpi2000Repo = {
export const hpi1999Repo = {
getHPIByITL3,
};
8 changes: 4 additions & 4 deletions app/services/hpiService.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// __tests__/hpiService.test.ts
import { hpiService } from "../services/hpiService"; // Adjust the path according to your structure
import { hpi2000Repo } from "../data/hpiRepo"; // Adjust the path according to your structure
import { hpi1999Repo } from "../data/hpiRepo"; // Adjust the path according to your structure

jest.mock("../data/hpiRepo");

Expand All @@ -14,20 +14,20 @@ describe("hpiService.getByITL3", () => {
it("should return HPI for a valid ITL3", async () => {
// Arrange
const itl3 = "ITL3-123";
(hpi2000Repo.getHPIByITL3 as jest.Mock).mockResolvedValueOnce(mockHPI);
(hpi1999Repo.getHPIByITL3 as jest.Mock).mockResolvedValueOnce(mockHPI);

// Act
const result = await hpiService.getByITL3(itl3);

// Assert
expect(hpi2000Repo.getHPIByITL3).toHaveBeenCalledWith(itl3);
expect(hpi1999Repo.getHPIByITL3).toHaveBeenCalledWith(itl3);
expect(result).toBe(mockHPI);
});

it("should throw an error when the repo fails", async () => {
// Arrange
const errorMessage = "Failed to fetch HPI";
(hpi2000Repo.getHPIByITL3 as jest.Mock).mockRejectedValueOnce(
(hpi1999Repo.getHPIByITL3 as jest.Mock).mockRejectedValueOnce(
new Error(errorMessage)
);

Expand Down
4 changes: 2 additions & 2 deletions app/services/hpiService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hpi2000Repo } from "../data/hpiRepo";
import { hpi1999Repo } from "../data/hpiRepo";

const getByITL3 = async (itl3: string) => {
return await hpi2000Repo.getHPIByITL3(itl3);
return await hpi1999Repo.getHPIByITL3(itl3);
};

export const hpiService = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "hpi" RENAME COLUMN "hpi_2000" TO "hpi_1999";
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ model HPI {
id Int @id @default(autoincrement())
region String @db.VarChar(250)
itl3 String @db.VarChar(250)
hpi2000 Float @map("hpi_2000")
hpi1999 Float @map("hpi_1999")
@@map("hpi")
}
Expand Down

0 comments on commit ff3c966

Please sign in to comment.