Can find by date and objects

pull/2/head
Louis Chatriot 12 years ago
parent 973996fb8c
commit 8d0b4b03f6
  1. 11
      lib/model.js
  2. 45
      test/db.test.js
  3. 2
      test/model.test.js

@ -265,15 +265,6 @@ function areThingsEqual (a, b) {
}
/**
* Compare two things. That can be any serialized data: strings, booleans, numbers, dates, null, undefined, objects
* Returns -1 if a is less than b
* 0 if equal
* 1 if greater
*/
/**
* Test for field equality
* @param {Object} obj The model to check
@ -286,7 +277,7 @@ matcherFunctions.$eq = function (obj, field, value) {
if (!obj) { return false; } // field cannot be empty here so that means there is no match
if (fieldParts.length === 1) {
return obj[fieldParts[0]] === value; // TODO: check for real equality, incl. deep object equality
return areThingsEqual(obj[fieldParts[0]], value);
} else {
return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1), value);
}

@ -233,15 +233,33 @@ describe('Database', function () {
], done);
});
it('Can find dates and objects', function (done) {
var now = new Date();
it('Can find dates and objects (non JS-native types)', function (done) {
var date1 = new Date(1234543)
, date2 = new Date(9999)
;
d.insert({ now: now, sth: { name: 'nedb' } }, function () {
d.findOne({ now: now }, function (err, doc) {
//console.log(doc);
// TODO
d.insert({ now: date1, sth: { name: 'nedb' } }, function () {
d.findOne({ now: date1 }, function (err, doc) {
assert.isNull(err);
doc.sth.name.should.equal('nedb');
done();
d.findOne({ now: date2 }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
d.findOne({ sth: { name: 'nedb' } }, function (err, doc) {
assert.isNull(err);
doc.sth.name.should.equal('nedb');
d.findOne({ sth: { name: 'other' } }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
done();
});
});
});
});
});
});
@ -249,9 +267,20 @@ describe('Database', function () {
it('Can use dot-notation to query subfields', function (done) {
d.insert({ greeting: { english: 'hello' } }, function () {
d.findOne({ "greeting.english": 'hello' }, function (err, doc) {
assert.isNull(err);
doc.greeting.english.should.equal('hello');
done();
d.findOne({ "greeting.english": 'hellooo' }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
d.findOne({ "greeting.englis": 'hello' }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
done();
});
});
});
});
});

@ -330,7 +330,7 @@ describe('Model', function () {
describe('Finding documents', function () {
describe.only('Comparing things', function () {
describe('Comparing things', function () {
it('Two things of different types cannot be equal, two identical native things are equal', function () {
var toTest = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }]

Loading…
Cancel
Save