Skip to content

Commit

Permalink
Fixed indexes and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maritz committed Jul 20, 2010
1 parent 4ad1438 commit 978b798
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/nohm.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var ModelClass = {
p, doit,
uniqueLocker = function uniqueLocker(propName, callback) {
if (!self.id) {
sys.debug('this sucks...' + sys.inspect(self));
self.logError('Checking for uniques without having an id set. self:' + sys.inspect(self));
}
redis.setnx(prefix + ':uniques:' + self.modelName + ':' + propName + ':' + self.p(propName), self.id, function (err, value) {
/*
Expand Down Expand Up @@ -199,11 +199,11 @@ var ModelClass = {
}
if (props[p].index === true && props[p].__updated) {
if (self.__inDB) {
redis.srem(prefix + ':index:' + self.modelName + ':' + p + ':' + props[p].__oldValue, self.logError);
redis.srem(prefix + ':index:' + self.modelName + ':' + p + ':' + props[p].__oldValue, self.id, self.logError);
}
redis.sadd(prefix + ':index:' + self.modelName + ':' + p + ':' + props[p].value, self.id, self.logError);
}
self.__inDB = true;
self.__inDB = true;
self.property(p, props[p].value || 0); // this ensures typecasing/behaviours
props[p].__updated = false;
props[p].__oldValue = props[p].value;
Expand Down
25 changes: 24 additions & 1 deletion test/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ var UserMockup = nohm.Model.extend({
validations: [
'email'
]
},
country: {
type: 'string',
value: 'Tibet',
index: true
}
};
nohm.Model.call(this);
Expand Down Expand Up @@ -172,7 +177,8 @@ exports.allProperties = function (t) {
should = {
name: user.p('name'),
visits: user.p('visits'),
email: user.p('email')
email: user.p('email'),
country: user.p('country')
}; // yes, this absolutely must be set correct for this test to work. sorry

t.same(should, user.allProperties(), 'Getting all properties failed.');
Expand Down Expand Up @@ -287,6 +293,23 @@ exports.unique = function (t) {
});
};

exports.indexes = function (t) {
var user = new UserMockup();
t.expect(3);

user.p('name', 'indexTest');
user.p('email', 'indexTest@test.de');
user.p('country', 'indexTestCountry');
user.save(function (err) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
redis.sismember('nohm:index:UserMockup:country:indexTestCountry', user.id, function (err, value) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
t.ok(value === 1, 'The index did not have the user as one of its ids.');
t.done();
});
});
};

exports.__updated = function (t) {
var user = new UserMockup();
t.expect(2);
Expand Down
5 changes: 3 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require.paths.unshift('../lib/conductor/lib'); // class system

var nodeunit = require('nodeunit')
, sys = require('sys');

// testrunner copied from nodeunit and edited a little
run = function(files){

Expand Down Expand Up @@ -47,6 +47,7 @@ run = function(files){
};
redis.keys('nohm:hashes:UserMockup:*', deleteKeys);
redis.keys('nohm:uniques:UserMockup:*', deleteKeys);
redis.keys('nohm:index:UserMockup:*', deleteKeys);
setTimeout(function () {
// timeout here because else the deletes don't go through fast enough and executing the tests again will result in failure.
if(assertions.failures){
Expand All @@ -71,4 +72,4 @@ run = function(files){
};

process.chdir(__dirname);
run(['features.js', 'validations.js']);
run(['features.js', 'validations.js']);

0 comments on commit 978b798

Please sign in to comment.