@declaro/data 2.0.0-beta.98 → 2.0.0-beta.99
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/dist/browser/index.js +7 -7
- package/dist/browser/index.js.map +3 -3
- package/dist/node/index.cjs +59 -17
- package/dist/node/index.cjs.map +3 -3
- package/dist/node/index.js +59 -17
- package/dist/node/index.js.map +3 -3
- package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts +2 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts.map +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts +2 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts.map +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts +2 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts.map +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts +2 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts.map +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts.map +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts.map +1 -1
- package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts +2 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts.map +1 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts +2 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts.map +1 -0
- package/package.json +5 -5
- package/src/test/mock/repositories/mock-memory-repository.assign.test.ts +215 -0
- package/src/test/mock/repositories/mock-memory-repository.basic.test.ts +129 -0
- package/src/test/mock/repositories/mock-memory-repository.bulk-upsert.test.ts +159 -0
- package/src/test/mock/repositories/mock-memory-repository.count.test.ts +98 -0
- package/src/test/mock/repositories/mock-memory-repository.custom-lookup.test.ts +0 -0
- package/src/test/mock/repositories/mock-memory-repository.search.test.ts +265 -0
- package/src/test/mock/repositories/mock-memory-repository.ts +67 -16
- package/src/test/mock/repositories/mock-memory-repository.upsert.test.ts +108 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.test.d.ts +0 -2
- package/dist/ts/test/mock/repositories/mock-memory-repository.test.d.ts.map +0 -1
- package/src/test/mock/repositories/mock-memory-repository.test.ts +0 -919
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'bun:test'
|
|
2
|
+
import { MockBookSchema } from '../models/mock-book-models'
|
|
3
|
+
import { MockMemoryRepository } from './mock-memory-repository'
|
|
4
|
+
|
|
5
|
+
describe('MockMemoryRepository - Count Functionality', () => {
|
|
6
|
+
const mockSchema = MockBookSchema
|
|
7
|
+
|
|
8
|
+
let repository: MockMemoryRepository<typeof mockSchema>
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
repository = new MockMemoryRepository({ schema: mockSchema })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should count all items when no filter is provided', async () => {
|
|
15
|
+
// Create test data
|
|
16
|
+
await repository.create({ title: 'Book 1', author: 'Author 1', publishedDate: new Date() })
|
|
17
|
+
await repository.create({ title: 'Book 2', author: 'Author 2', publishedDate: new Date() })
|
|
18
|
+
await repository.create({ title: 'Book 3', author: 'Author 3', publishedDate: new Date() })
|
|
19
|
+
|
|
20
|
+
const count = await repository.count({})
|
|
21
|
+
expect(count).toBe(3)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should count filtered items when filter function is provided', async () => {
|
|
25
|
+
const repositoryWithFilter = new MockMemoryRepository({
|
|
26
|
+
schema: mockSchema,
|
|
27
|
+
filter: (data, filters) => {
|
|
28
|
+
if (filters.text) {
|
|
29
|
+
return data.title.toLowerCase().includes(filters.text.toLowerCase())
|
|
30
|
+
}
|
|
31
|
+
return true
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
// Create test data
|
|
36
|
+
await repositoryWithFilter.create({ title: 'Test Book A', author: 'Author 1', publishedDate: new Date() })
|
|
37
|
+
await repositoryWithFilter.create({ title: 'Test Book B', author: 'Author 2', publishedDate: new Date() })
|
|
38
|
+
await repositoryWithFilter.create({ title: 'Other Book', author: 'Author 3', publishedDate: new Date() })
|
|
39
|
+
await repositoryWithFilter.create({ title: 'Another Book', author: 'Author 4', publishedDate: new Date() })
|
|
40
|
+
|
|
41
|
+
// Count all items
|
|
42
|
+
const totalCount = await repositoryWithFilter.count({})
|
|
43
|
+
expect(totalCount).toBe(4)
|
|
44
|
+
|
|
45
|
+
// Count filtered items
|
|
46
|
+
const filteredCount = await repositoryWithFilter.count({ text: 'Test' })
|
|
47
|
+
expect(filteredCount).toBe(2)
|
|
48
|
+
|
|
49
|
+
// Count with no matches
|
|
50
|
+
const noMatchCount = await repositoryWithFilter.count({ text: 'Ruby' })
|
|
51
|
+
expect(noMatchCount).toBe(0)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should count items correctly after CRUD operations', async () => {
|
|
55
|
+
const repositoryWithFilter = new MockMemoryRepository({
|
|
56
|
+
schema: mockSchema,
|
|
57
|
+
filter: (data, filters) => {
|
|
58
|
+
if (filters.text) {
|
|
59
|
+
return data.title.toLowerCase().includes(filters.text.toLowerCase())
|
|
60
|
+
}
|
|
61
|
+
return true
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// Initial count should be 0
|
|
66
|
+
expect(await repositoryWithFilter.count({})).toBe(0)
|
|
67
|
+
|
|
68
|
+
// Create items
|
|
69
|
+
const book1 = await repositoryWithFilter.create({
|
|
70
|
+
title: 'Test Book 1',
|
|
71
|
+
author: 'Author 1',
|
|
72
|
+
publishedDate: new Date(),
|
|
73
|
+
})
|
|
74
|
+
const book2 = await repositoryWithFilter.create({
|
|
75
|
+
title: 'Other Book',
|
|
76
|
+
author: 'Author 2',
|
|
77
|
+
publishedDate: new Date(),
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// Count after creation
|
|
81
|
+
expect(await repositoryWithFilter.count({})).toBe(2)
|
|
82
|
+
expect(await repositoryWithFilter.count({ text: 'Test' })).toBe(1)
|
|
83
|
+
|
|
84
|
+
// Remove an item
|
|
85
|
+
await repositoryWithFilter.remove({ id: book1.id })
|
|
86
|
+
|
|
87
|
+
// Count after removal
|
|
88
|
+
expect(await repositoryWithFilter.count({})).toBe(1)
|
|
89
|
+
expect(await repositoryWithFilter.count({ text: 'Test' })).toBe(0)
|
|
90
|
+
|
|
91
|
+
// Restore the item
|
|
92
|
+
await repositoryWithFilter.restore({ id: book1.id })
|
|
93
|
+
|
|
94
|
+
// Count after restore
|
|
95
|
+
expect(await repositoryWithFilter.count({})).toBe(2)
|
|
96
|
+
expect(await repositoryWithFilter.count({ text: 'Test' })).toBe(1)
|
|
97
|
+
})
|
|
98
|
+
})
|
|
File without changes
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'bun:test'
|
|
2
|
+
import { MockBookSchema } from '../models/mock-book-models'
|
|
3
|
+
import { MockMemoryRepository } from './mock-memory-repository'
|
|
4
|
+
|
|
5
|
+
describe('MockMemoryRepository - Search Functionality', () => {
|
|
6
|
+
const mockSchema = MockBookSchema
|
|
7
|
+
|
|
8
|
+
let repository: MockMemoryRepository<typeof mockSchema>
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
repository = new MockMemoryRepository({ schema: mockSchema })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should search for all items when no filter is provided', async () => {
|
|
15
|
+
const input1 = { title: 'Book One', author: 'Author A', publishedDate: new Date('2023-01-01') }
|
|
16
|
+
const input2 = { title: 'Book Two', author: 'Author B', publishedDate: new Date('2023-02-01') }
|
|
17
|
+
const input3 = { title: 'Book Three', author: 'Author C', publishedDate: new Date('2023-03-01') }
|
|
18
|
+
|
|
19
|
+
const item1 = await repository.create(input1)
|
|
20
|
+
const item2 = await repository.create(input2)
|
|
21
|
+
const item3 = await repository.create(input3)
|
|
22
|
+
|
|
23
|
+
const results = await repository.search({})
|
|
24
|
+
|
|
25
|
+
expect(results.results).toHaveLength(3)
|
|
26
|
+
expect(results.results).toEqual(expect.arrayContaining([item1, item2, item3]))
|
|
27
|
+
expect(results.pagination.total).toBe(3)
|
|
28
|
+
expect(results.pagination.page).toBe(1)
|
|
29
|
+
expect(results.pagination.pageSize).toBe(25)
|
|
30
|
+
expect(results.pagination.totalPages).toBe(1)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should filter items using custom filter function', async () => {
|
|
34
|
+
const repositoryWithFilter = new MockMemoryRepository({
|
|
35
|
+
schema: mockSchema,
|
|
36
|
+
filter: (data, filters) => {
|
|
37
|
+
if (filters.text) {
|
|
38
|
+
return (
|
|
39
|
+
data.title.toLowerCase().includes(filters.text.toLowerCase()) ||
|
|
40
|
+
data.author.toLowerCase().includes(filters.text.toLowerCase())
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
return true
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const input1 = { title: 'JavaScript Guide', author: 'John Doe', publishedDate: new Date() }
|
|
48
|
+
const input2 = { title: 'Python Handbook', author: 'Jane Smith', publishedDate: new Date() }
|
|
49
|
+
const input3 = { title: 'Java Programming', author: 'John Johnson', publishedDate: new Date() }
|
|
50
|
+
|
|
51
|
+
const item1 = await repositoryWithFilter.create(input1)
|
|
52
|
+
const item2 = await repositoryWithFilter.create(input2)
|
|
53
|
+
const item3 = await repositoryWithFilter.create(input3)
|
|
54
|
+
|
|
55
|
+
// Search by title
|
|
56
|
+
const titleResults = await repositoryWithFilter.search({ text: 'JavaScript' })
|
|
57
|
+
expect(titleResults.results).toEqual([item1])
|
|
58
|
+
|
|
59
|
+
// Search by author
|
|
60
|
+
const authorResults = await repositoryWithFilter.search({ text: 'John' })
|
|
61
|
+
expect(authorResults.results).toHaveLength(2)
|
|
62
|
+
expect(authorResults.results).toEqual(expect.arrayContaining([item1, item3]))
|
|
63
|
+
|
|
64
|
+
// Search with no matches
|
|
65
|
+
const noResults = await repositoryWithFilter.search({ text: 'Ruby' })
|
|
66
|
+
expect(noResults.results).toEqual([])
|
|
67
|
+
expect(noResults.pagination.total).toBe(0)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should handle pagination correctly', async () => {
|
|
71
|
+
// Create 10 items
|
|
72
|
+
for (let i = 1; i <= 10; i++) {
|
|
73
|
+
const input = { title: `Book ${i}`, author: `Author ${i}`, publishedDate: new Date() }
|
|
74
|
+
await repository.create(input)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Test first page with pageSize 3
|
|
78
|
+
const page1 = await repository.search(
|
|
79
|
+
{},
|
|
80
|
+
{
|
|
81
|
+
pagination: { page: 1, pageSize: 3 },
|
|
82
|
+
},
|
|
83
|
+
)
|
|
84
|
+
expect(page1.results).toHaveLength(3)
|
|
85
|
+
expect(page1.pagination.page).toBe(1)
|
|
86
|
+
expect(page1.pagination.pageSize).toBe(3)
|
|
87
|
+
expect(page1.pagination.total).toBe(10)
|
|
88
|
+
expect(page1.pagination.totalPages).toBe(4)
|
|
89
|
+
|
|
90
|
+
// Test second page
|
|
91
|
+
const page2 = await repository.search(
|
|
92
|
+
{},
|
|
93
|
+
{
|
|
94
|
+
pagination: { page: 2, pageSize: 3 },
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
expect(page2.results).toHaveLength(3)
|
|
98
|
+
expect(page2.pagination.page).toBe(2)
|
|
99
|
+
expect(page2.pagination.pageSize).toBe(3)
|
|
100
|
+
|
|
101
|
+
// Test last page (should have 1 item)
|
|
102
|
+
const page4 = await repository.search(
|
|
103
|
+
{},
|
|
104
|
+
{
|
|
105
|
+
pagination: { page: 4, pageSize: 3 },
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
expect(page4.results).toHaveLength(1)
|
|
109
|
+
expect(page4.pagination.page).toBe(4)
|
|
110
|
+
|
|
111
|
+
// Test page beyond available data
|
|
112
|
+
const pageEmpty = await repository.search(
|
|
113
|
+
{},
|
|
114
|
+
{
|
|
115
|
+
pagination: { page: 5, pageSize: 3 },
|
|
116
|
+
},
|
|
117
|
+
)
|
|
118
|
+
expect(pageEmpty.results).toHaveLength(0)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('should use default pagination when not provided', async () => {
|
|
122
|
+
// Create 30 items to test default pagination
|
|
123
|
+
for (let i = 1; i <= 30; i++) {
|
|
124
|
+
await repository.create({ title: `Book ${i}`, author: `Author ${i}`, publishedDate: new Date() })
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const results = await repository.search({})
|
|
128
|
+
expect(results.pagination.page).toBe(1)
|
|
129
|
+
expect(results.pagination.pageSize).toBe(25)
|
|
130
|
+
expect(results.pagination.total).toBe(30)
|
|
131
|
+
expect(results.pagination.totalPages).toBe(2)
|
|
132
|
+
expect(results.results).toHaveLength(25)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('should handle edge cases with pagination', async () => {
|
|
136
|
+
// Test with empty repository
|
|
137
|
+
const emptyResults = await repository.search(
|
|
138
|
+
{},
|
|
139
|
+
{
|
|
140
|
+
pagination: { page: 1, pageSize: 10 },
|
|
141
|
+
},
|
|
142
|
+
)
|
|
143
|
+
expect(emptyResults.results).toEqual([])
|
|
144
|
+
expect(emptyResults.pagination.total).toBe(0)
|
|
145
|
+
expect(emptyResults.pagination.totalPages).toBe(0)
|
|
146
|
+
|
|
147
|
+
// Test with page 0 (results in empty due to slice calculation)
|
|
148
|
+
await repository.create({ title: 'Test Book', author: 'Test Author', publishedDate: new Date() })
|
|
149
|
+
const page0Results = await repository.search(
|
|
150
|
+
{},
|
|
151
|
+
{
|
|
152
|
+
pagination: { page: 0, pageSize: 10 },
|
|
153
|
+
},
|
|
154
|
+
)
|
|
155
|
+
expect(page0Results.pagination.page).toBe(0)
|
|
156
|
+
expect(page0Results.results).toHaveLength(0) // slice(-10, 0) returns empty array
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('should handle sorting correctly', async () => {
|
|
160
|
+
const input1 = { title: 'C Book', author: 'Author Z', publishedDate: new Date('2023-03-01') }
|
|
161
|
+
const input2 = { title: 'A Book', author: 'Author Y', publishedDate: new Date('2023-01-01') }
|
|
162
|
+
const input3 = { title: 'B Book', author: 'Author X', publishedDate: new Date('2023-02-01') }
|
|
163
|
+
|
|
164
|
+
const item1 = await repository.create(input1)
|
|
165
|
+
const item2 = await repository.create(input2)
|
|
166
|
+
const item3 = await repository.create(input3)
|
|
167
|
+
|
|
168
|
+
// Sort by title ascending
|
|
169
|
+
const titleAscResults = await repository.search(
|
|
170
|
+
{},
|
|
171
|
+
{
|
|
172
|
+
sort: [{ title: 'asc' }],
|
|
173
|
+
},
|
|
174
|
+
)
|
|
175
|
+
expect(titleAscResults.results.map((r) => r.title)).toEqual(['A Book', 'B Book', 'C Book'])
|
|
176
|
+
|
|
177
|
+
// Sort by title descending
|
|
178
|
+
const titleDescResults = await repository.search(
|
|
179
|
+
{},
|
|
180
|
+
{
|
|
181
|
+
sort: [{ title: 'desc' }],
|
|
182
|
+
},
|
|
183
|
+
)
|
|
184
|
+
expect(titleDescResults.results.map((r) => r.title)).toEqual(['C Book', 'B Book', 'A Book'])
|
|
185
|
+
|
|
186
|
+
// Sort by author ascending
|
|
187
|
+
const authorAscResults = await repository.search(
|
|
188
|
+
{},
|
|
189
|
+
{
|
|
190
|
+
sort: [{ author: 'asc' }],
|
|
191
|
+
},
|
|
192
|
+
)
|
|
193
|
+
expect(authorAscResults.results.map((r) => r.author)).toEqual(['Author X', 'Author Y', 'Author Z'])
|
|
194
|
+
|
|
195
|
+
// Multiple field sort: title asc, then author desc
|
|
196
|
+
const multiSortResults = await repository.search(
|
|
197
|
+
{},
|
|
198
|
+
{
|
|
199
|
+
sort: [{ title: 'asc' }, { author: 'desc' }],
|
|
200
|
+
},
|
|
201
|
+
)
|
|
202
|
+
expect(multiSortResults.results.map((r) => r.title)).toEqual(['A Book', 'B Book', 'C Book'])
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('should handle sorting with pagination', async () => {
|
|
206
|
+
for (let i = 1; i <= 5; i++) {
|
|
207
|
+
await repository.create({
|
|
208
|
+
title: `Book ${String.fromCharCode(69 - i)}`, // D, C, B, A, @
|
|
209
|
+
author: `Author ${i}`,
|
|
210
|
+
publishedDate: new Date(),
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Sort by title ascending and get first page
|
|
215
|
+
const sortedPage1 = await repository.search(
|
|
216
|
+
{},
|
|
217
|
+
{
|
|
218
|
+
sort: [{ title: 'asc' }],
|
|
219
|
+
pagination: { page: 1, pageSize: 2 },
|
|
220
|
+
},
|
|
221
|
+
)
|
|
222
|
+
expect(sortedPage1.results.map((r) => r.title)).toEqual(['Book @', 'Book A'])
|
|
223
|
+
expect(sortedPage1.pagination.totalPages).toBe(3)
|
|
224
|
+
|
|
225
|
+
// Get second page with same sort
|
|
226
|
+
const sortedPage2 = await repository.search(
|
|
227
|
+
{},
|
|
228
|
+
{
|
|
229
|
+
sort: [{ title: 'asc' }],
|
|
230
|
+
pagination: { page: 2, pageSize: 2 },
|
|
231
|
+
},
|
|
232
|
+
)
|
|
233
|
+
expect(sortedPage2.results.map((r) => r.title)).toEqual(['Book B', 'Book C'])
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('should handle combined filtering, sorting, and pagination', async () => {
|
|
237
|
+
const repositoryWithFilter = new MockMemoryRepository({
|
|
238
|
+
schema: mockSchema,
|
|
239
|
+
filter: (data, filters) => {
|
|
240
|
+
if (filters.text) {
|
|
241
|
+
return data.title.toLowerCase().includes(filters.text.toLowerCase())
|
|
242
|
+
}
|
|
243
|
+
return true
|
|
244
|
+
},
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
await repositoryWithFilter.create({ title: 'Test Z Book', author: 'Author 1', publishedDate: new Date() })
|
|
248
|
+
await repositoryWithFilter.create({ title: 'Test A Book', author: 'Author 2', publishedDate: new Date() })
|
|
249
|
+
await repositoryWithFilter.create({ title: 'Other Book', author: 'Author 3', publishedDate: new Date() })
|
|
250
|
+
await repositoryWithFilter.create({ title: 'Test B Book', author: 'Author 4', publishedDate: new Date() })
|
|
251
|
+
|
|
252
|
+
const results = await repositoryWithFilter.search(
|
|
253
|
+
{ text: 'Test' },
|
|
254
|
+
{
|
|
255
|
+
sort: [{ title: 'asc' }],
|
|
256
|
+
pagination: { page: 1, pageSize: 2 },
|
|
257
|
+
},
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
expect(results.results).toHaveLength(2)
|
|
261
|
+
expect(results.results.map((r) => r.title)).toEqual(['Test A Book', 'Test B Book'])
|
|
262
|
+
expect(results.pagination.total).toBe(3) // 3 "Test" books total
|
|
263
|
+
expect(results.pagination.totalPages).toBe(2)
|
|
264
|
+
})
|
|
265
|
+
})
|
|
@@ -53,8 +53,20 @@ export class MockMemoryRepository<TSchema extends AnyModelSchema> implements IRe
|
|
|
53
53
|
throw new Error('Primary key is not defined in the schema metadata')
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
const
|
|
57
|
-
|
|
56
|
+
const results: InferDetail<TSchema>[] = []
|
|
57
|
+
for (const input of inputs) {
|
|
58
|
+
let item: InferDetail<TSchema> | undefined
|
|
59
|
+
if (typeof this.args.lookup === 'function') {
|
|
60
|
+
item = Array.from(this.data.values()).find((data) => this.args.lookup!(data, input))
|
|
61
|
+
} else {
|
|
62
|
+
// Default lookup by primary key
|
|
63
|
+
item = this.data.get(input[this.entityMetadata.primaryKey!])
|
|
64
|
+
}
|
|
65
|
+
if (item) {
|
|
66
|
+
results.push(item)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return results
|
|
58
70
|
}
|
|
59
71
|
async search(
|
|
60
72
|
input: InferFilters<TSchema>,
|
|
@@ -107,14 +119,27 @@ export class MockMemoryRepository<TSchema extends AnyModelSchema> implements IRe
|
|
|
107
119
|
throw new Error('Primary key is not defined in the schema metadata')
|
|
108
120
|
}
|
|
109
121
|
|
|
110
|
-
|
|
122
|
+
let item: InferDetail<TSchema> | undefined
|
|
123
|
+
let itemKey: string
|
|
124
|
+
|
|
125
|
+
if (typeof this.args.lookup === 'function') {
|
|
126
|
+
item = Array.from(this.data.values()).find((data) => this.args.lookup!(data, lookup))
|
|
127
|
+
if (item) {
|
|
128
|
+
itemKey = item[this.entityMetadata.primaryKey!]
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
// Default lookup by primary key
|
|
132
|
+
itemKey = lookup[this.entityMetadata.primaryKey]
|
|
133
|
+
item = this.data.get(itemKey)
|
|
134
|
+
}
|
|
135
|
+
|
|
111
136
|
if (!item) {
|
|
112
137
|
throw new Error('Item not found')
|
|
113
138
|
}
|
|
114
139
|
// Move the item to trash
|
|
115
|
-
this.trash.set(
|
|
140
|
+
this.trash.set(itemKey!, item)
|
|
116
141
|
// Remove the item from data
|
|
117
|
-
this.data.delete(
|
|
142
|
+
this.data.delete(itemKey!)
|
|
118
143
|
return item
|
|
119
144
|
}
|
|
120
145
|
async restore(lookup: InferLookup<TSchema>): Promise<InferSummary<TSchema>> {
|
|
@@ -122,12 +147,25 @@ export class MockMemoryRepository<TSchema extends AnyModelSchema> implements IRe
|
|
|
122
147
|
throw new Error('Primary key is not defined in the schema metadata')
|
|
123
148
|
}
|
|
124
149
|
|
|
125
|
-
|
|
150
|
+
let item: InferDetail<TSchema> | undefined
|
|
151
|
+
let itemKey: string
|
|
152
|
+
|
|
153
|
+
if (typeof this.args.lookup === 'function') {
|
|
154
|
+
item = Array.from(this.trash.values()).find((data) => this.args.lookup!(data, lookup))
|
|
155
|
+
if (item) {
|
|
156
|
+
itemKey = item[this.entityMetadata.primaryKey!]
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
// Default lookup by primary key
|
|
160
|
+
itemKey = lookup[this.entityMetadata.primaryKey]
|
|
161
|
+
item = this.trash.get(itemKey)
|
|
162
|
+
}
|
|
163
|
+
|
|
126
164
|
if (!item) {
|
|
127
165
|
throw new Error('Item not found in trash')
|
|
128
166
|
}
|
|
129
|
-
this.trash.delete(
|
|
130
|
-
this.data.set(
|
|
167
|
+
this.trash.delete(itemKey!)
|
|
168
|
+
this.data.set(itemKey!, item)
|
|
131
169
|
return item
|
|
132
170
|
}
|
|
133
171
|
|
|
@@ -157,18 +195,31 @@ export class MockMemoryRepository<TSchema extends AnyModelSchema> implements IRe
|
|
|
157
195
|
if (!this.entityMetadata?.primaryKey) {
|
|
158
196
|
throw new Error('Primary key is not defined in the schema metadata')
|
|
159
197
|
}
|
|
160
|
-
const primaryKeyValue = lookup[this.entityMetadata.primaryKey]
|
|
161
|
-
if (!primaryKeyValue) {
|
|
162
|
-
throw new Error('Primary key value must be provided')
|
|
163
|
-
}
|
|
164
198
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
199
|
+
let existingItem: InferDetail<TSchema> | undefined
|
|
200
|
+
let itemKey: string
|
|
201
|
+
|
|
202
|
+
if (typeof this.args.lookup === 'function') {
|
|
203
|
+
existingItem = Array.from(this.data.values()).find((data) => this.args.lookup!(data, lookup))
|
|
204
|
+
if (existingItem) {
|
|
205
|
+
itemKey = existingItem[this.entityMetadata.primaryKey!]
|
|
206
|
+
} else {
|
|
207
|
+
throw new Error('Item not found')
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
// Default lookup by primary key
|
|
211
|
+
itemKey = lookup[this.entityMetadata.primaryKey]
|
|
212
|
+
if (!itemKey) {
|
|
213
|
+
throw new Error('Primary key value must be provided')
|
|
214
|
+
}
|
|
215
|
+
existingItem = this.data.get(itemKey)
|
|
216
|
+
if (!existingItem) {
|
|
217
|
+
throw new Error('Item not found')
|
|
218
|
+
}
|
|
168
219
|
}
|
|
169
220
|
|
|
170
221
|
const updatedItem = this.assignInput(existingItem, input)
|
|
171
|
-
this.data.set(
|
|
222
|
+
this.data.set(itemKey!, updatedItem)
|
|
172
223
|
return updatedItem
|
|
173
224
|
}
|
|
174
225
|
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'bun:test'
|
|
2
|
+
import { MockBookSchema } from '../models/mock-book-models'
|
|
3
|
+
import { MockMemoryRepository } from './mock-memory-repository'
|
|
4
|
+
|
|
5
|
+
describe('MockMemoryRepository - Upsert Functionality', () => {
|
|
6
|
+
const mockSchema = MockBookSchema
|
|
7
|
+
|
|
8
|
+
let repository: MockMemoryRepository<typeof mockSchema>
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
repository = new MockMemoryRepository({ schema: mockSchema })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should create a new item when no existing item with primary key exists', async () => {
|
|
15
|
+
const input = { id: 42, title: 'New Book', author: 'Author Name', publishedDate: new Date() }
|
|
16
|
+
|
|
17
|
+
const upsertedItem = await repository.upsert(input)
|
|
18
|
+
|
|
19
|
+
expect(upsertedItem).toEqual(input)
|
|
20
|
+
expect(await repository.load({ id: 42 })).toEqual(input)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should update an existing item when primary key matches', async () => {
|
|
24
|
+
// Create initial item
|
|
25
|
+
const initial = {
|
|
26
|
+
id: 42,
|
|
27
|
+
title: 'Original Book',
|
|
28
|
+
author: 'Original Author',
|
|
29
|
+
publishedDate: new Date('2023-01-01'),
|
|
30
|
+
}
|
|
31
|
+
await repository.create(initial)
|
|
32
|
+
|
|
33
|
+
// Upsert with same ID but different data
|
|
34
|
+
const update = {
|
|
35
|
+
id: 42,
|
|
36
|
+
title: 'Updated Book',
|
|
37
|
+
author: 'Updated Author',
|
|
38
|
+
publishedDate: new Date('2023-12-01'),
|
|
39
|
+
}
|
|
40
|
+
const upsertedItem = await repository.upsert(update)
|
|
41
|
+
|
|
42
|
+
expect(upsertedItem).toEqual(update)
|
|
43
|
+
expect(await repository.load({ id: 42 })).toEqual(update)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should generate primary key when upserting without one', async () => {
|
|
47
|
+
const input = { title: 'Book Without ID', author: 'Author Name', publishedDate: new Date() }
|
|
48
|
+
|
|
49
|
+
const upsertedItem = await repository.upsert(input)
|
|
50
|
+
|
|
51
|
+
expect(upsertedItem.id).toBeDefined()
|
|
52
|
+
expect(upsertedItem.title).toBe(input.title)
|
|
53
|
+
expect(upsertedItem.author).toBe(input.author)
|
|
54
|
+
expect(await repository.load({ id: upsertedItem.id })).toEqual(upsertedItem)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('should merge with existing item properties when updating', async () => {
|
|
58
|
+
// Create initial item with multiple properties
|
|
59
|
+
const initial = {
|
|
60
|
+
id: 42,
|
|
61
|
+
title: 'Original Book',
|
|
62
|
+
author: 'Original Author',
|
|
63
|
+
publishedDate: new Date('2023-01-01'),
|
|
64
|
+
}
|
|
65
|
+
await repository.create(initial)
|
|
66
|
+
|
|
67
|
+
// Upsert with partial update (only title) - need to provide required fields
|
|
68
|
+
const partialUpdate = {
|
|
69
|
+
id: 42,
|
|
70
|
+
title: 'Updated Title',
|
|
71
|
+
author: 'Original Author', // Keep original
|
|
72
|
+
publishedDate: new Date('2023-01-01'), // Keep original
|
|
73
|
+
}
|
|
74
|
+
const upsertedItem = await repository.upsert(partialUpdate)
|
|
75
|
+
|
|
76
|
+
// Should have updated title but kept other properties
|
|
77
|
+
expect(upsertedItem.id).toBe(42)
|
|
78
|
+
expect(upsertedItem.title).toBe('Updated Title')
|
|
79
|
+
expect(upsertedItem.author).toBe('Original Author')
|
|
80
|
+
expect(upsertedItem.publishedDate).toEqual(initial.publishedDate)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('should handle upsert with null/undefined primary key', async () => {
|
|
84
|
+
const input = {
|
|
85
|
+
id: undefined,
|
|
86
|
+
title: 'Book With Undefined ID',
|
|
87
|
+
author: 'Author Name',
|
|
88
|
+
publishedDate: new Date(),
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const upsertedItem = await repository.upsert(input)
|
|
92
|
+
|
|
93
|
+
expect(upsertedItem.id).toBeDefined()
|
|
94
|
+
expect(typeof upsertedItem.id).toBe('number')
|
|
95
|
+
expect(upsertedItem.title).toBe(input.title)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('should increment auto-generated IDs correctly', async () => {
|
|
99
|
+
const input1 = { title: 'Book 1', author: 'Author 1', publishedDate: new Date() }
|
|
100
|
+
const input2 = { title: 'Book 2', author: 'Author 2', publishedDate: new Date() }
|
|
101
|
+
|
|
102
|
+
const item1 = await repository.upsert(input1)
|
|
103
|
+
const item2 = await repository.upsert(input2)
|
|
104
|
+
|
|
105
|
+
expect(item1.id).toBe(1)
|
|
106
|
+
expect(item2.id).toBe(2)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mock-memory-repository.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.test.ts"],"names":[],"mappings":""}
|