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/statistical/COUNTTest.ts
-rw-r--r--
2852
 1import { assert } from "chai";
 2import { it, describe } from "../../testutils/TestUtils";
 3import { stub } from "sinon";
 4import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
 5import { NAException } from "../../../../main/js/errors/NAException";
 6import { RefException } from "../../../../main/js/errors/RefException";
 7import { COUNT } from "../../../../main/js/formulas/statistical/COUNT";
 8import { Grid } from "../../../../main/js/models/common/Grid";
 9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("COUNT", function () {
13  const GRID = Grid.builder()
14    .add(0, 0, 22.1)
15    .add(0, 1, 324.3)
16    .add(0, 2, 22.2312223131232)
17    .add(0, 3, 442309.4)
18    .add(0, 4, 131289731)
19    .build();
20
21  it("should do normal operations", function () {
22    assert.deepEqual(COUNT.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8), 8);
23    assert.deepEqual(COUNT.SELF.run(null, 0), 1);
24    assert.deepEqual(COUNT.SELF.run(null, 2984723.99382), 1);
25    assert.deepEqual(COUNT.SELF.run(null, 1, 2), 2);
26  });
27
28  it("should count only numerics inside and outside of grids", function () {
29    assert.deepEqual(
30      COUNT.SELF.run(
31        null,
32        Grid.builder()
33          .add(0, 0, 22.1)
34          .add(0, 1, 324.3)
35          .add(0, 2, "No count.")
36          .add(0, 3, 442309.4)
37          .add(0, 4, 131289731)
38          .build(),
39        "No Count",
40        1,
41        2,
42        true
43      ),
44      7
45    );
46    assert.deepEqual(
47      COUNT.SELF.run(null, Grid.builder().add(0, 0, 22.1).add(0, 1, "No.").build(), "No.", true),
48      2
49    );
50  });
51
52  it("should count booleans", function () {
53    assert.deepEqual(COUNT.SELF.run(null, true, false, true, false), 4);
54  });
55
56  it("should ignore errors inside of grids", function () {
57    assert.deepEqual(COUNT.SELF.run(null, Grid.builder().add(0, 0, new RefException()).build()), 0);
58  });
59
60  it("should ignore errors outside of grids", function () {
61    assert.deepEqual(
62      COUNT.SELF.run(null, Grid.builder().add(0, 0, 1).build(), new RefException(), 2),
63      2
64    );
65  });
66
67  it("should use lookup function for range queries", function () {
68    const lookup = stub();
69    const collateralLookup = stub();
70    const F = new COUNT(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
71    lookup.withArgs(CommonModels.M22_RANGE).returns(GRID);
72    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 5);
73    assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
74    assert.equal(lookup.callCount, 1);
75    assert.isTrue(collateralLookup.notCalled);
76  });
77
78  it("should return error when argument lengths are wrong", function () {
79    assert.deepEqual((COUNT.SELF.run(null) as NAException).name, F7ExceptionName.NA);
80  });
81});