name:
src/test/js/formulas/statistical/MAXATest.ts
-rw-r--r--
3448
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 { MAXA } from "../../../../main/js/formulas/statistical/MAXA";
8import { Grid } from "../../../../main/js/models/common/Grid";
9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11import { assertF7ExceptionByName } from "../../testutils/TestUtils";
12
13describe("MAXA", 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 const GRID_WITH_TEXT_VALUE = Grid.builder()
22 .add(0, 0, -22.1)
23 .add(0, 1, "To zero.")
24 .add(0, 2, -10)
25 .build();
26 const GRID_WITH_BLANK_VALUES = Grid.builder()
27 .add(0, 0, 2)
28 .add(0, 1, 3)
29 .add(0, 2, 2)
30 // Leaving gap in range to simulate null/blank values.
31 .add(0, 100, 4)
32 .build();
33
34 it("should do min value for numbers", function () {
35 assert.deepEqual(MAXA.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8), 8);
36 assert.deepEqual(MAXA.SELF.run(null, 1, 2, 3, 4, 5, 10, 7, 8), 10);
37 assert.deepEqual(MAXA.SELF.run(null, 0), 0);
38 assert.deepEqual(MAXA.SELF.run(null, 2984723.99382), 2984723.99382);
39 assert.deepEqual(MAXA.SELF.run(null, 1, -2, 1000), 1000);
40 });
41
42 it("should return VALUE error when encountering non-coercable text values outside ranges", function () {
43 assertF7ExceptionByName(MAXA.SELF.run(null, 1, 2, "Bad"), F7ExceptionName.VALUE);
44 assert.deepEqual(MAXA.SELF.run(null, 0, "10"), 10);
45 });
46
47 it("should convert to text values to zero inside of grids", function () {
48 assert.deepEqual(MAXA.SELF.run(null, GRID_WITH_TEXT_VALUE), 0);
49 });
50
51 it("should return 0 when the resulting filter range is all null", function () {
52 assert.deepEqual(MAXA.SELF.run(null, Grid.builder().build()), 0);
53 });
54
55 it("should consider booleans", function () {
56 assert.deepEqual(MAXA.SELF.run(null, true, false, -10), 1);
57 });
58
59 it("should throw errors inside and outside of grids", function () {
60 assertF7ExceptionByName(MAXA.SELF.run(null, 1, 2, new RefException()), F7ExceptionName.REF);
61 assertF7ExceptionByName(
62 MAXA.SELF.run(null, Grid.builder().add(0, 0, new RefException()).build()),
63 F7ExceptionName.REF
64 );
65 });
66
67 it("should ignore blank cells (null values)", function () {
68 assert.deepEqual(MAXA.SELF.run(null, GRID_WITH_BLANK_VALUES), 4);
69 });
70
71 it("should use lookup function for range queries", function () {
72 const lookup = stub();
73 const collateralLookup = stub();
74 const F = new MAXA(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
75 lookup.withArgs(CommonModels.M22_RANGE).returns(GRID);
76 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 131289731);
77 assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
78 assert.equal(lookup.callCount, 1);
79 assert.isTrue(collateralLookup.notCalled);
80 });
81
82 it("should return error when argument lengths are wrong", function () {
83 assert.deepEqual((MAXA.SELF.run(null) as NAException).name, F7ExceptionName.NA);
84 });
85});