Permalink
Browse files

[test] Tested abstract auto id gen

  • Loading branch information...
pksunkara committed Dec 21, 2011
1 parent 1ca70f6 commit 741fc08bf882bc711318ec8fce8ac30e1dfef4ed
View
@@ -71,7 +71,7 @@ Now lets instantiate a Creature, and feed it:
``` js
var wolf = new(Creature)({
- diet: 'carnivor',
+ diet: 'carnivore',
vertebrate: true
});
@@ -11,7 +11,7 @@ var Creature = resourceful.define('creature', function () {
});
var wolf = new(Creature)({
- diet: 'carnivor',
+ diet: 'carnivore',
vertebrate: true
});
@@ -11,7 +11,7 @@ Creature.prototype.feed = function (food) {
};
var wolf = new(Creature)({
- diet: 'carnivor',
+ diet: 'carnivore',
vertebrate: true
});
@@ -65,16 +65,16 @@ Couchdb.prototype.put = function (id, doc, callback) {
if (e) return callback(e);
res.status = 201;
- callback(null, resourceful.mixin(doc, res));
+ callback(null, resourceful.mixin({}, doc, res));
});
};
Couchdb.prototype.post = Couchdb.prototype.create = function (doc, callback) {
return this.request('post', doc, function (e, res) {
if (e) return callback(e);
-
+
res.status = 201;
- callback(null, res);
+ callback(null, resourceful.mixin({}, doc, res));
});
};
@@ -10,9 +10,8 @@ var Memory = exports.Memory = function (options) {
options = options || {};
this.uri = options.uri;
- this.getNextID = function getNextID () {
- counter ++;
- return counter;
+ this.increment = function () {
+ return ++counter;
}
if (typeof(this.uri) === 'string') {
@@ -66,7 +65,8 @@ Memory.prototype.save = function (key, val, callback) {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop(), val = args.pop();
if (!args.length || !key) {
- key = this.getNextID();
+ key = this.increment();
+ val._id = key;
}
this.request(function () {
@@ -141,7 +141,12 @@ Resource._request = function (/* method, [key, obj], callback */) {
}
obj._rev = result.rev;
}
- result = resourceful.instantiate.call(that, method === 'get' || method === 'update' ? result : obj);
+
+ if (method === 'save') {
+ obj._id = result._id || result.id;
+ }
+
+ result = resourceful.instantiate.call(that, ['get', 'update'].indexOf(method) !== -1 ? result : obj);
if (method === 'update') {
// TODO: Not necessary for identity map
View
@@ -286,5 +286,18 @@ engines.forEach(function (e) {
}
}
}
+ }).addBatch({
+ "Creating object without 'id'": {
+ topic: function () {
+ resources[e].Author.create({ age: 51, hair: 'white' }, this.callback);
+ },
+ "should be successful": function (err, obj) {
+ assert.isNull(err);
+ assert.notEqual(obj._id, undefined);
+ assert.equal(obj.age, 51);
+ assert.equal(obj.hair, 'white');
+ assert.equal(obj.resource, 'Author');
+ }
+ }
}).export(module)
});

0 comments on commit 741fc08

Please sign in to comment.