1 |
|
2 |
|
3 |
|
4 | import { expect } from 'chai';
|
5 | import { Categorical, Temporal, Binned, Continuous } from './fields';
|
6 | import { createFields, createUnitFieldFromPartial } from './field-creator';
|
7 | import { MeasureSubtype, DimensionSubtype } from './enums';
|
8 |
|
9 | describe('Creating Field', () => {
|
10 | describe('#createUnitFieldFromPartial', () => {
|
11 | it('should return an array of correct field instances', () => {
|
12 | let mockedPartialField = {
|
13 | schema: { name: 'Country' }
|
14 | };
|
15 | let mockedRowDiffset = '1-2';
|
16 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Categorical).to.be.true;
|
17 |
|
18 | mockedPartialField.schema = { name: 'Country', type: 'dimension', subtype: DimensionSubtype.CATEGORICAL };
|
19 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Categorical).to.be.true;
|
20 |
|
21 | mockedPartialField.schema = { name: 'Country', type: 'dimension', subtype: DimensionSubtype.TEMPORAL };
|
22 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Temporal).to.be.true;
|
23 |
|
24 | mockedPartialField.schema = { name: 'Country', type: 'dimension', subtype: DimensionSubtype.BINNED };
|
25 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Binned).to.be.true;
|
26 |
|
27 | mockedPartialField.schema = { name: 'Country', type: 'dimension' };
|
28 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Categorical).to.be.true;
|
29 |
|
30 | mockedPartialField.schema = { name: 'Country', type: 'measure' };
|
31 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Continuous).to.be.true;
|
32 |
|
33 | mockedPartialField.schema = { name: 'Country', type: 'measure', subtype: MeasureSubtype.CONTINUOUS };
|
34 | expect(createUnitFieldFromPartial(mockedPartialField, mockedRowDiffset) instanceof Continuous).to.be.true;
|
35 | });
|
36 | });
|
37 |
|
38 | describe('#createFields', () => {
|
39 | it('should return an array of correct field instances', () => {
|
40 | const data = [
|
41 | ['India', 'China'],
|
42 | ['John', 'Smith'],
|
43 | ['2018-01-01', '2018-01-01'],
|
44 | ['10-20', '20-30'],
|
45 | [2000000, 4800000],
|
46 | [56000, 100000]
|
47 | ];
|
48 | const schema = [
|
49 | { name: 'Country', type: 'dimension' },
|
50 | { name: 'Name', type: 'dimension', subtype: DimensionSubtype.CATEGORICAL },
|
51 | { name: 'Date', type: 'dimension', subtype: DimensionSubtype.TEMPORAL, format: '%Y-%m-%d' },
|
52 | { name: 'Age', type: 'dimension', subtype: DimensionSubtype.BINNED, bins: [10, 20, 30, 45] },
|
53 | { name: 'Job', type: 'measure' },
|
54 | { name: 'Salary', type: 'measure', subtype: MeasureSubtype.CONTINUOUS }
|
55 | ];
|
56 | const headers = ['Country', 'Name', 'Date', 'Age', 'Job', 'Salary'];
|
57 | const fieldsArr = createFields(data, schema, headers);
|
58 |
|
59 | expect(fieldsArr.length === 6).to.be.true;
|
60 | expect(fieldsArr[0] instanceof Categorical).to.be.true;
|
61 | expect(fieldsArr[1] instanceof Categorical).to.be.true;
|
62 | expect(fieldsArr[2] instanceof Temporal).to.be.true;
|
63 | expect(fieldsArr[3] instanceof Binned).to.be.true;
|
64 | expect(fieldsArr[4] instanceof Continuous).to.be.true;
|
65 | expect(fieldsArr[5] instanceof Continuous).to.be.true;
|
66 | });
|
67 |
|
68 | it('should return categorical dimension field when type is not specified', () => {
|
69 | const data = [
|
70 | ['India', 'China'],
|
71 | [99000, 100000]
|
72 | ];
|
73 | const schema = [
|
74 | { name: 'Country' },
|
75 | { name: 'Salary', type: 'measure', subtype: MeasureSubtype.CONTINUOUS }
|
76 | ];
|
77 | const headers = ['Country', 'Salary'];
|
78 | const fieldsArr = createFields(data, schema, headers);
|
79 |
|
80 | expect(fieldsArr[0] instanceof Categorical).to.be.true;
|
81 | });
|
82 | });
|
83 | });
|
84 |
|