Skip to content

Commit 5dfbdca

Browse files
committed
refactor(query-parser): move stringify tests to dedicated file
1 parent f160ded commit 5dfbdca

File tree

2 files changed

+288
-296
lines changed

2 files changed

+288
-296
lines changed

packages/query-parser/src/index.spec.ts

Lines changed: 0 additions & 296 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import {
1515
parseFilter,
1616
parseProject,
1717
parseSort,
18-
stringify,
19-
toJSString,
2018
DEFAULT_LIMIT,
2119
DEFAULT_MAX_TIME_MS,
2220
DEFAULT_SKIP,
@@ -328,300 +326,6 @@ describe('mongodb-query-parser', function () {
328326
});
329327
});
330328

331-
describe('toJSString', function () {
332-
it('should default to two spaces', function () {
333-
assert.equal(
334-
toJSString({ a: { $exists: true } }),
335-
`{
336-
a: {
337-
$exists: true
338-
}
339-
}`
340-
);
341-
});
342-
343-
it('should allow falsy indentation', function () {
344-
assert.equal(
345-
toJSString({ a: { $exists: true } }, 0),
346-
'{a:{$exists:true}}'
347-
);
348-
});
349-
350-
it('allows passing custom indent', function () {
351-
assert.equal(
352-
toJSString({ a: { $exists: true } }, 'pineapple'),
353-
`{
354-
pineapplea: {
355-
pineapplepineapple$exists: true
356-
pineapple}
357-
}`
358-
);
359-
});
360-
361-
it('retains double spaces and new lines in strings', function () {
362-
assert.equal(
363-
toJSString(
364-
{
365-
a: {
366-
name: `multi-line with s p a c
367-
368-
e s`,
369-
},
370-
},
371-
0
372-
),
373-
"{a:{name:'multi-line with s p a c\\n \\ne s'}}"
374-
);
375-
});
376-
});
377-
378-
describe('stringify', function () {
379-
it('should work', function () {
380-
const res = parseFilter('{_id: ObjectId("58c33a794d08b991e3648fd2")}');
381-
const stringified = stringify(res);
382-
assert.equal(stringified, "{_id: ObjectId('58c33a794d08b991e3648fd2')}");
383-
});
384-
it('should not added extra space when nesting', function () {
385-
assert.equal(stringify({ a: { $exists: true } }), '{a: {$exists: true}}');
386-
});
387-
388-
// stringify is now deprecated as a result of this.
389-
it('changes multi-space values', function () {
390-
assert.equal(
391-
stringify({
392-
a: {
393-
name: `multi-line with s p a c
394-
395-
e s`,
396-
},
397-
}),
398-
"{a: {name: 'multi-line with s p a c\\n \\ne s'}}"
399-
);
400-
});
401-
402-
context('when providing a long', function () {
403-
it('correctly converts to NumberLong', function () {
404-
const stringified = stringify({ test: bson.Long.fromNumber(5) });
405-
assert.equal(stringified, "{test: NumberLong('5')}");
406-
407-
assert.equal(
408-
stringify({ test: new bson.Long('123456789123456789') }),
409-
"{test: NumberLong('123456789123456789')}"
410-
);
411-
});
412-
});
413-
414-
context('when providing a decimal128', function () {
415-
it('correctly converts to NumberDecimal', function () {
416-
const stringified = stringify({
417-
test: bson.Decimal128.fromString('5.5'),
418-
});
419-
assert.equal(stringified, "{test: NumberDecimal('5.5')}");
420-
});
421-
});
422-
423-
context('when providing an int32', function () {
424-
it('correctly converts to Int32', function () {
425-
const stringified = stringify({
426-
test: new bson.Int32(123),
427-
});
428-
assert.equal(stringified, "{test: NumberInt('123')}");
429-
});
430-
});
431-
432-
context('when providing a Double', function () {
433-
it('correctly converts to Double', function () {
434-
const stringified = stringify({
435-
test: new bson.Double(0.8),
436-
});
437-
assert.equal(stringified, "{test: Double('0.8')}");
438-
});
439-
});
440-
441-
context('when providing a geo query', function () {
442-
const query = {
443-
coordinates: {
444-
$geoWithin: {
445-
$centerSphere: [[-79, 28], 0.04],
446-
},
447-
},
448-
};
449-
450-
it('correctly replaces nested tabs with single spaces', function () {
451-
const stringified = stringify(query);
452-
assert.equal(
453-
stringified,
454-
'{coordinates: {$geoWithin: { $centerSphere: [ [ -79, 28 ], 0.04 ]}}}'
455-
);
456-
});
457-
});
458-
459-
context('when providing a Date', function () {
460-
it('correctly converts to an ISODate', function () {
461-
const res = parseFilter("{test: new Date('2017-01-01T12:35:31.000Z')}");
462-
const stringified = stringify(res);
463-
assert.equal(
464-
stringified,
465-
"{test: ISODate('2017-01-01T12:35:31.000Z')}"
466-
);
467-
});
468-
469-
it('fallbacks to an invalid ISODate if the provided Date is invalid', function () {
470-
const res = parseFilter("{test: new Date('invalid')}");
471-
const stringified = stringify(res);
472-
assert.equal(stringified, "{test: ISODate('Invalid Date')}");
473-
});
474-
});
475-
476-
context('when providing an ISODate', function () {
477-
it('correctly converts to an ISODate', function () {
478-
const res = parseFilter("{test: ISODate('2017-01-01T12:35:31.000Z')}");
479-
const stringified = stringify(res);
480-
assert.equal(
481-
stringified,
482-
"{test: ISODate('2017-01-01T12:35:31.000Z')}"
483-
);
484-
});
485-
486-
it('fallbacks to an invalid ISODate if the provided ISODate is invalid', function () {
487-
const res = parseFilter("{test: ISODate('invalid')}");
488-
const stringified = stringify(res);
489-
assert.equal(stringified, "{test: ISODate('Invalid Date')}");
490-
});
491-
});
492-
493-
context('when providing a DBRef with (collection, oid)', function () {
494-
it('correctly converts to a DBRef', function () {
495-
const res = parseFilter("{dbref: DBRef('col', 1)}");
496-
const stringified = stringify(res);
497-
assert.equal(stringified, "{dbref: DBRef('col', '1')}");
498-
});
499-
});
500-
501-
context('when providing a DBRef with (db.collection, oid)', function () {
502-
it('correctly converts to a DBRef', function () {
503-
const res = parseFilter("{dbref: DBRef('db.col', 1)}");
504-
const stringified = stringify(res);
505-
assert.equal(stringified, "{dbref: DBRef('col', '1', 'db')}");
506-
});
507-
});
508-
509-
context('when providing a DBRef with (collection, oid, db)', function () {
510-
it('correctly converts to a DBRef', function () {
511-
const res = parseFilter("{dbref: DBRef('col', 1, 'db')}");
512-
const stringified = stringify(res);
513-
assert.equal(stringified, "{dbref: DBRef('col', '1', 'db')}");
514-
});
515-
});
516-
517-
context('when provided a RegExp', function () {
518-
it('correctly formats the options', function () {
519-
const res = parseFilter('{name: /foo/i}');
520-
const stringified = stringify(res);
521-
assert.equal(stringified, '{name: RegExp("foo", \'i\')}');
522-
});
523-
524-
it('escapes quotes', function () {
525-
const res = parseFilter("{name: /'/}");
526-
const stringified = stringify(res);
527-
assert.equal(stringified, '{name: RegExp("\'")}');
528-
});
529-
530-
it('handles $regex object format (keeps format)', function () {
531-
const res = parseFilter(
532-
'{"name": {"$regex": "pineapple", "$options": "i"}}'
533-
);
534-
const stringified = stringify(res);
535-
assert.equal(
536-
stringified,
537-
"{name: {$regex: 'pineapple',$options: 'i'}}"
538-
);
539-
});
540-
541-
it('handles /regex/ format', function () {
542-
const res = {
543-
name: /pineapple/,
544-
};
545-
const stringified = stringify(res);
546-
assert.equal(stringified, '{name: RegExp("pineapple")}');
547-
});
548-
});
549-
550-
context('when provided a BSONRegExp', function () {
551-
it('stringifies correctly with options', function () {
552-
const res = {
553-
name: new bson.BSONRegExp('pineapple', 'i'),
554-
};
555-
const stringified = stringify(res);
556-
assert.equal(stringified, '{name: RegExp("pineapple", \'i\')}');
557-
});
558-
559-
it('stringifies correctly with quotes', function () {
560-
const res = {
561-
name: new bson.BSONRegExp('"\'', 'i'),
562-
};
563-
const stringified = stringify(res);
564-
assert.equal(stringified, '{name: RegExp("\\"\'", \'i\')}');
565-
});
566-
567-
it('stringifies correctly without options', function () {
568-
const res = {
569-
name: new bson.BSONRegExp('pineapple'),
570-
};
571-
const stringified = stringify(res);
572-
assert.equal(stringified, '{name: RegExp("pineapple")}');
573-
});
574-
575-
it('stringifies into BSONRegExp when js RegExp cannot handle an option', function () {
576-
const res = {
577-
name: new bson.BSONRegExp(
578-
'pineapple',
579-
'x' /* x flag is not valid in js but valid in BSONRegExp*/
580-
),
581-
};
582-
const stringified = stringify(res);
583-
assert.equal(stringified, '{name: BSONRegExp("pineapple", \'x\')}');
584-
});
585-
586-
it('stringifies into BSONRegExp when js RegExp cannot handle the regex', function () {
587-
const res = {
588-
name: new bson.BSONRegExp(
589-
// Perl Compatible Regular Expressions supported feature that isn't in regular
590-
// js RegExp: case-insensitive match.
591-
'(?i)a(?-i)cme',
592-
'i'
593-
),
594-
};
595-
const stringified = stringify(res);
596-
assert.equal(stringified, '{name: BSONRegExp("(?i)a(?-i)cme", \'i\')}');
597-
});
598-
});
599-
600-
context('when provided a Binary', function () {
601-
it('should support BinData', function () {
602-
const res = parseFilter(
603-
`{name: new BinData(${bson.Binary.SUBTYPE_BYTE_ARRAY}, "OyQRAeK7QlWMr0E2xWapYg==")}`
604-
);
605-
const stringified = stringify(res);
606-
assert.equal(
607-
stringified,
608-
`{name: BinData(${bson.Binary.SUBTYPE_BYTE_ARRAY}, 'OyQRAeK7QlWMr0E2xWapYg==')}`
609-
);
610-
});
611-
612-
it('should support UUID', function () {
613-
const res = parseFilter(
614-
'{name: UUID("3b241101-e2bb-4255-8caf-4136c566a962")}'
615-
);
616-
const stringified = stringify(res);
617-
assert.equal(
618-
stringified,
619-
"{name: UUID('3b241101-e2bb-4255-8caf-4136c566a962')}"
620-
);
621-
});
622-
});
623-
});
624-
625329
describe('project', function () {
626330
it('should default to null', function () {
627331
assert.equal(parseProject(''), null);

0 commit comments

Comments
 (0)