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

Add blend pricing tests #12

Merged
merged 14 commits into from
Sep 25, 2024
Merged
61 changes: 61 additions & 0 deletions projects/planner/src/app/shared/blend-pricing.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { UnitConversion } from './UnitConversion';
import { GasBlenderService } from './gas-blender.service';
import { BlendPricingService } from './blend-pricing.service';

describe('BlendPricingService', () => {
let sut: BlendPricingService;
let gasBlender: GasBlenderService;

const createGasBlenderService = (imperialUnits: boolean = false): GasBlenderService => {
const units = new UnitConversion();
units.imperialUnits = imperialUnits;
return new GasBlenderService(units);
};

beforeEach(() => {
jirkapok marked this conversation as resolved.
Show resolved Hide resolved
gasBlender = createGasBlenderService();
sut = new BlendPricingService(gasBlender);

gasBlender.targetTank.o2 = 25;
gasBlender.targetTank.he = 25;
gasBlender.calculate();

sut.o2UnitPrice = 2;
sut.heUnitPrice = 3;
sut.topMixUnitPrice = 4;
});

it('Calculates gas prices', () => {
sut.calculate();

expect(sut.o2Price).toBeCloseTo(5.88235294, 8);
expect(sut.hePrice).toBeCloseTo(150);
expect(sut.topMixPrice).toBeCloseTo(588.2352941176471, 8);
expect(sut.totalPrice).toBeCloseTo(744.1176470576471, 8);
});

describe('Imperial units', () => {
beforeEach(() => {
gasBlender = createGasBlenderService(true);
sut = new BlendPricingService(gasBlender);

gasBlender.targetTank.o2 = 25;
gasBlender.targetTank.he = 25;
gasBlender.calculate();

sut.o2UnitPrice = 2;
sut.heUnitPrice = 3;
sut.topMixUnitPrice = 4;
});

it('Calculates gas prices in imperial units', () => {
sut.calculate();

expect(sut.o2Price).toBeCloseTo(85.31631629483086, 8);
expect(sut.hePrice).toBeCloseTo(2175.5660659533, 8);
expect(sut.topMixPrice).toBeCloseTo(8531.631631189413, 8);
expect(sut.totalPrice).toBeCloseTo(10792.514013437543, 8);
});
});
});

108 changes: 108 additions & 0 deletions projects/scuba-physics/src/lib/blendPricing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { BlendPricing, BlendCosts, BlendPrice } from './blendPricing';

describe('BlendPricing', () => {

it('should calculate prices according declared amount and price of gas', () => {

const costs: BlendCosts = {
addO2: 2,
o2UnitPrice: 5,
addHe: 3,
heUnitPrice: 8,
addTop: 4,
topMixUnitPrice: 10
};

const expected: BlendPrice = {
o2Price: 10,
hePrice: 24,
topMixPrice: 40,
totalPrice: 74
};

const result = BlendPricing.price(costs);

expect(result).toEqual(expected);
});

describe('Negative costs validation', () => {
it('should throw an error for negative addO2', () => {
const negativeCosts: BlendCosts = {
addO2: -1,
o2UnitPrice: 5,
addHe: 3,
heUnitPrice: 8,
addTop: 4,
topMixUnitPrice: 10
};

expect(() => BlendPricing.price(negativeCosts)).toThrowError('addO2 must be positive number');
});

it('should throw an error for negative o2UnitPrice', () => {
const negativeCosts: BlendCosts = {
addO2: 2,
o2UnitPrice: -5,
addHe: 3,
heUnitPrice: 8,
addTop: 4,
topMixUnitPrice: 10
};

expect(() => BlendPricing.price(negativeCosts)).toThrowError('o2UnitPrice must be positive number');
});

it('should throw an error for negative addHe', () => {
const negativeCosts: BlendCosts = {
addO2: 2,
o2UnitPrice: 5,
addHe: -3,
heUnitPrice: 8,
addTop: 4,
topMixUnitPrice: 10
};

expect(() => BlendPricing.price(negativeCosts)).toThrowError('addHe must be positive number');
});

it('should throw an error for negative heUnitPrice', () => {
const negativeCosts: BlendCosts = {
addO2: 2,
o2UnitPrice: 5,
addHe: 3,
heUnitPrice: -8,
addTop: 4,
topMixUnitPrice: 10
};

expect(() => BlendPricing.price(negativeCosts)).toThrowError('heUnitPrice must be positive number');
});

it('should throw an error for negative addTop', () => {
const negativeCosts: BlendCosts = {
addO2: 2,
o2UnitPrice: 5,
addHe: 3,
heUnitPrice: 8,
addTop: -4,
topMixUnitPrice: 10
};

expect(() => BlendPricing.price(negativeCosts)).toThrowError('addTop must be positive number');
});

it('should throw an error for negative topMixUnitPrice', () => {
const negativeCosts: BlendCosts = {
addO2: 2,
o2UnitPrice: 5,
addHe: 3,
heUnitPrice: 8,
addTop: 4,
topMixUnitPrice: -10
};

expect(() => BlendPricing.price(negativeCosts)).toThrowError('topMixUnitPrice must be positive number');
});
});
});