@koalarx/nest-cli 3.0.41 → 3.0.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { CreatePersonHandler } from '@/application/person/create/create-person.handler'
|
|
2
|
+
import { ReadManyPersonHandler } from '@/application/person/read-many/read-many-person.handler'
|
|
3
|
+
import { ReadPersonHandler } from '@/application/person/read/read-person.handler'
|
|
4
|
+
import { createUnitTestApp } from '@/test/create-unit-test-app'
|
|
5
|
+
import { PaginationDto } from '@koalarx/nest/core/dtos/pagination.dto'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Testes E2E do RepositoryBase - Lazy Loading
|
|
9
|
+
*
|
|
10
|
+
* Valida o carregamento efetivo de relacionamentos em diferentes cenários:
|
|
11
|
+
* - findById: carrega recursivamente todos os relacionamentos
|
|
12
|
+
* - findFirst/findUnique: carrega recursivamente todos os relacionamentos
|
|
13
|
+
* - findMany: carrega com otimização para listas
|
|
14
|
+
*/
|
|
15
|
+
describe('RepositoryBase - Lazy Loading (E2E)', () => {
|
|
16
|
+
const app = createUnitTestApp()
|
|
17
|
+
|
|
18
|
+
describe('findById - Carregamento de Relacionamentos', () => {
|
|
19
|
+
it('should load person with all relationships using findById', async () => {
|
|
20
|
+
// Criar uma pessoa com endereço e telefones
|
|
21
|
+
const createHandler = app.get(CreatePersonHandler)
|
|
22
|
+
const createResult = await createHandler.handle({
|
|
23
|
+
name: 'João Silva',
|
|
24
|
+
phones: [{ phone: '11999999999' }, { phone: '11888888888' }],
|
|
25
|
+
address: {
|
|
26
|
+
address: 'Rua Principal, 123 - São Paulo',
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
expect(createResult.isOk()).toBeTruthy()
|
|
31
|
+
|
|
32
|
+
if (!createResult.isOk()) {
|
|
33
|
+
throw new Error('Falha ao criar pessoa')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const personId = createResult.value.id
|
|
37
|
+
|
|
38
|
+
// Buscar a pessoa com findById
|
|
39
|
+
const readHandler = app.get(ReadPersonHandler)
|
|
40
|
+
const readResult = await readHandler.handle(personId)
|
|
41
|
+
|
|
42
|
+
expect(readResult.isOk()).toBeTruthy()
|
|
43
|
+
|
|
44
|
+
if (!readResult.isOk()) {
|
|
45
|
+
throw new Error('Falha ao ler pessoa')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const person = readResult.value
|
|
49
|
+
|
|
50
|
+
// Validar entidade raiz
|
|
51
|
+
expect(person).toBeDefined()
|
|
52
|
+
expect(person.id).toBe(personId)
|
|
53
|
+
expect(person.name).toBe('João Silva')
|
|
54
|
+
|
|
55
|
+
// Validar relacionamentos carregados
|
|
56
|
+
expect(person.address).toBeDefined()
|
|
57
|
+
expect(person.address.address).toBe('Rua Principal, 123 - São Paulo')
|
|
58
|
+
|
|
59
|
+
expect(person.phones).toBeDefined()
|
|
60
|
+
const isPhonesList =
|
|
61
|
+
person.phones &&
|
|
62
|
+
typeof person.phones === 'object' &&
|
|
63
|
+
'toArray' in person.phones
|
|
64
|
+
expect(Array.isArray(person.phones) || isPhonesList).toBe(true)
|
|
65
|
+
|
|
66
|
+
// Validar conteúdo dos telefones
|
|
67
|
+
const phonesArray = Array.isArray(person.phones)
|
|
68
|
+
? person.phones
|
|
69
|
+
: isPhonesList
|
|
70
|
+
? (person.phones as any).toArray('all')
|
|
71
|
+
: []
|
|
72
|
+
expect(phonesArray.length).toBe(2)
|
|
73
|
+
expect(phonesArray.map((p: any) => p.phone).sort()).toEqual([
|
|
74
|
+
'11888888888',
|
|
75
|
+
'11999999999',
|
|
76
|
+
])
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('should have all relationships loaded and accessible', async () => {
|
|
80
|
+
const createHandler = app.get(CreatePersonHandler)
|
|
81
|
+
const createResult = await createHandler.handle({
|
|
82
|
+
name: 'Test Person',
|
|
83
|
+
phones: [{ phone: '21987654321' }],
|
|
84
|
+
address: {
|
|
85
|
+
address: 'Test Address',
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
expect(createResult.isOk()).toBeTruthy()
|
|
90
|
+
|
|
91
|
+
if (!createResult.isOk()) {
|
|
92
|
+
throw new Error('Falha ao criar pessoa')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const personId = createResult.value.id
|
|
96
|
+
|
|
97
|
+
// Buscar com findById
|
|
98
|
+
const readHandler = app.get(ReadPersonHandler)
|
|
99
|
+
const readResult = await readHandler.handle(personId)
|
|
100
|
+
|
|
101
|
+
expect(readResult.isOk()).toBeTruthy()
|
|
102
|
+
|
|
103
|
+
if (!readResult.isOk()) {
|
|
104
|
+
throw new Error('Falha ao ler pessoa')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const person = readResult.value
|
|
108
|
+
|
|
109
|
+
// Validar que relacionamentos foram carregados
|
|
110
|
+
expect(person.id).toBe(personId)
|
|
111
|
+
expect(person.address).toBeDefined()
|
|
112
|
+
expect(person.address.address).toBeDefined()
|
|
113
|
+
expect(person.phones).toBeDefined()
|
|
114
|
+
|
|
115
|
+
// Validar acesso aos dados
|
|
116
|
+
const phonesArray = Array.isArray(person.phones)
|
|
117
|
+
? person.phones
|
|
118
|
+
: 'toArray' in (person.phones || {})
|
|
119
|
+
? (person.phones as any).toArray('all')
|
|
120
|
+
: []
|
|
121
|
+
expect(phonesArray.length).toBeGreaterThan(0)
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
describe('findMany - Carregamento Otimizado para Listas', () => {
|
|
126
|
+
it('should load persons with relationships in findMany', async () => {
|
|
127
|
+
const createHandler = app.get(CreatePersonHandler)
|
|
128
|
+
|
|
129
|
+
for (let i = 0; i < 2; i++) {
|
|
130
|
+
await createHandler.handle({
|
|
131
|
+
name: `Pessoa ${i + 1}`,
|
|
132
|
+
phones: [{ phone: `1199999999${i}` }],
|
|
133
|
+
address: {
|
|
134
|
+
address: `Rua ${i + 1}, 100`,
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const readManyHandler = app.get(ReadManyPersonHandler)
|
|
140
|
+
const readManyResult = await readManyHandler.handle(new PaginationDto())
|
|
141
|
+
|
|
142
|
+
expect(readManyResult.isOk()).toBeTruthy()
|
|
143
|
+
|
|
144
|
+
if (!readManyResult.isOk()) {
|
|
145
|
+
throw new Error('Falha ao ler pessoas')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const { items } = readManyResult.value
|
|
149
|
+
|
|
150
|
+
expect(Array.isArray(items)).toBe(true)
|
|
151
|
+
expect(items.length).toBeGreaterThan(0)
|
|
152
|
+
|
|
153
|
+
// Validar estrutura básica
|
|
154
|
+
const person = items[0]
|
|
155
|
+
expect(person.id).toBeDefined()
|
|
156
|
+
expect(person.name).toBeDefined()
|
|
157
|
+
expect(person.address).toBeDefined()
|
|
158
|
+
expect(person.phones).toBeDefined()
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('should load multiple persons efficiently', async () => {
|
|
162
|
+
const createHandler = app.get(CreatePersonHandler)
|
|
163
|
+
|
|
164
|
+
const createdIds: number[] = []
|
|
165
|
+
for (let i = 0; i < 3; i++) {
|
|
166
|
+
const result = await createHandler.handle({
|
|
167
|
+
name: `ListPerson ${i + 1}`,
|
|
168
|
+
phones: [{ phone: `2188888888${i}` }],
|
|
169
|
+
address: {
|
|
170
|
+
address: `Rua Lista ${i + 1}`,
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
if (result.isOk()) {
|
|
175
|
+
createdIds.push(result.value.id)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
expect(createdIds.length).toBe(3)
|
|
180
|
+
|
|
181
|
+
const readManyHandler = app.get(ReadManyPersonHandler)
|
|
182
|
+
const readManyResult = await readManyHandler.handle(new PaginationDto())
|
|
183
|
+
|
|
184
|
+
expect(readManyResult.isOk()).toBeTruthy()
|
|
185
|
+
|
|
186
|
+
if (!readManyResult.isOk()) {
|
|
187
|
+
throw new Error('Falha ao ler pessoas')
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const { items, count } = readManyResult.value
|
|
191
|
+
|
|
192
|
+
expect(items.length).toBeGreaterThan(0)
|
|
193
|
+
expect(count).toBeGreaterThan(0)
|
|
194
|
+
|
|
195
|
+
// Validar que criados estão na lista
|
|
196
|
+
const returnedIds = items.map((p) => p.id)
|
|
197
|
+
createdIds.forEach((id) => {
|
|
198
|
+
expect(returnedIds).toContain(id)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
// Cada pessoa deve ter dados básicos
|
|
202
|
+
items.forEach((person) => {
|
|
203
|
+
expect(person.id).toBeDefined()
|
|
204
|
+
expect(person.name).toBeDefined()
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe('Comparação entre findById e findMany', () => {
|
|
210
|
+
it('should load relationships in both methods', async () => {
|
|
211
|
+
const createHandler = app.get(CreatePersonHandler)
|
|
212
|
+
const createResult = await createHandler.handle({
|
|
213
|
+
name: 'Comparação Test',
|
|
214
|
+
phones: [{ phone: '3122222222' }, { phone: '3133333333' }],
|
|
215
|
+
address: {
|
|
216
|
+
address: 'Rua Comparação, 789',
|
|
217
|
+
},
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
expect(createResult.isOk()).toBeTruthy()
|
|
221
|
+
|
|
222
|
+
if (!createResult.isOk()) {
|
|
223
|
+
throw new Error('Falha ao criar pessoa')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const personId = createResult.value.id
|
|
227
|
+
|
|
228
|
+
// Buscar com findById
|
|
229
|
+
const readHandler = app.get(ReadPersonHandler)
|
|
230
|
+
const findByIdResult = await readHandler.handle(personId)
|
|
231
|
+
|
|
232
|
+
expect(findByIdResult.isOk()).toBeTruthy()
|
|
233
|
+
|
|
234
|
+
if (!findByIdResult.isOk()) {
|
|
235
|
+
throw new Error('Falha ao ler pessoa com findById')
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const personFromFindById = findByIdResult.value
|
|
239
|
+
|
|
240
|
+
// Buscar com findMany
|
|
241
|
+
const readManyHandler = app.get(ReadManyPersonHandler)
|
|
242
|
+
const findManyResult = await readManyHandler.handle(new PaginationDto())
|
|
243
|
+
|
|
244
|
+
expect(findManyResult.isOk()).toBeTruthy()
|
|
245
|
+
|
|
246
|
+
if (!findManyResult.isOk()) {
|
|
247
|
+
throw new Error('Falha ao ler pessoas com findMany')
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const personFromFindMany = findManyResult.value.items.find(
|
|
251
|
+
(p) => p.id === personId,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
expect(personFromFindMany).toBeDefined()
|
|
255
|
+
|
|
256
|
+
// Ambas devem ter a entidade raiz
|
|
257
|
+
expect(personFromFindById.id).toBe(personFromFindMany?.id)
|
|
258
|
+
expect(personFromFindById.name).toBe(personFromFindMany?.name)
|
|
259
|
+
|
|
260
|
+
// findById deve ter relacionamentos carregados
|
|
261
|
+
expect(personFromFindById.address).toBeDefined()
|
|
262
|
+
expect(personFromFindById.phones).toBeDefined()
|
|
263
|
+
const findByIdPhonesArray = Array.isArray(personFromFindById.phones)
|
|
264
|
+
? personFromFindById.phones
|
|
265
|
+
: 'toArray' in (personFromFindById.phones || {})
|
|
266
|
+
? (personFromFindById.phones as any).toArray('all')
|
|
267
|
+
: []
|
|
268
|
+
expect(findByIdPhonesArray.length).toBeGreaterThan(0)
|
|
269
|
+
|
|
270
|
+
// findMany também deve ter relacionamentos
|
|
271
|
+
expect(personFromFindMany?.address).toBeDefined()
|
|
272
|
+
expect(personFromFindMany?.phones).toBeDefined()
|
|
273
|
+
|
|
274
|
+
// Ambas estratégias carregam relacionamentos efetivamente
|
|
275
|
+
const findManyPhonesArray = Array.isArray(personFromFindMany?.phones)
|
|
276
|
+
? personFromFindMany?.phones
|
|
277
|
+
: personFromFindMany?.phones && 'toArray' in personFromFindMany.phones
|
|
278
|
+
? (personFromFindMany.phones as any).toArray('all')
|
|
279
|
+
: []
|
|
280
|
+
expect(findManyPhonesArray.length).toBeGreaterThan(0)
|
|
281
|
+
expect(findByIdPhonesArray.length).toBeGreaterThan(0)
|
|
282
|
+
})
|
|
283
|
+
})
|
|
284
|
+
})
|