water-orm
Version:
A monolith version of Standalone waterline ORM
98 lines (78 loc) • 2.8 kB
JavaScript
var Waterline = require('../../../lib/waterline'),
assert = require('assert');
describe('Model', function() {
describe('associations hasMany', function() {
describe('.add() with an object', function() {
/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////
var collections = {};
var fooValues = [];
before(function(done) {
var waterline = new Waterline();
var User = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'person',
attributes: {
preferences: {
collection: 'preference',
via: 'user'
}
}
});
var Preference = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'preference',
attributes: {
foo: 'string',
user: {
model: 'person'
}
}
});
waterline.loadCollection(User);
waterline.loadCollection(Preference);
var _values = [
{ id: 1, preference: [{ foo: 'bar' }, { foo: 'foobar' }] },
{ id: 2, preference: [{ foo: 'a' }, { foo: 'b' }] },
];
var adapterDef = {
find: function(con, col, criteria, cb) { return cb(null, _values); },
create: function(con, col, values, cb) {
fooValues.push(values.foo);
return cb(null, values);
},
update: function(con, col, criteria, values, cb) { return cb(null, values); }
};
var connections = {
'my_foo': {
adapter: 'foobar'
}
};
waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) {
if(err) done(err);
collections = colls.collections;
done();
});
});
/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////
it('should pass model values to create method for each relationship', function(done) {
collections.person.find().exec(function(err, models) {
if(err) return done(err);
var person = models[0];
person.preferences.add({ foo: 'foo' });
person.preferences.add({ foo: 'bar' });
person.save(function(err) {
if(err) return done(err);
assert(fooValues.length === 2);
assert(fooValues[0] === 'foo');
assert(fooValues[1] === 'bar');
done();
});
});
});
});
});
});