-
Notifications
You must be signed in to change notification settings - Fork 2
/
query-builder.test.ts
180 lines (169 loc) · 6.42 KB
/
query-builder.test.ts
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
import { QueryBuilder } from '../src/query-builder';
it('should return empty array without options', () => {
const query = new QueryBuilder().toQuery();
const expected = '';
expect(query).toEqual(expected);
});
describe('top', () => {
it('should return top', () => {
const query = new QueryBuilder().top(10).toQuery();
const expected = '?$top=10';
expect(query).toEqual(expected);
});
it('should return top with last value with multiple top calls', () => {
const query = new QueryBuilder().top(10).top(15).toQuery();
const expected = '?$top=15';
expect(query).toEqual(expected);
});
});
describe('skip', () => {
it('should return skip', () => {
const query = new QueryBuilder().skip(10).toQuery();
const expected = '?$skip=10';
expect(query).toEqual(expected);
});
it('should return skip with last value with multiple skip calls', () => {
const query = new QueryBuilder().skip(10).skip(15).toQuery();
const expected = '?$skip=15';
expect(query).toEqual(expected);
});
});
describe('count', () => {
it('should return count', () => {
const query = new QueryBuilder().count().toQuery();
const expected = '?$count=true';
expect(query).toEqual(expected);
});
it('should return count with multiple count calls', () => {
const query = new QueryBuilder().count().count().toQuery();
const expected = '?$count=true';
expect(query).toEqual(expected);
});
});
describe('select', () => {
it('should return select', () => {
const query = new QueryBuilder().select('id', 'name').toQuery();
const expected = '?$select=id,name';
expect(query).toEqual(expected);
});
it('should return joint select with multiple select calls', () => {
const query = new QueryBuilder().select('id', 'name').select('age').toQuery();
const expected = '?$select=id,name,age';
expect(query).toEqual(expected);
});
});
describe('orderBy', () => {
it('should return orderby', () => {
const query = new QueryBuilder().orderBy('id desc', 'name').toQuery();
const expected = '?$orderby=id desc,name';
expect(query).toEqual(expected);
});
it('should return joint orderby with multiple orderBy calls', () => {
const query = new QueryBuilder().orderBy('id desc', 'name').orderBy('age asc').toQuery();
const expected = '?$orderby=id desc,name,age asc';
expect(query).toEqual(expected);
});
});
describe('expand', () => {
it('should return expand without nesting', () => {
const query = new QueryBuilder().expand('Books').toQuery();
const expected = '?$expand=Books';
expect(query).toEqual(expected);
});
it('should return expand with select and expand nesting', () => {
const query = new QueryBuilder().expand('Books', e => e.select('id', 'name').expand('Chapters')).toQuery();
const expected = '?$expand=Books($select=id,name;$expand=Chapters)';
expect(query).toEqual(expected);
});
it('should return expand with filter nesting', () => {
const query = new QueryBuilder().expand('Books', e => e.filter(f => f.eq('id', 1))).toQuery();
const expected = '?$expand=Books($filter=id eq 1)';
expect(query).toEqual(expected);
});
it('should return joint expand with multiple expand calls', () => {
const query = new QueryBuilder().expand('Books').expand('Friends', e => e.select('id', 'name')).toQuery();
const expected = '?$expand=Books,Friends($select=id,name)';
expect(query).toEqual(expected);
});
});
describe('filter', () => {
it('should return filter with one comparison operator', () => {
const query = new QueryBuilder().filter(f => f.eq('name', 'John Doe')).toQuery();
const expected = '?$filter=name eq \'John Doe\'';
expect(query).toEqual(expected);
});
it('should return filter with default and operator', () => {
const query = new QueryBuilder().filter(f => f.eq('name', 'John Doe').eq('id', 1)).toQuery();
const expected = '?$filter=name eq \'John Doe\' and id eq 1';
expect(query).toEqual(expected);
});
it('should return filter with or operator', () => {
const query = new QueryBuilder().filter(f => f.or(a => a.eq('name', 'John Doe').eq('id', 1))).toQuery();
const expected = '?$filter=name eq \'John Doe\' or id eq 1';
expect(query).toEqual(expected);
});
it('should return empty string if filter is empty', () => {
const query = new QueryBuilder().filter().toQuery();
const expected = '';
expect(query).toEqual(expected);
});
it('should return filter with double nesting', () => {
const query = new QueryBuilder().filter(f =>
f.or(o => o
.eq('name', 'John Doe')
.eq('id', 1)
.and(a => a
.eq('name', 'Jane')
.eq('rating', 5)
)
)
).toQuery();
const expected = '?$filter=name eq \'John Doe\' or id eq 1 or (name eq \'Jane\' and rating eq 5)';
expect(query).toEqual(expected);
});
it('should return joint filter with multiple filter calls', () => {
const query = new QueryBuilder()
.filter(f => f.eq('id', 1))
.filter(f => f.eq('name', 'John Doe'))
.toQuery();
const expected = '?$filter=id eq 1 and name eq \'John Doe\'';
expect(query).toEqual(expected);
});
});
it('should return query from the readme', () => {
const query = new QueryBuilder()
.top(10)
.skip(10)
.count()
.select('name')
.orderBy('name')
.expand('Books', e => e.select('title'))
.filter(f => f.eq('name', 'John Doe'))
.toQuery();
const expected = '?$top=10&$skip=10&$count=true&$select=name&$orderby=name&$expand=Books($select=title)&$filter=name eq \'John Doe\'';
expect(query).toEqual(expected);
});
describe('special charachters', () => {
it('should escape special charachters', () => {
const query = new QueryBuilder()
.select('name\'s')
.orderBy('name+s')
.expand('Book/s', e => e.select('title?s'))
.filter(f => f.or(o => o
.eq('name', 'John Doe%s')
.eq('name', 'John Doe#s')
.eq('name', 'John Doe&s')
))
.toQuery();
const expected = '?$select=name\'\'s&$orderby=name%2Bs&$expand=Book%2Fs($select=title%3Fs)&$filter=name eq \'John Doe%25s\' or name eq \'John Doe%23s\' or name eq \'John Doe%26s\'';
expect(query).toEqual(expected);
});
it('should escape double special charachters', () => {
const query = new QueryBuilder()
.select('name+s')
.orderBy('name+s')
.toQuery();
const expected = '?$select=name%2Bs&$orderby=name%2Bs';
expect(query).toEqual(expected);
});
});