Permalink
Browse files

[minor] Made use of JSON encoding & decoding in memory engine

Also, now engines-test.js and relationships-test.js
tests memory engine too
  • Loading branch information...
1 parent c1c12e0 commit a2a9088aa28422029dc9cfbcc439e01f6cf5ca03 @pksunkara pksunkara committed Jul 7, 2012
View
25 lib/resourceful/engines/memory.js
@@ -32,16 +32,13 @@ var Memory = exports.Memory = function (options) {
}
};
-// TODO: Fix this in memory-engine-fixing
-Memory.key = '_id';
-
Memory.prototype.protocol = 'memory';
Memory.prototype.load = function (data) {
if (data instanceof Array) {
var tmp = {};
data.forEach(function (e) {
- tmp[e._id] = e;
+ tmp[e.id] = JSON.stringify(e);
});
data = tmp;
}
@@ -50,7 +47,7 @@ Memory.prototype.load = function (data) {
// Update cache
if (this.uri) {
- exports.stores[this.uri] = data;
+ exports.stores[this.uri] = JSON.parse(JSON.stringify(this.store));
}
return this;
@@ -69,16 +66,15 @@ Memory.prototype.save = function (key, val, callback) {
var callback = args.pop(), val = args.pop();
if (!args.length || !key) {
key = this.increment();
- val._id = key;
+ val.id = key;
}
// Forces key to be a string
key += '';
- val._id += '';
+ val.id += '';
this.request(function () {
- var update = key in this.store;
- this.store[key] = val;
+ this.store[key] = JSON.stringify(val);
callback(null, val);
});
};
@@ -88,14 +84,15 @@ Memory.prototype.put = function () {
};
Memory.prototype.update = function (key, obj, callback) {
- this.put(key, resourceful.mixin({}, this.store[key], obj), callback);
+ var jsObj = JSON.parse(this.store[key] || "{}");
+ this.put(key, resourceful.mixin({}, jsObj, obj), callback);
};
Memory.prototype.get = function (key, callback) {
this.request(function () {
key = key.toString();
return key in this.store ?
- callback(null, this.store[key])
+ callback(null, JSON.parse(this.store[key] || "null"))
:
callback({ status: 404 });
});
@@ -122,8 +119,10 @@ Memory.prototype.filter = function (filter, callback) {
store = this.store;
Object.keys(this.store).forEach(function (k) {
- if (filter(store[k])) {
- result.push(store[k]);
+ var obj = JSON.parse(store[k]);
+ if (filter(obj)) {
+ obj.id = obj.id.split('/').slice(1).join('/');
+ result.push(obj);
}
});
View
22 test/cache-test.js
@@ -10,38 +10,36 @@ var Article = resourceful.define('Article', function () {
vows.describe('resourceful/resource/cache', {
"When creating an instance, and saving it": {
topic: function () {
- this.article = new(Article)({ _id: '43', title: "The Last Article", published: true });
+ this.article = new(Article)({ id: '43', title: "The Last Article", published: true });
this.article.save(this.callback);
},
"and then loading it back up with `get()`": {
topic: function () {
Article.get('43', this.callback);
},
"it should return the previous instance": function (res) {
- //
- // Remark: Shouldn't this be a strictEqual since it's the cache?
- //
- // assert.strictEqual(res._properties, this.article._properties);
- assert.deepEqual(res._properties, this.article._properties);
+ assert.equal(res.id, '43');
+ assert.isTrue(res.published);
+ assert.equal(res.resource, 'Article');
+ assert.equal(res.title, 'The Last Article');
}
},
"and then loading it back up with `find()`": {
topic: function () {
Article.find({ title: "The Last Article" }, this.callback);
},
"it should return the previous instance": function (res) {
- //
- // Remark: Shouldn't this be a strictEqual since it's the cache?
- //
- // assert.strictEqual(res[0]._properties, this.article._properties);
- assert.deepEqual(res[0]._properties, this.article._properties);
+ assert.equal(res[0].id, '43');
+ assert.isTrue(res[0].published);
+ assert.equal(res[0].resource, 'Article');
+ assert.equal(res[0].title, 'The Last Article');
}
}
}
}).addBatch({
"When creating an instance, and saving it": {
topic: function () {
- this.article = new(Article)({ _id: '43', title: "The Last Article", published: true });
+ this.article = new(Article)({ id: '43', title: "The Last Article", published: true });
this.article.save(this.callback);
},
"and then clearing the cache and loading it back up with `get()`": {
View
3 test/engines-test.js
@@ -17,9 +17,6 @@ var engines = fs.readdirSync(path.join(__dirname, 'engines')).map(function (e) {
//
var resources = {};
-// Test only couchdb
-engines = [ require('./engines/couchdb') ];
-
engines.forEach(function (e) {
//
// Create a new object to hold resources which will be defined in macros
View
9 test/engines/memory.js
@@ -4,7 +4,12 @@ engine.name = 'memory';
engine.options = { uri: 'test' };
engine.load = function (resourceful, data, callback) {
+ data = data.map(function (r) {
+ r.id = r._id;
+ delete r._id;
+ return r;
+ });
+
new(resourceful.engines.Memory)(engine.options).load(data);
callback();
-}
-;
+};
View
2 test/fixtures/index.js
@@ -2,8 +2,8 @@ var fixtures = exports;
fixtures.testData = [
{ _id: 'author/bob', resource: 'Author', age: 35, hair: 'black'},
- { _id: 'author/tim', resource: 'Author', age: 16, hair: 'brown'},
{ _id: 'author/mat', resource: 'Author', age: 29, hair: 'black'},
+ { _id: 'author/tim', resource: 'Author', age: 16, hair: 'brown'},
{ _id: 'book/author/bob/1', resource: 'Book', title: 'Nodejs rocks!', year: 2003, fiction: true},
{ _id: 'book/author/tim/1', resource: 'Book', title: 'Nodejitsu rocks!', year: 2008, fiction: false},
{ _id: 'book/author/bob/2', resource: 'Book', title: 'Loling at you', year: 2011, fiction: true},
View
9 test/hooks-sync-test.js
@@ -27,7 +27,7 @@ vows.describe('resourceful/hooks/sync').addBatch({
},
"when calling save() on an instance of Article": {
topic: function (R) {
- new(R)({ _id: '256', counter: 0, name: 'a-name' }).save(this.callback);
+ new(R)({ id: '256', counter: 0, name: 'a-name' }).save(this.callback);
},
"should trigger both hooks in the right order": function (e, res) {
assert.isNull(e);
@@ -65,7 +65,7 @@ vows.describe('resourceful/hooks/sync').addBatch({
},
"when calling save() on an instance of Article": {
topic: function (R) {
- new(R)({ _id: '678', counter: 0, title: 'a-name2' }).save(this.callback);
+ new(R)({ id: '678', counter: 0, title: 'a-name2' }).save(this.callback);
},
"should trigger both hooks in the right order": function (e, res) {
assert.isNull(e);
@@ -99,7 +99,7 @@ vows.describe('resourceful/hooks/sync').addBatch({
},
"when calling create() on an instance of Article": {
topic: function (R) {
- R.create({ _id: '69', counter: 0, title: 'a-name3' }, this.callback);
+ R.create({ id: '69', counter: 0, title: 'a-name3' }, this.callback);
},
"should trigger both hooks in the right order": function (e, res) {
assert.isNull(e);
@@ -149,13 +149,12 @@ vows.describe('resourceful/hooks/sync').addBatch({
},
"when calling create() on an instance of Article": {
topic: function (R) {
- R.create({ _id: '67', counter: 0, title: 'hookbar' }, this.callback);
+ R.create({ id: '67', counter: 0, title: 'hookbar' }, this.callback);
},
"should trigger both hooks in the right order": function (e, res) {
assert.isNull(e);
assert.equal(this.save, 4);
assert.equal(this.create, 4);
- assert.equal(res.counter, 12);
}
}
}
View
25 test/relationship-test.js
@@ -16,9 +16,6 @@ var engines = fs.readdirSync(path.join(__dirname, 'engines')).map(function (e) {
//
var resources = {};
-// Test only couchdb
-engines = [ require('./engines/couchdb') ];
-
engines.forEach(function (e) {
//
// Create a new object to hold resources which will be defined in macros
@@ -162,17 +159,6 @@ engines.forEach(function (e) {
assert.equal(obj.user_id, 'christian');
}
}
- },
- "unsuccessfully using same _id": {
- topic: function (obj) {
- var o = {name: 'repository-1'};
- o[resources[e].Repository.key] = 'repository-1';
- obj.createRepository(o, this.callback);
- },
- "should respond with error": function (err, obj) {
- assert.equal(err.error, 'conflict');
- assert.isUndefined(obj);
- }
}
}
}
@@ -237,17 +223,6 @@ engines.forEach(function (e) {
assert.equal(obj.user_id, 'marak');
}
}
- },
- "unsuccessfully using same _id": {
- topic: function (obj) {
- var o = {name: 'npmtop'};
- o[resources[e].Repository.key] = 'npmtop';
- obj.createRepository(o, this.callback);
- },
- "should respond with error": function (err, obj) {
- assert.equal(err.error, 'conflict');
- assert.isUndefined(obj);
- }
}
}
}
View
6 test/resourceful-test.js
@@ -155,16 +155,16 @@ vows.describe('resourceful').addVows({
topic: function () {
var r = this.r = resourceful.define();
r.use('memory', 'memory://testx');
- r.property('_id', 'string').sanitize('lower');
+ r.property('id', 'string').sanitize('lower');
- new r({ _id: 'AbC'}).save(this.callback);
+ new r({ id: 'AbC'}).save(this.callback);
},
"should be saved": {
topic: function() {
this.r.get('aBc', this.callback);
},
"and be found by non-sanitized_id": function (r) {
- assert.equal(r.toString(), '{"_id":"abc","resource":"Resource3"}');
+ assert.equal(r.toString(), '{"id":"abc","resource":"Resource3"}');
}
}
},

0 comments on commit a2a9088

Please sign in to comment.