From c8ee3da3c55ccb7aa8d189d09f5df57541888e15 Mon Sep 17 00:00:00 2001 From: Dallas <71103219+LeleDallas@users.noreply.github.com> Date: Mon, 5 Jun 2023 11:56:19 +0200 Subject: [PATCH] test: update test --- db/controller/controller.ts | 22 +++------- test/controller.test.ts | 85 ++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 31 deletions(-) diff --git a/db/controller/controller.ts b/db/controller/controller.ts index 75e3e10..3184f5f 100644 --- a/db/controller/controller.ts +++ b/db/controller/controller.ts @@ -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) => { @@ -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); @@ -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; @@ -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(); bills.forEach((bill) => { diff --git a/test/controller.test.ts b/test/controller.test.ts index 18ad698..32454ea 100644 --- a/test/controller.test.ts +++ b/test/controller.test.ts @@ -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"; @@ -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: { @@ -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: { @@ -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: { @@ -262,4 +262,59 @@ describe('Bills controller', async () => { expect(hasCountedDay(allDay, allDay[0])).toBe(false) }); -}); \ No newline at end of file + 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 }, + }); + }); +});