forked from thomas4019/mongo-query-to-postgres-jsonb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
131 lines (119 loc) · 4.92 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var assert = require('chai').assert;
var convert = require('../index')
describe('string equality', function () {
it('should use ->>', function () {
assert.equal("data->>'name'='thomas'", convert('data', {name: 'thomas'}));
});
it('nesting does exact document matching', function() {
assert.equal("data->'test'='{\"cat\":{\"name\":\"oscar\"}}'::jsonb", convert('data', {test: {cat: {name: "oscar"}} }));
});
it('should support nesting using the dot operator', function() {
assert.equal("data->'test'->'cat'->>'name'='oscar'", convert('data', {'test.cat.name': "oscar"}));
});
});
describe('array equality', function () {
it('should use =', function () {
assert.equal("data->'roles'='[\"Admin\"]'::jsonb", convert('data', {'roles': ['Admin']}));
});
it('should matching numeric indexes', function() {
assert.equal("data->'roles'->>0='Admin'", convert('data', {'roles.0': 'Admin'}));
});
it('support element matching', function() {
assert.equal("data->'roles' @> '\"Admin\"'::jsonb", convert('data', {'roles': {$elemMatch: 'Admin'}}));
});
});
describe('boolean equality', function () {
it('should use ->', function () {
assert.equal("data->'hidden'='false'::jsonb", convert('data', {'hidden': false}));
});
});
describe('$or', function () {
it('returns false with no parameters', function () {
assert.equal("FALSE", convert('data', {$or: []}));
});
it('work with one parameter', function () {
assert.equal("(data->>'name'='thomas')", convert('data', {$or: [{name: 'thomas'}]}));
});
it('work with two parameters', function () {
assert.equal("(data->>'name'='thomas' OR data->>'name'='hansen')", convert('data', {$or: [{name: 'thomas'}, {name: 'hansen'}]}));
});
});
describe('$and', function () {
it('returns true with no parameters', function () {
assert.equal("TRUE", convert('data', {$and: []}));
});
it('work with one parameter', function () {
assert.equal("(data->>'name'='thomas')", convert('data', {$and: [{name: 'thomas'}]}));
});
it('work with two parameters', function () {
assert.equal("(data->>'name'='thomas' AND data->>'name'='hansen')", convert('data', {$and: [{name: 'thomas'}, {name: 'hansen'}]}));
});
it('should work implicitly', function () {
assert.equal("(data->>'type'='food' and data->'price'<'9.95'::jsonb)", convert('data', { type: 'food', price: { $lt: 9.95 } }));
});
});
describe('$in', function () {
it('should work with strings', function () {
assert.equal("data->>'type' IN ('food', 'snacks')", convert('data', { type: { $in: [ 'food', 'snacks' ] } }));
});
it('should work with numbers', function () {
assert.equal("data->'count' IN ('1'::jsonb, '5'::jsonb)", convert('data', { count: { $in: [ 1, 5 ] } }));
});
});
describe('$nin', function () {
it('should work with strings', function () {
assert.equal("data->>'type' NOT IN ('food', 'snacks')", convert('data', { type: { $nin: [ 'food', 'snacks' ] } }));
});
it('should work with numbers', function () {
assert.equal("data->'count' NOT IN ('1'::jsonb, '5'::jsonb)", convert('data', { count: { $nin: [ 1, 5 ] } }));
});
});
describe('$not', function () {
it('should add NOT and wrap in paratheses', function () {
assert.equal("(NOT data->>'name'='thomas')", convert('data', { $not : {name: 'thomas'} }));
});
});
describe('comparision operators', function() {
it('$eq', function () {
assert.equal("data->>'type'='food'", convert('data', { type: { $eq : 'food' } }));
});
it('$ne', function () {
assert.equal("data->>'type'!='food'", convert('data', { type: { $ne : 'food' } }));
});
it('$gt', function () {
assert.equal("data->'count'>'5'::jsonb", convert('data', { count: { $gt : 5 } }));
});
it('$gte', function () {
assert.equal("data->'count'>='5'::jsonb", convert('data', { count: { $gte : 5 } }));
});
it('$lt', function () {
assert.equal("data->'count'<'5'::jsonb", convert('data', { count: { $lt : 5 } }));
});
it('$lte', function () {
assert.equal("data->'count'<='5'::jsonb", convert('data', { count: { $lte : 5 } }));
});
});
describe('regular expressions', function() {
it('basic', function () {
assert.equal("data->>'type' ~ 'food'", convert('data', { type: { $regex : 'food' } }));
});
it('case insensitive', function () {
assert.equal("data->>'type' ~* 'food'", convert('data', { type: { $regex : 'food', $options: 'i' } }));
});
});
describe('combined tests', function () {
it('should handle ANDs and ORs together', function() {
assert.equal('(data->>\'type\'=\'food\' and (data->\'qty\'>\'100\'::jsonb OR data->\'price\'<\'9.95\'::jsonb))', convert('data', {
type: 'food',
$or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
}));
});
it('should add NOT and wrap in paratheses', function () {
assert.equal("(data->>\'city\'=\'provo\' and data->\'pop\'>\'1000\'::jsonb)", convert('data', {city: 'provo', pop : { $gt : 1000 } }));
});
});
describe('special cases', function () {
it('should return true when passed no parameters', function() {
assert.equal('TRUE', convert('data', {}));
});
});