Skip to content
Browse files

Fixes #48 - error if the hash in the db has fields that aren't defined

  • Loading branch information...
1 parent 9fc6d5d commit e9d6f7863a623510b43549dbcea37475883eb712 @maritz committed Mar 14, 2012
Showing with 37 additions and 1 deletion.
  1. +3 −1 lib/retrieve.js
  2. +34 −0 test/findTests.js
View
4 lib/retrieve.js
@@ -42,7 +42,7 @@ exports.load = function (id, callback) {
}
if (Array.isArray(keys) && keys.length > 0) {
for (p in values) {
- if (values.hasOwnProperty(p)) {
+ if (values.hasOwnProperty(p) && self.properties.hasOwnProperty(p)) {
value = values[p] !== null ? values[p].toString() : null;
if (self.properties[p].load_pure) {
self.properties[p].value = value;
@@ -51,6 +51,8 @@ exports.load = function (id, callback) {
}
return_props[p] = self.p(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;
View
34 test/findTests.js
@@ -952,5 +952,39 @@ loadArray: function (t) {
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.
Something went wrong with that request. Please try again.