UNPKG

water-orm

Version:

A monolith version of Standalone waterline ORM

118 lines (94 loc) 2.95 kB
var Waterline = require('../../../lib/waterline'), assert = require('assert'); describe('.beforeValidate()', function() { describe('basic function', function() { var person; before(function(done) { var waterline = new Waterline(); var Model = Waterline.Collection.extend({ identity: 'user', connection: 'foo', attributes: { name: 'string' }, beforeValidate: function(values, cb) { values.name = values.name + ' updated'; cb(); } }); waterline.loadCollection(Model); // Fixture Adapter Def var adapterDef = { update: function(con, col, criteria, values, cb) { return cb(null, [values]); }}; var connections = { 'foo': { adapter: 'foobar' } }; waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) { if(err) done(err); person = colls.collections.user; done(); }); }); /** * Update */ describe('.update()', function() { it('should run beforeValidate and mutate values', function(done) { person.update({ name: 'criteria' }, { name: 'test' }, function(err, users) { assert(!err); assert(users[0].name === 'test updated'); done(); }); }); }); }); /** * Test Callbacks can be defined as arrays and run in order. */ describe('array of functions', function() { var person, status; before(function(done) { var waterline = new Waterline(); var Model = Waterline.Collection.extend({ identity: 'user', connection: 'foo', attributes: { name: 'string' }, beforeValidate: [ // Function 1 function(values, cb) { values.name = values.name + ' fn1'; cb(); }, // Function 2 function(values, cb) { values.name = values.name + ' fn2'; cb(); } ] }); waterline.loadCollection(Model); // Fixture Adapter Def var adapterDef = { update: function(con, col, criteria, values, cb) { return cb(null, [values]); }}; var connections = { 'foo': { adapter: 'foobar' } }; waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) { if(err) done(err); person = colls.collections.user; done(); }); }); it('should run the functions in order', function(done) { person.update({ name: 'criteria' }, { name: 'test' }, function(err, users) { assert(!err); assert(users[0].name === 'test fn1 fn2'); done(); }); }); }); });