Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename gdhi column #185

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,7 +23,7 @@ model GDHI {
itlLevel String @map("itl_level") @db.VarChar(250)
itl3 String @db.VarChar(250)
region String @db.VarChar(250)
gdhi2020 Float @map("gdhi_2020")
gdhi Float

@@map("gdhi")
}
Expand Down
Loading