query-generator.test.js
3.41 KB
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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Op = require('../../../../lib/operators'),
getAbstractQueryGenerator = require(__dirname + '/../../support').getAbstractQueryGenerator;
describe('QueryGenerator', () => {
describe('whereItemQuery', () => {
it('should generate correct query for Symbol operators', function() {
const QG = getAbstractQueryGenerator(this.sequelize);
QG.whereItemQuery(Op.or, [{test: {[Op.gt]: 5}}, {test: {[Op.lt]: 3}}, {test: {[Op.in]: [4]}}])
.should.be.equal('(test > 5 OR test < 3 OR test IN (4))');
QG.whereItemQuery(Op.and, [{test: {[Op.between]: [2, 5]}}, {test: {[Op.ne]: 3}}, {test: {[Op.not]: 4}}])
.should.be.equal('(test BETWEEN 2 AND 5 AND test != 3 AND test != 4)');
});
it('should not parse any strings as aliases operators', function() {
const QG = getAbstractQueryGenerator(this.sequelize);
expect(() => QG.whereItemQuery('$or', [{test: 5}, {test: 3}]))
.to.throw('Invalid value { test: 5 }');
expect(() => QG.whereItemQuery('$and', [{test: 5}, {test: 3}]))
.to.throw('Invalid value { test: 5 }');
expect(() => QG.whereItemQuery('test', {$gt: 5}))
.to.throw('Invalid value { \'$gt\': 5 }');
expect(() => QG.whereItemQuery('test', {$between: [2, 5]}))
.to.throw('Invalid value { \'$between\': [ 2, 5 ] }');
expect(() => QG.whereItemQuery('test', {$ne: 3}))
.to.throw('Invalid value { \'$ne\': 3 }');
expect(() => QG.whereItemQuery('test', {$not: 3}))
.to.throw('Invalid value { \'$not\': 3 }');
expect(() => QG.whereItemQuery('test', {$in: [4]}))
.to.throw('Invalid value { \'$in\': [ 4 ] }');
});
it('should parse set aliases strings as operators', function() {
const QG = getAbstractQueryGenerator(this.sequelize),
aliases = {
OR: Op.or,
'!': Op.not,
'^^': Op.gt
};
QG.setOperatorsAliases(aliases);
QG.whereItemQuery('OR', [{test: {'^^': 5}}, {test: {'!': 3}}, {test: {[Op.in]: [4]}}])
.should.be.equal('(test > 5 OR test != 3 OR test IN (4))');
QG.whereItemQuery(Op.and, [{test: {[Op.between]: [2, 5]}}, {test: {'!': 3}}, {test: {'^^': 4}}])
.should.be.equal('(test BETWEEN 2 AND 5 AND test != 3 AND test > 4)');
expect(() => QG.whereItemQuery('OR', [{test: {'^^': 5}}, {test: {$not: 3}}, {test: {[Op.in]: [4]}}]))
.to.throw('Invalid value { \'$not\': 3 }');
expect(() => QG.whereItemQuery('OR', [{test: {$gt: 5}}, {test: {'!': 3}}, {test: {[Op.in]: [4]}}]))
.to.throw('Invalid value { \'$gt\': 5 }');
expect(() => QG.whereItemQuery('$or', [{test: 5}, {test: 3}]))
.to.throw('Invalid value { test: 5 }');
expect(() => QG.whereItemQuery('$and', [{test: 5}, {test: 3}]))
.to.throw('Invalid value { test: 5 }');
expect(() => QG.whereItemQuery('test', {$gt: 5}))
.to.throw('Invalid value { \'$gt\': 5 }');
expect(() => QG.whereItemQuery('test', {$between: [2, 5]}))
.to.throw('Invalid value { \'$between\': [ 2, 5 ] }');
expect(() => QG.whereItemQuery('test', {$ne: 3}))
.to.throw('Invalid value { \'$ne\': 3 }');
expect(() => QG.whereItemQuery('test', {$not: 3}))
.to.throw('Invalid value { \'$not\': 3 }');
expect(() => QG.whereItemQuery('test', {$in: [4]}))
.to.throw('Invalid value { \'$in\': [ 4 ] }');
});
});
});