name:
src/test/js/formulas/statistical/AVERAGETest.ts
-rw-r--r--
2985
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 { AVERAGE } from "../../../../main/js/formulas/statistical/AVERAGE";
9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { CommonModels } from "../../testutils/CommonModels";
12
13describe("AVERAGE", 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
22 it("should do normal operations", function () {
23 assert.deepEqual(AVERAGE.SELF.run(null, 1, 2, 3, 4, 5, 6, 7, 8), 4.5);
24 assert.deepEqual(AVERAGE.SELF.run(null, 0), 0);
25 assert.deepEqual(AVERAGE.SELF.run(null, 2984723.99382), 2984723.99382);
26 assert.deepEqual(AVERAGE.SELF.run(null, 2984723e3), 2984723e3);
27 });
28
29 it("should do string conversion", function () {
30 assert.equal(AVERAGE.SELF.run(null, "10", "10"), 10);
31 assert.equal(AVERAGE.SELF.run(null, "10", "2"), 6);
32 assert.equal(AVERAGE.SELF.run(null, "10", "3"), 6.5);
33 assert.deepEqual(AVERAGE.SELF.run(null, "1", "2", "3", "4", "5", "6", "7", "8"), 4.5);
34 assert.deepEqual(AVERAGE.SELF.run(null, true, false, true, false, true), 0.6);
35 assert.deepEqual(AVERAGE.SELF.run(null, GRID), 1.3173240903122231e8 / 5);
36 });
37
38 it("should do pass-through errors", function () {
39 assert.deepEqual(
40 (AVERAGE.SELF.run(null, 1, new RefException()) as F7Exception).name,
41 F7ExceptionName.REF
42 );
43 assert.deepEqual(
44 (
45 AVERAGE.SELF.run(
46 null,
47 Grid.builder()
48 .add(0, 0, 22.1)
49 .add(0, 1, 324.3)
50 .add(0, 2, new RefException())
51 .add(0, 3, 442309.4)
52 .add(0, 4, 131289731)
53 .build()
54 ) as F7Exception
55 ).name,
56 F7ExceptionName.REF
57 );
58 });
59
60 it("should use lookup function for range queries", function () {
61 const lookup = stub();
62 const collateralLookup = stub();
63 const F = new AVERAGE(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
64 lookup.withArgs(CommonModels.M22_RANGE).returns(GRID);
65 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1.3173240903122231e8 / 5);
66 assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
67 assert.equal(lookup.callCount, 1);
68 assert.isTrue(collateralLookup.notCalled);
69 });
70
71 it("should return error when argument lengths are wrong", function () {
72 assert.deepEqual((AVERAGE.SELF.run(null) as NAException).name, F7ExceptionName.NA);
73 });
74});