Skip to content

Commit

Permalink
Reject CREATE with a duplicate id
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Miroslav Bajtoš committed Mar 16, 2015
1 parent 7dc0fcd commit 6f771f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/connectors/memory.js
Expand Up @@ -210,6 +210,11 @@ Memory.prototype.create = function create(model, data, callback) {
if(!this.collection(model)) {
this.collection(model, {});
}

if (this.collection(model)[id]) {
return callback(new Error('Duplicate entry for ' + model + '.' + idName));
}

this.collection(model)[id] = serialize(data);
this.saveToFile(id, callback);
};
Expand Down
19 changes: 19 additions & 0 deletions test/manipulation.test.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 6f771f5

Please sign in to comment.