/
query.test.ts
227 lines (190 loc) · 8.14 KB
/
query.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
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
import { assert, describe, expect, test } from 'vitest'
import { createPipelineFetcher } from '../../../src/runtime/query/match/pipeline'
import { createQuery } from '../../../src/runtime/query/query'
import database from './db.json'
const shuffledDatabase: Array<any> = [...database].sort(() => Math.random() - 0.5)
const pipelineFetcher = createPipelineFetcher(() => Promise.resolve(shuffledDatabase))
describe('Database Provider', () => {
test('Matches nested exact match', async () => {
const result: Array<any> = await createQuery(pipelineFetcher)
.where({ 'nested.users.0': 'Ahad' })
.find()
assert(result.length > 0)
assert(result.every(item => item.nested.users.includes('Ahad')) === true)
})
test('Matches nested with operator', async () => {
// $contains
const result: Array<any> = await createQuery(pipelineFetcher)
.where({
'nested.users': {
$contains: 'Pooya'
}
})
.find()
assert(result.length > 0)
assert(result.every(item => item.nested.users.includes('Pooya')) === true)
})
test('Apply limit', async () => {
const first3 = await createQuery(pipelineFetcher).limit(3).find()
assert(first3.length === 3)
})
test('Apply skip', async () => {
const first3 = await createQuery(pipelineFetcher).limit(3).find()
const limit3skip2 = await createQuery(pipelineFetcher).skip(2).limit(3).find()
assert(limit3skip2[0].id === first3[2].id)
})
test('Apply sort', async () => {
const nameAsc = await createQuery(pipelineFetcher).sort({ name: 1 }).find()
assert(nameAsc[0].name === database[0].name)
const nameDesc = await createQuery(pipelineFetcher).sort({ name: -1 }).find()
assert(nameDesc[0].name === database[database.length - 1].name)
})
test('Apply sort and skip', async () => {
const nameAsc = await createQuery(pipelineFetcher).sort({ name: 1 }).skip(2).find()
assert(nameAsc[0].name === database[2].name)
})
test('Apply sort $sensitivity', async () => {
const textOrder = ['aab', 'aaB', 'aAb', 'aAB', 'Aab', 'AaB', 'AAb', 'AAB']
const sensitivityCase = await createQuery(pipelineFetcher).sort({ text: 1, $sensitivity: 'case' }).find()
textOrder.forEach((text, index) => {
expect(sensitivityCase[index].text).toBe(text)
})
})
test('Apply sort $caseFirst', async () => {
const textOrder = ['aab', 'aaB', 'aAb', 'aAB', 'Aab', 'AaB', 'AAb', 'AAB']
const caseLower = await createQuery(pipelineFetcher).sort({ text: 1, $caseFirst: 'lower' }).find()
textOrder.forEach((text, index) => {
expect(caseLower[index].text).toBe(text)
})
// upper case first
const caseUpper = await createQuery(pipelineFetcher).sort({ text: 1, $caseFirst: 'upper' }).find()
textOrder.reverse().forEach((text, index) => {
expect(caseUpper[index].text).toBe(text)
})
})
test('Apply sort $numeric', async () => {
// sort string alphabetically
const nonNumericSort = await createQuery(pipelineFetcher).sort({ numberString: 1 }).find()
const nonNumericOrder = [1, 10, 100, 2, 20, 3, 30, 4]
nonNumericOrder.forEach((number, index) => {
expect(nonNumericSort[index].numberString).toBe(String(number))
})
// sort string numerically
const numericSort = await createQuery(pipelineFetcher).sort({ numberString: 1, $numeric: true }).find()
const numericOrder = [1, 2, 3, 4, 10, 20, 30, 100]
numericOrder.forEach((number, index) => {
expect(numericSort[index].numberString).toBe(String(number))
})
})
test('Sort Nullable fields', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, name: 'Saman' }, { id: 2, name: 'Ebi' }, { id: 3, name: 'Narges' }, { id: 4, name: null }] as any[]))
const result = await createQuery(fetcher)
.sort({ name: 1 })
.find()
assert(result[0].name === 'Ebi')
assert(result[1].name === 'Narges')
assert(result[2].name === 'Saman')
assert(result[3].name === null)
})
test('Apply $in', async () => {
const result = await createQuery(pipelineFetcher)
.where({
category: { $in: 'c1' }
})
.find()
assert(result.every((item: any) => item.category === 'c1') === true)
})
test('Apply $in array', async () => {
const result = await createQuery(pipelineFetcher)
.where({
category: { $in: ['c1', 'c3'] }
})
.find()
assert(result.every((item: any) => ['c1', 'c3'].includes(item.category)) === true)
})
test('Apply $or + $in', async () => {
const result = await createQuery(pipelineFetcher)
.where({
$or: [{ category: { $in: 'c1' } }, { category: { $in: 'c2' } }]
})
.find()
assert(result.every((item: any) => ['c1', 'c2'].includes(item.category)) === true)
})
test('Apply $contains string', async () => {
const result = await createQuery(pipelineFetcher)
.where({
quote: { $contains: ['best', 'way'] }
})
.find()
assert(result.every((item: any) => item.quote.includes('way')) === true)
})
test('Apply $containsAny string', async () => {
const result = await createQuery(pipelineFetcher)
.where({
author: { $containsAny: ['Wilson', 'William'] }
})
.find()
assert(result.every((item: any) => item.author.includes('William')) === false)
assert(result.every((item: any) => item.author.includes('Wilson')) === false)
assert(result.every((item: any) => item.author.includes('Wilson') || item.author.includes('William')) === true)
})
test('Surround with path (default)', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.findSurround('/b')
assert(result[0]._path === '/a')
assert(result[1]._path === '/c')
})
test('Surround more that 1 item with path', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.findSurround('/b', { before: 2, after: 1 })
assert((result as Array<any>).length === 3)
assert(result[0] === null)
assert(result[1]._path === '/a')
assert(result[2]._path === '/c')
})
test('Surround with object', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.findSurround({ id: 3 }, { before: 2, after: 1 })
assert((result as Array<any>).length === 3)
assert(result[0]._path === '/a')
assert(result[1]._path === '/b')
assert(result[2] === null)
})
test('Chain multiple where conditions', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, path: '/a' }, { id: 2, path: '/b' }, { id: 3, path: '/c' }] as any[]))
const query = createQuery(fetcher).where({ id: { $in: [1, 2] } })
const singleWhereResult = await query.find()
assert((singleWhereResult as Array<any>).length === 2)
assert(singleWhereResult[0].path === '/a')
assert(singleWhereResult[1].path === '/b')
// Test with second condition
const doubleWhereResult = await query.where({ path: { $contains: 'b' } }).find()
assert((doubleWhereResult as Array<any>).length === 1)
assert(doubleWhereResult[0].path === '/b')
})
test('Select specific keys', async () => {
const query = createQuery(pipelineFetcher)
.where({ id: { $in: [1, 2] } })
.only(['name', 'id', '_'])
const result = await query.find()
expect(result.length).toBeGreaterThan(0)
result.forEach((item) => {
expect(Object.keys(item)).toMatchObject(['id', 'name', '_deleted'])
})
})
test('Drop specific keys', async () => {
const query = createQuery(pipelineFetcher)
.where({ id: { $in: [1, 2] } })
.without(['name', '_'])
const result = await query.find()
expect(result.length).toBeGreaterThan(0)
result.forEach((item) => {
expect(item.id).toBeDefined()
expect(item.name).toBeUndefined()
expect(item._deleted).toBeUndefined()
})
})
})