Skip to content

Commit

Permalink
test: Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Jan 9, 2025
1 parent f42a974 commit 2a26cf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 112 deletions.
119 changes: 19 additions & 100 deletions app/data/pricesPaidRepo.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// __tests__/pricesPaidRepo.test.ts
import { pricesPaidRepo } from "./pricesPaidRepo"; // Adjust the import according to your file structure
import prisma from "./db"; // Your Prisma setup file
import { pricesPaidRepo } from "./pricesPaidRepo";
import prisma from "./db";

jest.mock("./db", () => ({
pricesPaid: {
aggregate: jest.fn(), // Mock the aggregate method
pricesPaidSummary: {
findFirst: jest.fn(),
},
}));

const postcodeDistrict = "SW1A";
const postcodeArea = "SW1";
const postcodeSector = "SW1A1";
const houseType = "Detached";

describe("pricesPaidRepo", () => {
afterEach(() => {
jest.clearAllMocks(); // Clear mocks after each test
jest.clearAllMocks();
jest.resetAllMocks();
});

it("should return prices paid data for valid sector", async () => {
const postcodeDistrict = "SW1A"; // Example postcode district
const postcodeArea = "SW1"; // Example postcode area
const postcodeSector = "SW1A1"; // Example postcode sector
const houseType = "Detached"; // Example house type

// Mock the Prisma client response for sector
(prisma.pricesPaid.aggregate as jest.Mock).mockResolvedValueOnce({
_count: { id: 35 }, // Enough postcodes
_avg: { price: 500000 }, // Average price
it("should return prices paid data", async () => {
(prisma.pricesPaidSummary.findFirst as jest.Mock).mockResolvedValueOnce({
averagePrice: 500000,
transactionCount: 35,
postcode: postcodeSector,
});

const result = await pricesPaidRepo.getPricesPaidByPostcodeAndHouseType(
Expand All @@ -39,83 +39,8 @@ describe("pricesPaidRepo", () => {
});
});

it("should return prices paid data for valid district when sector count is below minimum", async () => {
const postcodeDistrict = "SW1A"; // Example postcode district
const postcodeArea = "SW1"; // Example postcode area
const postcodeSector = "SW1A1"; // Example postcode sector
const houseType = "Detached"; // Example house type

// Mock the Prisma client response for sector (below minimum)
(prisma.pricesPaid.aggregate as jest.Mock)
.mockResolvedValueOnce({
_count: { id: 25 }, // Below minimum
_avg: { price: 500000 },
})
.mockResolvedValueOnce({
_count: { id: 35 }, // Enough postcodes for district
_avg: { price: 600000 },
});

const result = await pricesPaidRepo.getPricesPaidByPostcodeAndHouseType(
postcodeDistrict,
postcodeArea,
postcodeSector,
houseType
);

expect(result).toEqual({
averagePrice: 600000,
numberOfTransactions: 35,
granularityPostcode: postcodeDistrict,
});
});

it("should return prices paid data for valid area when district count is below minimum", async () => {
const postcodeDistrict = "SW1A"; // Example postcode district
const postcodeArea = "SW1"; // Example postcode area
const postcodeSector = "SW1A1"; // Example postcode sector
const houseType = "Detached"; // Example house type

// Mock the Prisma client response for sector (below minimum)
(prisma.pricesPaid.aggregate as jest.Mock)
.mockResolvedValueOnce({
_count: { id: 25 }, // Below minimum
_avg: { price: 500000 },
})
.mockResolvedValueOnce({
_count: { id: 20 }, // Below minimum for district
_avg: { price: 600000 },
})
.mockResolvedValueOnce({
_count: { id: 40 }, // Enough postcodes for area
_avg: { price: 700000 },
});

const result = await pricesPaidRepo.getPricesPaidByPostcodeAndHouseType(
postcodeDistrict,
postcodeArea,
postcodeSector,
houseType
);

expect(result).toEqual({
averagePrice: 700000,
numberOfTransactions: 40,
granularityPostcode: postcodeArea,
});
});

it("should throw an error when average price is null", async () => {
const postcodeDistrict = "SW1A"; // Example postcode district
const postcodeArea = "SW1"; // Example postcode area
const postcodeSector = "SW1A1"; // Example postcode sector
const houseType = "Detached"; // Example house type

// Mock the Prisma client response for sector (below minimum)
(prisma.pricesPaid.aggregate as jest.Mock).mockResolvedValueOnce({
_count: { id: 35 }, // Enough postcodes
_avg: { price: null }, // Null average price
});
it("should throw an error if no transaction data is found", async () => {
(prisma.pricesPaidSummary.findFirst as jest.Mock).mockResolvedValueOnce(null);

await expect(
pricesPaidRepo.getPricesPaidByPostcodeAndHouseType(
Expand All @@ -130,13 +55,7 @@ describe("pricesPaidRepo", () => {
});

it("should throw an error for any other error", async () => {
const postcodeDistrict = "SW1A"; // Example postcode district
const postcodeArea = "SW1"; // Example postcode area
const postcodeSector = "SW1A1"; // Example postcode sector
const houseType = "Detached"; // Example house type

// Mock the Prisma client to throw an error
(prisma.pricesPaid.aggregate as jest.Mock).mockRejectedValueOnce(
(prisma.pricesPaidSummary.findFirst as jest.Mock).mockRejectedValueOnce(
new Error("Database error")
);

Expand Down
21 changes: 9 additions & 12 deletions app/services/calculationService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// __tests__/householdData.test.ts
import { getHouseholdData } from "./calculationService";
import { itlService } from "./itlService";
import { gdhiService } from "./gdhiService";
Expand All @@ -22,19 +21,17 @@ jest.mock("./socialRentAdjustmentsService");
jest.mock("./socialRentEarningsService");
jest.mock("./rentService");

// Mock the Prisma client
jest.mock("../data/db", () => {
return {
__esModule: true,
default: {
$disconnect: jest.fn().mockResolvedValue(undefined), // Mock disconnect to resolve successfully
$disconnect: jest.fn().mockResolvedValue(undefined),
socialRent: {
aggregate: jest.fn().mockReturnValue(Promise.resolve([])), // Mock aggregate method
aggregate: jest.fn().mockReturnValue(Promise.resolve([])),
},
pricesPaid: {
aggregate: jest.fn().mockReturnValue(Promise.resolve([])), // Mock aggregate for pricesPaid as well
pricesPaidSummary: {
findFirst: jest.fn().mockReturnValue(Promise.resolve([])),
},
// Add any other Prisma model methods that need to be mocked
},
};
});
Expand Down Expand Up @@ -73,7 +70,7 @@ describe("getHouseholdData", () => {
const mockGasBillYearly = 1200;
const mockHPI = 1.05;
const mockBuildPrice = 250000;
const mockPricesPaid = {
const mockPricesPaidSummary = {
averagePrice: 280000,
numberOfTransactions: 50,
granularityPostcode: "SE17",
Expand All @@ -98,7 +95,7 @@ describe("getHouseholdData", () => {
).mockResolvedValueOnce(mockBuildPrice);
(
pricesPaidService.getPricesPaidByPostcodeAndHouseType as jest.Mock
).mockResolvedValueOnce(mockPricesPaid);
).mockResolvedValueOnce(mockPricesPaidSummary);
(rentService.getByITL3 as jest.Mock).mockResolvedValueOnce(
mockAverageRentMonthly
);
Expand All @@ -119,16 +116,16 @@ describe("getHouseholdData", () => {
houseBedrooms: mockInput.houseBedrooms,
houseSize: mockInput.houseSize,
maintenancePercentage: mockInput.maintenancePercentage,
averagePrice: parseFloat(mockPricesPaid.averagePrice.toFixed(2)),
averagePrice: parseFloat(mockPricesPaidSummary.averagePrice.toFixed(2)),
itl3: mockITL3,
gdhi: mockGDHI,
hpi: mockHPI,
buildPrice: mockBuildPrice,
averageRentMonthly: mockAverageRentMonthly,
socialRentAdjustments: mockSocialRentAdjustments,
socialRentAverageEarning: mockSocialRentAverageEarning,
numberOfTransactions: mockPricesPaid.numberOfTransactions,
granularityPostcode: mockPricesPaid.granularityPostcode,
numberOfTransactions: mockPricesPaidSummary.numberOfTransactions,
granularityPostcode: mockPricesPaidSummary.granularityPostcode,
gasBillYearly: mockGasBillYearly,
});
});
Expand Down

0 comments on commit 2a26cf3

Please sign in to comment.