meadow 2.0.17 → 2.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,931 @@
1
+ /**
2
+ * Unit tests for the Meadow "SQLite" Provider
3
+ *
4
+ * These tests use an in-process SQLite database via better-sqlite3.
5
+ *
6
+ * @license MIT
7
+ *
8
+ * @author Steven Velozo <steven@velozo.com>
9
+ */
10
+
11
+ var Chai = require("chai");
12
+ var Expect = Chai.expect;
13
+ var Assert = Chai.assert;
14
+
15
+ var libFS = require('fs');
16
+
17
+ var libMeadowConnectionSQLite = require('meadow-connection-sqlite');
18
+
19
+ var _SQLiteDBPath = __dirname + '/../dist/FableTest-SQLite.db';
20
+
21
+ var tmpFableSettings = (
22
+ {
23
+ SQLite:
24
+ {
25
+ SQLiteFilePath: _SQLiteDBPath
26
+ },
27
+ LogStreams:
28
+ [
29
+ {
30
+ level: 'fatal',
31
+ streamtype:'process.stdout',
32
+ },
33
+ {
34
+ level: 'trace',
35
+ path: __dirname+'/../tests.log'
36
+ }
37
+ ]
38
+ });
39
+
40
+ var libFable = new (require('fable'))(tmpFableSettings);
41
+
42
+ // Register the SQLite connection service
43
+ libFable.serviceManager.addServiceType('MeadowSQLiteProvider', libMeadowConnectionSQLite);
44
+ libFable.serviceManager.instantiateServiceProvider('MeadowSQLiteProvider');
45
+
46
+
47
+ var _AnimalJsonSchema = (
48
+ {
49
+ title: "Animal",
50
+ description: "A creature that lives in a meadow.",
51
+ type: "object",
52
+ properties: {
53
+ IDAnimal: {
54
+ description: "The unique identifier for an animal",
55
+ type: "integer"
56
+ },
57
+ Name: {
58
+ description: "The animal's name",
59
+ type: "string"
60
+ },
61
+ Type: {
62
+ description: "The type of the animal",
63
+ type: "string"
64
+ }
65
+ },
66
+ required: ["IDAnimal", "Name", "CreatingIDUser"]
67
+ });
68
+ var _AnimalSchema = (
69
+ [
70
+ { Column: "IDAnimal", Type:"AutoIdentity" },
71
+ { Column: "GUIDAnimal", Type:"AutoGUID" },
72
+ { Column: "CreateDate", Type:"CreateDate" },
73
+ { Column: "CreatingIDUser", Type:"CreateIDUser" },
74
+ { Column: "UpdateDate", Type:"UpdateDate" },
75
+ { Column: "UpdatingIDUser", Type:"UpdateIDUser" },
76
+ { Column: "Deleted", Type:"Deleted" },
77
+ { Column: "DeletingIDUser", Type:"DeleteIDUser" },
78
+ { Column: "DeleteDate", Type:"DeleteDate" }
79
+ ]);
80
+ var _AnimalDefault = (
81
+ {
82
+ IDAnimal: null,
83
+ GUIDAnimal: '',
84
+
85
+ CreateDate: false,
86
+ CreatingIDUser: 0,
87
+ UpdateDate: false,
88
+ UpdatingIDUser: 0,
89
+ Deleted: 0,
90
+ DeleteDate: false,
91
+ DeletingIDUser: 0,
92
+
93
+ Name: 'Unknown',
94
+ Type: 'Unclassified'
95
+ });
96
+
97
+ suite
98
+ (
99
+ 'Meadow-Provider-SQLite',
100
+ function()
101
+ {
102
+ var _SpooledUp = false;
103
+
104
+ var newMeadow = function()
105
+ {
106
+ return require('../source/Meadow.js')
107
+ .new(libFable, 'FableTest')
108
+ .setProvider('SQLite')
109
+ .setSchema(_AnimalSchema)
110
+ .setJsonSchema(_AnimalJsonSchema)
111
+ .setDefaultIdentifier('IDAnimal')
112
+ .setDefault(_AnimalDefault)
113
+ };
114
+
115
+ suiteSetup
116
+ (
117
+ function(fDone)
118
+ {
119
+ // Only do this for the first test.
120
+ if (!_SpooledUp)
121
+ {
122
+ // Remove any previous test database
123
+ if (libFS.existsSync(_SQLiteDBPath))
124
+ {
125
+ libFS.unlinkSync(_SQLiteDBPath);
126
+ }
127
+
128
+ // Ensure the dist directory exists
129
+ var tmpDistDir = __dirname + '/../dist';
130
+ if (!libFS.existsSync(tmpDistDir))
131
+ {
132
+ libFS.mkdirSync(tmpDistDir, { recursive: true });
133
+ }
134
+
135
+ // Connect to SQLite
136
+ libFable.MeadowSQLiteProvider.connectAsync(
137
+ function(pError)
138
+ {
139
+ if (pError)
140
+ {
141
+ return fDone(pError);
142
+ }
143
+
144
+ var tmpDB = libFable.MeadowSQLiteProvider.db;
145
+
146
+ // Create the FableTest table
147
+ tmpDB.exec(
148
+ "CREATE TABLE IF NOT EXISTS FableTest (" +
149
+ " IDAnimal INTEGER PRIMARY KEY AUTOINCREMENT," +
150
+ " GUIDAnimal TEXT NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'," +
151
+ " CreateDate TEXT," +
152
+ " CreatingIDUser INTEGER NOT NULL DEFAULT 0," +
153
+ " UpdateDate TEXT," +
154
+ " UpdatingIDUser INTEGER NOT NULL DEFAULT 0," +
155
+ " Deleted INTEGER NOT NULL DEFAULT 0," +
156
+ " DeleteDate TEXT," +
157
+ " DeletingIDUser INTEGER NOT NULL DEFAULT 0," +
158
+ " Name TEXT NOT NULL DEFAULT ''," +
159
+ " Type TEXT NOT NULL DEFAULT ''" +
160
+ ");"
161
+ );
162
+
163
+ // Insert test data
164
+ var tmpInsert = tmpDB.prepare(
165
+ "INSERT INTO FableTest (GUIDAnimal, CreateDate, CreatingIDUser, UpdateDate, UpdatingIDUser, Deleted, DeleteDate, DeletingIDUser, Name, Type) " +
166
+ "VALUES ('00000000-0000-0000-0000-000000000000', datetime('now'), 1, datetime('now'), 1, 0, NULL, 0, ?, ?)"
167
+ );
168
+
169
+ tmpInsert.run('Foo Foo', 'Bunny');
170
+ tmpInsert.run('Red Riding Hood', 'Girl');
171
+ tmpInsert.run('Red', 'Dog');
172
+ tmpInsert.run('Spot', 'Dog');
173
+ tmpInsert.run('Gertrude', 'Frog');
174
+
175
+ _SpooledUp = true;
176
+ fDone();
177
+ }
178
+ );
179
+ }
180
+ else
181
+ {
182
+ fDone();
183
+ }
184
+ }
185
+ );
186
+
187
+ suiteTeardown(function(fDone)
188
+ {
189
+ // Close the SQLite database
190
+ try
191
+ {
192
+ var tmpDB = libFable.MeadowSQLiteProvider.db;
193
+ if (tmpDB)
194
+ {
195
+ tmpDB.close();
196
+ }
197
+ }
198
+ catch (pError)
199
+ {
200
+ // Ignore close errors
201
+ }
202
+ fDone();
203
+ });
204
+
205
+ suite
206
+ (
207
+ 'Object Sanity',
208
+ function()
209
+ {
210
+ test
211
+ (
212
+ 'The SQLite class should initialize itself into a happy little object.',
213
+ function()
214
+ {
215
+ var testMeadow = require('../source/Meadow.js').new(libFable).setProvider('SQLite');
216
+ Expect(testMeadow).to.be.an('object', 'Meadow should initialize as an object directly from the require statement.');
217
+ Expect(testMeadow.providerName).to.equal('SQLite');
218
+ }
219
+ );
220
+ }
221
+ );
222
+ suite
223
+ (
224
+ 'Query Processing',
225
+ function()
226
+ {
227
+ test
228
+ (
229
+ 'Create a record in the database',
230
+ function(fDone)
231
+ {
232
+ var testMeadow = newMeadow().setIDUser(90210);
233
+
234
+ // Ensure this query is "slow"...
235
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
236
+
237
+ var tmpQuery = testMeadow.query.clone().setLogLevel(5)
238
+ .addRecord({Name:'Blastoise', Type:'Pokemon'});
239
+
240
+ testMeadow.doCreate(tmpQuery,
241
+ function(pError, pQuery, pQueryRead, pRecord)
242
+ {
243
+ // We should have a record ....
244
+ Expect(pRecord.Name)
245
+ .to.equal('Blastoise');
246
+ Expect(pRecord.CreatingIDUser)
247
+ .to.equal(90210);
248
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
249
+ fDone();
250
+ }
251
+ )
252
+ }
253
+ );
254
+ test
255
+ (
256
+ 'Create a record in the database with Deleted bit already set',
257
+ function(fDone)
258
+ {
259
+ var testMeadow = newMeadow().setIDUser(90210);
260
+
261
+ var tmpQuery = testMeadow.query.clone().setLogLevel(5)
262
+ .setDisableDeleteTracking(true)
263
+ .addRecord({Name:'Charmander', Type:'Pokemon', Deleted: 1});
264
+
265
+ testMeadow.doCreate(tmpQuery,
266
+ function(pError, pQuery, pQueryRead, pRecord)
267
+ {
268
+ // We should have a record ....
269
+ Expect(pRecord.Name)
270
+ .to.equal('Charmander');
271
+ Expect(pRecord.CreatingIDUser)
272
+ .to.equal(90210);
273
+ fDone();
274
+ }
275
+ )
276
+ }
277
+ );
278
+ test
279
+ (
280
+ 'Read a record from the database',
281
+ function(fDone)
282
+ {
283
+ var testMeadow = newMeadow();
284
+
285
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
286
+
287
+ var tmpQuery = testMeadow.query
288
+ .addFilter('IDAnimal', 1);
289
+
290
+ testMeadow.doRead(tmpQuery,
291
+ function(pError, pQuery, pRecord)
292
+ {
293
+ // We should have a record ....
294
+ Expect(pRecord.IDAnimal)
295
+ .to.equal(1);
296
+ Expect(pRecord.Name)
297
+ .to.equal('Foo Foo');
298
+
299
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
300
+
301
+ fDone();
302
+ }
303
+ )
304
+ }
305
+ );
306
+ test
307
+ (
308
+ 'Read all records from the database',
309
+ function(fDone)
310
+ {
311
+ var testMeadow = newMeadow();
312
+
313
+ testMeadow.doReads(testMeadow.query,
314
+ function(pError, pQuery, pRecords)
315
+ {
316
+ // We should have records ....
317
+ Expect(pRecords[0].IDAnimal)
318
+ .to.equal(1);
319
+ Expect(pRecords[0].Name)
320
+ .to.equal('Foo Foo');
321
+ Expect(pRecords[1].IDAnimal)
322
+ .to.equal(2);
323
+ Expect(pRecords[1].Name)
324
+ .to.equal('Red Riding Hood');
325
+ Expect(pRecords[1].Type)
326
+ .to.equal('Girl');
327
+ fDone();
328
+ }
329
+ )
330
+ }
331
+ );
332
+ test
333
+ (
334
+ 'Update a record in the database',
335
+ function(fDone)
336
+ {
337
+ var testMeadow = newMeadow();
338
+
339
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
340
+
341
+ var tmpQuery = testMeadow.query
342
+ .addRecord({IDAnimal:2, Type:'Human'});
343
+
344
+ testMeadow.doUpdate(tmpQuery,
345
+ function(pError, pQuery, pQueryRead, pRecord)
346
+ {
347
+ // We should have a record ....
348
+ Expect(pRecord.Type)
349
+ .to.equal('Human');
350
+
351
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
352
+
353
+ fDone();
354
+ }
355
+ )
356
+ }
357
+ );
358
+ test
359
+ (
360
+ 'Delete a record in the database',
361
+ function(fDone)
362
+ {
363
+ var testMeadow = newMeadow();
364
+
365
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
366
+ var tmpQuery = testMeadow.query.addFilter('IDAnimal',3);
367
+
368
+ testMeadow.doDelete(tmpQuery,
369
+ function(pError, pQuery, pRecord)
370
+ {
371
+ // It returns the number of rows deleted
372
+ Expect(pRecord)
373
+ .to.equal(1);
374
+
375
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
376
+
377
+ fDone();
378
+ }
379
+ )
380
+ }
381
+ );
382
+ test
383
+ (
384
+ 'Undelete a record in the database',
385
+ function(fDone)
386
+ {
387
+ var testMeadow = newMeadow();
388
+
389
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
390
+ var tmpDeleteQuery = testMeadow.query.addFilter('IDAnimal',5);
391
+
392
+ // Make sure the record is deleted!
393
+ testMeadow.doDelete(tmpDeleteQuery,
394
+ function(pDeleteError, pDeleteQuery, pDeleteRecord)
395
+ {
396
+ var tmpQuery = testMeadow.query.addFilter('IDAnimal',5);
397
+ testMeadow.doUndelete(tmpQuery,
398
+ function(pError, pQuery, pRecord)
399
+ {
400
+ // It returns the number of rows undeleted
401
+ Expect(pRecord)
402
+ .to.equal(1);
403
+
404
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
405
+
406
+ fDone();
407
+ });
408
+ });
409
+ }
410
+ );
411
+ test
412
+ (
413
+ 'Count all records from the database',
414
+ function(fDone)
415
+ {
416
+ var testMeadow = newMeadow();
417
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
418
+
419
+ Expect(testMeadow.query.parameters.result.executed)
420
+ .to.equal(false);
421
+ testMeadow.doCount(testMeadow.query,
422
+ function(pError, pQuery, pRecord)
423
+ {
424
+ // There should be 5 non-deleted records (3 not deleted seeded + Blastoise + Gertrude)
425
+ Expect(pRecord)
426
+ .to.equal(5);
427
+ Expect(pQuery.parameters.result.executed)
428
+ .to.equal(true);
429
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
430
+ fDone();
431
+ }
432
+ )
433
+ }
434
+ );
435
+ test
436
+ (
437
+ 'Perform operations with a schema-based instantiation',
438
+ function(fDone)
439
+ {
440
+ var testMeadow = require('../source/Meadow.js').new(libFable)
441
+ .loadFromPackage(__dirname+'/Animal.json').setProvider('SQLite');
442
+
443
+ // Make sure the authentication stuff got loaded
444
+ Expect(testMeadow.schemaFull.authorizer.User)
445
+ .to.be.an('object');
446
+ Expect(testMeadow.schemaFull.authorizer.User.Create)
447
+ .to.equal('Allow');
448
+
449
+ var tmpQuery = testMeadow.query
450
+ .addRecord({Name:'Grommet', Type:'Dog'});
451
+
452
+ testMeadow.doCreate(tmpQuery,
453
+ function(pError, pQuery, pQueryRead, pRecord)
454
+ {
455
+ // We should have a record ....
456
+ Expect(pRecord.Name)
457
+ .to.equal('Grommet');
458
+ fDone();
459
+ }
460
+ )
461
+ }
462
+ );
463
+ }
464
+ );
465
+ suite
466
+ (
467
+ 'Logged Query Processing',
468
+ function()
469
+ {
470
+ test
471
+ (
472
+ 'Create a record in the database',
473
+ function(fDone)
474
+ {
475
+ var testMeadow = newMeadow();
476
+
477
+ var tmpQuery = testMeadow.query
478
+ .setLogLevel(5)
479
+ .addRecord({Name:'MewTwo', Type:'Pokemon'});
480
+
481
+ testMeadow.doCreate(tmpQuery,
482
+ function(pError, pQuery, pQueryRead, pRecord)
483
+ {
484
+ // We should have a record ....
485
+ Expect(pRecord.Name)
486
+ .to.equal('MewTwo');
487
+ fDone();
488
+ }
489
+ )
490
+ }
491
+ );
492
+ test
493
+ (
494
+ 'Create a record in the database with a predefined GUID',
495
+ function(fDone)
496
+ {
497
+ var testMeadow = newMeadow();
498
+
499
+ var tmpQuery = testMeadow.query
500
+ .setLogLevel(5)
501
+ .addRecord({Name:'MewThree', GUIDAnimal:'0x12345', Type:'Pokemon'});
502
+
503
+ testMeadow.doCreate(tmpQuery,
504
+ function(pError, pQuery, pQueryRead, pRecord)
505
+ {
506
+ // We should have a record ....
507
+ Expect(pRecord.Name)
508
+ .to.equal('MewThree');
509
+ fDone();
510
+ }
511
+ )
512
+ }
513
+ );
514
+ test
515
+ (
516
+ 'Create a record in the database with a previously predefined GUID -- expect failure',
517
+ function(fDone)
518
+ {
519
+ var testMeadow = newMeadow();
520
+
521
+ var tmpQuery = testMeadow.query
522
+ .setLogLevel(5)
523
+ .addRecord({Name:'MewThree', GUIDAnimal:'0x12345', Type:'Pokemon'});
524
+
525
+ testMeadow.doCreate(tmpQuery,
526
+ function(pError, pQuery, pQueryRead, pRecord)
527
+ {
528
+ Expect(pError)
529
+ .to.equal("Record with GUID 0x12345 already exists!");
530
+ fDone();
531
+ }
532
+ )
533
+ }
534
+ );
535
+ test
536
+ (
537
+ 'Read a record from the database',
538
+ function(fDone)
539
+ {
540
+ var testMeadow = newMeadow();
541
+
542
+ var tmpQuery = testMeadow.query
543
+ .setLogLevel(5)
544
+ .addFilter('IDAnimal', 1);
545
+
546
+ testMeadow.doRead(tmpQuery,
547
+ function(pError, pQuery, pRecord)
548
+ {
549
+ // We should have a record ....
550
+ Expect(pRecord.IDAnimal)
551
+ .to.equal(1);
552
+ Expect(pRecord.Name)
553
+ .to.equal('Foo Foo');
554
+ fDone();
555
+ }
556
+ )
557
+ }
558
+ );
559
+ test
560
+ (
561
+ 'Read all records from the database',
562
+ function(fDone)
563
+ {
564
+ var testMeadow = newMeadow();
565
+
566
+ testMeadow.doReads(testMeadow.query.setLogLevel(5),
567
+ function(pError, pQuery, pRecords)
568
+ {
569
+ // We should have records ....
570
+ Expect(pRecords[0].IDAnimal)
571
+ .to.equal(1);
572
+ Expect(pRecords[0].Name)
573
+ .to.equal('Foo Foo');
574
+ Expect(pRecords[1].IDAnimal)
575
+ .to.equal(2);
576
+ Expect(pRecords[1].Name)
577
+ .to.equal('Red Riding Hood');
578
+ Expect(pRecords[1].Type)
579
+ .to.equal('Human');
580
+ fDone();
581
+ }
582
+ )
583
+ }
584
+ );
585
+ test
586
+ (
587
+ 'Update a record in the database',
588
+ function(fDone)
589
+ {
590
+ var testMeadow = newMeadow();
591
+
592
+ var tmpQuery = testMeadow.query
593
+ .setLogLevel(5)
594
+ .addRecord({IDAnimal:2, Type:'HumanGirl'});
595
+
596
+ testMeadow.doUpdate(tmpQuery,
597
+ function(pError, pQuery, pQueryRead, pRecord)
598
+ {
599
+ // We should have a record ....
600
+ Expect(pRecord.Type)
601
+ .to.equal('HumanGirl');
602
+ fDone();
603
+ }
604
+ )
605
+ }
606
+ );
607
+ test
608
+ (
609
+ 'Delete a record in the database',
610
+ function(fDone)
611
+ {
612
+ var testMeadow = newMeadow();
613
+
614
+ var tmpQuery = testMeadow.query
615
+ .setLogLevel(5)
616
+ .addFilter('IDAnimal',4);
617
+
618
+ testMeadow.doDelete(tmpQuery,
619
+ function(pError, pQuery, pRecord)
620
+ {
621
+ // It returns the number of rows deleted
622
+ Expect(pRecord)
623
+ .to.equal(1);
624
+ fDone();
625
+ }
626
+ )
627
+ }
628
+ );
629
+ test
630
+ (
631
+ 'Count all records from the database',
632
+ function(fDone)
633
+ {
634
+ var testMeadow = newMeadow();
635
+
636
+ testMeadow.doCount(testMeadow.query.setLogLevel(5),
637
+ function(pError, pQuery, pRecord)
638
+ {
639
+ // Count non-deleted records
640
+ Expect(pRecord)
641
+ .to.be.a('number');
642
+ Expect(pRecord).to.be.above(0);
643
+ fDone();
644
+ }
645
+ )
646
+ }
647
+ );
648
+ test
649
+ (
650
+ 'Read a record from the database that had a defined GUID',
651
+ function(fDone)
652
+ {
653
+ var testMeadow = newMeadow();
654
+
655
+ var tmpQuery = testMeadow.query
656
+ .addFilter('GUIDAnimal', '0x12345');
657
+
658
+ testMeadow.doRead(tmpQuery,
659
+ function(pError, pQuery, pRecord)
660
+ {
661
+ // We should have a record ....
662
+ Expect(pRecord.Name)
663
+ .to.equal('MewThree');
664
+ fDone();
665
+ }
666
+ )
667
+ }
668
+ );
669
+ test
670
+ (
671
+ 'Create a record in the database with a defined creating user',
672
+ function(fDone)
673
+ {
674
+ var testMeadow = newMeadow();
675
+ var tmpQuery = testMeadow.query
676
+ .setIDUser(800)
677
+ .addRecord({Name:'MewSix', GUIDAnimal:'0x123456', Type:'Pokemon'});
678
+
679
+ testMeadow.doCreate(tmpQuery,
680
+ function(pError, pQuery, pQueryRead, pRecord)
681
+ {
682
+ // We should have a record ....
683
+ Expect(pRecord.Name)
684
+ .to.equal('MewSix');
685
+ Expect(pRecord.CreatingIDUser)
686
+ .to.equal(800);
687
+ fDone();
688
+ }
689
+ )
690
+ }
691
+ );
692
+ }
693
+ );
694
+ suite
695
+ (
696
+ 'The Bad Kind of Query Processing',
697
+ function()
698
+ {
699
+ test
700
+ (
701
+ 'Count all records from a nonexistent table',
702
+ function(fDone)
703
+ {
704
+ var testMeadow = newMeadow();
705
+
706
+ testMeadow.doCount(testMeadow.query.setScope('BadTable'),
707
+ function(pError, pQuery, pRecord)
708
+ {
709
+ Expect(pError)
710
+ .to.be.an('object');
711
+ fDone();
712
+ }
713
+ )
714
+ }
715
+ );
716
+ test
717
+ (
718
+ 'Create a record in the database with no record',
719
+ function(fDone)
720
+ {
721
+ var testMeadow = newMeadow().setDefaultIdentifier('Type');
722
+
723
+ testMeadow.doCreate(testMeadow.query,
724
+ function(pError, pQuery, pQueryRead, pRecord)
725
+ {
726
+ Expect(pError)
727
+ .to.equal('No record submitted');
728
+ fDone();
729
+ }
730
+ )
731
+ }
732
+ );
733
+ test
734
+ (
735
+ 'Read a record from the database with no data returned',
736
+ function(fDone)
737
+ {
738
+ var testMeadow = newMeadow();
739
+
740
+ var tmpQuery = testMeadow.query
741
+ .addFilter('IDAnimal', 5000);
742
+ testMeadow.doRead(tmpQuery,
743
+ function(pError, pQuery, pRecord)
744
+ {
745
+ Expect(pRecord)
746
+ .to.equal(false);
747
+ fDone();
748
+ }
749
+ )
750
+ }
751
+ );
752
+ test
753
+ (
754
+ 'Read records from the database with no data returned',
755
+ function(fDone)
756
+ {
757
+ var testMeadow = newMeadow();
758
+
759
+ var tmpQuery = testMeadow.query
760
+ .addFilter('IDAnimal', 5000);
761
+
762
+ testMeadow.doReads(tmpQuery,
763
+ function(pError, pQuery, pRecord)
764
+ {
765
+ Expect(pRecord.length)
766
+ .to.equal(0);
767
+ fDone();
768
+ }
769
+ )
770
+ }
771
+ );
772
+ test
773
+ (
774
+ 'Update a record in the database with a bad filter',
775
+ function(fDone)
776
+ {
777
+ var testMeadow = newMeadow();
778
+
779
+ var tmpQuery = testMeadow.query
780
+ .setLogLevel(5)
781
+ .addRecord({IDAnimal:undefined, Type:'HumanGirl'});
782
+
783
+ testMeadow.doUpdate(tmpQuery,
784
+ function(pError, pQuery, pQueryRead, pRecord)
785
+ {
786
+ // We should have an error ....
787
+ Expect(pError)
788
+ .to.equal('Automated update missing filters... aborting!');
789
+ fDone();
790
+ }
791
+ )
792
+ }
793
+ );
794
+ test
795
+ (
796
+ 'Update a record in the database without passing a record in',
797
+ function(fDone)
798
+ {
799
+ var testMeadow = newMeadow();
800
+
801
+ testMeadow.doUpdate(testMeadow.query,
802
+ function(pError, pQuery, pQueryRead, pRecord)
803
+ {
804
+ Expect(pError)
805
+ .to.equal('No record submitted');
806
+ fDone();
807
+ }
808
+ )
809
+ }
810
+ );
811
+ test
812
+ (
813
+ 'Update a record in the database with a bad record passed in (no default identifier)',
814
+ function(fDone)
815
+ {
816
+ var testMeadow = newMeadow();
817
+
818
+ var tmpQuery = testMeadow.query
819
+ .addRecord({Name:'Bill'});
820
+
821
+ testMeadow.doUpdate(tmpQuery,
822
+ function(pError, pQuery, pQueryRead, pRecord)
823
+ {
824
+ Expect(pError)
825
+ .to.equal('Automated update missing default identifier');
826
+ fDone();
827
+ }
828
+ )
829
+ }
830
+ );
831
+ test
832
+ (
833
+ 'Update a record in the database that does not exist',
834
+ function(fDone)
835
+ {
836
+ var testMeadow = newMeadow();
837
+
838
+ var tmpQuery = testMeadow.query
839
+ .addRecord({IDAnimal:983924});
840
+
841
+ testMeadow.doUpdate(tmpQuery,
842
+ function(pError, pQuery, pQueryRead, pRecord)
843
+ {
844
+ Expect(pError)
845
+ .to.equal('No record found to update!');
846
+ fDone();
847
+ }
848
+ )
849
+ }
850
+ );
851
+ test
852
+ (
853
+ 'Set a raw Query',
854
+ function(fDone)
855
+ {
856
+ var testMeadow = newMeadow();
857
+ testMeadow.rawQueries.setQuery('Read', 'SELECT Something from SomethingElse;');
858
+
859
+ Expect(testMeadow.rawQueries.getQuery('Read'))
860
+ .to.equal('SELECT Something from SomethingElse;');
861
+ fDone();
862
+ }
863
+ );
864
+ test
865
+ (
866
+ 'Load a raw Query',
867
+ function(fDone)
868
+ {
869
+ var testMeadow = newMeadow();
870
+
871
+ testMeadow.rawQueries.loadQuery('Read', __dirname+ '/Meadow-Provider-SQLite-AnimalReadQuery.sql',
872
+ function(pSuccess)
873
+ {
874
+ Expect(testMeadow.rawQueries.getQuery('Read'))
875
+ .to.contain('SELECT');
876
+ fDone();
877
+ });
878
+ }
879
+ );
880
+ test
881
+ (
882
+ 'Load a bad raw Query',
883
+ function(fDone)
884
+ {
885
+ var testMeadow = newMeadow();
886
+
887
+ testMeadow.rawQueries.loadQuery('Read', __dirname+ '/Meadow-Provider-SQLite-BADAnimalReadQuery.sql',
888
+ function(pSuccess)
889
+ {
890
+ Expect(testMeadow.rawQueries.getQuery('Read'))
891
+ .to.equal('');
892
+ fDone();
893
+ });
894
+ }
895
+ );
896
+ test
897
+ (
898
+ 'Check for a query that is not there',
899
+ function()
900
+ {
901
+ var testMeadow = newMeadow();
902
+ Expect(testMeadow.rawQueries.getQuery('Read'))
903
+ .to.equal(false);
904
+ }
905
+ );
906
+ test
907
+ (
908
+ 'Read a record from a custom query',
909
+ function(fDone)
910
+ {
911
+ var testMeadow = newMeadow();
912
+
913
+ testMeadow.rawQueries.loadQuery('Read', __dirname+ '/Meadow-Provider-SQLite-AnimalReadQuery.sql',
914
+ function(pSuccess)
915
+ {
916
+ // Now try to read the record
917
+ testMeadow.doRead(testMeadow.query.addFilter('IDAnimal', 2),
918
+ function(pError, pQuery, pRecord)
919
+ {
920
+ Expect(pRecord.AnimalTypeCustom)
921
+ .to.equal('Bunny');
922
+ fDone();
923
+ }
924
+ )
925
+ });
926
+ }
927
+ );
928
+ }
929
+ );
930
+ }
931
+ );