Skip to content

Instantly share code, notes, and snippets.

@LoganBarnett
Last active May 10, 2016 22:23
Show Gist options
  • Save LoganBarnett/362d3da1b75cc564a18407dc8e180fe8 to your computer and use it in GitHub Desktop.
Save LoganBarnett/362d3da1b75cc564a18407dc8e180fe8 to your computer and use it in GitHub Desktop.
Example test with db-api agnostic usage
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...
'use strict';
const R = require('ramda');
const exportFn = (dbCreate) => {
return {
create: (shipmentData) => {
const id = dbCreate(shipmentData);
return R.merge(shipmentData, {id: id});
},
};
};
'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);
});
});
'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