Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
LeleDallas committed Jun 5, 2023
1 parent bfd6e54 commit c8ee3da
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
22 changes: 6 additions & 16 deletions db/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export const updateData = asyncHandler(async (req, res) => {
if (bills) {
res.status(200).json(bills)
}
} else {
throw Error("Error")
}
} else { throw Error("Error") }
})

export const getBills = asyncHandler(async (req, res) => {
Expand Down Expand Up @@ -233,22 +231,14 @@ export const getBillsAggregatedFiltered = asyncHandler(async (req, result) => {

export const getBillsByOrganizationIdAggregated = asyncHandler(async (req, res) => {
const id = req.params.id;
if (!id) {

throw Error('Error');
}
if (!id) { throw Error('Error'); }

const bills = await collections?.bills?.find({ organizationId: new ObjectId(id) }).toArray();
if (!bills || bills.length === 0) {

throw Error('Error');
}
if (!bills || bills.length === 0) { throw Error('Error'); }

const organization = await fetchOrganization(id);
if (!organization) {

throw Error('Error');
}
if (!organization) { throw Error('Error'); }

const { electric, gas, water } = computeTotals(bills, organization);

Expand All @@ -272,7 +262,7 @@ async function fetchOrganization(id: string) {
}
}

function computeTotals(bills: any[], organization: any) {
export function computeTotals(bills: any[], organization: any) {
let electric = 0;
let gas = 0;
let water = 0;
Expand All @@ -292,7 +282,7 @@ function computeTotals(bills: any[], organization: any) {
};
}

function computeAggregatedCosts(bills: any[], organization: any) {
export function computeAggregatedCosts(bills: any[], organization: any) {
const aggregated = new Map<any, any>();

bills.forEach((bill) => {
Expand Down
85 changes: 70 additions & 15 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeAll, describe, expect, it, vi } from "vitest";
import { hasCountedDay, addData, updateData, getBills, getBillsByOrganizationId, getBuildingBills, getBillsRenewableOnly, getBillsByOrganizationIdAggregated, getBillsAggregatedFiltered } from "../db/controller/controller";
import { hasCountedDay, addData, updateData, getBills, getBillsByOrganizationId, getBuildingBills, getBillsRenewableOnly, getBillsByOrganizationIdAggregated, getBillsAggregatedFiltered, computeTotals, computeAggregatedCosts } from "../db/controller/controller";
import { ObjectId } from "mongodb";
import { collections, connectToDatabase } from "../db/services/database.service";

Expand Down Expand Up @@ -40,10 +40,10 @@ describe('Bills controller', async () => {
body: {
buildingId: new ObjectId("111111111111"),
organizationId: new ObjectId("111111111111"),
electric: 9,
gas: 9,
water: 9,
resources: [{ Solar: 99 }, { Wind: 99 }, { Geo: 99 }, { Hydro: 99 }],
electric: 9,
gas: 9,
water: 9,
resources: [{ Solar: 99 }, { Wind: 99 }, { Geo: 99 }, { Hydro: 99 }],
date: Date.now()
},
params: {
Expand All @@ -55,16 +55,16 @@ describe('Bills controller', async () => {
expect(res.status).toHaveBeenCalledWith(200);
});


it('should return error for existing bill', async () => {
const req = {
body: {
buildingId: new ObjectId("111111111111"),
organizationId: new ObjectId("111111111111"),
electric: 10,
gas: 10,
water: 10,
resources: [{ Solar: 100 }, { Wind: 100 }, { Geo: 100 }, { Hydro: 100 }],
electric: 10,
gas: 10,
water: 10,
resources: [{ Solar: 100 }, { Wind: 100 }, { Geo: 100 }, { Hydro: 100 }],
date: Date.now()
},
params: {
Expand All @@ -89,10 +89,10 @@ describe('Bills controller', async () => {
const req = {
body: {
buildingId: new ObjectId("111111111111"),
electric: 100,
gas: 100,
water: 100,
resources: [{ Solar: 99 }, { Wind: 99 }, { Geo: 99 }, { Hydro: 99 }],
electric: 100,
gas: 100,
water: 100,
resources: [{ Solar: 99 }, { Wind: 99 }, { Geo: 99 }, { Hydro: 99 }],
date: Date.now()
},
params: {
Expand Down Expand Up @@ -262,4 +262,59 @@ describe('Bills controller', async () => {
expect(hasCountedDay(allDay, allDay[0])).toBe(false)
});

});
let bills: any = [
{
bills: [
{ electric: 10, gas: 20, water: 30 },
{ electric: 5, gas: 15, water: 25 },
],
},
{
bills: [
{ electric: 7, gas: 14, water: 21 },
{ electric: 3, gas: 12, water: 18 },
],
},
];

const organization = { type: ["Electric", "Gas", "Water"] };

const totals = computeTotals(bills, organization);

it("should calculate the total electric cost correctly", () => {
expect(totals.electric).toBe(25)
});

it("should calculate the total gas cost correctly", () => {
expect(totals.gas).toBe(61)
});

it("should calculate the total water cost correctly", () => {
expect(totals.water).toBe(94)
});


bills = [
{
bills: [
{ date: "2023-01-01", electric: 10, gas: 20, water: 30 },
{ date: "2023-01-01", electric: 5, gas: 15, water: 25 },
],
},
{
bills: [
{ date: "2023-01-02", electric: 7, gas: 14, water: 21 },
{ date: "2023-01-02", electric: 3, gas: 12, water: 18 },
],
},
];

const aggregatedCosts = computeAggregatedCosts(bills, organization);

it("should aggregate costs by date correctly", () => {
expect(aggregatedCosts).toStrictEqual({
"2023-01-01": { date: "2023-01-01", electric: 15, gas: 35, water: 55 },
"2023-01-02": { date: "2023-01-02", electric: 10, gas: 26, water: 39 },
});
});
});

0 comments on commit c8ee3da

Please sign in to comment.