meadow 2.0.5 → 2.0.9

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,1060 @@
1
+ /**
2
+ * Unit tests for the Meadow "MSSQL" Provider
3
+ *
4
+ * These tests expect a MSSQL database.....
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
+
14
+ const libMeadowConnectionMSSQL = require('meadow-connection-mssql');
15
+
16
+ var tmpFableSettings = (
17
+ {
18
+ "Product": "MeadowMSSQLTestBookstore",
19
+ "ProductVersion": "1.0.0",
20
+
21
+ "UUID":
22
+ {
23
+ "DataCenter": 0,
24
+ "Worker": 0
25
+ },
26
+ "LogStreams":
27
+ [
28
+ {
29
+ "streamtype": "console"
30
+ }
31
+ ],
32
+
33
+ "MSSQL":
34
+ {
35
+ "server": "127.0.0.1",
36
+ "port": 3306,
37
+ "user": "sa",
38
+ "password": "1234567890abc.",
39
+ "database": "bookstore",
40
+ "ConnectionPoolLimit": 20
41
+ }
42
+ });
43
+
44
+ var libFable = new (require('fable'))(tmpFableSettings);
45
+
46
+ libFable.serviceManager.addServiceType('MeadowMSSQLProvider', libMeadowConnectionMSSQL);
47
+ libFable.serviceManager.instantiateServiceProvider('MeadowMSSQLProvider');
48
+
49
+ var _AnimalJsonSchema = (
50
+ {
51
+ title: "Animal",
52
+ description: "A creature that lives in a meadow.",
53
+ type: "object",
54
+ properties: {
55
+ IDAnimal: {
56
+ description: "The unique identifier for an animal",
57
+ type: "integer"
58
+ },
59
+ Name: {
60
+ description: "The animal's name",
61
+ type: "string"
62
+ },
63
+ Type: {
64
+ description: "The type of the animal",
65
+ type: "string"
66
+ }
67
+ },
68
+ required: ["IDAnimal", "Name", "CreatingIDUser"]
69
+ });
70
+ var _AnimalSchema = (
71
+ [
72
+ { Column: "IDAnimal", Type: "AutoIdentity" },
73
+ { Column: "GUIDAnimal", Type: "AutoGUID" },
74
+ { Column: "CreateDate", Type: "CreateDate" },
75
+ { Column: "CreatingIDUser", Type: "CreateIDUser" },
76
+ { Column: "UpdateDate", Type: "UpdateDate" },
77
+ { Column: "UpdatingIDUser", Type: "UpdateIDUser" },
78
+ { Column: "Deleted", Type: "Deleted" },
79
+ { Column: "DeletingIDUser", Type: "DeleteIDUser" },
80
+ { Column: "DeleteDate", Type: "DeleteDate" },
81
+ { Column: "Name", Type: "String" },
82
+ { Column: "Type", Type: "String" },
83
+ { Column: "Age", Type: "Integer" }
84
+ ]);
85
+ var _AnimalDefault = (
86
+ {
87
+ IDAnimal: null,
88
+ GUIDAnimal: '',
89
+
90
+ CreateDate: false,
91
+ CreatingIDUser: 0,
92
+ UpdateDate: false,
93
+ UpdatingIDUser: 0,
94
+ Deleted: 0,
95
+ DeleteDate: false,
96
+ DeletingIDUser: 0,
97
+
98
+ Name: 'Unknown',
99
+ Type: 'Unclassified'
100
+ });
101
+
102
+ suite
103
+ (
104
+ 'Meadow-Provider-MSSQL',
105
+ function ()
106
+ {
107
+ var _SpooledUp = false;
108
+
109
+ var getAnimalInsert = function (pName, pType)
110
+ {
111
+ return "INSERT INTO FableTest (GUIDAnimal, CreateDate, CreatingIDUser, UpdateDate, UpdatingIDUser, Deleted, DeleteDate, DeletingIDUser, Name, Type) VALUES ('00000000-0000-0000-0000-000000000000', GETUTCDATE(), 1, GETUTCDATE(), 1, 0, NULL, 0, '" + pName + "', '" + pType + "'); ";
112
+ };
113
+
114
+ var newMeadow = function ()
115
+ {
116
+ return require('../source/Meadow.js')
117
+ .new(libFable, 'FableTest')
118
+ .setProvider('MSSQL')
119
+ .setSchema(_AnimalSchema)
120
+ .setJsonSchema(_AnimalJsonSchema)
121
+ .setDefaultIdentifier('IDAnimal')
122
+ .setDefault(_AnimalDefault)
123
+ };
124
+
125
+ suiteSetup
126
+ (
127
+ function (fDone)
128
+ {
129
+ // Only do this for the first test.
130
+ if (!_SpooledUp)
131
+ {
132
+ // Tear down previous test data
133
+ libFable.Utility.waterfall(
134
+ [
135
+ function (fStageComplete)
136
+ {
137
+ libFable.MeadowMSSQLProvider.connectAsync(
138
+ (pError, pConnectionPool) =>
139
+ {
140
+ if (pError)
141
+ {
142
+ libFable.log.error(`Error connecting to MS SQL Database: ${pError}`);
143
+ fStageComplete(pError);
144
+ }
145
+
146
+ libFable.log.info('Connection opened!');
147
+ return fStageComplete();
148
+ }
149
+ );
150
+ },
151
+ function (fStageComplete)
152
+ {
153
+ libFable.MeadowMSSQLProvider.pool.query('DROP TABLE IF EXISTS FableTest').then(()=>{ return fStageComplete(); }).catch(fStageComplete);
154
+ },
155
+ function (fStageComplete)
156
+ {
157
+ libFable.MeadowMSSQLProvider.pool.query("CREATE TABLE FableTest (IDAnimal INT IDENTITY(1,1) NOT NULL, GUIDAnimal VARCHAR(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000', CreateDate DATETIME, CreatingIDUser INT NOT NULL DEFAULT '0', UpdateDate DATETIME, UpdatingIDUser INT NOT NULL DEFAULT '0', Deleted TINYINT NOT NULL DEFAULT '0', DeleteDate DATETIME, DeletingIDUser INT NOT NULL DEFAULT '0', Name VARCHAR(128) NOT NULL DEFAULT '', Type VARCHAR(128) NOT NULL DEFAULT '' );").then(()=>{ return fStageComplete(); }).catch(fStageComplete);
158
+ },
159
+ function (fStageComplete)
160
+ {
161
+ libFable.MeadowMSSQLProvider.pool.query(getAnimalInsert('Foo Foo', 'Bunny')).then(()=>{ return fStageComplete(); }).catch(fStageComplete);
162
+ },
163
+ function (fStageComplete)
164
+ {
165
+ libFable.MeadowMSSQLProvider.pool.query(getAnimalInsert('Red Riding Hood', 'Girl')).then(()=>{ return fStageComplete(); }).catch(fStageComplete);
166
+ },
167
+ function (fStageComplete)
168
+ {
169
+ libFable.MeadowMSSQLProvider.pool.query(getAnimalInsert('Red', 'Dog')).then(()=>{ return fStageComplete(); }).catch(fStageComplete);
170
+ },
171
+ function (fStageComplete)
172
+ {
173
+ libFable.MeadowMSSQLProvider.pool.query(getAnimalInsert('Spot', 'Dog')).then(()=>{ return fStageComplete(); }).catch(fStageComplete);
174
+ },
175
+ function (fStageComplete)
176
+ {
177
+ libFable.MeadowMSSQLProvider.pool.query(getAnimalInsert('Gertrude', 'Frog')).then(()=>{ return fStageComplete(); }).catch(fStageComplete);
178
+ }
179
+ ],
180
+ function (pError, pResult)
181
+ {
182
+ // Now continue the tests.
183
+ _SpooledUp = true;
184
+ fDone();
185
+ }
186
+ );
187
+ }
188
+ else
189
+ {
190
+ fDone();
191
+ }
192
+ }
193
+ );
194
+
195
+ suiteTeardown((fDone) =>
196
+ {
197
+ //_SQLConnectionPool.end(fDone);
198
+ }
199
+ );
200
+
201
+ suite
202
+ (
203
+ 'Object Sanity',
204
+ function ()
205
+ {
206
+ test
207
+ (
208
+ 'The MSSQL class should initialize itself into a happy little object.',
209
+ function ()
210
+ {
211
+ var testMeadow = require('../source/Meadow.js').new(libFable).setProvider('MSSQL');
212
+ Expect(testMeadow).to.be.an('object', 'Meadow should initialize as an object directly from the require statement.');
213
+ }
214
+ );
215
+ }
216
+ );
217
+ suite
218
+ (
219
+ 'Query Processing',
220
+ function ()
221
+ {
222
+ test
223
+ (
224
+ 'Create a record in the database',
225
+ function (fDone)
226
+ {
227
+ var testMeadow = newMeadow().setIDUser(90210);
228
+
229
+ // Ensure this query is "slow"...
230
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
231
+
232
+ var tmpQuery = testMeadow.query.clone().setLogLevel(5)
233
+ .addRecord({ Name: 'Blastoise', Type: 'Pokemon' });
234
+
235
+ testMeadow.doCreate(tmpQuery,
236
+ function (pError, pQuery, pQueryRead, pRecord)
237
+ {
238
+ // We should have a record ....
239
+ Expect(pRecord.Name)
240
+ .to.equal('Blastoise');
241
+ Expect(pRecord.CreatingIDUser)
242
+ .to.equal(90210);
243
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
244
+ fDone();
245
+ }
246
+ )
247
+ }
248
+ );
249
+ test
250
+ (
251
+ 'Create a record in the database with Deleted bit already set',
252
+ function (fDone)
253
+ {
254
+ var testMeadow = newMeadow().setIDUser(90210);
255
+
256
+ var tmpQuery = testMeadow.query.clone().setLogLevel(5)
257
+ .setDisableDeleteTracking(true)
258
+ .addRecord({ Name: 'Blastoise', Type: 'Pokemon', Deleted: true });
259
+
260
+ testMeadow.doCreate(tmpQuery,
261
+ function (pError, pQuery, pQueryRead, pRecord)
262
+ {
263
+ // We should have a record ....
264
+ Expect(pRecord.Name)
265
+ .to.equal('Blastoise');
266
+ Expect(pRecord.CreatingIDUser)
267
+ .to.equal(90210);
268
+ fDone();
269
+ }
270
+ )
271
+ }
272
+ );
273
+ test
274
+ (
275
+ 'Read a record from the database',
276
+ function (fDone)
277
+ {
278
+ var testMeadow = newMeadow();
279
+
280
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
281
+
282
+ var tmpQuery = testMeadow.query
283
+ .addFilter('IDAnimal', 1);
284
+
285
+ testMeadow.doRead(tmpQuery,
286
+ function (pError, pQuery, pRecord)
287
+ {
288
+ // We should have a record ....
289
+ Expect(pRecord.IDAnimal)
290
+ .to.equal(1);
291
+ Expect(pRecord.Name)
292
+ .to.equal('Foo Foo');
293
+
294
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
295
+
296
+ fDone();
297
+ }
298
+ )
299
+ }
300
+ );
301
+ test
302
+ (
303
+ 'Read all records from the database',
304
+ function (fDone)
305
+ {
306
+ var testMeadow = newMeadow();
307
+
308
+ testMeadow.doReads(testMeadow.query,
309
+ function (pError, pQuery, pRecords)
310
+ {
311
+ // We should have a record ....
312
+ Expect(pRecords[0].IDAnimal)
313
+ .to.equal(1);
314
+ Expect(pRecords[0].Name)
315
+ .to.equal('Foo Foo');
316
+ Expect(pRecords[1].IDAnimal)
317
+ .to.equal(2);
318
+ Expect(pRecords[1].Name)
319
+ .to.equal('Red Riding Hood');
320
+ Expect(pRecords[1].Type)
321
+ .to.equal('Girl');
322
+ fDone();
323
+ }
324
+ )
325
+ }
326
+ );
327
+ test
328
+ (
329
+ 'Update a record in the database',
330
+ function (fDone)
331
+ {
332
+ var testMeadow = newMeadow();
333
+
334
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
335
+
336
+ var tmpQuery = testMeadow.query
337
+ .addRecord({ IDAnimal: 2, Type: 'Human' });
338
+
339
+ testMeadow.doUpdate(tmpQuery,
340
+ function (pError, pQuery, pQueryRead, pRecord)
341
+ {
342
+ // We should have a record ....
343
+ Expect(pRecord.Type)
344
+ .to.equal('Human');
345
+
346
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
347
+
348
+ fDone();
349
+ }
350
+ )
351
+ }
352
+ );
353
+ test
354
+ (
355
+ 'Delete a record in the database',
356
+ function (fDone)
357
+ {
358
+ var testMeadow = newMeadow();
359
+
360
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
361
+ var tmpQuery = testMeadow.query.addFilter('IDAnimal', 3);
362
+
363
+ testMeadow.doDelete(tmpQuery,
364
+ function (pError, pQuery, pRecord)
365
+ {
366
+ // It returns the number of rows deleted
367
+ Expect(pRecord)
368
+ .to.equal(1);
369
+
370
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
371
+
372
+ fDone();
373
+ }
374
+ )
375
+ }
376
+ );
377
+ test
378
+ (
379
+ 'Undelete a record in the database',
380
+ function (fDone)
381
+ {
382
+ var testMeadow = newMeadow();
383
+
384
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
385
+ var tmpDeleteQuery = testMeadow.query.addFilter('IDAnimal', 5);
386
+
387
+ // Make sure the record is deleted!
388
+ testMeadow.doDelete(tmpDeleteQuery,
389
+ function (pDeleteError, pDeleteQuery, pDeleteRecord)
390
+ {
391
+ var tmpQuery = testMeadow.query.addFilter('IDAnimal', 5);
392
+ testMeadow.doUndelete(tmpQuery,
393
+ function (pError, pQuery, pRecord)
394
+ {
395
+ // It returns the number of rows deleted
396
+ Expect(pRecord)
397
+ .to.equal(1);
398
+
399
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
400
+
401
+ fDone();
402
+ });
403
+ });
404
+ }
405
+ );
406
+ test
407
+ (
408
+ 'Count all records from the database',
409
+ function (fDone)
410
+ {
411
+ var testMeadow = newMeadow();
412
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1;
413
+
414
+ Expect(testMeadow.query.parameters.result.executed)
415
+ .to.equal(false);
416
+ testMeadow.doCount(testMeadow.query,
417
+ function (pError, pQuery, pRecord)
418
+ {
419
+ // There should be 5 records
420
+ Expect(pRecord)
421
+ .to.equal(5);
422
+ Expect(pQuery.parameters.result.executed)
423
+ .to.equal(true);
424
+ testMeadow.fable.settings.QueryThresholdWarnTime = 1000;
425
+ fDone();
426
+ }
427
+ )
428
+ }
429
+ );
430
+ test
431
+ (
432
+ 'Perform operations with a schema-based instantiation',
433
+ function (fDone)
434
+ {
435
+ var testMeadow = require('../source/Meadow.js').new(libFable)
436
+ .loadFromPackage(__dirname + '/Animal.json').setProvider('MSSQL');
437
+
438
+ // Make sure the authentication stuff got loaded
439
+ Expect(testMeadow.schemaFull.authorizer.User)
440
+ .to.be.an('object');
441
+ Expect(testMeadow.schemaFull.authorizer.User.Create)
442
+ .to.equal('Allow');
443
+
444
+ var tmpQuery = testMeadow.query
445
+ .addRecord({ Name: 'Grommet', Type: 'Dog' });
446
+
447
+ testMeadow.doCreate(tmpQuery,
448
+ function (pError, pQuery, pQueryRead, pRecord)
449
+ {
450
+ // We should have a record ....
451
+ Expect(pRecord.Name)
452
+ .to.equal('Grommet');
453
+ fDone();
454
+ }
455
+ )
456
+ }
457
+ );
458
+ }
459
+ );
460
+ suite
461
+ (
462
+ 'Logged Query Processing',
463
+ function ()
464
+ {
465
+ test
466
+ (
467
+ 'Create a record in the database',
468
+ function (fDone)
469
+ {
470
+ var testMeadow = newMeadow();
471
+
472
+ var tmpQuery = testMeadow.query
473
+ .setLogLevel(5)
474
+ .addRecord({ Name: 'MewTwo', Type: 'Pokemon' });
475
+
476
+ testMeadow.doCreate(tmpQuery,
477
+ function (pError, pQuery, pQueryRead, pRecord)
478
+ {
479
+ // We should have a record ....
480
+ Expect(pRecord.Name)
481
+ .to.equal('MewTwo');
482
+ fDone();
483
+ }
484
+ )
485
+ }
486
+ );
487
+ test
488
+ (
489
+ 'Create a record in the database with a predefined GUID',
490
+ function (fDone)
491
+ {
492
+ var testMeadow = newMeadow();
493
+
494
+ var tmpQuery = testMeadow.query
495
+ .setLogLevel(5)
496
+ .addRecord({ Name: 'MewThree', GUIDAnimal: '0x12345', Type: 'Pokemon' });
497
+
498
+ testMeadow.doCreate(tmpQuery,
499
+ function (pError, pQuery, pQueryRead, pRecord)
500
+ {
501
+ // We should have a record ....
502
+ Expect(pRecord.Name)
503
+ .to.equal('MewThree');
504
+ fDone();
505
+ }
506
+ )
507
+ }
508
+ );
509
+ test
510
+ (
511
+ 'Create a record in the database with a previously predefined GUID -- expect failure',
512
+ function (fDone)
513
+ {
514
+ var testMeadow = newMeadow();
515
+
516
+ var tmpQuery = testMeadow.query
517
+ .setLogLevel(5)
518
+ .addRecord({ Name: 'MewThree', GUIDAnimal: '0x12345', Type: 'Pokemon' });
519
+
520
+ testMeadow.doCreate(tmpQuery,
521
+ function (pError, pQuery, pQueryRead, pRecord)
522
+ {
523
+ Expect(pError)
524
+ .to.equal("Record with GUID 0x12345 already exists!");
525
+ fDone();
526
+ }
527
+ )
528
+ }
529
+ );
530
+ test
531
+ (
532
+ 'Read a record from the database',
533
+ function (fDone)
534
+ {
535
+ var testMeadow = newMeadow();
536
+
537
+ var tmpQuery = testMeadow.query
538
+ .setLogLevel(5)
539
+ .addFilter('IDAnimal', 1);
540
+
541
+ testMeadow.doRead(tmpQuery,
542
+ function (pError, pQuery, pRecord)
543
+ {
544
+ // We should have a record ....
545
+ Expect(pRecord.IDAnimal)
546
+ .to.equal(1);
547
+ Expect(pRecord.Name)
548
+ .to.equal('Foo Foo');
549
+ fDone();
550
+ }
551
+ )
552
+ }
553
+ );
554
+ test
555
+ (
556
+ 'Read all records from the database',
557
+ function (fDone)
558
+ {
559
+ var testMeadow = newMeadow();
560
+
561
+ testMeadow.doReads(testMeadow.query.setLogLevel(5),
562
+ function (pError, pQuery, pRecords)
563
+ {
564
+ // We should have a record ....
565
+ Expect(pRecords[0].IDAnimal)
566
+ .to.equal(1);
567
+ Expect(pRecords[0].Name)
568
+ .to.equal('Foo Foo');
569
+ Expect(pRecords[1].IDAnimal)
570
+ .to.equal(2);
571
+ Expect(pRecords[1].Name)
572
+ .to.equal('Red Riding Hood');
573
+ Expect(pRecords[1].Type)
574
+ .to.equal('Human');
575
+ fDone();
576
+ }
577
+ )
578
+ }
579
+ );
580
+ test
581
+ (
582
+ 'Update a record in the database',
583
+ function (fDone)
584
+ {
585
+ var testMeadow = newMeadow();
586
+
587
+ var tmpQuery = testMeadow.query
588
+ .setLogLevel(5)
589
+ .addRecord({ IDAnimal: 2, Type: 'HumanGirl' });
590
+
591
+ testMeadow.doUpdate(tmpQuery,
592
+ function (pError, pQuery, pQueryRead, pRecord)
593
+ {
594
+ // We should have a record ....
595
+ Expect(pRecord.Type)
596
+ .to.equal('HumanGirl');
597
+ fDone();
598
+ }
599
+ )
600
+ }
601
+ );
602
+ test
603
+ (
604
+ 'Delete a record in the database',
605
+ function (fDone)
606
+ {
607
+ var testMeadow = newMeadow();
608
+
609
+ var tmpQuery = testMeadow.query
610
+ .setLogLevel(5)
611
+ .addFilter('IDAnimal', 4);
612
+
613
+ testMeadow.doDelete(tmpQuery,
614
+ function (pError, pQuery, pRecord)
615
+ {
616
+ // It returns the number of rows deleted
617
+ Expect(pRecord)
618
+ .to.equal(1);
619
+ fDone();
620
+ }
621
+ )
622
+ }
623
+ );
624
+ test
625
+ (
626
+ 'Count all records from the database',
627
+ function (fDone)
628
+ {
629
+ var testMeadow = newMeadow();
630
+
631
+ testMeadow.doCount(testMeadow.query.setLogLevel(5),
632
+ function (pError, pQuery, pRecord)
633
+ {
634
+ // There should be 7 records
635
+ Expect(pRecord)
636
+ .to.equal(7);
637
+ fDone();
638
+ }
639
+ )
640
+ }
641
+ );
642
+ test
643
+ (
644
+ 'Read a record from the database that had a defined GUID',
645
+ function (fDone)
646
+ {
647
+ var testMeadow = newMeadow();
648
+
649
+ var tmpQuery = testMeadow.query
650
+ .addFilter('GUIDAnimal', '0x12345');
651
+
652
+ testMeadow.doRead(tmpQuery,
653
+ function (pError, pQuery, pRecord)
654
+ {
655
+ // We should have a record ....
656
+ Expect(pRecord.IDAnimal)
657
+ .to.equal(10);
658
+ Expect(pRecord.Name)
659
+ .to.equal('MewThree');
660
+ fDone();
661
+ }
662
+ )
663
+ }
664
+ );
665
+ test
666
+ (
667
+ 'Create a record in the database with a defined creating user',
668
+ function (fDone)
669
+ {
670
+ var testMeadow = newMeadow();
671
+ var tmpQuery = testMeadow.query
672
+ .setIDUser(800)
673
+ .addRecord({ Name: 'MewSix', GUIDAnimal: '0x123456', Type: 'Pokemon' });
674
+
675
+ testMeadow.doCreate(tmpQuery,
676
+ function (pError, pQuery, pQueryRead, pRecord)
677
+ {
678
+ // We should have a record ....
679
+ Expect(pRecord.Name)
680
+ .to.equal('MewSix');
681
+ Expect(pRecord.CreatingIDUser)
682
+ .to.equal(800);
683
+ fDone();
684
+ }
685
+ )
686
+ }
687
+ );
688
+ }
689
+ );
690
+ suite
691
+ (
692
+ 'The Bad Kind of Query Processing',
693
+ function ()
694
+ {
695
+ test
696
+ (
697
+ 'Count all records from the database from a nonexistent table',
698
+ function (fDone)
699
+ {
700
+ var testMeadow = newMeadow();
701
+
702
+ testMeadow.doCount(testMeadow.query.setScope('BadTable'),
703
+ function (pError, pQuery, pRecord)
704
+ {
705
+ Expect(pError.code)
706
+ .to.equal("ENOTPREPARED");
707
+ fDone();
708
+ }
709
+ )
710
+ }
711
+ );
712
+ test
713
+ (
714
+ 'Create a record in the database with an invalid default identifier',
715
+ function (fDone)
716
+ {
717
+ var testMeadow = newMeadow().setDefaultIdentifier('BadIdentifier');
718
+
719
+ var tmpQuery = testMeadow.query
720
+ .addRecord({ Name: 'Scaley', Type: 'Chameleon' });
721
+
722
+ testMeadow.doCreate(tmpQuery,
723
+ function (pError, pQuery, pQueryRead, pRecord)
724
+ {
725
+ // We should have no record because the default id is IDFableTest and our tables identity is IDAnimal
726
+ Expect(pError.code)
727
+ .to.equal('ENOTPREPARED');
728
+ fDone();
729
+ }
730
+ )
731
+ }
732
+ );
733
+ test
734
+ (
735
+ 'Create a record in the database with the wrong default identifier',
736
+ function (fDone)
737
+ {
738
+ var testMeadow = newMeadow().setDefaultIdentifier('Type');
739
+
740
+ var tmpQuery = testMeadow.query
741
+ .addRecord({ Name: 'Tina', Type: 'Chameleon' });
742
+
743
+ testMeadow.doCreate(tmpQuery,
744
+ function (pError, pQuery, pQueryRead, pRecord)
745
+ {
746
+ // We should have no record because the default id is IDFableTest and our tables identity is IDAnimal
747
+ Expect(pError.code)
748
+ .to.equal('EPARAM');
749
+ fDone();
750
+ }
751
+ )
752
+ }
753
+ );
754
+ test
755
+ (
756
+ 'Create a record in the database with no record',
757
+ function (fDone)
758
+ {
759
+ var testMeadow = newMeadow().setDefaultIdentifier('Type');
760
+
761
+ testMeadow.doCreate(testMeadow.query,
762
+ function (pError, pQuery, pQueryRead, pRecord)
763
+ {
764
+ // We should have no record because the default id is IDFableTest and our tables identity is IDAnimal
765
+ Expect(pError)
766
+ .to.equal('No record submitted');
767
+ fDone();
768
+ }
769
+ )
770
+ }
771
+ );
772
+ test
773
+ (
774
+ 'Read a record from the database with no data returned',
775
+ function (fDone)
776
+ {
777
+ var testMeadow = newMeadow();
778
+
779
+ var tmpQuery = testMeadow.query
780
+ .addFilter('IDAnimal', 5000);
781
+ testMeadow.doRead(tmpQuery,
782
+ function (pError, pQuery, pRecord)
783
+ {
784
+ Expect(pRecord)
785
+ .to.equal(false);
786
+ fDone();
787
+ }
788
+ )
789
+ }
790
+ );
791
+ test
792
+ (
793
+ 'Read records from the database with no data returned',
794
+ function (fDone)
795
+ {
796
+ var testMeadow = newMeadow();
797
+
798
+ var tmpQuery = testMeadow.query
799
+ .addFilter('IDAnimal', 5000);
800
+
801
+ testMeadow.doReads(tmpQuery,
802
+ function (pError, pQuery, pRecord)
803
+ {
804
+ Expect(pRecord.length)
805
+ .to.equal(0);
806
+ fDone();
807
+ }
808
+ )
809
+ }
810
+ );
811
+ test
812
+ (
813
+ 'Read records from the database with an invalid query',
814
+ function (fDone)
815
+ {
816
+ var testMeadow = newMeadow();
817
+
818
+ var tmpQuery = testMeadow.query
819
+ .addFilter('IDAnimalFarmGeorge', 5000);
820
+
821
+ testMeadow.doReads(tmpQuery,
822
+ function (pError, pQuery, pRecord)
823
+ {
824
+ Expect(pError.code)
825
+ .to.equal('ENOTPREPARED');
826
+ fDone();
827
+ }
828
+ )
829
+ }
830
+ );
831
+ test
832
+ (
833
+ 'Read a single record from the database with an invalid query',
834
+ function (fDone)
835
+ {
836
+ var testMeadow = newMeadow();
837
+
838
+ var tmpQuery = testMeadow.query
839
+ .addFilter('IDAnimalFarmGeorge', 5000);
840
+
841
+ testMeadow.doRead(tmpQuery,
842
+ function (pError, pQuery, pRecord)
843
+ {
844
+ Expect(pError.code)
845
+ .to.equal('ENOTPREPARED');
846
+ fDone();
847
+ }
848
+ )
849
+ }
850
+ );
851
+ test
852
+ (
853
+ 'Delete with a bad query',
854
+ function (fDone)
855
+ {
856
+ var testMeadow = newMeadow();
857
+
858
+ var tmpQuery = testMeadow.query
859
+ .addFilter('IDAnimalHouse', 4);
860
+
861
+ testMeadow.doDelete(tmpQuery,
862
+ function (pError, pQuery, pRecord)
863
+ {
864
+ Expect(pError.code)
865
+ .to.equal('ENOTPREPARED');
866
+ fDone();
867
+ }
868
+ )
869
+ }
870
+ );
871
+ test
872
+ (
873
+ 'Update a record in the database with a bad filter',
874
+ function (fDone)
875
+ {
876
+ var testMeadow = newMeadow();
877
+
878
+ var tmpQuery = testMeadow.query
879
+ .setLogLevel(5)
880
+ .addRecord({ IDAnimal: undefined, Type: 'HumanGirl' });
881
+
882
+ testMeadow.doUpdate(tmpQuery,
883
+ function (pError, pQuery, pQueryRead, pRecord)
884
+ {
885
+ // We should have a record ....
886
+ Expect(pError)
887
+ .to.equal('Automated update missing filters... aborting!');
888
+ fDone();
889
+ }
890
+ )
891
+ }
892
+ );
893
+ test
894
+ (
895
+ 'Update a record in the database without passing a record in',
896
+ function (fDone)
897
+ {
898
+ var testMeadow = newMeadow();
899
+
900
+ testMeadow.doUpdate(testMeadow.query,
901
+ function (pError, pQuery, pQueryRead, pRecord)
902
+ {
903
+ Expect(pError)
904
+ .to.equal('No record submitted');
905
+ fDone();
906
+ }
907
+ )
908
+ }
909
+ );
910
+ test
911
+ (
912
+ 'Update a record in the database with a bad record passed in (no default identifier)',
913
+ function (fDone)
914
+ {
915
+ var testMeadow = newMeadow();
916
+
917
+ var tmpQuery = testMeadow.query
918
+ .addRecord({ Name: 'Bill' });
919
+
920
+ testMeadow.doUpdate(tmpQuery,
921
+ function (pError, pQuery, pQueryRead, pRecord)
922
+ {
923
+ Expect(pError)
924
+ .to.equal('Automated update missing default identifier');
925
+ fDone();
926
+ }
927
+ )
928
+ }
929
+ );
930
+ test
931
+ (
932
+ 'Update a record in the database that does not exist',
933
+ function (fDone)
934
+ {
935
+ var testMeadow = newMeadow();
936
+
937
+ var tmpQuery = testMeadow.query
938
+ .addRecord({ IDAnimal: 983924 });
939
+
940
+ testMeadow.doUpdate(tmpQuery,
941
+ function (pError, pQuery, pQueryRead, pRecord)
942
+ {
943
+ Expect(pError)
944
+ .to.equal('No record found to update!');
945
+ fDone();
946
+ }
947
+ )
948
+ }
949
+ );
950
+ test
951
+ (
952
+ 'Set a raw Query',
953
+ function (fDone)
954
+ {
955
+ var testMeadow = newMeadow();
956
+ testMeadow.rawQueries.setQuery('Read', 'SELECT Something from SomethingElse;');
957
+
958
+ Expect(testMeadow.rawQueries.getQuery('Read'))
959
+ .to.equal('SELECT Something from SomethingElse;');
960
+ fDone();
961
+ }
962
+ );
963
+ test
964
+ (
965
+ 'Load a raw Query',
966
+ function (fDone)
967
+ {
968
+ var testMeadow = newMeadow();
969
+
970
+ testMeadow.rawQueries.loadQuery('Read', __dirname + '/Meadow-Provider-MSSQL-AnimalReadQuery.sql',
971
+ function (pSuccess)
972
+ {
973
+ Expect(testMeadow.rawQueries.getQuery('Read'))
974
+ .to.contain('SELECT');
975
+ fDone();
976
+ });
977
+ }
978
+ );
979
+ test
980
+ (
981
+ 'Load a bad raw Query',
982
+ function (fDone)
983
+ {
984
+ var testMeadow = newMeadow();
985
+
986
+ testMeadow.rawQueries.loadQuery('Read', __dirname + '/Meadow-Provider-MSSQL-BADAnimalReadQuery.sql',
987
+ function (pSuccess)
988
+ {
989
+ Expect(testMeadow.rawQueries.getQuery('Read'))
990
+ .to.equal('');
991
+ fDone();
992
+ });
993
+ }
994
+ );
995
+ test
996
+ (
997
+ 'Load a raw query with no callback',
998
+ function ()
999
+ {
1000
+ var testMeadow = newMeadow();
1001
+
1002
+ testMeadow.rawQueries.loadQuery('Read', __dirname + '/Meadow-Provider-MSSQL-AnimalReadQuery.sql');
1003
+ }
1004
+ );
1005
+ test
1006
+ (
1007
+ 'Check for a query that is not there',
1008
+ function ()
1009
+ {
1010
+ var testMeadow = newMeadow();
1011
+ Expect(testMeadow.rawQueries.getQuery('Read'))
1012
+ .to.equal(false);
1013
+ }
1014
+ );
1015
+ test
1016
+ (
1017
+ 'Read a record from a custom query',
1018
+ function (fDone)
1019
+ {
1020
+ var testMeadow = newMeadow();
1021
+
1022
+ testMeadow.rawQueries.loadQuery('Read', __dirname + '/Meadow-Provider-MSSQL-AnimalReadQuery.sql',
1023
+ function (pSuccess)
1024
+ {
1025
+ // Now try to read the record
1026
+ testMeadow.doRead(testMeadow.query.addFilter('IDAnimal', 2),
1027
+ function (pError, pQuery, pRecord)
1028
+ {
1029
+ Expect(pRecord.AnimalTypeCustom)
1030
+ .to.equal('Bunny');
1031
+ fDone();
1032
+ }
1033
+ )
1034
+ });
1035
+ }
1036
+ );
1037
+ test
1038
+ (
1039
+ 'Create a record in the database with bad fields',
1040
+ function (fDone)
1041
+ {
1042
+ var testMeadow = newMeadow();
1043
+ // NOTE: Bad fields passed in are polluting the schema forever.
1044
+ var tmpQuery = testMeadow.query
1045
+ .addRecord({ Name: 'Tina', TypeWriter: 'Chameleon' });
1046
+
1047
+ testMeadow.doCreate(tmpQuery,
1048
+ function (pError, pQuery, pQueryRead, pRecord)
1049
+ {
1050
+ Expect(pError.code)
1051
+ .to.equal('ENOTPREPARED');
1052
+ fDone();
1053
+ }
1054
+ )
1055
+ }
1056
+ );
1057
+ }
1058
+ );
1059
+ }
1060
+ );