/
datawell.test.js
343 lines (294 loc) · 10.3 KB
/
datawell.test.js
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const DataWell = require("./datawell");
let sut = null;
const CAPACITY = 10;
const SORT_ON_PROPERTY = "timestamp";
beforeEach(() => {
sut = new DataWell(CAPACITY, true, SORT_ON_PROPERTY);
});
afterEach(() => {
sut = null;
});
/*
*** Helper Functions ***
*/
const maxData = () => {
let data = [];
for (let i = 0; i < CAPACITY; i++) {
const element = new Object;
element.sample = i;
element[SORT_ON_PROPERTY] = i;
data.push(element);
}
return data;
}
const singleData = (id, reference = id) => {
const element = new Object;
element.sample = reference;
element[SORT_ON_PROPERTY] = id;
return element;
}
/*
*** Test Initialization ***
*/
test('DataWell throws error when initialized without capacity', () => {
let initSut;
expect(() => {
initSut = new DataWell();
}).toThrow('You must supply the capacity as a positive integer value');
})
test('DataWell throws error when supplied capacity is not an integer', () => {
let initSut;
expect(() => {
initSut = new DataWell(10.7);
}).toThrow('You must supply the capacity as a positive integer value');
initSut = null;
expect(() => {
initSut = new DataWell("10");
}).toThrow('You must supply the capacity as a positive integer value');
initSut = null;
expect(() => {
initSut = new DataWell(true);
}).toThrow('You must supply the capacity as a positive integer value');
initSut = null;
expect(() => {
initSut = new DataWell(-10);
}).toThrow('You must supply the capacity as a positive integer value');
initSut = null;
})
test('DataWell does not throw error when initialized without arguments for sorted and sortOnProperty', () => {
let initSut;
expect(() => {
initSut = new DataWell(10);
}).not.toThrow();
initSut = null;
})
test('DataWell does not throw error when initialized without argument for sortOnProperty', () => {
let initSut;
expect(() => {
initSut = new DataWell(10, true);
}).not.toThrow();
initSut = null;
})
test('DataWell throws error when supplied sorted argument is not a boolean', () => {
let initSut;
expect(() => {
initSut = new DataWell(10, 10);
}).toThrow('You must supply the sorted argument as a boolean value');
initSut = null;
expect(() => {
initSut = new DataWell(10, "10");
}).toThrow('You must supply the sorted argument as a boolean value');
initSut = null;
expect(() => {
initSut = new DataWell(10,-10);
}).toThrow('You must supply the sorted argument as a boolean value');
initSut = null;
expect(() => {
initSut = new DataWell(10, 10.1);
}).toThrow('You must supply the sorted argument as a boolean value');
initSut = null;
})
test('DataWell throws error when supplied sortOnProperty argument is not a string', () => {
let initSut;
expect(() => {
initSut = new DataWell(10, true, 10);
}).toThrow('You must supply the sortOnProperty argument as a string value');
initSut = null;
expect(() => {
initSut = new DataWell(10, true, -10.1);
}).toThrow('You must supply the sortOnProperty argument as a string value');
initSut = null;
expect(() => {
initSut = new DataWell(10, true, true);
}).toThrow('You must supply the sortOnProperty argument as a string value');
initSut = null;
expect(() => {
initSut = new DataWell(10,true, false);
}).toThrow('You must supply the sortOnProperty argument as a string value');
initSut = null;
})
/*
*** Test of Status Functions ***
*/
test("DataWell is empty upon creation", () => {
expect(sut.count()).toBe(0);
});
test("DataWell capacity upon creation returns the capacity", () => {
expect(sut.capacity()).toBe(CAPACITY);
})
test("DataWell capacity after push returns the capacity", () => {
sut.push(singleData(0));
expect(sut.capacity()).toBe(CAPACITY);
sut.push(maxData());
expect(sut.capacity()).toBe(CAPACITY);
})
test('DataWell.isEmpty returns true if empty', () => {
expect(sut.isEmpty()).toBeTruthy();
})
test('DataWell.isEmpty returns false if not empty', () => {
sut.push(singleData(1));
expect(sut.isEmpty()).toBeFalsy();
sut.push(maxData());
expect(sut.isEmpty()).toBeFalsy();
})
test('DataWell.isEmpty returns false if full', () => {
sut.push(maxData());
expect(sut.isEmpty()).toBeFalsy();
})
test('DataWell.isFull returns false if empty', () => {
expect(sut.isFull()).toBeFalsy();
})
test('DataWell.isFull returns false if not empty and not full', () => {
sut.push(singleData(1));
expect(sut.isFull()).toBeFalsy();
})
test('DataWell.isFull returns true if full', () => {
sut.push(maxData());
expect(sut.isFull()).toBeTruthy();
})
/*
*** Test Push Function ***
*/
test('DataWell.push will accept a single object', () => {
expect(() => {
sut.push(singleData(0));;
}).not.toThrow();
});
test('DataWell.push will accept a list with a single object', () => {
expect(() => {
sut.push([singleData(1)]);;
}).not.toThrow();
});
test('DataWell.push will accept a list of objects', () => {
expect(() => {
sut.push(maxData());
}).not.toThrow();
});
test('Sorted DataWell to contain one item when one item was pushed', () => {
sut.push(singleData(1));
expect(sut.count()).toBe(1);
});
test('Sorted DataWell to accept a list of items for push', () => {
sut.push(maxData());
expect(sut.isFull()).toBeTruthy();
});
test('DataWell will only hold CAPACITY elements', () => {
sut.push(maxData());
sut.push(singleData(CAPACITY + 1));
expect(sut.count()).toBe(CAPACITY);
});
test('Sorted DataWell will only hold highest CAPACITY elements pushed', () => {
sut.push(singleData(CAPACITY + 1));
sut.push(maxData());
expect(sut.top()[SORT_ON_PROPERTY]).toBe(CAPACITY + 1);
});
test('Sorted DataWell will position higher id to top of stack', () => {
const sampleData = singleData(8);
sut.push(sampleData);
for (let i = 0; i < 8; i++) {
sut.push(singleData(i));
}
expect(sut.count()).toBe(9);
expect(sut.top()).toEqual(sampleData);
})
test('Sorted DataWell will override data with same SORT_ON_PROPERTY', () => {
for (let i = 0; i < 9; i++) {
sut.push(singleData(i));
}
let variedData = singleData(8, 8.1);
sut.push(variedData);
expect(sut.count()).toBe(9);
expect(sut.top()).toEqual(variedData);
})
test('Unsorted DataWell will push data with same SORT_ON_PROPERTY to top', () => {
let noSortSut = new DataWell(CAPACITY, false, SORT_ON_PROPERTY);
noSortSut.push(maxData());
const unsortedData = singleData(0);
noSortSut.push(unsortedData);
expect(noSortSut.top()).toEqual(unsortedData);
})
test('Sorted DataWell will throw an error if the property to be sorted on does not exist', () => {
const invalidPropertyData = new Object;
invalidPropertyData.sample = 1;
invalidPropertyData['not_a_' + SORT_ON_PROPERTY] = 1;
expect(() => {
sut.push(invalidPropertyData);
}).toThrow(`Supplied data in element 0 does not contain the property ${ SORT_ON_PROPERTY }, which you defined to sort on.`);
})
test('Unsorted DataWell does not throw an error if the property to be sorted on does not exist', () => {
let noSortSut = new DataWell(CAPACITY, false, SORT_ON_PROPERTY);
const invalidPropertyData = new Object;
invalidPropertyData.sample = 1;
invalidPropertyData['not_a_' + SORT_ON_PROPERTY] = 1;
expect(() => {
noSortSut.push(invalidPropertyData);
}).not.toThrow();
})
test('Sorted DataWell will throw an error if the property to be sorted on is not a of type number', () => {
let invalidPropertyData = singleData('1', 1);
expect(() => {
sut.push(invalidPropertyData);
}).toThrow(`Supplied data property ${ SORT_ON_PROPERTY } to sort on in element 0 is not a number.`);
invalidPropertyData = singleData(true, 1);
data = [
singleData(0),
invalidPropertyData
];
expect(() => {
sut.push(data);
}).toThrow(`Supplied data property ${ SORT_ON_PROPERTY } to sort on in element 1 is not a number.`);
})
test('Unsorted DataWell does not throw an error if the property to be sorted on is not a of type number', () => {
let noSortSut = new DataWell(CAPACITY, false, 'SORT_ON_PROPERTY');
let invalidPropertyData = singleData('1', 1);
expect(() => {
noSortSut.push(invalidPropertyData);
}).not.toThrow();
})
test('Sorted DataWell will merge new data with old data when processing data with same sortOnproperty', () => {
sut.push(maxData());
const sampleData1 = singleData(CAPACITY - 1);
const sampleData2 = {...sampleData1, otherData: CAPACITY - 1};
expect(sut.top()).toEqual(sampleData1);
sut.push(sampleData2);
expect(sut.top()).toEqual(sampleData2);
})
test('Sorted DataWell will keep newer data on merge with old data when processing data with same sortOnProperty', () => {
sut.push(maxData());
expect(sut.top()).toEqual(singleData(CAPACITY - 1));
const sampleData1 = {...singleData(CAPACITY), uniqueData: CAPACITY - 1, data: CAPACITY};
sut.push(sampleData1);
expect(sut.top()).toEqual(sampleData1);
const sampleData2 = {...singleData(CAPACITY), data: CAPACITY + 1, otherData: CAPACITY + 1};
sut.push(sampleData2);
const expectedData = {...singleData(CAPACITY), uniqueData: CAPACITY - 1, data: CAPACITY + 1, otherData: CAPACITY + 1};
expect(sut.top()).toEqual(expectedData);
})
/*
* Test Top Function
*/
test('DataWell.top will return top element and keep it on stack', () => {
sut.push(maxData());
expect(sut.count()).toBe(CAPACITY);
expect(sut.top()[SORT_ON_PROPERTY]).toBe(CAPACITY - 1);
expect(sut.count()).toBe(CAPACITY);
expect(sut.top()[SORT_ON_PROPERTY]).toBe(CAPACITY - 1);
})
test('Unsorted DataWell.top will return last added element', () => {
let noSortSut = new DataWell(CAPACITY);
noSortSut.push(maxData());
const lastdata = singleData(4, 4.1);
noSortSut.push(lastdata);
expect(noSortSut.count()).toBe(CAPACITY);
expect(noSortSut.top()).toEqual(lastdata);
noSortSut = null;
})
/*
* Test Content Function
*/
test('DataWell.content will return a list of data items', () => {
const testData = maxData();
sut.push(testData);
expect(sut.count()).toBe(CAPACITY);
expect(sut.content()).toEqual(testData);
})