UNPKG

foxhound

Version:

A Database Query generation library.

419 lines (365 loc) 9.69 kB
/** * Unit tests for FoxHound * * @license MIT * * @author Steven Velozo <steven@velozo.com> */ var Chai = require('chai'); var Expect = Chai.expect; var Assert = Chai.assert; var libFable = require('fable'); const _Fable = new libFable({Product:'FoxhoundTests'}); var libFoxHound = require('../source/FoxHound.js'); suite ( 'FoxHound', function() { setup ( function() { } ); suite ( 'Object Sanity', function() { test ( 'initialize should build a happy little object', function() { var testFoxHound = libFoxHound.new(_Fable); Expect(testFoxHound) .to.be.an('object', 'FoxHound should initialize as an object directly from the require statement.'); Expect(testFoxHound).to.have.a.property('uuid') .that.is.a('string'); Expect(testFoxHound).to.have.a.property('logLevel'); } ); test ( 'basic class parameters', function() { var testFoxHound = libFoxHound.new(_Fable); Expect(testFoxHound).to.have.a.property('parameters') .that.is.a('object'); Expect(testFoxHound.parameters).to.have.a.property('scope') .that.is.a('boolean'); // Scope is boolean false by default. Expect(testFoxHound.parameters).to.have.a.property('dataElements') .that.is.a('boolean'); // Scope is boolean false by default. Expect(testFoxHound.parameters).to.have.a.property('filter') .that.is.a('boolean'); // Scope is boolean false by default. Expect(testFoxHound.parameters).to.have.a.property('begin') .that.is.a('boolean'); // Scope is boolean false by default. Expect(testFoxHound.parameters).to.have.a.property('cap') .that.is.a('boolean'); // Scope is boolean false by default. Expect(testFoxHound.parameters).to.have.a.property('sort') .that.is.a('boolean'); // Scope is boolean false by default. } ); } ); suite ( 'Basic Query Generation', function() { test ( 'generate a simple query of all data in a set', function() { // The default dialect is English. This one-liner is all-in. Expect(libFoxHound.new(_Fable).setLogLevel().setScope('Widget').buildReadQuery().query.body) .to.equal('Please give me all your Widget records. Thanks.'); } ); test ( 'change the dialect', function() { var tmpQuery = libFoxHound.new(_Fable).setLogLevel(5); // Give a scope for the data to come from tmpQuery.setScope('Widget'); // Expect there to not be a dialect yet Expect(tmpQuery).to.have.a.property('dialect') .that.is.a('boolean'); // Build the query tmpQuery.buildReadQuery(); // Expect implicit dialect of English to be instantiated Expect(tmpQuery.dialect.name) .to.equal('English'); // Now submit a bad dialect tmpQuery.setDialect(); // Expect implicit dialect of English to be instantiated Expect(tmpQuery.dialect.name) .to.equal('English'); // This is the query generated by the English dialect Expect(tmpQuery.query.body) .to.equal('Please give me all your Widget records. Thanks.'); // Now change to MySQL tmpQuery.setDialect('MySQL'); Expect(tmpQuery.dialect.name) .to.equal('MySQL'); // Build the query tmpQuery.buildReadQuery(); // This is the query generated by the English dialect Expect(tmpQuery.query.body) .to.equal('SELECT `Widget`.* FROM `Widget`;'); } ); } ); suite ( 'State Management', function() { test ( 'change the scope by function', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.scope) .to.equal(false); tmpQuery.setScope('Widget'); Expect(tmpQuery.parameters.scope) .to.equal('Widget'); } ); test ( 'merge parameters', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.scope) .to.equal(false); tmpQuery.mergeParameters({scope:'Fridget'}); Expect(tmpQuery.parameters.scope) .to.equal('Fridget'); } ); test ( 'clone the object', function() { // Create a query var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.scope).to.equal(false); // Clone it var tmpQueryCloneOne = tmpQuery.clone(); Expect(tmpQueryCloneOne.parameters.scope).to.equal(false); // Set the first queries scope tmpQuery.setScope('Widget'); Expect(tmpQuery.parameters.scope).to.equal('Widget'); Expect(tmpQueryCloneOne.parameters.scope).to.equal(false); // Now clone again var tmpQueryCloneTwo = tmpQuery.clone(); Expect(tmpQueryCloneTwo.parameters.scope).to.equal('Widget'); // Set some state on the second clone, make sure it doesn't pollute other objects tmpQueryCloneTwo.setScope('Sprocket'); Expect(tmpQuery.parameters.scope).to.equal('Widget'); Expect(tmpQueryCloneOne.parameters.scope).to.equal(false); Expect(tmpQueryCloneTwo.parameters.scope).to.equal('Sprocket'); } ); test ( 'fail to change the scope by function', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.scope) .to.equal(false); // Numbers are not valid scopes tmpQuery.setScope(100); Expect(tmpQuery.parameters.scope) .to.equal(false); } ); test ( 'change the cap by function', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.cap) .to.equal(false); tmpQuery.setCap(50); Expect(tmpQuery.parameters.cap) .to.equal(50); } ); test ( 'fail to change the cap by function', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.cap) .to.equal(false); tmpQuery.setCap('Disaster'); Expect(tmpQuery.parameters.cap) .to.equal(false); } ); test ( 'change the user ID', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.userID) .to.equal(0); tmpQuery.setIDUser(1); Expect(tmpQuery.parameters.userID) .to.equal(1); Expect(tmpQuery.parameters.query.IDUser) .to.equal(1); } ); test ( 'fail to change the user ID', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.userID) .to.equal(0); tmpQuery.setLogLevel(3); tmpQuery.setIDUser('Disaster'); Expect(tmpQuery.parameters.userID) .to.equal(0); } ); test ( 'change the begin by function', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.begin) .to.equal(false); tmpQuery.setBegin(2); Expect(tmpQuery.parameters.begin) .to.equal(2); } ); test ( 'fail to change the begin by function', function() { var tmpQuery = libFoxHound.new(_Fable); Expect(tmpQuery.parameters.begin) .to.equal(false); tmpQuery.setBegin('Looming'); Expect(tmpQuery.parameters.begin) .to.equal(false); } ); test ( 'Manually set the parameters object', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.parameters = {Frogs:'YES'}; Expect(tmpQuery.parameters.Frogs) .to.equal('YES'); } ); test ( 'Manually set the query object', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.query = {body:'GET TO ALL DE CHOPPA RECORDS'}; Expect(tmpQuery.query.body) .to.contain('CHOPPA'); } ); test ( 'Manually set the result object', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.result = {executed:true, value:[{Name:'Wendy'},{Name:'Griffin'}]}; Expect(tmpQuery.result.executed) .to.equal(true); } ); test ( 'Set a bad dialect', function() { var tmpQuery = libFoxHound.new(_Fable).setDialect('Esperanto'); Expect(tmpQuery.dialect.name) .to.equal('English'); } ); test ( 'Try to pass bad filters', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.addFilter(); tmpQuery.addFilter('Name'); Expect(tmpQuery.parameters.filter) .to.equal(false); } ); test ( 'Pass many filters', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.addFilter('Name', 'Smith'); tmpQuery.addFilter('City', 'Seattle'); Expect(tmpQuery.parameters.filter.length) .to.equal(2); tmpQuery.addFilter('Age', 25, '>', 'AND', 'AgeParameter'); Expect(tmpQuery.parameters.filter.length) .to.equal(3); } ); test ( 'Pass bad records', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.addRecord(); Expect(tmpQuery.query.records) .to.equal(false); } ); test ( 'Pass multiple records', function() { var tmpQuery = libFoxHound.new(_Fable); tmpQuery.addRecord({ID:10}); tmpQuery.addRecord({ID:100}); tmpQuery.addRecord({ID:1000}); Expect(tmpQuery.query.records.length) .to.equal(3); } ); } ); } );