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