Skip to content

Commit

Permalink
Fixes #48 - error if the hash in the db has fields that aren't defined
Browse files Browse the repository at this point in the history
  • Loading branch information
maritz committed Mar 14, 2012
1 parent 9fc6d5d commit e9d6f78
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/retrieve.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports.load = function (id, callback) {
} }
if (Array.isArray(keys) && keys.length > 0) { if (Array.isArray(keys) && keys.length > 0) {
for (p in values) { for (p in values) {
if (values.hasOwnProperty(p)) { if (values.hasOwnProperty(p) && self.properties.hasOwnProperty(p)) {
value = values[p] !== null ? values[p].toString() : null; value = values[p] !== null ? values[p].toString() : null;
if (self.properties[p].load_pure) { if (self.properties[p].load_pure) {
self.properties[p].value = value; self.properties[p].value = value;
Expand All @@ -51,6 +51,8 @@ exports.load = function (id, callback) {
} }
return_props[p] = self.p(p); return_props[p] = self.p(p);
self.__resetProp(p); self.__resetProp(p);
} else if ( ! self.properties.hasOwnProperty(p)) {
Nohm.logError('WARNING: A hash in the DB contained a key that is not in the model definition. This might be because of model changes or database corruption/intrusion.')
} }
} }
self.id = id; self.id = id;
Expand Down
34 changes: 34 additions & 0 deletions test/findTests.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -952,5 +952,39 @@ loadArray: function (t) {
t.done(); t.done();
}); });
} }
},

"load hash with extra properties": function(t) {
var user = new UserFindMockup(),
findUser = new UserFindMockup();
t.expect(7);

user.p({
name: 'hurgelwurz',
email: 'hurgelwurz@hurgel.de',
json: {
test: 1
}
});

user.save(function(err) {
if (err) {
console.dir(err);
t.done();
}
redis.hset(nohm.prefix.hash+findUser.modelName+':'+user.id, 'not_a_real_property', 'something... :-)', function (err) {
t.ok(!err, 'Unexpected redis error in custom query');
console.log('There should be an error in the next line');
findUser.load(user.id, function(err) {
t.ok(!err, 'Unexpected load error');
t.equals(user.p('name'), findUser.p('name'), 'The loaded version of the name was not the same as a set one.');
t.equals(user.p('email'), findUser.p('email'), 'The loaded version of the email was not the same as a set one.');
t.equals(findUser.p('json').test, 1, 'The loaded version of the json was not the same as the set one.');
t.equals(user.id, findUser.id, 'The loaded version of the email was not the same as a set one.');
t.equals(user.p('bool'), false, 'The loaded version of the boolean was not the same as a set one.');
t.done();
});
});
});
} }
}; };

0 comments on commit e9d6f78

Please sign in to comment.