| 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 |
1×
1×
1×
1×
1×
1×
1×
1×
32×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
5×
1×
1×
1×
1×
1×
1×
5×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
5×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var Realm = require("realm");
var realm_query_1 = require("./realm-query");
var chai_1 = require("chai");
describe("RealmQuery", function () {
var query;
beforeEach(function () {
query = realm_query_1.default.create();
});
it('should able to add new criteria', function () {
chai_1.expect(query['criteria'].length).to.equal(0);
query.addCriteria('id == 1');
chai_1.expect(query['criteria'].length).to.equal(1);
});
describe("equalTo", function () {
it('equalTo number', function () {
query.equalTo('id', 1001);
chai_1.expect(query.toString()).to.equal('id == 1001');
});
it('equalTo string', function () {
query.equalTo('name', "username");
chai_1.expect(query.toString()).to.equal('name == "username"');
});
it('equalTo date', function () {
var now = new Date();
query.equalTo('createdAt', now);
chai_1.expect(query.toString()).to.equal('createdAt == ' + now.toString());
});
it('equalTo boolean', function () {
query.equalTo('isDeleted', true);
chai_1.expect(query.toString()).to.equal('isDeleted == true');
});
});
describe("notEqualTo", function () {
it('notEqualTo number', function () {
query.notEqualTo('id', 1001);
chai_1.expect(query.toString()).to.equal('id <> 1001');
});
it('notEqualTo string', function () {
query.notEqualTo('name', "username");
chai_1.expect(query.toString()).to.equal('name <> "username"');
});
it('notEqualTo date', function () {
var now = new Date();
query.notEqualTo('createdAt', now);
chai_1.expect(query.toString()).to.equal('createdAt <> ' + now.toString());
});
it('notEqualTo boolean', function () {
query.notEqualTo('isDeleted', true);
chai_1.expect(query.toString()).to.equal('isDeleted <> true');
});
});
describe("beginsWith", function () {
it('beginsWith not casing', function () {
query.beginsWith('name', 'phu');
chai_1.expect(query.toString()).to.equal('name BEGINSWITH "phu"');
});
it('beginsWith casing', function () {
query.beginsWith('name', 'phu', true);
chai_1.expect(query.toString()).to.equal('name BEGINSWITH[c] "phu"');
});
});
describe("endsWith", function () {
it('endsWith not casing', function () {
query.endsWith('name', 'phu');
chai_1.expect(query.toString()).to.equal('name ENDSWITH "phu"');
});
it('endsWith casing', function () {
query.endsWith('name', 'phu', true);
chai_1.expect(query.toString()).to.equal('name ENDSWITH[c] "phu"');
});
});
describe("contains", function () {
it('contains not casing', function () {
query.contains('name', 'phu');
chai_1.expect(query.toString()).to.equal('name CONTAINS "phu"');
});
it('contains casing', function () {
query.contains('name', 'phu', true);
chai_1.expect(query.toString()).to.equal('name CONTAINS[c] "phu"');
});
});
describe("between", function () {
it('between number to number', function () {
query.between('age', 20, 30);
chai_1.expect(query.toString()).to.equal('age >= 20 AND age <= 30');
});
it('between date to date', function () {
var start = new Date();
var end = new Date();
query.between('createdAt', start, end);
chai_1.expect(query.toString()).to.equal("createdAt >= " + start + " AND createdAt <= " + end);
});
});
describe("Basic comparation", function () {
it("greaterThan number", function () {
query.greaterThan('age', 20);
chai_1.expect(query.toString()).to.equal('age > 20');
});
it("greaterThan a date", function () {
var now = new Date();
query.greaterThan('createdAt', now);
chai_1.expect(query.toString()).to.equal('createdAt > ' + now.toString());
});
it("greaterThanOrEqualTo number", function () {
query.greaterThanOrEqualTo('age', 20);
chai_1.expect(query.toString()).to.equal('age >= 20');
});
it("greaterThanOrEqualTo a date", function () {
var now = new Date();
query.greaterThanOrEqualTo('createdAt', now);
chai_1.expect(query.toString()).to.equal('createdAt >= ' + now.toString());
});
it("lessThan number", function () {
query.lessThan('age', 20);
chai_1.expect(query.toString()).to.equal('age < 20');
});
it("lessThan a date", function () {
var now = new Date();
query.lessThan('createdAt', now);
chai_1.expect(query.toString()).to.equal('createdAt < ' + now.toString());
});
it("lessThanOrEqualTo number", function () {
query.lessThanOrEqualTo('age', 20);
chai_1.expect(query.toString()).to.equal('age <= 20');
});
it("lessThanOrEqualTo a date", function () {
var now = new Date();
query.lessThanOrEqualTo('createdAt', now);
chai_1.expect(query.toString()).to.equal('createdAt <= ' + now.toString());
});
});
describe('In', function () {
it('In array of number', function () {
query.in('id', [1, 2]);
chai_1.expect(query.toString()).to.equal('(id == 1 OR id == 2)');
});
it('In array of string', function () {
query.in('status', ["ACTIVE", "DEACTIVE"]);
chai_1.expect(query.toString()).to.equal('(status == "ACTIVE" OR status == "DEACTIVE")');
});
it('In array of mix values', function () {
query.in('id', [1001, "abcd"]);
chai_1.expect(query.toString()).to.equal('(id == 1001 OR id == "abcd")');
});
});
describe('Query with compound criterias', function () {
it('complex query with or', function () {
query
.contains('name', 'phu', true)
.beginGroup()
.greaterThan('age', 25)
.or()
.in('id', [1001, 1002]);
var expectedFilted = 'name CONTAINS[c] "phu" AND (age > 25 OR (id == 1001 OR id == 1002))';
chai_1.expect(query.toString()).to.equal(expectedFilted);
});
it('complex query with and', function () {
query
.contains('name', 'phu', true)
.beginGroup()
.greaterThan('age', 25)
.and()
.in('id', [1001, 1002]);
var expectedFilted = 'name CONTAINS[c] "phu" AND (age > 25 AND (id == 1001 OR id == 1002))';
chai_1.expect(query.toString()).to.equal(expectedFilted);
});
it('complex query with not', function () {
query
.not()
.contains('name', 'phu', true)
.beginGroup()
.greaterThan('age', 25)
.and()
.in('id', [1001, 1002]);
var expectedFilted = 'NOT(name CONTAINS[c] "phu" AND (age > 25 AND (id == 1001 OR id == 1002)))';
chai_1.expect(query.toString()).to.equal(expectedFilted);
});
it('combine complex query', function () {
query = realm_query_1.default.create().contains('name', 'phu', true);
var query2 = realm_query_1.default.create().in('id', [1001, 1002]);
query.join(query2);
chai_1.expect(query.toString()).to.equal('name CONTAINS[c] "phu" AND (id == 1001 OR id == 1002)');
});
});
});
describe('Get objects with RealmQuery', function () {
var PersonSchema = {
name: 'Person',
properties: {
id: 'int',
name: 'string',
age: 'int',
createdAt: 'date'
}
};
var realm = new Realm({
path: 'io.izlab.realmquery.test',
schema: [PersonSchema]
});
realm.write(function () {
realm.deleteAll();
realm.create('Person', { id: 1, name: 'clinton', age: 18, createdAt: new Date("2004-12-06 03:34:06") });
realm.create('Person', { id: 2, name: 'necati', age: 34, createdAt: new Date("2011-09-26 16:42:17") });
realm.create('Person', { id: 3, name: 'norman', age: 28, createdAt: new Date("2015-06-14 20:57:46") });
realm.create('Person', { id: 4, name: 'elias', age: 42, createdAt: new Date("2006-06-13 04:35:02") });
realm.create('Person', { id: 5, name: 'martin', age: 18, createdAt: new Date("2003-01-14 14:12:50") });
});
it('Find all', function () {
var query = realm_query_1.default.where(realm.objects('Person'));
var results = query.findAll();
chai_1.expect(results.length).to.equal(5);
});
it('Find all with filtered', function () {
var results = realm_query_1.default
.where(realm.objects('Person'))
.greaterThan('age', 30)
.findAll();
chai_1.expect(results.length).to.equal(2);
});
it('Find first', function () {
var query = realm_query_1.default.where(realm.objects('Person'));
var result = query.findFirst();
var expected = realm.objects('Person')[0];
chai_1.expect(JSON.stringify(result)).to.equal(JSON.stringify(expected));
});
it('Find first with filtered', function () {
var result = realm_query_1.default
.where(realm.objects('Person'))
.greaterThan('age', 30)
.findFirst();
var expected = realm.objects('Person').filtered('age > 30')[0];
chai_1.expect(JSON.stringify(result)).to.equal(JSON.stringify(expected));
});
it('Count', function () {
var total = realm_query_1.default
.where(realm.objects('Person'))
.count();
chai_1.expect(total).to.equal(5);
});
it('Count with filtered', function () {
var total = realm_query_1.default
.where(realm.objects('Person'))
.greaterThan('age', 30)
.count();
chai_1.expect(total).to.equal(2);
});
it('Distinct', function () {
var results = realm_query_1.default
.where(realm.objects('Person'))
.distinct('age');
chai_1.expect(results.length).to.equal(4);
});
it('Average', function () {
var avg = realm_query_1.default
.where(realm.objects('Person'))
.average('age');
var expected = 0;
var allPersons = realm.objects('Person');
allPersons.map(function (p) { return expected += p['age']; });
expected = expected / allPersons.length;
chai_1.expect(avg).to.equal(expected);
});
it('Sum', function () {
var sum = realm_query_1.default
.where(realm.objects('Person'))
.sum('age');
var expected = 0;
var allPersons = realm.objects('Person');
allPersons.map(function (p) { return expected += p['age']; });
chai_1.expect(sum).to.equal(expected);
});
it('Max', function () {
var result = realm_query_1.default.where(realm.objects('Person')).max('age');
chai_1.expect(result['age']).to.equal(42);
});
it('Min', function () {
var result = realm_query_1.default.where(realm.objects('Person')).min('age');
chai_1.expect(result['age']).to.equal(18);
});
it('should sort by age ASC', function () {
var results = realm_query_1.default.where(realm.objects('Person')).sort('age').findAll();
var actual = JSON.stringify(_.map(_.toArray(results), 'age'));
var expected = JSON.stringify([18, 18, 28, 34, 42]);
chai_1.expect(actual).to.equal(expected);
});
it('should sort by age DESC', function () {
var results = realm_query_1.default.where(realm.objects('Person')).sort('age', 'DESC').findAll();
var actual = JSON.stringify(_.map(_.toArray(results), 'age'));
var expected = JSON.stringify([42, 34, 28, 18, 18]);
chai_1.expect(actual).to.equal(expected);
});
});
describe('Methods that RealmQuery not yet supported', function () {
var query;
beforeEach(function () {
query = realm_query_1.default.create();
});
it('isEmpty', function () {
chai_1.expect(query.isEmpty).to.throw('Not yet supported "isEmpty');
});
it('isNotEmpty', function () {
chai_1.expect(query.isNotEmpty).to.throw('Not yet supported "isNotEmpty');
});
it('isNotNull', function () {
chai_1.expect(query.isNotNull).to.throw('Not yet supported "isNotNull');
});
it('isNull', function () {
chai_1.expect(query.isNull).to.throw('Not yet supported "isNull');
});
it('like', function () {
chai_1.expect(query.like).to.throw('Not yet supported "like');
});
});
//# sourceMappingURL=realm-query.test.js.map |