Permalink
Browse files

Fix find() to be compatible with integer uniques

  • Loading branch information...
1 parent fb43118 commit 9fc6d5dd0e0dd4cd84ea153fcd0d935dd09a4530 @maritz committed Mar 14, 2012
Showing with 34 additions and 3 deletions.
  1. +6 −3 lib/retrieve.js
  2. +28 −0 test/findTests.js
View
@@ -132,10 +132,13 @@ exports.find = function find(searches, callback) {
if (searches.hasOwnProperty(s) && this.properties.hasOwnProperty(s)) {
prop = this.properties[s];
if (prop.unique) {
- if ( ! searches[s].toLowerCase) {
- return returnFunction('Invalid search parameters: Searching for a unique with a non-string value is not supported.');
+ if (prop.type === 'string') {
+ if ( ! searches[s].toLowerCase) {
+ return returnFunction('Invalid search parameters: Searching for a unique (type "string") with a non-string value is not supported.');
+ }
+ searches[s] = searches[s].toLowerCase()
}
- var key = Nohm.prefix.unique+self.modelName+':'+s+':'+searches[s].toLowerCase();
+ var key = Nohm.prefix.unique+self.modelName+':'+s+':'+searches[s];
return this.getClient().get([key], returnFunction);
}
var isNum = ! isNaN(parseInt(searches[s], 10));
View
@@ -59,6 +59,15 @@ var UserFindNoIncrementMockup = nohm.model('UserFindNoIncrementMockup', {
}
});
+nohm.model('UniqueIntegerFind', {
+ properties: {
+ unique: {
+ type: 'integer',
+ unique: true
+ }
+ }
+});
+
var errLogger = function(err) {
if (err) {
console.dir(err);
@@ -276,6 +285,25 @@ loadArray: function (t) {
t.done();
});
},
+
+ findByIntegerUnique: function(t) {
+ var saveObj = nohm.factory('UniqueIntegerFind');
+ var findObj = nohm.factory('UniqueIntegerFind');
+ t.expect(3);
+
+ saveObj.p('unique', 123);
+ saveObj.save(function (err) {
+ t.ok(!err, 'Unexpected saving error');
+
+ findObj.find({
+ unique: saveObj.p('unique')
+ }, function(err, ids) {
+ t.ok(!err, 'Unexpected finding error');
+ t.same(ids, [saveObj.id], 'The found id did not match the id of the saved object.');
+ t.done();
+ });
+ });
+ },
findByStringIndex: function(t) {
var findUser = new UserFindMockup();

0 comments on commit 9fc6d5d

Please sign in to comment.