Merge pull request #416 from strongloop/feature/fix-lb-1058

Fix id type issue for update
This commit is contained in:
Raymond Feng 2015-02-02 08:58:10 -08:00
commit fd97b06bcc
2 changed files with 140 additions and 97 deletions

View File

@ -573,6 +573,9 @@ Memory.prototype.update =
async.each(ids, function (id, done) { async.each(ids, function (id, done) {
var inst = self.fromDb(model, cache[id]); var inst = self.fromDb(model, cache[id]);
if (!filter || filter(inst)) { if (!filter || filter(inst)) {
// The id value from the cache is string
// Get the real id from the inst
id = self.getIdValue(model, inst);
self.updateAttributes(model, id, data, done); self.updateAttributes(model, id, data, done);
} else { } else {
process.nextTick(done); process.nextTick(done);

View File

@ -27,22 +27,37 @@ describe('Memory connector', function () {
}); });
}); });
it('should save to a json file', function (done) { describe('with file', function() {
function createUserModel() {
var ds = new DataSource({ var ds = new DataSource({
connector: 'memory', connector: 'memory',
file: file file: file
}); });
var User = ds.createModel('User', { var User = ds.createModel('User', {
id: {
type: Number,
id: true,
generated: true
},
name: String, name: String,
bio: String, bio: String,
approved: Boolean, approved: Boolean,
joinedAt: Date, joinedAt: Date,
age: Number age: Number
}); });
return User;
}
var count = 0; var User;
var ids = []; var ids = [];
before(function() {
User = createUserModel();
});
it('should persist create', function(done) {
var count = 0;
async.eachSeries(['John1', 'John2', 'John3'], function(item, cb) { async.eachSeries(['John1', 'John2', 'John3'], function(item, cb) {
User.create({name: item}, function(err, result) { User.create({name: item}, function(err, result) {
ids.push(result.id); ids.push(result.id);
@ -52,40 +67,64 @@ describe('Memory connector', function () {
cb(err); cb(err);
}); });
}); });
}, function (err, results) { }, done);
});
it('should persist delete', function(done) {
// Now try to delete one // Now try to delete one
User.deleteById(ids[0], function(err) { User.deleteById(ids[0], function(err) {
if (err) {
return done(err);
}
readModels(function(err, json) { readModels(function(err, json) {
if (err) {
return done(err);
}
assert.equal(Object.keys(json.models.User).length, 2); assert.equal(Object.keys(json.models.User).length, 2);
User.upsert({id: ids[1], name: 'John'}, function(err, result) {
readModels(function (err, json) {
assert.equal(Object.keys(json.models.User).length, 2);
var user = JSON.parse(json.models.User[ids[1]]);
assert.equal(user.name, 'John');
done(); done();
}); });
}); });
}); });
it('should persist upsert', function(done) {
User.upsert({id: ids[1], name: 'John'}, function(err, result) {
if (err) {
return done(err);
}
readModels(function(err, json) {
if (err) {
return done(err);
}
assert.equal(Object.keys(json.models.User).length, 2);
var user = JSON.parse(json.models.User[ids[1]]);
assert.equal(user.name, 'John');
assert(user.id === ids[1]);
done();
});
}); });
}); });
it('should persist update', function(done) {
User.update({id: ids[1]}, {name: 'John1'},
function(err, result) {
if (err) {
return done(err);
}
readModels(function(err, json) {
if (err) {
return done(err);
}
assert.equal(Object.keys(json.models.User).length, 2);
var user = JSON.parse(json.models.User[ids[1]]);
assert.equal(user.name, 'John1');
assert(user.id === ids[1]);
done();
});
});
}); });
// The saved memory.json from previous test should be loaded // The saved memory.json from previous test should be loaded
it('should load from the json file', function(done) { it('should load from the json file', function(done) {
var ds = new DataSource({
connector: 'memory',
file: file
});
var User = ds.createModel('User', {
name: String,
bio: String,
approved: Boolean,
joinedAt: Date,
age: Number
});
User.find(function(err, users) { User.find(function(err, users) {
// There should be 2 records // There should be 2 records
assert.equal(users.length, 2); assert.equal(users.length, 2);
@ -93,6 +132,7 @@ describe('Memory connector', function () {
}); });
}); });
});
describe('Query for memory connector', function() { describe('Query for memory connector', function() {
var ds = new DataSource({ var ds = new DataSource({