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