1 | $(function(){
|
2 | var $b = $('#builder');
|
3 |
|
4 | QUnit.module('plugins.mongo-support', {
|
5 | afterEach: function() {
|
6 | $b.queryBuilder('destroy');
|
7 | }
|
8 | });
|
9 |
|
10 | QUnit.test('Basics', function(assert) {
|
11 | var basic_rules_mongodb = {
|
12 | '$and': [
|
13 | {'price': {'$lt': 10.25}},
|
14 | {'name': null},
|
15 | {
|
16 | '$or': [
|
17 | {'category': {'$in': ['mo', 'mu']}},
|
18 | {'id': {'$ne': '1234-azer-5678'}}
|
19 | ]
|
20 | }
|
21 | ]
|
22 | };
|
23 |
|
24 | $b.queryBuilder({
|
25 | filters: basic_filters,
|
26 | rules: basic_rules
|
27 | });
|
28 |
|
29 | assert.deepEqual(
|
30 | $b.queryBuilder('getMongo'),
|
31 | basic_rules_mongodb,
|
32 | 'Should create MongoDB query'
|
33 | );
|
34 |
|
35 | assert.deepEqual(
|
36 | $b.queryBuilder('getRulesFromMongo', basic_rules_mongodb),
|
37 | basic_rules,
|
38 | 'Should return rules object from MongoDB query'
|
39 | );
|
40 | });
|
41 |
|
42 | QUnit.test('All operators', function(assert) {
|
43 | $b.queryBuilder({
|
44 | filters: basic_filters,
|
45 | rules: all_operators_rules
|
46 | });
|
47 |
|
48 | assert.deepEqual(
|
49 | $b.queryBuilder('getMongo'),
|
50 | all_operators_rules_mongodb,
|
51 | 'Should successfully convert all kind of operators to MongoDB'
|
52 | );
|
53 |
|
54 | $b.queryBuilder('reset');
|
55 |
|
56 | $b.queryBuilder('setRulesFromMongo', all_operators_rules_mongodb);
|
57 |
|
58 | assert.rulesMatch(
|
59 | $b.queryBuilder('getRules'),
|
60 | all_operators_rules,
|
61 | 'Should successfully parse all kind of operators from MongoDB'
|
62 | );
|
63 | });
|
64 |
|
65 | QUnit.test('Automatically use filter from field', function(assert) {
|
66 | var rules = {
|
67 | condition: 'AND',
|
68 | rules: [
|
69 | {
|
70 | id: 'name',
|
71 | operator: 'equal',
|
72 | value: 'Mistic'
|
73 | }
|
74 | ]
|
75 | };
|
76 |
|
77 | var mongo = {
|
78 | $and: [{
|
79 | username: 'Mistic'
|
80 | }]
|
81 | };
|
82 |
|
83 | $b.queryBuilder({
|
84 | filters: [
|
85 | {
|
86 | id: 'name',
|
87 | field: 'username',
|
88 | type: 'string'
|
89 | },
|
90 | {
|
91 | id: 'last_days',
|
92 | field: 'display_date',
|
93 | type: 'integer'
|
94 | }
|
95 | ]
|
96 | });
|
97 |
|
98 | $b.queryBuilder('setRulesFromMongo', mongo);
|
99 |
|
100 | assert.rulesMatch(
|
101 | $b.queryBuilder('getRules'),
|
102 | rules,
|
103 | 'Should use "name" filter from "username" field'
|
104 | );
|
105 | });
|
106 |
|
107 |
|
108 | var all_operators_rules = {
|
109 | condition: 'AND',
|
110 | rules: [{
|
111 | id: 'name',
|
112 | operator: 'equal',
|
113 | value: 'foo'
|
114 | }, {
|
115 | id: 'name',
|
116 | operator: 'not_equal',
|
117 | value: 'foo'
|
118 | }, {
|
119 | id: 'category',
|
120 | operator: 'in',
|
121 | value: ['bk','mo']
|
122 | }, {
|
123 | id: 'category',
|
124 | operator: 'not_in',
|
125 | value: ['bk','mo']
|
126 | }, {
|
127 | id: 'price',
|
128 | operator: 'less',
|
129 | value: '5'
|
130 | }, {
|
131 | id: 'price',
|
132 | operator: 'less_or_equal',
|
133 | value: '5'
|
134 | }, {
|
135 | id: 'price',
|
136 | operator: 'greater',
|
137 | value: '4'
|
138 | }, {
|
139 | id: 'price',
|
140 | operator: 'greater_or_equal',
|
141 | value: '4'
|
142 | }, {
|
143 | id: 'price',
|
144 | operator: 'between',
|
145 | value: ['4','5']
|
146 | }, {
|
147 | id: 'price',
|
148 | operator: 'not_between',
|
149 | value: ['4','5']
|
150 | }, {
|
151 | id: 'name',
|
152 | operator: 'begins_with',
|
153 | value: 'foo'
|
154 | }, {
|
155 | id: 'name',
|
156 | operator: 'not_begins_with',
|
157 | value: 'foo'
|
158 | }, {
|
159 | id: 'name',
|
160 | operator: 'contains',
|
161 | value: 'foo'
|
162 | }, {
|
163 | id: 'name',
|
164 | operator: 'not_contains',
|
165 | value: 'foo'
|
166 | }, {
|
167 | id: 'name',
|
168 | operator: 'ends_with',
|
169 | value: 'foo'
|
170 | }, {
|
171 | id: 'name',
|
172 | operator: 'not_ends_with',
|
173 | value: 'foo'
|
174 | }, {
|
175 | id: 'name',
|
176 | operator: 'is_empty',
|
177 | value: null
|
178 | }, {
|
179 | id: 'name',
|
180 | operator: 'is_not_empty',
|
181 | value: null
|
182 | }, {
|
183 | id: 'name',
|
184 | operator: 'is_null',
|
185 | value: null
|
186 | }, {
|
187 | id: 'name',
|
188 | operator: 'is_not_null',
|
189 | value: null
|
190 | }]
|
191 | };
|
192 |
|
193 | var all_operators_rules_mongodb = {
|
194 | $and: [
|
195 | { name: 'foo' },
|
196 | { name: {$ne: 'foo'} },
|
197 | { category: { $in: ['bk','mo'] }},
|
198 | { category: { $nin: ['bk','mo'] }},
|
199 | { price: {$lt: 5} },
|
200 | { price: {$lte: 5} },
|
201 | { price: {$gt: 4} },
|
202 | { price: {$gte: 4} },
|
203 | { price: {$gte: 4, $lte: 5} },
|
204 | { price: {$lt: 4, $gt: 5} },
|
205 | { name: {$regex: '^foo'} },
|
206 | { name: {$regex: '^(?!foo)'} },
|
207 | { name: {$regex: 'foo'} },
|
208 | { name: {$regex: '^((?!foo).)*$', $options: 's'} },
|
209 | { name: {$regex: 'foo$'} },
|
210 | { name: {$regex: '(?<!foo)$'} },
|
211 | { name: '' },
|
212 | { name: {$ne: ''} },
|
213 | { name: null },
|
214 | { name: {$ne: null} }
|
215 | ]
|
216 | };
|
217 | });
|