Last active
May 10, 2016 22:23
-
-
Save LoganBarnett/362d3da1b75cc564a18407dc8e180fe8 to your computer and use it in GitHub Desktop.
Example test with db-api agnostic usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const R = require('ramda'); | |
const dbFn = require('./sql-db'); | |
const shipmentModelFn = require('shipment-model'); | |
const db = dbFn(); | |
const shipmentModel = shipmentModelFn(db.create); | |
// provide shipmentModel to the controller or something... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const R = require('ramda'); | |
const exportFn = (dbCreate) => { | |
return { | |
create: (shipmentData) => { | |
const id = dbCreate(shipmentData); | |
return R.merge(shipmentData, {id: id}); | |
}, | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const modelFn = require('./shipment-model'); | |
describe('shipment model', () => { | |
it('creates shipments using provided shipment data', () => { | |
const dbCreate = jasmine.createSpy('dbCreate'); | |
const model = modelFn(dbCreate); | |
model.create({foo: 'bar'}); | |
expect(dbCreate).toHaveBeenCalledWith({foo: 'bar'}); | |
}); | |
it('provides a shipment id when creating a shipment', () => { | |
const dbCreate = jasmine.createSpy('dbCreate').and.returnValue(4); | |
const model = modelFn(dbCreate); | |
const result = model.create({foo: 'bar'}); | |
expect(result.id).toEqual(4); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const sqlLibrary9000 = require('sql-library-9000'); | |
const dbFn = () => { | |
return { | |
create: (tableName, data) => { | |
const sqlValues = R.map((key, value) => '(' + key + ') = ' + value); | |
const sqlValuesClause = R.join(' ', sqlValues); | |
// sql injection can happen here, but contrived example | |
const id = sqlLibrary9000.exec('INSERT INTO ' + tableName + ' VALUES ' + sqlValuesClause + ';'); | |
return id; | |
} | |
}; | |
}; | |
module.exports = dbFn; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment