Fix memory adapter and test

This commit is contained in:
Anatoliy Chakkaev 2013-03-27 17:10:13 +04:00
parent b6fb04ffe1
commit e75029ebbf
2 changed files with 29 additions and 11 deletions

View File

@ -19,7 +19,7 @@ Memory.prototype.define = function defineModel(descr) {
Memory.prototype.create = function create(model, data, callback) {
var id = data.id || this.ids[model]++;
data.id = id;
this.cache[model][id] = data;
this.cache[model][id] = JSON.stringify(data);
process.nextTick(function () {
callback(null, id);
});
@ -40,7 +40,7 @@ Memory.prototype.updateOrCreate = function (model, data, callback) {
};
Memory.prototype.save = function save(model, data, callback) {
this.cache[model][data.id] = data;
this.cache[model][data.id] = JSON.stringify(data);
process.nextTick(function () {
callback(null, data);
});
@ -54,7 +54,7 @@ Memory.prototype.exists = function exists(model, id, callback) {
Memory.prototype.find = function find(model, id, callback) {
process.nextTick(function () {
callback(null, this.cache[model][id]);
callback(null, id in this.cache[model] && JSON.parse(this.cache[model][id]));
}.bind(this));
};
@ -64,8 +64,9 @@ Memory.prototype.destroy = function destroy(model, id, callback) {
};
Memory.prototype.all = function all(model, filter, callback) {
var self = this;
var nodes = Object.keys(this.cache[model]).map(function (key) {
return this.cache[model][key];
return JSON.parse(this.cache[model][key]);
}.bind(this));
if (filter) {
@ -97,7 +98,11 @@ Memory.prototype.all = function all(model, filter, callback) {
}
process.nextTick(function () {
callback(null, nodes);
if (filter && filter.include) {
self._models[model].model.include(nodes, filter.include, callback);
} else {
callback(null, nodes);
}
});
function sorting(a, b) {
@ -133,6 +138,15 @@ function applyFilter(filter) {
}
if (typeof example === 'undefined') return undefined;
if (typeof value === 'undefined') return undefined;
if (typeof example === 'object') {
if (example.inq) {
if (!value) return false;
for (var i = 0; i < example.inq.length; i += 1) {
if (example.inq[i] == value) return true;
}
return false;
}
}
// not strict equality
return (example !== null ? example.toString() : example) == (value !== null ? value.toString() : value);
}
@ -153,7 +167,7 @@ Memory.prototype.count = function count(model, callback, where) {
data = data.filter(function (id) {
var ok = true;
Object.keys(where).forEach(function (key) {
if (cache[id][key] != where[key]) {
if (JSON.parse(cache[id])[key] != where[key]) {
ok = false;
}
});

View File

@ -10,10 +10,12 @@ describe('include', function() {
passports.length.should.be.ok;
passports.forEach(function(p) {
p.__cachedRelations.should.have.property('owner');
if (p.ownerId === null) {
should.not.exist(p.__cachedRelations.owner);
var owner = p.__cachedRelations.owner;
if (!p.ownerId) {
should.not.exist(owner);
} else {
p.__cachedRelations.owner.id.should.equal(p.ownerId);
should.exist(owner);
owner.id.should.equal(p.ownerId);
}
});
done();
@ -43,9 +45,10 @@ describe('include', function() {
passports.forEach(function(p) {
p.__cachedRelations.should.have.property('owner');
var user = p.__cachedRelations.owner;
if (p.ownerId === null) {
if (!p.ownerId) {
should.not.exist(user);
} else {
should.exist(user);
user.id.should.equal(p.ownerId);
user.__cachedRelations.should.have.property('posts');
user.__cachedRelations.posts.forEach(function(pp) {
@ -67,9 +70,10 @@ describe('include', function() {
passports.forEach(function(p) {
p.__cachedRelations.should.have.property('owner');
var user = p.__cachedRelations.owner;
if (p.ownerId === null) {
if (!p.ownerId) {
should.not.exist(user);
} else {
should.exist(user);
user.id.should.equal(p.ownerId);
user.__cachedRelations.should.have.property('posts');
user.__cachedRelations.posts.forEach(function(pp) {