Fix id type issue for update

https://github.com/strongloop/loopback/issues/1058
This commit is contained in:
Raymond Feng 2015-02-02 08:44:36 -08:00
parent 842e543bf7
commit 4afb2385a9
2 changed files with 140 additions and 97 deletions

View File

@ -573,6 +573,9 @@ Memory.prototype.update =
async.each(ids, function (id, done) {
var inst = self.fromDb(model, cache[id]);
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);
} else {
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({
connector: 'memory',
file: file
});
var User = ds.createModel('User', {
id: {
type: Number,
id: true,
generated: true
},
name: String,
bio: String,
approved: Boolean,
joinedAt: Date,
age: Number
});
return User;
}
var count = 0;
var User;
var ids = [];
before(function() {
User = createUserModel();
});
it('should persist create', function(done) {
var count = 0;
async.eachSeries(['John1', 'John2', 'John3'], function(item, cb) {
User.create({name: item}, function(err, result) {
ids.push(result.id);
@ -52,40 +67,64 @@ describe('Memory connector', function () {
cb(err);
});
});
}, function (err, results) {
}, done);
});
it('should persist delete', function(done) {
// Now try to delete one
User.deleteById(ids[0], function(err) {
if (err) {
return done(err);
}
readModels(function(err, json) {
if (err) {
return done(err);
}
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();
});
});
});
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
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) {
// There should be 2 records
assert.equal(users.length, 2);
@ -93,6 +132,7 @@ describe('Memory connector', function () {
});
});
});
describe('Query for memory connector', function() {
var ds = new DataSource({