Skip to content

Commit

Permalink
Merge pull request #185 from theopensystemslab/oz/rename-gdhi-column
Browse files Browse the repository at this point in the history
feat: rename gdhi column
  • Loading branch information
zz-hh-aa authored Dec 18, 2024
2 parents 4eb3215 + 6953710 commit 919bb1c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
14 changes: 7 additions & 7 deletions app/data/gdhiRepo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe("gdhiRepo", () => {

// Mock the Prisma client response
(prisma.gDHI.findFirstOrThrow as jest.Mock).mockResolvedValueOnce({
gdhi2020: mockGDHI,
gdhi: mockGDHI,
});

// Call the function
const result = await gdhiRepo.getGDHI2020ByITL3(itl3);
const result = await gdhiRepo.getGDHIByITL3(itl3);

// Assertions
expect(result).toBe(mockGDHI);
Expand All @@ -33,7 +33,7 @@ describe("gdhiRepo", () => {
itl3: { equals: itl3 },
},
},
select: { gdhi2020: true },
select: { gdhi: true },
});
});

Expand All @@ -46,8 +46,8 @@ describe("gdhiRepo", () => {
);

// Call the function and expect an error
await expect(gdhiRepo.getGDHI2020ByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find gdhi2020 for itl3 ${itl3}`
await expect(gdhiRepo.getGDHIByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find gdhi for itl3 ${itl3}`
);
});

Expand All @@ -60,8 +60,8 @@ describe("gdhiRepo", () => {
);

// Call the function and expect an error
await expect(gdhiRepo.getGDHI2020ByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find gdhi2020 for itl3 ${itl3}`
await expect(gdhiRepo.getGDHIByITL3(itl3)).rejects.toThrow(
`Data error: Unable to find gdhi for itl3 ${itl3}`
);
});
});
13 changes: 6 additions & 7 deletions app/data/gdhiRepo.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import prisma from "./db";

const getGDHI2020ByITL3 = async (itl3: string): Promise<number> => {
const getGDHIByITL3 = async (itl3: string): Promise<number> => {
try {
const { gdhi2020 } = await prisma.gDHI.findFirstOrThrow({
const { gdhi } = await prisma.gDHI.findFirstOrThrow({
where: {
AND: {
itl3: { equals: itl3 },
},
},
select: { gdhi2020: true },
select: { gdhi: true },
});

return gdhi2020;
return gdhi;
} catch (error) {
throw Error(`Data error: Unable to find gdhi2020 for itl3 ${itl3}`);
throw Error(`Data error: Unable to find gdhi for itl3 ${itl3}`);
}
};

export const gdhiRepo = {
getGDHI2020ByITL3,
getGDHIByITL3,
};

6 changes: 3 additions & 3 deletions app/services/gdhiService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ describe("gdhiService.getByITL3", () => {
it("should return GDHI for a valid ITL3", async () => {
// Arrange
const itl3 = "ITL3-123";
(gdhiRepo.getGDHI2020ByITL3 as jest.Mock).mockResolvedValueOnce(mockGDHI);
(gdhiRepo.getGDHIByITL3 as jest.Mock).mockResolvedValueOnce(mockGDHI);

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

// Assert
expect(gdhiRepo.getGDHI2020ByITL3).toHaveBeenCalledWith(itl3);
expect(gdhiRepo.getGDHIByITL3).toHaveBeenCalledWith(itl3);
expect(result).toBe(mockGDHI);
});

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

Expand Down
2 changes: 1 addition & 1 deletion app/services/gdhiService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gdhiRepo } from "../data/gdhiRepo";

const getByITL3 = async (itl3: string) => {
return await gdhiRepo.getGDHI2020ByITL3(itl3);
return await gdhiRepo.getGDHIByITL3(itl3);
};

export const gdhiService = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "gdhi" RENAME COLUMN "gdhi_2020" TO "gdhi";
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ model GDHI {
id Int @id @default(autoincrement())
itl3 String @db.VarChar(250)
region String @db.VarChar(250)
gdhi2020 Float @map("gdhi_2020")
gdhi Float
@@map("gdhi")
}
Expand Down

0 comments on commit 919bb1c

Please sign in to comment.