forked from doug-martin/nestjs-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.builder.spec.ts
148 lines (131 loc) · 5.19 KB
/
where.builder.spec.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
import { getQueryTypeormMetadata } from '../../src/common';
import { WhereBuilder } from '../../src/query';
import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture';
import { TestEntity } from '../__fixtures__/test.entity';
import { getCustomFilterRegistry } from '../utils';
import { TestEntityFilter } from '../__fixtures__/types';
describe('WhereBuilder', (): void => {
beforeEach(() => createTestConnection());
afterEach(() => closeTestConnection());
const getRepo = () => getTestConnection().getRepository(TestEntity);
const getQueryBuilder = () => getRepo().createQueryBuilder();
const createWhereBuilder = () =>
new WhereBuilder<TestEntity>(getQueryTypeormMetadata(getTestConnection()), {
customFilterRegistry: getCustomFilterRegistry(getTestConnection()),
});
const expectSQLSnapshot = (filter: TestEntityFilter): void => {
const selectQueryBuilder = createWhereBuilder().build(getQueryBuilder(), filter, {}, TestEntity, 'TestEntity');
const [sql, params] = selectQueryBuilder.getQueryAndParameters();
expect(sql).toMatchSnapshot();
expect(params).toMatchSnapshot();
};
it('should accept a empty filter', (): void => {
expectSQLSnapshot({});
});
it('or multiple operators for a single field together', (): void => {
expectSQLSnapshot({ numberType: { gt: 10, lt: 20, gte: 21, lte: 31 } });
});
it('and multiple field comparisons together', (): void => {
expectSQLSnapshot({ numberType: { eq: 1 }, stringType: { like: 'foo%' }, boolType: { is: true } });
});
it('should accept custom filters alongside regular filters', (): void => {
expectSQLSnapshot({
// This has the global isMultipleOf filter
numberType: { gte: 1, lte: 10, isMultipleOf: 5 },
// Here, the isMultipleOf filter was overridden for dateType only
dateType: { isMultipleOf: 3 },
// This is a more complex filter involving geospatial queries
fakePointType: { distanceFrom: { point: { lat: 45.3, lng: 9.5 }, radius: 50000 } },
// This is a custom filter that does a subquery
pendingTestRelations: { gt: 5 },
});
});
describe('and', (): void => {
it('and multiple expressions together', (): void => {
expectSQLSnapshot({
and: [
{ numberType: { gt: 10 } },
{ numberType: { lt: 20 } },
{ numberType: { gte: 30 } },
{ numberType: { lte: 40 } },
],
});
});
it('and multiple expressions together with custom filters', (): void => {
expectSQLSnapshot({
and: [
{ numberType: { gt: 10 } },
{ numberType: { lt: 20 } },
{ numberType: { gte: 30 } },
{ numberType: { lte: 40 } },
{ numberType: { isMultipleOf: 5 } },
{ dateType: { isMultipleOf: 3 } },
{ fakePointType: { distanceFrom: { point: { lat: 45.3, lng: 9.5 }, radius: 50000 } } },
],
});
});
it('and multiple filters together with multiple fields', (): void => {
expectSQLSnapshot({
and: [
{ numberType: { gt: 10 }, stringType: { like: 'foo%' } },
{ numberType: { lt: 20 }, stringType: { like: '%bar' } },
],
});
});
it('should support nested ors', (): void => {
expectSQLSnapshot({
and: [
{ or: [{ numberType: { gt: 10 } }, { numberType: { lt: 20 } }] },
{ or: [{ numberType: { gte: 30 } }, { numberType: { lte: 40 } }] },
],
});
});
it('should properly group AND with a sibling field comparison', (): void => {
expectSQLSnapshot({ and: [{ numberType: { gt: 2 } }, { numberType: { lt: 10 } }], stringType: { eq: 'foo' } });
});
});
describe('or', (): void => {
it('or multiple expressions together', (): void => {
expectSQLSnapshot({
or: [
{ numberType: { gt: 10 } },
{ numberType: { lt: 20 } },
{ numberType: { gte: 30 } },
{ numberType: { lte: 40 } },
],
});
});
it('or multiple expressions together with custom filter', (): void => {
expectSQLSnapshot({
or: [
{ numberType: { gt: 10 } },
{ numberType: { lt: 20 } },
{ numberType: { gte: 30 } },
{ numberType: { lte: 40 } },
{ numberType: { isMultipleOf: 5 } },
{ dateType: { isMultipleOf: 3 } },
{ fakePointType: { distanceFrom: { point: { lat: 45.3, lng: 9.5 }, radius: 50000 } } },
],
});
});
it('and multiple and filters together', (): void => {
expectSQLSnapshot({
or: [
{ numberType: { gt: 10 }, stringType: { like: 'foo%' } },
{ numberType: { lt: 20 }, stringType: { like: '%bar' } },
],
});
});
it('should support nested ands', (): void => {
expectSQLSnapshot({
or: [
{ and: [{ numberType: { gt: 10 } }, { numberType: { lt: 20 } }] },
{ and: [{ numberType: { gte: 30 } }, { numberType: { lte: 40 } }] },
],
});
});
it('should properly group OR with a sibling field comparison', (): void => {
expectSQLSnapshot({ or: [{ numberType: { eq: 2 } }, { numberType: { gt: 10 } }], stringType: { eq: 'foo' } });
});
});
});