name:
src/test/js/formulas/math/PRODUCTTest.ts
-rw-r--r--
3170
1import { assert } from "chai";
2import { it, describe } from "../../testutils/TestUtils";
3import { stub } from "sinon";
4import { F7Exception } from "../../../../main/js/errors/F7Exception";
5import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
6import { NAException } from "../../../../main/js/errors/NAException";
7import { RefException } from "../../../../main/js/errors/RefException";
8import { PRODUCT } from "../../../../main/js/formulas/math/PRODUCT";
9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { CommonModels } from "../../testutils/CommonModels";
12
13describe("PRODUCT", function () {
14 const GRID = Grid.builder()
15 .add(0, 0, 22.1)
16 .add(0, 1, 324.3)
17 .add(0, 2, 22.2312223131232)
18 .add(0, 3, 442309.4)
19 .add(0, 4, 13)
20 .build();
21
22 it("should do normal operations", function () {
23 assert.deepEqual(PRODUCT.SELF.run(null, 10, 10.0), 100);
24 assert.deepEqual(PRODUCT.SELF.run(null, 10, 2), 20);
25 assert.deepEqual(PRODUCT.SELF.run(null, 10, 3), 30);
26 assert.deepEqual(PRODUCT.SELF.run(null, 0, 1628736813.2), 0);
27 assert.deepEqual(PRODUCT.SELF.run(null, 21.22, 2876.111), 61031.075419999994);
28 assert.deepEqual(PRODUCT.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8), 40320);
29 assert.deepEqual(PRODUCT.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8, -1), -40320);
30 });
31
32 it("should do string conversion", function () {
33 assert.equal(PRODUCT.SELF.run(null, "10", "10"), 100);
34 assert.equal(PRODUCT.SELF.run(null, "10", "2"), 20);
35 assert.equal(PRODUCT.SELF.run(null, "10", "3"), 30);
36 assert.deepEqual(PRODUCT.SELF.run(null, "1", "2", "3", "4", "5", "6", "7", "8"), 40320);
37 assert.deepEqual(PRODUCT.SELF.run(null, true, false, true, false, true), 0);
38 assert.deepEqual(PRODUCT.SELF.run(null, GRID), 916161601382.0216);
39 });
40
41 it("should do pass-through errors", function () {
42 assert.deepEqual(
43 (PRODUCT.SELF.run(null, 1, new RefException()) as F7Exception).name,
44 F7ExceptionName.REF
45 );
46 assert.deepEqual(
47 (
48 PRODUCT.SELF.run(
49 null,
50 Grid.builder()
51 .add(0, 0, 22.1)
52 .add(0, 1, 324.3)
53 .add(0, 2, new RefException())
54 .add(0, 3, 442309.4)
55 .add(0, 4, 131289731)
56 .build()
57 ) as F7Exception
58 ).name,
59 F7ExceptionName.REF
60 );
61 });
62
63 it("should use lookup function for range queries", function () {
64 const lookup = stub();
65 const collateralLookup = stub();
66 const F = new PRODUCT(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
67 lookup.withArgs(CommonModels.M22_RANGE).returns(GRID);
68 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 916161601382.0216);
69 assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
70 assert.equal(lookup.callCount, 1);
71 assert.isTrue(collateralLookup.notCalled);
72 });
73
74 it("should return error when argument lengths are wrong", function () {
75 assert.deepEqual((PRODUCT.SELF.run(null) as NAException).name, F7ExceptionName.NA);
76 });
77});