f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/test/js/formulas/math/SUMTest.ts
-rw-r--r--
2898
 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 { SUM } from "../../../../main/js/formulas/math/SUM";
 9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { CommonModels } from "../../testutils/CommonModels";
12
13describe("SUM", 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, 131289731)
20    .build();
21
22  it("should do normal operations", function () {
23    assert.deepEqual(SUM.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8), 36);
24    assert.deepEqual(SUM.SELF.run(null, 0), 0);
25    assert.deepEqual(SUM.SELF.run(null, 2984723.99382), 2984723.99382);
26    assert.deepEqual(SUM.SELF.run(null, 2984723e3), 2984723e3);
27  });
28
29  it("should do string conversion", function () {
30    assert.equal(SUM.SELF.run(null, "10", "10"), 20);
31    assert.equal(SUM.SELF.run(null, "10", "2"), 12);
32    assert.equal(SUM.SELF.run(null, "10", "3"), 13);
33    assert.deepEqual(SUM.SELF.run(null, "1", "2", "3", "4", "5", "6", "7", "8"), 36);
34    assert.deepEqual(SUM.SELF.run(null, true, false, true, false, true), 3);
35    assert.deepEqual(SUM.SELF.run(null, GRID), 1.3173240903122231e8);
36  });
37
38  it("should do pass-through errors", function () {
39    assert.deepEqual(
40      (SUM.SELF.run(null, 1, new RefException()) as F7Exception).name,
41      F7ExceptionName.REF
42    );
43    assert.deepEqual(
44      (
45        SUM.SELF.run(
46          null,
47          Grid.builder()
48            .add(0, 0, 22.1)
49            .add(0, 1, 324.3)
50            .add(0, 2, new RefException())
51            .add(0, 3, 442309.4)
52            .add(0, 4, 131289731)
53            .build()
54        ) as F7Exception
55      ).name,
56      F7ExceptionName.REF
57    );
58  });
59
60  it("should use lookup function for range queries", function () {
61    const lookup = stub();
62    const collateralLookup = stub();
63    const F = new SUM(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
64    lookup.withArgs(CommonModels.M22_RANGE).returns(GRID);
65    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1.3173240903122231e8);
66    assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
67    assert.equal(lookup.callCount, 1);
68    assert.isTrue(collateralLookup.notCalled);
69  });
70
71  it("should return error when argument lengths are wrong", function () {
72    assert.deepEqual((SUM.SELF.run(null) as NAException).name, F7ExceptionName.NA);
73  });
74});