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

Accounting improvements #247

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
200 changes: 105 additions & 95 deletions src/lib/server/db/getCompanyCosts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ const testDays = [midnight - 30 * DAY];

describe('test accounting', () => {
it('empty db', async () => {
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(0);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(0);
});
it('no customer', async () => {
const c1 = await addCompany(1, dummyCoordinates);
const v1 = await addTaxi(c1, dummyCapacities);
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 1000);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(1000);
expect(companyCostsPerDay[0].capped).toBe(1000);
expect(companyCostsPerDay[0].uncapped).toBe(1000);
expect(companyCostsPerDay[0].customerCount).toBe(0);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(1000);
expect(costPerDayAndVehicle[0].capped).toBe(1000);
expect(costPerDayAndVehicle[0].uncapped).toBe(1000);
expect(costPerDayAndVehicle[0].customerCount).toBe(0);
});

it('one customer', async () => {
Expand All @@ -45,13 +45,13 @@ describe('test accounting', () => {
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 1000))!.id;
await setRequest(t1, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(1000);
expect(companyCostsPerDay[0].uncapped).toBe(700);
expect(companyCostsPerDay[0].capped).toBe(700);
expect(companyCostsPerDay[0].customerCount).toBe(1);
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(1000);
expect(costPerDayAndVehicle[0].uncapped).toBe(700);
expect(costPerDayAndVehicle[0].capped).toBe(700);
expect(costPerDayAndVehicle[0].customerCount).toBe(1);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
});

it('two customers', async () => {
Expand All @@ -62,13 +62,13 @@ describe('test accounting', () => {
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 400))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t1, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(400);
expect(companyCostsPerDay[0].uncapped).toBe(-200);
expect(companyCostsPerDay[0].capped).toBe(-200);
expect(companyCostsPerDay[0].customerCount).toBe(2);
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(400);
expect(costPerDayAndVehicle[0].uncapped).toBe(-200);
expect(costPerDayAndVehicle[0].capped).toBe(-200);
expect(costPerDayAndVehicle[0].customerCount).toBe(2);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
});

it('cap reached', async () => {
Expand All @@ -78,13 +78,13 @@ describe('test accounting', () => {
await setAvailability(v1, testDays[0] + HOUR * 5, testDays[0] + HOUR * 6);
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5000))!.id;
await setRequest(t1, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(5000);
expect(companyCostsPerDay[0].uncapped).toBe(4700);
expect(companyCostsPerDay[0].capped).toBe(3800);
expect(companyCostsPerDay[0].customerCount).toBe(1);
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(5000);
expect(costPerDayAndVehicle[0].uncapped).toBe(4700);
expect(costPerDayAndVehicle[0].capped).toBe(3800);
expect(costPerDayAndVehicle[0].customerCount).toBe(1);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
});

it('two tours on same day - cap reached', async () => {
Expand All @@ -96,13 +96,13 @@ describe('test accounting', () => {
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3400))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t2, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(5600);
expect(companyCostsPerDay[0].uncapped).toBe(5000);
expect(companyCostsPerDay[0].capped).toBe(3875);
expect(companyCostsPerDay[0].customerCount).toBe(2);
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(5600);
expect(costPerDayAndVehicle[0].uncapped).toBe(5000);
expect(costPerDayAndVehicle[0].capped).toBe(3875);
expect(costPerDayAndVehicle[0].customerCount).toBe(2);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
});

it('two tours and two availabilities on same day', async () => {
Expand All @@ -118,13 +118,13 @@ describe('test accounting', () => {
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3400))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t2, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(7650);
expect(companyCostsPerDay[0].uncapped).toBe(7050);
expect(companyCostsPerDay[0].capped).toBe(5700);
expect(companyCostsPerDay[0].customerCount).toBe(2);
expect(companyCostsPerDay[0].availabilityDuration).toBe(90 * MINUTE);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(7650);
expect(costPerDayAndVehicle[0].uncapped).toBe(7050);
expect(costPerDayAndVehicle[0].capped).toBe(5700);
expect(costPerDayAndVehicle[0].customerCount).toBe(2);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(90 * MINUTE);
});

it('availabilitiy on insignificant day', async () => {
Expand All @@ -134,13 +134,13 @@ describe('test accounting', () => {
await setAvailability(v1, midnight + 30 * MINUTE, midnight + 90 * MINUTE);
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 2600))!.id;
await setRequest(t1, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(2600);
expect(companyCostsPerDay[0].uncapped).toBe(2300);
expect(companyCostsPerDay[0].capped).toBe(575);
expect(companyCostsPerDay[0].availabilityDuration).toBe(0);
expect(companyCostsPerDay[0].customerCount).toBe(1);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(2600);
expect(costPerDayAndVehicle[0].uncapped).toBe(2300);
expect(costPerDayAndVehicle[0].capped).toBe(575);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(0);
expect(costPerDayAndVehicle[0].customerCount).toBe(1);
});

it('two tours same vehicle', async () => {
Expand All @@ -151,13 +151,13 @@ describe('test accounting', () => {
const t2 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 3000))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t2, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(5600);
expect(companyCostsPerDay[0].uncapped).toBe(5000);
expect(companyCostsPerDay[0].capped).toBe(1250);
expect(companyCostsPerDay[0].availabilityDuration).toBe(0);
expect(companyCostsPerDay[0].customerCount).toBe(2);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(5600);
expect(costPerDayAndVehicle[0].uncapped).toBe(5000);
expect(costPerDayAndVehicle[0].capped).toBe(1250);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(0);
expect(costPerDayAndVehicle[0].customerCount).toBe(2);
});

it('two tours different vehicles', async () => {
Expand All @@ -166,18 +166,23 @@ describe('test accounting', () => {
const v1 = await addTaxi(c1, dummyCapacities);
const v2 = await addTaxi(c1, dummyCapacities);
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
await setAvailability(v2, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
await setAvailability(v2, testDays[0] + 2 * HOUR, testDays[0] + 4 * HOUR);
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5100))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t2, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(10200);
expect(companyCostsPerDay[0].uncapped).toBe(9600);
expect(companyCostsPerDay[0].capped).toBe(7650);
expect(companyCostsPerDay[0].availabilityDuration).toBe(2 * HOUR);
expect(companyCostsPerDay[0].customerCount).toBe(2);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(2);
expect(costPerDayAndVehicle[0].taxameter).toBe(5100);
expect(costPerDayAndVehicle[0].uncapped).toBe(4800);
expect(costPerDayAndVehicle[0].capped).toBe(3825);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
expect(costPerDayAndVehicle[0].customerCount).toBe(1);
expect(costPerDayAndVehicle[1].taxameter).toBe(5100);
expect(costPerDayAndVehicle[1].uncapped).toBe(4800);
expect(costPerDayAndVehicle[1].capped).toBe(4800);
expect(costPerDayAndVehicle[1].availabilityDuration).toBe(2 * HOUR);
expect(costPerDayAndVehicle[1].customerCount).toBe(1);
});

it('tours with 2 customers', async () => {
Expand All @@ -187,13 +192,13 @@ describe('test accounting', () => {
await setAvailability(v1, testDays[0] + 3 * HOUR, testDays[0] + 4 * HOUR);
const t1 = (await setTour(v1, testDays[0] + HOUR, testDays[0] + HOUR * 2, 5700))!.id;
await setRequest(t1, u.id, '', 2, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(5700);
expect(companyCostsPerDay[0].uncapped).toBe(5100);
expect(companyCostsPerDay[0].capped).toBe(3900);
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
expect(companyCostsPerDay[0].customerCount).toBe(2);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(1);
expect(costPerDayAndVehicle[0].taxameter).toBe(5700);
expect(costPerDayAndVehicle[0].uncapped).toBe(5100);
expect(costPerDayAndVehicle[0].capped).toBe(3900);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
expect(costPerDayAndVehicle[0].customerCount).toBe(2);
});

it('overlapping availabilities', async () => {
Expand All @@ -208,13 +213,18 @@ describe('test accounting', () => {
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 35300))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t2, u.id, '', 1, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(1);
expect(companyCostsPerDay[0].taxameter).toBe(40400);
expect(companyCostsPerDay[0].uncapped).toBe(39800);
expect(companyCostsPerDay[0].capped).toBe(24050);
expect(companyCostsPerDay[0].availabilityDuration).toBe(7 * HOUR);
expect(companyCostsPerDay[0].customerCount).toBe(2);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(2);
expect(costPerDayAndVehicle[0].taxameter).toBe(5100);
expect(costPerDayAndVehicle[0].uncapped).toBe(4800);
expect(costPerDayAndVehicle[0].capped).toBe(4800);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(3 * HOUR);
expect(costPerDayAndVehicle[0].customerCount).toBe(1);
expect(costPerDayAndVehicle[1].taxameter).toBe(35300);
expect(costPerDayAndVehicle[1].uncapped).toBe(35000);
expect(costPerDayAndVehicle[1].capped).toBe(19250);
expect(costPerDayAndVehicle[1].availabilityDuration).toBe(4 * HOUR);
expect(costPerDayAndVehicle[1].customerCount).toBe(1);
});

it('2 companies', async () => {
Expand All @@ -229,21 +239,21 @@ describe('test accounting', () => {
const t2 = (await setTour(v2, testDays[0] + HOUR, testDays[0] + HOUR * 2, 9200))!.id;
await setRequest(t1, u.id, '', 1, true);
await setRequest(t2, u.id, '', 2, true);
const { companyCostsPerDay } = await getCompanyCosts();
expect(companyCostsPerDay).toHaveLength(2);
expect(companyCostsPerDay[0].timestamp).toBe(companyCostsPerDay[1].timestamp);
companyCostsPerDay.sort((c1, c2) => c1.companyId - c2.companyId);

expect(companyCostsPerDay[0].taxameter).toBe(5100);
expect(companyCostsPerDay[0].uncapped).toBe(4800);
expect(companyCostsPerDay[0].capped).toBe(3825);
expect(companyCostsPerDay[0].availabilityDuration).toBe(HOUR);
expect(companyCostsPerDay[0].customerCount).toBe(1);

expect(companyCostsPerDay[1].taxameter).toBe(9200);
expect(companyCostsPerDay[1].uncapped).toBe(8600);
expect(companyCostsPerDay[1].capped).toBe(7400);
expect(companyCostsPerDay[1].availabilityDuration).toBe(2 * HOUR);
expect(companyCostsPerDay[1].customerCount).toBe(2);
const { costPerDayAndVehicle } = await getCompanyCosts();
expect(costPerDayAndVehicle).toHaveLength(2);
expect(costPerDayAndVehicle[0].timestamp).toBe(costPerDayAndVehicle[1].timestamp);
costPerDayAndVehicle.sort((c1, c2) => c1.companyId - c2.companyId);

expect(costPerDayAndVehicle[0].taxameter).toBe(5100);
expect(costPerDayAndVehicle[0].uncapped).toBe(4800);
expect(costPerDayAndVehicle[0].capped).toBe(3825);
expect(costPerDayAndVehicle[0].availabilityDuration).toBe(HOUR);
expect(costPerDayAndVehicle[0].customerCount).toBe(1);

expect(costPerDayAndVehicle[1].taxameter).toBe(9200);
expect(costPerDayAndVehicle[1].uncapped).toBe(8600);
expect(costPerDayAndVehicle[1].capped).toBe(7400);
expect(costPerDayAndVehicle[1].availabilityDuration).toBe(2 * HOUR);
expect(costPerDayAndVehicle[1].customerCount).toBe(2);
});
});
Loading