name:
src/test/js/formulas/statistical/AVERAGEATest.ts
-rw-r--r--
4308
1import { assert } from "chai";
2import { it, describe } from "../../testutils/TestUtils";
3import { stub } from "sinon";
4import { F7Exception } from "../../../../main/js/errors/F7Exception";
5import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
6import { NAException } from "../../../../main/js/errors/NAException";
7import { RefException } from "../../../../main/js/errors/RefException";
8import { AVERAGEA } from "../../../../main/js/formulas/statistical/AVERAGEA";
9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { Converters } from "../../../../main/js/utils/Converters";
12import { CommonModels } from "../../testutils/CommonModels";
13
14describe("AVERAGEA", function () {
15 const GRID = Grid.builder()
16 .add(0, 0, 22.1)
17 .add(0, 1, 324.3)
18 .add(0, 2, 22.2312223131232)
19 .add(0, 3, 442309.4)
20 .add(0, 4, 131289731)
21 .build();
22 const GRID_CONTAINING_STRINGS = Grid.builder()
23 .add(0, 0, 1)
24 .add(0, 1, 2)
25 .add(0, 2, 3)
26 .add(0, 3, 4)
27 .add(0, 4, 5)
28 .add(0, 5, 6)
29 .add(0, 6, "6.0")
30 .add(0, 7, "To Zero Like Above")
31 .build();
32 const GRID_CONTAINING_ZEROS = Grid.builder()
33 .add(0, 0, 1)
34 .add(0, 1, 2)
35 .add(0, 2, 3)
36 .add(0, 3, 4)
37 .add(0, 4, 5)
38 .add(0, 5, 6)
39 .add(0, 6, 0)
40 .add(0, 7, 0)
41 .build();
42
43 it("should do normal operations", function () {
44 assert.deepEqual(AVERAGEA.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8), 4.5);
45 assert.deepEqual(AVERAGEA.SELF.run(null, 1, 2, 3, 4, 5, 6, 0, 0), 2.625);
46 assert.deepEqual(AVERAGEA.SELF.run(null, 0), 0);
47 assert.deepEqual(AVERAGEA.SELF.run(null, 2984723.99382), 2984723.99382);
48 assert.deepEqual(AVERAGEA.SELF.run(null, 2984723e3), 2984723e3);
49 });
50
51 it("should not filter values when they are plain parameters", function () {
52 assert.deepEqual(
53 Converters.castAsF7Exception(AVERAGEA.SELF.run(null, 1, 2, 3, 4, 5, 6, "6.o", "ToZero")).name,
54 F7ExceptionName.VALUE
55 );
56 });
57
58 it("should filter out zeros when they are plain parameters", function () {
59 assert.deepEqual(AVERAGEA.SELF.run(null, 1, 2, 3, 4, 5, 6, 0, 0), 2.625);
60 assert.deepEqual(AVERAGEA.SELF.run(null, 1, 2, 3, 4, 5, 6), 3.5);
61 });
62
63 it("should filter values when they are inside a range", function () {
64 assert.deepEqual(AVERAGEA.SELF.run(null, GRID_CONTAINING_STRINGS), 2.625);
65 assert.deepEqual(AVERAGEA.SELF.run(null, GRID_CONTAINING_ZEROS), 2.625);
66 });
67
68 it("should do string conversion outside of grid", function () {
69 assert.equal(AVERAGEA.SELF.run(null, "10", "10"), 10);
70 assert.equal(AVERAGEA.SELF.run(null, "10", "2"), 6);
71 assert.equal(AVERAGEA.SELF.run(null, "10", "3"), 6.5);
72 assert.deepEqual(AVERAGEA.SELF.run(null, "1", "2", "3", "4", "5", "6", "7", "8"), 4.5);
73 assert.deepEqual(AVERAGEA.SELF.run(null, true, false, true, false, true), 0.6);
74 assert.deepEqual(AVERAGEA.SELF.run(null, GRID), 1.3173240903122231e8 / 5);
75 });
76
77 it("should do pass-through errors", function () {
78 assert.deepEqual(
79 (AVERAGEA.SELF.run(null, 1, new RefException()) as F7Exception).name,
80 F7ExceptionName.REF
81 );
82 assert.deepEqual(
83 (
84 AVERAGEA.SELF.run(
85 null,
86 Grid.builder()
87 .add(0, 0, 22.1)
88 .add(0, 1, 324.3)
89 .add(0, 2, new RefException())
90 .add(0, 3, 442309.4)
91 .add(0, 4, 131289731)
92 .build()
93 ) as F7Exception
94 ).name,
95 F7ExceptionName.REF
96 );
97 });
98
99 it("should use lookup function for range queries", function () {
100 const lookup = stub();
101 const collateralLookup = stub();
102 const F = new AVERAGEA(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
103 lookup.withArgs(CommonModels.M22_RANGE).returns(GRID);
104 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1.3173240903122231e8 / 5);
105 assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
106 assert.equal(lookup.callCount, 1);
107 assert.isTrue(collateralLookup.notCalled);
108 });
109
110 it("should return error when argument lengths are wrong", function () {
111 assert.deepEqual((AVERAGEA.SELF.run(null) as NAException).name, F7ExceptionName.NA);
112 });
113});