-
Notifications
You must be signed in to change notification settings - Fork 73k
Expand file tree
/
Copy pathstorage.shape-handling.test.js
More file actions
468 lines (411 loc) · 14.4 KB
/
storage.shape-handling.test.js
File metadata and controls
468 lines (411 loc) · 14.4 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
'use strict';
var should = require('should');
var language = require('../lib/language')();
describe('Storage Layer Shape Handling - Direct Storage Tests', function () {
this.timeout(15000);
var self = this;
// Use before() instead of beforeEach() for app setup - boots once for all tests
before(function (done) {
process.env.API_SECRET = 'this is my long pass phrase';
self.env = require('../lib/server/env')();
self.env.settings.authDefaultRoles = 'readable';
self.env.settings.enable = ['careportal', 'api'];
require('../lib/server/bootevent')(self.env, language).boot(function booted(ctx) {
self.ctx = ctx;
self.ctx.ddata = require('../lib/data/ddata')();
done();
});
});
describe('Treatments Storage - lib/server/treatments.js', function () {
beforeEach(function (done) {
self.ctx.treatments.remove({ find: { created_at: { '$gte': '1999-01-01T00:00:00.000Z' } } }, function () {
done();
});
});
it('create() accepts single object', function (done) {
var now = new Date().toISOString();
self.ctx.treatments.create({
eventType: 'Note',
created_at: now,
notes: 'storage single object'
}, function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.be.greaterThanOrEqual(1);
result[0].notes.should.equal('storage single object');
done();
});
});
it('create() accepts array with single element', function (done) {
var now = new Date().toISOString();
self.ctx.treatments.create([{
eventType: 'Note',
created_at: now,
notes: 'storage array single'
}], function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.be.greaterThanOrEqual(1);
result[0].notes.should.equal('storage array single');
done();
});
});
it('create() accepts array with multiple elements', function (done) {
var now = Date.now();
self.ctx.treatments.create([
{ eventType: 'Note', created_at: new Date(now).toISOString(), notes: 'storage array 1' },
{ eventType: 'Note', created_at: new Date(now + 1000).toISOString(), notes: 'storage array 2' },
{ eventType: 'Note', created_at: new Date(now + 2000).toISOString(), notes: 'storage array 3' }
], function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.equal(3);
done();
});
});
it('create() handles large batch', function (done) {
var treatments = [];
var baseTime = Date.now();
for (var i = 0; i < 20; i++) {
treatments.push({
eventType: 'Note',
created_at: new Date(baseTime + i * 1000).toISOString(),
notes: 'batch ' + i
});
}
self.ctx.treatments.create(treatments, function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.equal(20);
done();
});
});
});
describe('Devicestatus Storage - lib/server/devicestatus.js', function () {
beforeEach(function (done) {
self.ctx.devicestatus.remove({ find: { created_at: { '$gte': '1999-01-01T00:00:00.000Z' } } }, function () {
done();
});
});
it('create() accepts single object', function (done) {
var now = new Date().toISOString();
self.ctx.devicestatus.create({
device: 'storage-test',
created_at: now,
uploaderBattery: 95
}, function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.equal(1);
result[0].uploaderBattery.should.equal(95);
done();
});
});
it('create() accepts array with single element', function (done) {
var now = new Date().toISOString();
self.ctx.devicestatus.create([{
device: 'storage-test-array',
created_at: now,
uploaderBattery: 90
}], function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.equal(1);
result[0].uploaderBattery.should.equal(90);
done();
});
});
it('create() accepts array with multiple elements', function (done) {
var now = Date.now();
self.ctx.devicestatus.create([
{ device: 'device-1', created_at: new Date(now).toISOString(), uploaderBattery: 80 },
{ device: 'device-2', created_at: new Date(now + 1000).toISOString(), uploaderBattery: 75 },
{ device: 'device-3', created_at: new Date(now + 2000).toISOString(), uploaderBattery: 70 }
], function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.equal(3);
done();
});
});
it('create() handles large batch', function (done) {
var statuses = [];
var baseTime = Date.now();
for (var i = 0; i < 20; i++) {
statuses.push({
device: 'batch-device-' + i,
created_at: new Date(baseTime + i * 1000).toISOString(),
uploaderBattery: 50 + i
});
}
self.ctx.devicestatus.create(statuses, function (err, result) {
should.not.exist(err);
should.exist(result);
result.should.be.instanceof(Array);
result.length.should.equal(20);
done();
});
});
});
describe('Entries Storage - lib/server/entries.js', function () {
beforeEach(function (done) {
self.ctx.entries().deleteMany({}, function () {
done();
});
});
afterEach(function (done) {
self.ctx.entries().deleteMany({}, function () {
done();
});
});
it('create() accepts single entry in array', function (done) {
var now = Date.now();
self.ctx.entries.create([{
type: 'sgv',
sgv: 100,
date: now,
dateString: new Date(now).toISOString()
}], function (err, result) {
should.not.exist(err);
done();
});
});
it('create() accepts array with multiple entries', function (done) {
var now = Date.now();
self.ctx.entries.create([
{ type: 'sgv', sgv: 100, date: now, dateString: new Date(now).toISOString() },
{ type: 'sgv', sgv: 110, date: now + 300000, dateString: new Date(now + 300000).toISOString() },
{ type: 'sgv', sgv: 120, date: now + 600000, dateString: new Date(now + 600000).toISOString() }
], function (err, result) {
should.not.exist(err);
self.ctx.entries.list({ count: 10 }, function (listErr, list) {
should.not.exist(listErr);
list.length.should.be.greaterThanOrEqual(3);
done();
});
});
});
});
describe('Profile Storage - lib/server/profile.js', function () {
beforeEach(function (done) {
self.ctx.profile().deleteMany({}, function () {
done();
});
});
afterEach(function (done) {
self.ctx.profile().deleteMany({}, function () {
done();
});
});
it('create() accepts single profile object', function (done) {
var profile = {
defaultProfile: 'Default',
store: {
Default: {
dia: 3,
carbratio: [{ time: '00:00', value: 30 }],
sens: [{ time: '00:00', value: 100 }],
basal: [{ time: '00:00', value: 0.5 }],
target_low: [{ time: '00:00', value: 80 }],
target_high: [{ time: '00:00', value: 120 }],
units: 'mg/dl'
}
},
startDate: new Date().toISOString(),
units: 'mg/dl'
};
self.ctx.profile.create(profile, function (err, doc) {
should.not.exist(err);
should.exist(doc);
doc.defaultProfile.should.equal('Default');
done();
});
});
it('save() updates an existing profile by _id', function (done) {
var original = {
defaultProfile: 'Default',
store: {
Default: {
dia: 3,
carbratio: [{ time: '00:00', value: 30 }],
sens: [{ time: '00:00', value: 100 }],
basal: [{ time: '00:00', value: 0.5 }],
target_low: [{ time: '00:00', value: 80 }],
target_high: [{ time: '00:00', value: 120 }],
units: 'mg/dl'
}
},
startDate: '2024-10-19T23:00:00.000Z',
created_at: '2024-10-26T20:32:49.173Z',
units: 'mg/dl'
};
self.ctx.profile.save(original, function (err, savedProfile) {
should.not.exist(err);
should.exist(savedProfile);
should.exist(savedProfile._id);
var savedId = savedProfile._id;
var updated = {
_id: savedId.toString(),
defaultProfile: 'Default',
store: {
Default: {
dia: 4,
carbratio: [{ time: '00:00', value: 30 }],
sens: [{ time: '00:00', value: 100 }],
basal: [{ time: '00:00', value: 0.5 }],
target_low: [{ time: '00:00', value: 80 }],
target_high: [{ time: '00:00', value: 120 }],
units: 'mg/dl'
}
},
startDate: '2024-10-19T23:00:00.000Z',
created_at: '2024-10-26T21:32:49.173Z',
units: 'mg/dl'
};
self.ctx.profile.save(updated, function (saveErr) {
should.not.exist(saveErr);
self.ctx.profile().find({ _id: savedId }).toArray(function (findErr, docs) {
should.not.exist(findErr);
docs.length.should.equal(1);
docs[0].store.Default.dia.should.equal(4);
docs[0].created_at.should.equal('2024-10-26T21:32:49.173Z');
done();
});
});
});
});
});
describe('Food Storage - lib/server/food.js', function () {
beforeEach(function (done) {
self.ctx.food().deleteMany({}, function () {
done();
});
});
afterEach(function (done) {
self.ctx.food().deleteMany({}, function () {
done();
});
});
it('create() accepts single food object', function (done) {
var food = {
name: 'Test Food',
category: 'Test',
carbs: 20,
protein: 10,
fat: 5
};
self.ctx.food.create(food, function (err, doc) {
should.not.exist(err);
should.exist(doc);
doc.name.should.equal('Test Food');
done();
});
});
});
describe('Activity Storage - lib/server/activity.js', function () {
beforeEach(function (done) {
self.ctx.activity().deleteMany({}, function () {
done();
});
});
afterEach(function (done) {
self.ctx.activity().deleteMany({}, function () {
done();
});
});
it('create() accepts array of activity objects (single object not supported)', function (done) {
var activity = [{
created_at: new Date().toISOString(),
heartrate: 80,
steps: 100,
activitylevel: 'walking'
}];
self.ctx.activity.create(activity, function (err, docs) {
should.not.exist(err);
should.exist(docs);
docs.length.should.equal(1);
done();
});
});
});
});
describe('MongoDB insertOne vs insertMany Behavior', function () {
this.timeout(15000);
var self = this;
beforeEach(function (done) {
process.env.API_SECRET = 'this is my long pass phrase';
self.env = require('../lib/server/env')();
self.env.settings.authDefaultRoles = 'readable';
self.env.settings.enable = ['careportal', 'api'];
require('../lib/server/bootevent')(self.env, language).boot(function booted(ctx) {
self.ctx = ctx;
self.ctx.ddata = require('../lib/data/ddata')();
done();
});
});
describe('Direct MongoDB operations - testing insertOne with array data', function () {
it('insertOne with object inserts correctly', function (done) {
var testCollection = self.ctx.store.collection('test_shape_handling');
testCollection.deleteMany({}, function () {
testCollection.insertOne({ type: 'test', value: 42 }, function (err, result) {
should.not.exist(err);
should.exist(result);
result.insertedId.should.be.ok();
testCollection.find({}).toArray(function (err, docs) {
docs.length.should.equal(1);
docs[0].value.should.equal(42);
testCollection.deleteMany({}, done);
});
});
});
});
it('insertOne with array creates single document containing array (NOT multiple docs)', function (done) {
var testCollection = self.ctx.store.collection('test_shape_handling');
testCollection.deleteMany({}, function () {
var arrayData = [
{ type: 'test', value: 1 },
{ type: 'test', value: 2 },
{ type: 'test', value: 3 }
];
testCollection.insertOne(arrayData, function (err, result) {
if (err) {
console.log('insertOne with array error:', err.message);
done();
} else {
testCollection.find({}).toArray(function (err, docs) {
console.log('Documents after insertOne with array:', JSON.stringify(docs, null, 2));
console.log('Number of documents:', docs.length);
testCollection.deleteMany({}, done);
});
}
});
});
});
it('insertMany with array creates multiple documents', function (done) {
var testCollection = self.ctx.store.collection('test_shape_handling');
testCollection.deleteMany({}, function () {
var arrayData = [
{ type: 'test', value: 1 },
{ type: 'test', value: 2 },
{ type: 'test', value: 3 }
];
testCollection.insertMany(arrayData, function (err, result) {
should.not.exist(err);
should.exist(result);
result.insertedCount.should.equal(3);
testCollection.find({}).toArray(function (err, docs) {
docs.length.should.equal(3);
testCollection.deleteMany({}, done);
});
});
});
});
});
});