Reject CREATE with a duplicate id
Modify the memory connector to reject requests to create a record with a duplicate id. Add a unit-test to verify this behaviour across all connectors.
This commit is contained in:
parent
7dc0fcd26f
commit
902772f1af
|
@ -210,6 +210,13 @@ Memory.prototype.create = function create(model, data, callback) {
|
|||
if(!this.collection(model)) {
|
||||
this.collection(model, {});
|
||||
}
|
||||
|
||||
if (this.collection(model)[id]) {
|
||||
return process.nextTick(function() {
|
||||
callback(new Error('Duplicate entry for ' + model + '.' + idName));
|
||||
});
|
||||
}
|
||||
|
||||
this.collection(model)[id] = serialize(data);
|
||||
this.saveToFile(id, callback);
|
||||
};
|
||||
|
|
|
@ -291,6 +291,25 @@ describe('manipulation', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should refuse to create object with duplicate id', function(done) {
|
||||
// NOTE(bajtos) We cannot reuse Person model here,
|
||||
// `settings.forceId` aborts the CREATE request at the validation step.
|
||||
var Product = db.define('Product', { name: String });
|
||||
db.automigrate(function(err) {
|
||||
if (err) return done(err);
|
||||
|
||||
Product.create({ name: 'a-name' }, function(err, p) {
|
||||
if (err) return done(err);
|
||||
Product.create({ id: p.id, name: 'duplicate' }, function(err) {
|
||||
if (!err) {
|
||||
return done(new Error('Create should have rejected duplicate id.'));
|
||||
}
|
||||
err.message.should.match(/duplicate/i);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('save', function () {
|
||||
|
|
Loading…
Reference in New Issue