Skip to content
This repository was archived by the owner on Jan 28, 2019. It is now read-only.

Commit 6bd49fa

Browse files
committed
Merge pull request #74 from medic/uniquewithin-validation-fix
fixed uniqueWithin validation bugs.
2 parents 0556822 + 54b2a8e commit 6bd49fa

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

db.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ module.exports.user = settings.username;
4242
module.exports.fti = function(index, data, cb) {
4343
var path = '/_fti/local' + settings.db
4444
+ '/_design' + settings.db + '/' + index;
45+
logger.debug('fti path: ', path);
46+
logger.debug('fti query: ', data);
4547
client.request({
4648
path: path,
4749
query: data

lib/validation.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ var _exists = function(doc, query, callback) {
3333

3434
var _formatParam = function(name, value) {
3535
name = name.replace(/"/g, '');
36-
value = value.replace(/"/g, '\\"');
37-
return name + ':"' + value + '"';
36+
if (typeof value === 'string') {
37+
value = value.replace(/"/g, '\\"');
38+
return name + ':"' + value + '"';
39+
}
40+
if (typeof value === 'number') {
41+
return name + '<int>:' + value;
42+
}
43+
return name + ':' + value;
3844
};
3945

4046
module.exports = {
@@ -92,12 +98,15 @@ module.exports = {
9298
});
9399
},
94100
uniqueWithin: function(doc, validation, callback) {
95-
var fields = validation.funcArgs;
101+
var fields = _.clone(validation.funcArgs);
96102
var duration = _parseDuration(fields.pop());
97103
var conjunctions = _.map(fields, function(field) {
98104
return _formatParam(field, doc[field]);
99105
});
100-
var start = moment().subtract(duration).toISOString();
106+
// lucene date range query bug
107+
// fails: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
108+
// works: "yyyy-MM-dd'T'HH:mm:ss.SSS"
109+
var start = moment().subtract(duration).toISOString().replace(/Z$/,'');
101110
var endOfTime = '3000-01-01T00:00:00';
102111
conjunctions.push(
103112
'reported_date<date>:[' + start + ' TO ' + endOfTime + ']'

test/unit/reminders.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ exports['runReminder decorates options with moment if found'] = function(test) {
121121
};
122122

123123
exports['does not match reminder if in next minute'] = function(test) {
124-
var window = sinon.stub(reminders, 'getReminderWindow').callsArgWithAsync(1, null, moment().subtract(1, 'hour')),
124+
var past = moment().subtract(1, 'hour'),
125125
now = moment();
126126

127+
var window = sinon.stub(reminders, 'getReminderWindow').callsArgWithAsync(1, null, past);
128+
127129
reminders.matchReminder({
128130
reminder: {
129131
cron: (now.minutes() + 1) + ' ' + now.format('HH * * *') // will generate cron job matching the current hour but 1 minute into future

test/unit/validations.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ exports['pass uniqueWithin validation on old doc'] = function(test) {
229229
xyz: '444'
230230
};
231231
validation.validate(doc, validations, function(errors) {
232-
var start = moment().subtract('weeks', 2).toISOString();
232+
// lucene date range query bug:
233+
// https://github.com/rnewson/couchdb-lucene/issues/225
234+
// fails: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
235+
// works: "yyyy-MM-dd'T'HH:mm:ss.SSS"
236+
var start = moment().subtract('weeks', 2).toISOString().replace('Z','');
233237
test.ok(fti.calledWith('data_records', {
234238
q: 'xyz:"444" AND reported_date<date>:[' + start + ' TO 3000-01-01T00:00:00]',
235239
include_docs: true
@@ -265,6 +269,22 @@ exports['formatParam rejects quotes in field names'] = function(test) {
265269
test.done();
266270
};
267271

272+
exports['formatParam quotes strings'] = function(test) {
273+
test.same(
274+
validation._formatParam('birds', 'pigeon'),
275+
'birds:"pigeon"'
276+
);
277+
test.done();
278+
};
279+
280+
exports['formatParam use <int> query on integers'] = function(test) {
281+
test.same(
282+
validation._formatParam('lmp', 11),
283+
'lmp<int>:11'
284+
);
285+
test.done();
286+
};
287+
268288
exports['pass exists validation when matching document'] = function(test) {
269289
test.expect(2);
270290
// simulate view results with doc attribute

0 commit comments

Comments
 (0)