-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathutils.test.ts
More file actions
281 lines (230 loc) · 11 KB
/
utils.test.ts
File metadata and controls
281 lines (230 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { assert } from "chai";
import { describe, it } from "mocha";
import { ArrayType1D } from "../dist/danfojs-base/shared/types";
import { Utils, __version } from "../dist/danfojs-node/src";
import packagejson from "../package.json";
const pversion = packagejson.version;
const utils = new Utils();
describe("Utils", function () {
it("should have a version", function () {
assert.equal(__version, pversion);
});
it("removes an element from an array", function () {
let arr = [1, 2, 3, 4];
assert.deepEqual(utils.removeElementFromArray(arr, 2), [1, 2, 4]);
});
it("Checks if variable is a string", function () {
let arr = ["1", "2"];
assert.isTrue(utils.isString(arr[0]));
});
it("Checks if variable is a number", function () {
let arr = [1, 2, 3, 4];
assert.isTrue(utils.isNumber(arr[0]));
});
it("Checks if value is null", function () {
let val = null;
let val2 = 1;
assert.isTrue(utils.isNull(val));
assert.isFalse(utils.isNull(val2));
});
it("Checks if value is undefined", function () {
let arr;
assert.isTrue(utils.isUndefined(arr));
});
describe("isEmpty", function () {
it("should return true for null values", function () {
assert.isTrue(utils.isEmpty(null));
});
it("should return true for undefined values", function () {
assert.isTrue(utils.isEmpty(undefined));
});
it("should return true for NaN values", function () {
assert.isTrue(utils.isEmpty(NaN));
});
it("should return false for strings (including empty strings)", function () {
assert.isFalse(utils.isEmpty(""));
assert.isFalse(utils.isEmpty(" "));
assert.isFalse(utils.isEmpty("hello"));
});
it("should return false for numbers (except NaN)", function () {
assert.isFalse(utils.isEmpty(0));
assert.isFalse(utils.isEmpty(-1));
assert.isFalse(utils.isEmpty(42.5));
});
it("should return false for BigInt values", function () {
assert.isFalse(utils.isEmpty(BigInt(9007199254740991)));
assert.isFalse(utils.isEmpty(BigInt(0)));
});
it("should return false for objects and arrays", function () {
assert.isFalse(utils.isEmpty({}));
assert.isFalse(utils.isEmpty([]));
assert.isFalse(utils.isEmpty({ key: "value" }));
assert.isFalse(utils.isEmpty([1, 2, 3]));
});
it("should return false for boolean values", function () {
assert.isFalse(utils.isEmpty(true));
assert.isFalse(utils.isEmpty(false));
});
});
it("Checks if value is a valid Date object", function () {
let date1 = new Date();
let date2 = "2021-01-01 00:00:00";
let isoDate = "2021-01-01T00:00:00.000Z";
assert.isTrue(utils.isDate(date1));
assert.isTrue(utils.isDate(date2));
assert.isTrue(utils.isDate(isoDate));
});
it("Generate numbers between two set of values [both inclusive]", function () {
let start = 0;
let end = 5;
let data = [0, 1, 2, 3, 4, 5];
assert.deepEqual(utils.range(start, end), data);
});
it("transposes an array", function () {
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40]];
let result = [[1, 4, 20], [2, 5, 30], [3, 6, 40]];
assert.deepEqual(utils.transposeArray(data), result);
});
describe("inferDtype", function () {
it("Returns string type present in an 1D array", function () {
let data = ['Alice', 'Boy', 'Girl', "39"];
let result = ['string'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns float type present in an 1D array", function () {
let data = [1.1, 2.1, 3.2, 4.4];
let result = ['float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns int type present in an 1D array", function () {
let data = [1, 2, 3, 45];
let result = ['int32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns float when there's a mixture of int and float in a 1D array", function () {
let data = [1, 2.1, 3, 45];
let result = ['float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns float type when NaN is present in an 1D array", function () {
let data = [1, 2, 3, 45, NaN];
let result = ['float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns correct dtype if NaN present in data", function () {
let data = utils.transposeArray([
[18.7, 17.4, 18, NaN, 19.3],
[20, NaN, 19, 18, 20]])
let result = ['float32', 'float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns the data type present in an 2D array", function () {
let data = utils.transposeArray([['Alice', 'Boy', 'Girl', "39"], [2, 5, 30, 89], [3.1, 6.1, 40.1, 78.2]])
let result = ['string', 'int32', 'float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns the string dtype when there's a mixture of dtypes in a 2D array", function () {
let data = utils.transposeArray([['Alice', 'Boy', 'Girl', 21], [2, 5, 30, "hey"], [3, 6, 40.1, 78.2]])
let result = ['string', 'string', 'float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns bool type in a 1D array", function () {
let data = [true, true, false, false, false, true];
let result = ['boolean'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns bool type in a 2D array", function () {
let data = utils.transposeArray([[true, false, true], ["boy", "girl", "man"], [20, 30, 24]])
let result = ['boolean', 'string', 'int32'];
assert.deepEqual(utils.inferDtype(data), result);
});
it("Returns string type if values are all NaN", function () {
let data = utils.transposeArray([[true, false, true], ["boy", "girl", "boy"], [NaN, NaN, NaN]])
let result = ['boolean', 'string', 'float32'];
assert.deepEqual(utils.inferDtype(data), result);
});
});
describe("mapIntegersToBooleans", function () {
it("map ints to bools in array of arrays", function () {
let data = [[1, 0, 1], [1, 1, 0]];
assert.deepEqual(utils.mapIntegersToBooleans(data, 2), [[true, false, true], [true, true, false]]);
});
it("map ints to bools in array", function () {
let data = [1, 0, 0, 1, 1];
assert.deepEqual(utils.mapIntegersToBooleans(data, 1), [true, false, false, true, true]);
});
});
describe("round", function () {
it("round elements in array to 1 dp", function () {
let data = [10.01, 2.2, 3.11, 20.505, 20.22, 40.0909];
assert.deepEqual(utils.round(data, 1, true), [10.0, 2.2, 3.1, 20.5, 20.2, 40.1]);
});
it("round elements in array to 2 dp", function () {
let data = [10.019, 2.2099, 3.1145, 20.506, 20.22, 40.0909];
assert.deepEqual(utils.round(data, 2, true), [10.02, 2.21, 3.11, 20.51, 20.22, 40.09]);
});
});
describe("replaceUndefinedWithNaN", function () {
it("replace undefined in Series with NaN", function () {
let data = [10.01, 2.2, undefined, 20.505, 20.22, undefined] as ArrayType1D;
assert.deepEqual(utils.replaceUndefinedWithNaN(data, true), [10.01, 2.2, NaN, 20.505, 20.22, NaN]);
});
it("replace undefined in DataFrame with NaN", function () {
let data = [[10.01, 2.2, undefined, 20.505, 20.22, undefined] as ArrayType1D,
[10.01, undefined, undefined, 20.505, 20, undefined] as ArrayType1D];
let result = [[10.01, 2.2, NaN, 20.505, 20.22, NaN],
[10.01, NaN, NaN, 20.505, 20, NaN]];
assert.deepEqual(utils.replaceUndefinedWithNaN(data, false), result);
});
it("replace null in Series with NaN", function () {
let data = [10.01, 2.2, null, 20.505, 20.22, null] as ArrayType1D;
assert.deepEqual(utils.replaceUndefinedWithNaN(data, true), [10.01, 2.2, NaN, 20.505, 20.22, NaN]);
});
it("replace null in DataFrame with NaN", function () {
let data = [[10.01, 2.2, null, 20.505, 20.22, null] as ArrayType1D,
[10.01, null, null, 20.505, 20, null] as ArrayType1D];
let result = [[10.01, 2.2, NaN, 20.505, 20.22, NaN],
[10.01, NaN, NaN, 20.505, 20, NaN]];
assert.deepEqual(utils.replaceUndefinedWithNaN(data, false), result);
});
});
describe("convert2DArrayToSeriesArray", function () {
it("convert 2D array of array to 1D of string values", function () {
let data = [[10.01, 2.2, "a"], [20.505, 20.22, "boy"]];
assert.deepEqual(utils.convert2DArrayToSeriesArray(data), ["10.01,2.2,a", "20.505,20.22,boy"]);
});
});
describe("throwErrorOnWrongParams", function () {
it("check if the right params are passed to a function", function () {
let paramsNeeded = ["replace", "with", "inplace"]
let kwargs = { "replae": 2, "with": 12, "inplace": true }
assert.throws(() => {
utils.throwErrorOnWrongParams(kwargs, paramsNeeded)
}, Error, `Params Error: Required parameter not found. Your params must include the following [${paramsNeeded}]`);
})
it("check if the right params are passed to a function 2", function () {
let paramsNeeded = ["replace", "with", "inplace"]
let kwargs = { "replace": 2, "with": 12, "inplace": true }
utils.throwErrorOnWrongParams(kwargs, paramsNeeded)
})
})
describe("getRowAndColValues", function () {
it("retreive rows and labels from column object", function () {
let data = { "Alpha": ["A", "B", "C", "D"], count: [1, 2, 3, 4], sum: [20.3, 30.456, 40.90, 90.1] };
let res = [["A", 1, 20.3], ["B", 2, 30.456], ["C", 3, 40.90], ["D", 4, 90.1]];
assert.deepEqual(utils.getRowAndColValues(data)[0], res as any);
assert.deepEqual(utils.getRowAndColValues(data)[1], ["Alpha", "count", "sum"]);
});
});
describe("getDuplicate", function () {
it("obtain duplicates and their index", function () {
let data = [1, 2, 3, 4, 5, 3, 4, 6, 4, 5];
let res = {
'3': { count: 2, index: [2, 5] },
'4': { count: 3, index: [3, 6, 8] },
'5': { count: 2, index: [4, 9] }
};
assert.deepEqual(utils.getDuplicate(data), res);
});
});
});