Query testing: findOne

This commit is contained in:
Anatoliy Chakkaev 2013-03-27 00:50:13 +04:00
parent 0d5f4a9aad
commit cba87ce339
2 changed files with 66 additions and 12 deletions

View File

@ -70,11 +70,6 @@ Memory.prototype.all = function all(model, filter, callback) {
if (filter) { if (filter) {
// do we need some filtration?
if (filter.where) {
nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
}
// do we need some sorting? // do we need some sorting?
if (filter.order) { if (filter.order) {
var props = this._models[model].properties; var props = this._models[model].properties;
@ -93,6 +88,12 @@ Memory.prototype.all = function all(model, filter, callback) {
}); });
nodes = nodes.sort(sorting.bind(orders)); nodes = nodes.sort(sorting.bind(orders));
} }
// do we need some filtration?
if (filter.where) {
nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
}
} }
process.nextTick(function () { process.nextTick(function () {
@ -108,7 +109,7 @@ Memory.prototype.all = function all(model, filter, callback) {
} }
} }
return 0; return 0;
} }
}; };
function applyFilter(filter) { function applyFilter(filter) {

View File

@ -134,16 +134,68 @@ describe('basic-querying', function() {
}); });
}); });
describe('findOne', function() {
before(seed);
it('should find first record (default sort by id)', function(done) {
User.all({sort: 'id'}, function(err, users) {
User.findOne(function(e, u) {
should.not.exist(e);
should.exist(u);
u.id.should.equal(users[0].id);
done();
});
});
});
it('should find first record', function(done) {
User.findOne({order: 'order'}, function(e, u) {
should.not.exist(e);
should.exist(u);
u.order.should.equal(1);
u.name.should.equal('Paul McCartney');
done();
});
});
it('should find last record', function(done) {
User.findOne({order: 'order DESC'}, function(e, u) {
should.not.exist(e);
should.exist(u);
u.order.should.equal(6);
u.name.should.equal('Ringo Starr');
done();
});
});
it('should find last record in filtered set', function(done) {
User.findOne({
where: {role: 'lead'},
order: 'order DESC'
}, function(e, u) {
should.not.exist(e);
should.exist(u);
u.order.should.equal(2);
u.name.should.equal('John Lennon');
done();
});
});
});
describe('exists', function() { describe('exists', function() {
before(seed); before(seed);
it('should check whether record exist', function(done) { it('should check whether record exist', function(done) {
User.exists(1, function(err, exists) { User.findOne(function(e, u) {
should.not.exist(err); User.exists(u.id, function(err, exists) {
should.exist(exists); should.not.exist(err);
exists.should.be.true; should.exist(exists);
done(); exists.should.be.true;
done();
});
}); });
}); });
@ -159,12 +211,13 @@ describe('basic-querying', function() {
}); });
}); });
function seed(done) { function seed(done) {
var count = 0; var count = 0;
var beatles = [ var beatles = [
{ id: 1, {
name: 'John Lennon', name: 'John Lennon',
mail: 'john@b3atl3s.co.uk', mail: 'john@b3atl3s.co.uk',
role: 'lead', role: 'lead',