Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterUpdate callback not provided the correct schema #971

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions test/unit/callbacks/afterUpdate.update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var Waterline = require('../../../lib/waterline'),
assert = require('assert');
_ = require('lodash');

describe('.afterUpdate()', function() {
var User, afterUpdateValues = null;

before(function (done) {
var waterline = new Waterline();
var Model = Waterline.Collection.extend({
identity: 'user',
connection: 'foo',
attributes: {
name: 'string',
employed: 'boolean',
meta: 'json',
hobbies: 'array'
},

afterUpdate: function (values, cb) {
afterUpdateValues = _.cloneDeep(values);
cb();
}
});

waterline.loadCollection(Model);

// Fixture Adapter Def
var adapterDef = {
update: function (con, col, criteria, values, cb) {
// emulate values coming back from (for example) sails-mysql adapter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is MySQL specific, so this test should be in the waterline-adapter-tests.

Object.keys(values).forEach(function (key) {
var val = values[key];
if (typeof val === 'object') {
// will stringify arrays and objects
values[key] = JSON.stringify(val);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This forces the values to change type, so I think this test will always fail.

}
if (typeof val === 'boolean') {
values[key] = val ? 1: 0;
}
});

cb(null, [values]);
}
};

var connections = {
foo: {
adapter: 'foobar'
}
};

waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function (err, colls) {
if (err) done(err);
User = colls.collections.user;
done();
});
});

it('should be passed deserialized values', function (done) {
var valuesToUpdate = {
employed: true,
hobbies: ['hiking', 'fishing'],
meta: {
lovesChocolate: true
}
};

User.update({name: 'criteria'}, valuesToUpdate, function (err /*, user */) {
assert(!err);
assert(afterUpdateValues !== null);
assert(typeof afterUpdateValues.employed === 'boolean', "'employed' is not of expected 'boolean' type");
assert(Array.isArray(afterUpdateValues.hobbies), "'hobbies' is not of expected 'Array' type");
assert(typeof afterUpdateValues.meta === 'object', "'meta' is not of expected 'object' type");
done();
});
});
});