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