UNPKG

9.68 kBJavaScriptView Raw
1/**
2* Unit tests for FoxHound
3*
4* @license MIT
5*
6* @author Steven Velozo <steven@velozo.com>
7*/
8
9var Chai = require('chai');
10var Expect = Chai.expect;
11var Assert = Chai.assert;
12
13var libFable = require('fable');
14var libFoxHound = require('../source/FoxHound.js');
15
16suite
17(
18 'FoxHound',
19 function()
20 {
21 setup
22 (
23 function()
24 {
25 }
26 );
27
28 suite
29 (
30 'Object Sanity',
31 function()
32 {
33 test
34 (
35 'initialize should build a happy little object',
36 function()
37 {
38 var testFoxHound = libFoxHound.new(libFable);
39 Expect(testFoxHound)
40 .to.be.an('object', 'FoxHound should initialize as an object directly from the require statement.');
41 Expect(testFoxHound).to.have.a.property('uuid')
42 .that.is.a('string');
43 Expect(testFoxHound).to.have.a.property('logLevel');
44 }
45 );
46 test
47 (
48 'basic class parameters',
49 function()
50 {
51 var testFoxHound = libFoxHound.new(libFable);
52 Expect(testFoxHound).to.have.a.property('parameters')
53 .that.is.a('object');
54 Expect(testFoxHound.parameters).to.have.a.property('scope')
55 .that.is.a('boolean'); // Scope is boolean false by default.
56 Expect(testFoxHound.parameters).to.have.a.property('dataElements')
57 .that.is.a('boolean'); // Scope is boolean false by default.
58 Expect(testFoxHound.parameters).to.have.a.property('filter')
59 .that.is.a('boolean'); // Scope is boolean false by default.
60 Expect(testFoxHound.parameters).to.have.a.property('begin')
61 .that.is.a('boolean'); // Scope is boolean false by default.
62 Expect(testFoxHound.parameters).to.have.a.property('cap')
63 .that.is.a('boolean'); // Scope is boolean false by default.
64 Expect(testFoxHound.parameters).to.have.a.property('sort')
65 .that.is.a('boolean'); // Scope is boolean false by default.
66 }
67 );
68 }
69 );
70
71 suite
72 (
73 'Basic Query Generation',
74 function()
75 {
76 test
77 (
78 'generate a simple query of all data in a set',
79 function()
80 {
81 // The default dialect is English. This one-liner is all-in.
82 Expect(libFoxHound.new(libFable).setLogLevel().setScope('Widget').buildReadQuery().query.body)
83 .to.equal('Please give me all your Widget records. Thanks.');
84 }
85 );
86 test
87 (
88 'change the dialect',
89 function()
90 {
91 var tmpQuery = libFoxHound.new(libFable).setLogLevel(5);
92
93 // Give a scope for the data to come from
94 tmpQuery.setScope('Widget');
95
96 // Expect there to not be a dialect yet
97 Expect(tmpQuery).to.have.a.property('dialect')
98 .that.is.a('boolean');
99
100 // Build the query
101 tmpQuery.buildReadQuery();
102
103 // Expect implicit dialect of English to be instantiated
104 Expect(tmpQuery.dialect.name)
105 .to.equal('English');
106 // Now submit a bad dialect
107 tmpQuery.setDialect();
108 // Expect implicit dialect of English to be instantiated
109 Expect(tmpQuery.dialect.name)
110 .to.equal('English');
111
112 // This is the query generated by the English dialect
113 Expect(tmpQuery.query.body)
114 .to.equal('Please give me all your Widget records. Thanks.');
115
116 // Now change to MySQL
117 tmpQuery.setDialect('MySQL');
118 Expect(tmpQuery.dialect.name)
119 .to.equal('MySQL');
120
121 // Build the query
122 tmpQuery.buildReadQuery();
123
124 // This is the query generated by the English dialect
125 Expect(tmpQuery.query.body)
126 .to.equal('SELECT * FROM `Widget`;');
127
128
129 }
130 );
131 }
132 );
133
134 suite
135 (
136 'State Management',
137 function()
138 {
139 test
140 (
141 'change the scope by function',
142 function()
143 {
144 var tmpQuery = libFoxHound.new(libFable);
145
146 Expect(tmpQuery.parameters.scope)
147 .to.equal(false);
148
149 tmpQuery.setScope('Widget');
150
151 Expect(tmpQuery.parameters.scope)
152 .to.equal('Widget');
153 }
154 );
155 test
156 (
157 'merge parameters',
158 function()
159 {
160 var tmpQuery = libFoxHound.new(libFable);
161
162 Expect(tmpQuery.parameters.scope)
163 .to.equal(false);
164
165 tmpQuery.mergeParameters({scope:'Fridget'});
166
167 Expect(tmpQuery.parameters.scope)
168 .to.equal('Fridget');
169 }
170 );
171 test
172 (
173 'clone the object',
174 function()
175 {
176 // Create a query
177 var tmpQuery = libFoxHound.new(libFable);
178 Expect(tmpQuery.parameters.scope).to.equal(false);
179
180 // Clone it
181 var tmpQueryCloneOne = tmpQuery.clone();
182 Expect(tmpQueryCloneOne.parameters.scope).to.equal(false);
183
184 // Set the first queries scope
185 tmpQuery.setScope('Widget');
186 Expect(tmpQuery.parameters.scope).to.equal('Widget');
187 Expect(tmpQueryCloneOne.parameters.scope).to.equal(false);
188
189 // Now clone again
190 var tmpQueryCloneTwo = tmpQuery.clone();
191 Expect(tmpQueryCloneTwo.parameters.scope).to.equal('Widget');
192
193 // Set some state on the second clone, make sure it doesn't pollute other objects
194 tmpQueryCloneTwo.setScope('Sprocket');
195 Expect(tmpQuery.parameters.scope).to.equal('Widget');
196 Expect(tmpQueryCloneOne.parameters.scope).to.equal(false);
197 Expect(tmpQueryCloneTwo.parameters.scope).to.equal('Sprocket');
198 }
199 );
200 test
201 (
202 'fail to change the scope by function',
203 function()
204 {
205 var tmpQuery = libFoxHound.new(libFable);
206
207 Expect(tmpQuery.parameters.scope)
208 .to.equal(false);
209
210 // Numbers are not valid scopes
211 tmpQuery.setScope(100);
212
213 Expect(tmpQuery.parameters.scope)
214 .to.equal(false);
215 }
216 );
217 test
218 (
219 'change the cap by function',
220 function()
221 {
222 var tmpQuery = libFoxHound.new(libFable);
223
224 Expect(tmpQuery.parameters.cap)
225 .to.equal(false);
226
227 tmpQuery.setCap(50);
228
229 Expect(tmpQuery.parameters.cap)
230 .to.equal(50);
231 }
232 );
233 test
234 (
235 'fail to change the cap by function',
236 function()
237 {
238 var tmpQuery = libFoxHound.new(libFable);
239
240 Expect(tmpQuery.parameters.cap)
241 .to.equal(false);
242
243 tmpQuery.setCap('Disaster');
244
245 Expect(tmpQuery.parameters.cap)
246 .to.equal(false);
247 }
248 );
249 test
250 (
251 'change the user ID',
252 function()
253 {
254 var tmpQuery = libFoxHound.new(libFable);
255
256 Expect(tmpQuery.parameters.userID)
257 .to.equal(0);
258
259 tmpQuery.setIDUser(1);
260
261 Expect(tmpQuery.parameters.userID)
262 .to.equal(1);
263 Expect(tmpQuery.parameters.query.IDUser)
264 .to.equal(1);
265 }
266 );
267 test
268 (
269 'fail to change the user ID',
270 function()
271 {
272 var tmpQuery = libFoxHound.new(libFable);
273
274 Expect(tmpQuery.parameters.userID)
275 .to.equal(0);
276
277 tmpQuery.setLogLevel(3);
278 tmpQuery.setIDUser('Disaster');
279
280 Expect(tmpQuery.parameters.userID)
281 .to.equal(0);
282 }
283 );
284 test
285 (
286 'change the begin by function',
287 function()
288 {
289 var tmpQuery = libFoxHound.new(libFable);
290
291 Expect(tmpQuery.parameters.begin)
292 .to.equal(false);
293
294 tmpQuery.setBegin(2);
295
296 Expect(tmpQuery.parameters.begin)
297 .to.equal(2);
298 }
299 );
300 test
301 (
302 'fail to change the begin by function',
303 function()
304 {
305 var tmpQuery = libFoxHound.new(libFable);
306
307 Expect(tmpQuery.parameters.begin)
308 .to.equal(false);
309
310 tmpQuery.setBegin('Looming');
311
312 Expect(tmpQuery.parameters.begin)
313 .to.equal(false);
314 }
315 );
316 test
317 (
318 'Manually set the parameters object',
319 function()
320 {
321 var tmpQuery = libFoxHound.new(libFable);
322
323 tmpQuery.parameters = {Frogs:'YES'};
324
325 Expect(tmpQuery.parameters.Frogs)
326 .to.equal('YES');
327 }
328 );
329 test
330 (
331 'Manually set the query object',
332 function()
333 {
334 var tmpQuery = libFoxHound.new(libFable);
335
336 tmpQuery.query = {body:'GET TO ALL DE CHOPPA RECORDS'};
337
338 Expect(tmpQuery.query.body)
339 .to.contain('CHOPPA');
340 }
341 );
342 test
343 (
344 'Manually set the result object',
345 function()
346 {
347 var tmpQuery = libFoxHound.new(libFable);
348
349 tmpQuery.result = {executed:true, value:[{Name:'Wendy'},{Name:'Griffin'}]};
350
351 Expect(tmpQuery.result.executed)
352 .to.equal(true);
353 }
354 );
355 test
356 (
357 'Set a bad dialect',
358 function()
359 {
360 var tmpQuery = libFoxHound.new(libFable).setDialect('Esperanto');
361 Expect(tmpQuery.dialect.name)
362 .to.equal('English');
363 }
364 );
365 test
366 (
367 'Try to pass bad filters',
368 function()
369 {
370 var tmpQuery = libFoxHound.new(libFable);
371 tmpQuery.addFilter();
372 tmpQuery.addFilter('Name');
373 Expect(tmpQuery.parameters.filter)
374 .to.equal(false);
375 }
376 );
377 test
378 (
379 'Pass many filters',
380 function()
381 {
382 var tmpQuery = libFoxHound.new(libFable);
383 tmpQuery.addFilter('Name', 'Smith');
384 tmpQuery.addFilter('City', 'Seattle');
385 Expect(tmpQuery.parameters.filter.length)
386 .to.equal(2);
387 tmpQuery.addFilter('Age', 25, '>', 'AND', 'AgeParameter');
388 Expect(tmpQuery.parameters.filter.length)
389 .to.equal(3); }
390 );
391 test
392 (
393 'Pass bad records',
394 function()
395 {
396 var tmpQuery = libFoxHound.new(libFable);
397 tmpQuery.addRecord();
398 Expect(tmpQuery.query.records)
399 .to.equal(false); }
400 );
401 test
402 (
403 'Pass multiple records',
404 function()
405 {
406 var tmpQuery = libFoxHound.new(libFable);
407 tmpQuery.addRecord({ID:10});
408 tmpQuery.addRecord({ID:100});
409 tmpQuery.addRecord({ID:1000});
410 Expect(tmpQuery.query.records.length)
411 .to.equal(3);
412 }
413 );
414 }
415 );
416 }
417);
\No newline at end of file