-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql-engine.test.js
415 lines (403 loc) · 18.1 KB
/
sql-engine.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
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
import SQLEngine from './sql-engine.js';
import { SQLServerTypes, PostgresTypes } from './sql-types.js';
import StashKu, { GetRequest, PostRequest, PutRequest, PatchRequest, DeleteRequest, OptionsRequest, Filter, Response } from '@appku/stashku';
import ProductionCultureModel from './test/models/production-culture.js';
import PurchasingVendorModel from './test/models/purchasing-vendor.js';
import SalesCurrencyModel from './test/models/sales-currency.js';
import fs from 'fs/promises';
import dotenv from 'dotenv';
let drivers = ['sql-server']; //, 'postgres'];
for (let driver of drivers) {
describe(`With "${driver}" driver...`, () => {
beforeAll(() => {
dotenv.config({ path: `driver-${driver}.env` });
});
describe('#constructor', () => {
it('sets the engine name to "sql".', () => {
expect(new SQLEngine().name).toBe('sql');
});
});
describe('#resources', () => {
it('gets a list of table and view names.', async () => {
let e = new SQLEngine();
e.configure();
let names = await e.resources();
expect(names.length).toBeGreaterThan(0);
for (let n of names) {
expect(n).toMatch(/.+\..+/i);
}
await e.destroy();
});
});
describe('#raw', () => {
let e = new SQLEngine();
beforeAll(() => e.configure());
afterAll(async () => {
await e.destroy();
});
it('runs a select query with parameters', async () => {
let results = await e.raw('SELECT FirstName, LastName, BusinessEntityID FROM Person.Person WHERE FirstName LIKE @fn AND BusinessEntityID > @idMin', { fn: 'Ab%', idMin: 3900 });
expect(Array.isArray(results)).toBe(true);
expect(results.length).toBe(91);
for (let r of results) {
expect(r.FirstName).toMatch(/^Ab/);
expect(r.BusinessEntityID).toBeGreaterThanOrEqual(3900);
}
});
});
describe('#bulk', () => {
let e = new SQLEngine();
beforeAll(() => e.configure());
afterAll(async () => {
await e.raw('DELETE FROM Person.ContactType WHERE ContactTypeID > 20;');
await e.destroy();
});
it('bulk inserts rows into a table.', async () => {
let columns = new Map();
let rows = [];
columns.set('Name', { type: SQLServerTypes.NVarChar, nullable: false });
columns.set('ModifiedDate', { type: SQLServerTypes.DateTime, nullable: false });
for (let x = 0; x < 250; x++) {
rows.push({
Name: `Test${x}`,
ModifiedDate: new Date()
});
}
let count = await e.bulk('Person.ContactType', columns, rows);
expect(count).toBe(250);
});
});
describe('#get', () => {
let stash = null;
beforeAll(() => {
stash = new StashKu({ engine: '@appku/stashku-sql' });
});
afterAll(() => {
return stash.destroy();
});
it('gets results from the database.', async () => {
await stash.engine;
let results = await stash.engine.get(new GetRequest()
.from('Person.Person')
.properties('FirstName', 'LastName', 'Title')
.where(f => f.and('FirstName', Filter.OP.STARTSWITH, 'K'))
.skip(0)
.take(15)
.sort('LastName', 'FirstName')
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.returned);
expect(results.total).toBe(1255);
expect(results.affected).toBe(0);
expect(results.returned).toBe(15);
});
it('runs a count-only query with paging.', async () => {
let results = await stash.engine.get(new GetRequest()
.from('Person.Person')
.properties('FirstName', 'LastName', 'Title')
.where(f => f.and('FirstName', Filter.OP.STARTSWITH, 'K'))
.skip(0)
.take(15)
.count()
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(0);
expect(results.total).toBe(1255);
expect(results.affected).toBe(0);
expect(results.returned).toBe(15);
});
it('runs a count-only distinct query with paging.', async () => {
let results = await stash.engine.get(new GetRequest()
.from('Person.Person')
.properties('Title')
.distinct()
.skip(5)
.take(5)
.count()
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(0);
expect(results.total).toBe(7);
expect(results.affected).toBe(0);
expect(results.returned).toBe(2);
});
it('runs a distinct query.', async () => {
let results = await stash.engine.get(new GetRequest()
.from('Person.Person')
.properties('FirstName')
.where(f => f.and('FirstName', Filter.OP.STARTSWITH, 'Ad'))
.distinct()
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(5);
expect(results.total).toBe(5);
});
it('runs a simple modeled query.', async () => {
let results = await stash.model(PurchasingVendorModel).get();
expect(results.affected).toBe(0);
expect(results.total).toBe(104);
expect(results.returned).toBe(104);
expect(results.data.length).toBe(104);
for (let m of results.data) {
expect(m).toBeInstanceOf(PurchasingVendorModel);
expect(m.BusinessEntityID).toBeTruthy();
expect(typeof m.Name).toBe('string');
}
});
it('runs a simple modeled query with constraints.', async () => {
let results = await stash.model(PurchasingVendorModel).get((r, m) => r
.properties(m.Name, m.CreditRating)
.where(f => f.and(m.Name, f.OP.CONTAINS, 'e'))
.take(3)
);
expect(results.affected).toBe(0);
expect(results.total).toBe(95);
expect(results.returned).toBe(3);
expect(results.data.length).toBe(3);
for (let m of results.data) {
expect(m).toBeInstanceOf(PurchasingVendorModel);
expect(m.BusinessEntityID).toBeUndefined();
expect(typeof m.Name).toBe('string');
expect(typeof m.CreditRating).toBe('number');
}
});
});
describe('#post', () => {
let stash = null;
beforeAll(() => {
stash = new StashKu({ engine: '@appku/stashku-sql' });
});
afterAll(async () => {
await stash.engine.raw('DELETE FROM Production.Location WHERE LocationID > 60;');
await stash.engine.raw('DELETE FROM Production.Culture WHERE ModifiedDate > \'01-01-2009\';');
return stash.destroy();
});
it('bulk+batch loads records.', async () => {
let rows = [];
//prep data
for (let x = 0; x < 100; x++) {
rows.push({
Name: `PostBulkBatch_Test${x}`,
CostRate: x * 0.5,
Availability: x + Math.random()
});
}
//run request
let results = await stash.engine.post(new PostRequest()
.to('Production.Location')
.objects(rows)
.headers({
batch: {
enabled: true,
size: 10
},
bulk: {
Name: { type: SQLServerTypes.NVarChar, nullable: false },
CostRate: { type: SQLServerTypes.SmallMoney, nullable: false },
Availability: { type: SQLServerTypes.Decimal, nullable: false, precision: 8, scale: 2 },
}
})
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(0);
expect(results.total).toBe(100);
expect(results.affected).toBe(100);
expect(results.returned).toBe(0);
}, 30000);
it('adds new objects to the database.', async () => {
let results = await stash.engine.post(new PostRequest()
.to('Production.Location')
.objects(
{
Name: 'AA',
CostRate: 1,
Availability: 123
},
{
Name: 'BB',
CostRate: 2,
Availability: 234
},
{
Name: 'CC',
CostRate: 3,
Availability: 345
}
)
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.total).toBe(3);
expect(results.affected).toBe(3);
expect(results.returned).toBe(3);
});
it('adds modeled objects to a table', async () => {
let m1 = new ProductionCultureModel();
let m2 = new ProductionCultureModel();
m1.CultureID = 'az-AZ';
m1.Name = 'Republic of Arizona';
m2.CultureID = 'cs-CA';
m2.Name = 'The Free People of Cascadia';
let results = await stash.model(ProductionCultureModel).post(r => r.objects(m1, m2));
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.total).toBe(2);
expect(results.affected).toBe(2);
expect(results.returned).toBe(2);
});
});
describe('#put', () => {
let stash = null;
beforeAll(() => {
stash = new StashKu({ engine: '@appku/stashku-sql' });
});
afterAll(async () => {
await stash.engine.raw('UPDATE Production.Culture SET [Name] = \'Arabic\' WHERE CultureID = \'ar\';');
return stash.destroy();
});
it('updates objects in the database.', async () => {
let results = await stash.engine.put(new PutRequest()
.to('Production.Product')
.pk('ProductID')
.objects(
{
ProductID: 1,
Name: 'Bananas',
ProductNumber: 'BAN-1111',
Color: 'Yellow',
SafetyStockLevel: 46
},
{
ProductID: 2,
Name: 'Kiwi',
ProductNumber: 'KIW-7984',
Color: 'Brown',
SafetyStockLevel: 64
}
)
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.total).toBe(2);
});
it('updates a modeled record.', async () => {
let m1 = new ProductionCultureModel();
m1.CultureID = 'ar';
m1.Name = 'Middle East';
let results = await stash.model(ProductionCultureModel).put((r, m) => r.objects(m1));
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.data[0].Name).toBe('Middle East');
expect(results.total).toBe(1);
expect(results.affected).toBe(1);
expect(results.returned).toBe(1);
});
});
describe('#patch', () => {
let stash = null;
beforeAll(() => {
stash = new StashKu({ engine: '@appku/stashku-sql' });
});
afterAll(async () => {
await stash.engine.raw('UPDATE Sales.Currency SET [Name] = \'Lek\' WHERE CurrencyCode = \'ALL\';');
return stash.destroy();
});
it('updates objects in the database from a template.', async () => {
let results = await stash.engine.patch(new PatchRequest()
.to('Production.Product')
.template(
{
Color: 'Assorted',
SafetyStockLevel: 1
}
)
.where(f => f
.and('ProductID', Filter.OP.IN, [317, 318, 319, 320])
.and('ProductNumber', Filter.OP.STARTSWITH, 'CA')
)
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.total).toBe(3);
});
it('updates using a modeled template.', async () => {
let m1 = new SalesCurrencyModel();
m1.Name = 'Hamburger';
delete m1.CurrencyCode;
delete m1.ModifiedDate;
let results = await stash.model(SalesCurrencyModel).patch((r, m) => r
.template(m1)
.where(f => f.and(m.CurrencyCode, f.OP.IN, ['ALL', 'ANY']))
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.data[0].CurrencyCode).toBe('ALL');
expect(results.data[0].Name).toBe('Hamburger');
expect(results.total).toBe(1);
expect(results.affected).toBe(1);
expect(results.returned).toBe(1);
});
});
describe('#delete', () => {
let stash = null;
beforeAll(async () => {
stash = new StashKu({ engine: '@appku/stashku-sql' });
await stash.engine;
let preload = JSON.parse(await fs.readFile('./test/delete/Person.PersonPhone.json'));
let promises = [];
for (let row of preload) {
promises.push(stash.engine.raw('INSERT INTO Person.PersonPhone (BusinessEntityID, PhoneNumber, PhoneNumberTypeID) VALUES (@BusinessEntityID, @PhoneNumber, @PhoneNumberTypeID)', row));
}
await Promise.all(promises);
});
afterAll(async () => {
await stash.engine.raw('DELETE FROM Person.PersonPhone WHERE PhoneNumber LIKE \'123-4%\';');
return stash.destroy();
});
it('deletes objects in the database matching conditions.', async () => {
let results = await stash.engine.delete(new DeleteRequest()
.from('Person.PersonPhone')
.where(f => f
.and('PhoneNumber', Filter.OP.STARTSWITH, '123-456')
)
);
expect(results).toBeInstanceOf(Response);
expect(results.data.length).toBe(results.total);
expect(results.total).toBe(5);
});
});
describe('#options', () => {
let stash = null;
beforeAll(() => {
stash = new StashKu({ engine: '@appku/stashku-sql' });
});
afterAll(async () => {
return stash.destroy();
});
it('throws a 404 when an invalid resource is specified.', async () => {
expect.assertions(2);
try {
await stash.engine.options(new OptionsRequest()
.from('test')
);
} catch (err) {
expect(err.toString()).toMatch(/resource.+not found/i);
expect(err.code).toBe(404);
}
});
it('returns a model type with a proper model configuration.', async () => {
let res = await stash.engine.options(new OptionsRequest('Person.Person'));
expect(res.code).toBe(200);
expect(res.data.length).toBe(1);
expect(res.data[0].name).toBe('PersonPersonModel');
expect(res.data[0].BusinessEntityID.target).toBe('BusinessEntityID');
expect(res.data[0].BusinessEntityID.pk).toBeTruthy();
expect(res.data[0].BusinessEntityID.omit).toEqual({ post: null, put: null });
expect(res.data[0].BusinessEntityID.default).toBeUndefined();
expect(res.data[0].FirstName.target).toBe('FirstName');
expect(res.data[0].FirstName.default).toBe('');
expect(res.data[0].$stashku).toBeTruthy();
});
});
});
}