@declaro/data 2.0.0-beta.97 → 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 +8 -8
- package/dist/browser/index.js.map +4 -4
- package/dist/node/index.cjs +78 -29
- package/dist/node/index.cjs.map +4 -4
- package/dist/node/index.js +78 -29
- package/dist/node/index.js.map +4 -4
- package/dist/ts/domain/services/model-service.d.ts +8 -0
- package/dist/ts/domain/services/model-service.d.ts.map +1 -1
- 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/domain/services/model-service.test.ts +245 -0
- package/src/domain/services/model-service.ts +37 -14
- 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,215 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, mock } from 'bun:test'
|
|
2
|
+
import { MockBookSchema } from '../models/mock-book-models'
|
|
3
|
+
import { MockMemoryRepository } from './mock-memory-repository'
|
|
4
|
+
|
|
5
|
+
describe('MockMemoryRepository - Assign 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 use default Object.assign for create when no custom assign function is provided', async () => {
|
|
15
|
+
const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
16
|
+
const createdItem = await repository.create(input)
|
|
17
|
+
|
|
18
|
+
expect(createdItem).toMatchObject(input)
|
|
19
|
+
expect(createdItem.id).toBeDefined()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should use default Object.assign for update when no custom assign function is provided', async () => {
|
|
23
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
24
|
+
const createdItem = await repository.create(input)
|
|
25
|
+
|
|
26
|
+
const updateInput = { title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
|
|
27
|
+
const updatedItem = await repository.update({ id: createdItem.id }, updateInput)
|
|
28
|
+
|
|
29
|
+
expect(updatedItem).toEqual({
|
|
30
|
+
id: createdItem.id,
|
|
31
|
+
title: 'Updated Book',
|
|
32
|
+
author: 'Updated Author',
|
|
33
|
+
publishedDate: updateInput.publishedDate,
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should use default Object.assign for upsert when no custom assign function is provided', async () => {
|
|
38
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
39
|
+
const upsertedItem = await repository.upsert(input)
|
|
40
|
+
|
|
41
|
+
expect(upsertedItem).toEqual(input)
|
|
42
|
+
|
|
43
|
+
const updateInput = { id: 42, title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
|
|
44
|
+
const updatedItem = await repository.upsert(updateInput)
|
|
45
|
+
|
|
46
|
+
expect(updatedItem).toEqual(updateInput)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('should use custom assign function for create operation', async () => {
|
|
50
|
+
const customAssignMock = mock((existing: any, input: any) => ({
|
|
51
|
+
...existing,
|
|
52
|
+
...input,
|
|
53
|
+
customField: 'custom_create_value',
|
|
54
|
+
}))
|
|
55
|
+
|
|
56
|
+
const customRepository = new MockMemoryRepository({
|
|
57
|
+
schema: mockSchema,
|
|
58
|
+
assign: customAssignMock,
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
62
|
+
const createdItem = await customRepository.create(input)
|
|
63
|
+
|
|
64
|
+
expect(customAssignMock).toHaveBeenCalledWith({}, input)
|
|
65
|
+
expect((createdItem as any).customField).toBe('custom_create_value')
|
|
66
|
+
expect(createdItem).toMatchObject(input)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('should use custom assign function for update operation', async () => {
|
|
70
|
+
const customAssignMock = mock((existing: any, input: any) => ({
|
|
71
|
+
...existing,
|
|
72
|
+
...input,
|
|
73
|
+
customField: 'custom_update_value',
|
|
74
|
+
lastModified: new Date('2023-01-01'),
|
|
75
|
+
}))
|
|
76
|
+
|
|
77
|
+
const customRepository = new MockMemoryRepository({
|
|
78
|
+
schema: mockSchema,
|
|
79
|
+
assign: customAssignMock,
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
83
|
+
const createdItem = await customRepository.create(input)
|
|
84
|
+
|
|
85
|
+
const updateInput = { title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
|
|
86
|
+
const updatedItem = await customRepository.update({ id: createdItem.id }, updateInput)
|
|
87
|
+
|
|
88
|
+
expect(customAssignMock).toHaveBeenCalledWith(createdItem, updateInput)
|
|
89
|
+
expect((updatedItem as any).customField).toBe('custom_update_value')
|
|
90
|
+
expect((updatedItem as any).lastModified).toEqual(new Date('2023-01-01'))
|
|
91
|
+
expect(updatedItem.title).toBe('Updated Book')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('should use custom assign function for upsert operation on existing item', async () => {
|
|
95
|
+
const customAssignMock = mock((existing: any, input: any) => ({
|
|
96
|
+
...existing,
|
|
97
|
+
...input,
|
|
98
|
+
mergeTimestamp: new Date('2023-01-01'),
|
|
99
|
+
isUpserted: true,
|
|
100
|
+
}))
|
|
101
|
+
|
|
102
|
+
const customRepository = new MockMemoryRepository({
|
|
103
|
+
schema: mockSchema,
|
|
104
|
+
assign: customAssignMock,
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
108
|
+
const createdItem = await customRepository.create(input)
|
|
109
|
+
|
|
110
|
+
const upsertInput = { id: 42, title: 'Upserted Book', author: 'Upserted Author', publishedDate: new Date() }
|
|
111
|
+
const upsertedItem = await customRepository.upsert(upsertInput)
|
|
112
|
+
|
|
113
|
+
// Should have been called twice: once for create, once for upsert
|
|
114
|
+
expect(customAssignMock).toHaveBeenCalledTimes(2)
|
|
115
|
+
expect(customAssignMock).toHaveBeenLastCalledWith(createdItem, upsertInput)
|
|
116
|
+
expect((upsertedItem as any).mergeTimestamp).toEqual(new Date('2023-01-01'))
|
|
117
|
+
expect((upsertedItem as any).isUpserted).toBe(true)
|
|
118
|
+
expect(upsertedItem.title).toBe('Upserted Book')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('should use custom assign function for upsert operation on new item', async () => {
|
|
122
|
+
const customAssignMock = mock((existing: any, input: any) => ({
|
|
123
|
+
...existing,
|
|
124
|
+
...input,
|
|
125
|
+
createdViaUpsert: true,
|
|
126
|
+
}))
|
|
127
|
+
|
|
128
|
+
const customRepository = new MockMemoryRepository({
|
|
129
|
+
schema: mockSchema,
|
|
130
|
+
assign: customAssignMock,
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
134
|
+
const upsertedItem = await customRepository.upsert(input)
|
|
135
|
+
|
|
136
|
+
expect(customAssignMock).toHaveBeenCalledWith({}, input)
|
|
137
|
+
expect((upsertedItem as any).createdViaUpsert).toBe(true)
|
|
138
|
+
expect(upsertedItem).toMatchObject(input)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('should use custom assign function for bulkUpsert operation', async () => {
|
|
142
|
+
const customAssignMock = mock((existing: any, input: any) => ({
|
|
143
|
+
...existing,
|
|
144
|
+
...input,
|
|
145
|
+
bulkProcessed: true,
|
|
146
|
+
}))
|
|
147
|
+
|
|
148
|
+
const customRepository = new MockMemoryRepository({
|
|
149
|
+
schema: mockSchema,
|
|
150
|
+
assign: customAssignMock,
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const inputs = [
|
|
154
|
+
{ id: 1, title: 'Book 1', author: 'Author 1', publishedDate: new Date() },
|
|
155
|
+
{ id: 2, title: 'Book 2', author: 'Author 2', publishedDate: new Date() },
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
const results = await customRepository.bulkUpsert(inputs)
|
|
159
|
+
|
|
160
|
+
expect(customAssignMock).toHaveBeenCalledTimes(2)
|
|
161
|
+
expect((results[0] as any).bulkProcessed).toBe(true)
|
|
162
|
+
expect((results[1] as any).bulkProcessed).toBe(true)
|
|
163
|
+
expect(results[0]).toMatchObject(inputs[0])
|
|
164
|
+
expect(results[1]).toMatchObject(inputs[1])
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('should handle complex custom assign logic with conditional merging', async () => {
|
|
168
|
+
const customAssign = (existing: any, input: any) => {
|
|
169
|
+
const result = { ...existing, ...input }
|
|
170
|
+
|
|
171
|
+
// Custom logic: preserve original author if input doesn't have one
|
|
172
|
+
if (!input.author && existing.author) {
|
|
173
|
+
result.author = existing.author
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Custom logic: track modification count
|
|
177
|
+
result.modificationCount = (existing.modificationCount || 0) + 1
|
|
178
|
+
|
|
179
|
+
return result
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const customRepository = new MockMemoryRepository({
|
|
183
|
+
schema: mockSchema,
|
|
184
|
+
assign: customAssign,
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
const input = { id: 42, title: 'Test Book', author: 'Original Author', publishedDate: new Date() }
|
|
188
|
+
const createdItem = await customRepository.create(input)
|
|
189
|
+
|
|
190
|
+
expect((createdItem as any).modificationCount).toBe(1)
|
|
191
|
+
|
|
192
|
+
// Update with partial data - use existing values for required fields
|
|
193
|
+
const updateInput = {
|
|
194
|
+
title: 'Updated Book',
|
|
195
|
+
author: createdItem.author,
|
|
196
|
+
publishedDate: createdItem.publishedDate,
|
|
197
|
+
}
|
|
198
|
+
const updatedItem = await customRepository.update({ id: createdItem.id }, updateInput)
|
|
199
|
+
|
|
200
|
+
expect(updatedItem.author).toBe('Original Author')
|
|
201
|
+
expect(updatedItem.title).toBe('Updated Book')
|
|
202
|
+
expect((updatedItem as any).modificationCount).toBe(2)
|
|
203
|
+
|
|
204
|
+
// Update with new author
|
|
205
|
+
const updateWithAuthor = {
|
|
206
|
+
title: updatedItem.title,
|
|
207
|
+
author: 'New Author',
|
|
208
|
+
publishedDate: updatedItem.publishedDate,
|
|
209
|
+
}
|
|
210
|
+
const finalItem = await customRepository.update({ id: createdItem.id }, updateWithAuthor)
|
|
211
|
+
|
|
212
|
+
expect(finalItem.author).toBe('New Author')
|
|
213
|
+
expect((finalItem as any).modificationCount).toBe(3)
|
|
214
|
+
})
|
|
215
|
+
})
|
|
@@ -0,0 +1,129 @@
|
|
|
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
|
+
import { z } from 'zod/v4'
|
|
5
|
+
import { ZodModel } from '@declaro/zod'
|
|
6
|
+
|
|
7
|
+
describe('MockMemoryRepository - Basic Operations', () => {
|
|
8
|
+
const mockSchema = MockBookSchema
|
|
9
|
+
|
|
10
|
+
let repository: MockMemoryRepository<typeof mockSchema>
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
repository = new MockMemoryRepository({ schema: mockSchema })
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should create an item', async () => {
|
|
17
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
18
|
+
const createdItem = await repository.create(input)
|
|
19
|
+
|
|
20
|
+
expect(createdItem).toEqual(input)
|
|
21
|
+
expect(await repository.load({ id: createdItem.id })).toEqual(createdItem)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should throw an error when creating an item with duplicate primary key', async () => {
|
|
25
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
26
|
+
const createdItem = await repository.create(input)
|
|
27
|
+
|
|
28
|
+
await expect(repository.create({ ...input, id: createdItem.id })).rejects.toThrow(
|
|
29
|
+
'Item with the same primary key already exists',
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should update an existing item', async () => {
|
|
34
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
35
|
+
const createdItem = await repository.create(input)
|
|
36
|
+
|
|
37
|
+
const updatedInput = { title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
|
|
38
|
+
const updatedItem = await repository.update({ id: createdItem.id }, updatedInput)
|
|
39
|
+
|
|
40
|
+
expect(updatedItem).toEqual({ id: createdItem.id, ...updatedInput })
|
|
41
|
+
expect(await repository.load({ id: createdItem.id })).toEqual({ id: createdItem.id, ...updatedInput })
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should throw an error when updating a non-existent item', async () => {
|
|
45
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
46
|
+
|
|
47
|
+
await expect(repository.update({ id: 999 }, input)).rejects.toThrow('Item not found')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('should remove an item', async () => {
|
|
51
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
52
|
+
const createdItem = await repository.create(input)
|
|
53
|
+
|
|
54
|
+
const removedItem = await repository.remove({ id: createdItem.id })
|
|
55
|
+
|
|
56
|
+
expect(removedItem).toEqual(input)
|
|
57
|
+
const loadedItem = await repository.load({ id: createdItem.id })
|
|
58
|
+
expect(loadedItem).toBeNull()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should restore a removed item', async () => {
|
|
62
|
+
const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
63
|
+
const createdItem = await repository.create(input)
|
|
64
|
+
await repository.remove({ id: createdItem.id })
|
|
65
|
+
|
|
66
|
+
const restoredItem = await repository.restore({ id: createdItem.id })
|
|
67
|
+
|
|
68
|
+
expect(restoredItem).toEqual(input)
|
|
69
|
+
expect(await repository.load({ id: createdItem.id })).toEqual(input)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('should throw an error when restoring a non-existent item', async () => {
|
|
73
|
+
await expect(repository.restore({ id: 999 })).rejects.toThrow('Item not found in trash')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should allow me to create an item without a primary key', async () => {
|
|
77
|
+
const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
78
|
+
const createdItem = await repository.create(input)
|
|
79
|
+
|
|
80
|
+
expect(createdItem.id).toBeDefined()
|
|
81
|
+
expect(createdItem.title).toBe(input.title)
|
|
82
|
+
expect(createdItem.author).toBe(input.author)
|
|
83
|
+
expect(await repository.load({ id: createdItem.id })).toEqual(createdItem)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('should increment ids when creating items without a primary key', async () => {
|
|
87
|
+
const input1 = { title: 'Test Book 1', author: 'Author Name 1', publishedDate: new Date() }
|
|
88
|
+
const input2 = { title: 'Test Book 2', author: 'Author Name 2', publishedDate: new Date() }
|
|
89
|
+
|
|
90
|
+
const createdItem1 = await repository.create(input1)
|
|
91
|
+
const createdItem2 = await repository.create(input2)
|
|
92
|
+
|
|
93
|
+
expect(createdItem1.id).toBe(1)
|
|
94
|
+
expect(createdItem2.id).toBe(2)
|
|
95
|
+
expect(await repository.load({ id: createdItem1.id })).toEqual(createdItem1)
|
|
96
|
+
expect(await repository.load({ id: createdItem2.id })).toEqual(createdItem2)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('should return null when loading a non-existent item', async () => {
|
|
100
|
+
const result = await repository.load({ id: 999 })
|
|
101
|
+
expect(result).toBeNull()
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('should be able to load items from a custom filter', async () => {
|
|
105
|
+
// Creating a hypothetical schema with a custom filter, and a title lookup attribute
|
|
106
|
+
const repository = new MockMemoryRepository({
|
|
107
|
+
schema: mockSchema.custom({
|
|
108
|
+
lookup: (h) =>
|
|
109
|
+
new ZodModel(
|
|
110
|
+
h.name,
|
|
111
|
+
z.object({
|
|
112
|
+
id: z.number().optional(),
|
|
113
|
+
title: z.string().optional(),
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
}),
|
|
117
|
+
lookup: (data, lookup) =>
|
|
118
|
+
data.id === lookup.id || data.title?.toLowerCase() === lookup.title?.toLowerCase(),
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
|
|
122
|
+
const createdItem = await repository.create(input)
|
|
123
|
+
|
|
124
|
+
const loadedItem = await repository.load({ id: createdItem.id })
|
|
125
|
+
const titleLoadedItem = await repository.load({ title: createdItem.title })
|
|
126
|
+
expect(loadedItem).toEqual(createdItem)
|
|
127
|
+
expect(titleLoadedItem).toEqual(createdItem)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
@@ -0,0 +1,159 @@
|
|
|
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 - Bulk 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 upsert multiple new items', async () => {
|
|
15
|
+
const inputs = [
|
|
16
|
+
{ id: 1, title: 'Book 1', author: 'Author 1', publishedDate: new Date('2023-01-01') },
|
|
17
|
+
{ id: 2, title: 'Book 2', author: 'Author 2', publishedDate: new Date('2023-02-01') },
|
|
18
|
+
{ id: 3, title: 'Book 3', author: 'Author 3', publishedDate: new Date('2023-03-01') },
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
const upsertedItems = await repository.bulkUpsert(inputs)
|
|
22
|
+
|
|
23
|
+
expect(upsertedItems).toHaveLength(3)
|
|
24
|
+
expect(upsertedItems).toEqual(inputs)
|
|
25
|
+
|
|
26
|
+
// Verify all items were created
|
|
27
|
+
for (const input of inputs) {
|
|
28
|
+
expect(await repository.load({ id: input.id })).toEqual(input)
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should update existing items and create new ones in same operation', async () => {
|
|
33
|
+
// Create some initial items
|
|
34
|
+
const existing1 = {
|
|
35
|
+
id: 1,
|
|
36
|
+
title: 'Original Book 1',
|
|
37
|
+
author: 'Original Author 1',
|
|
38
|
+
publishedDate: new Date('2023-01-01'),
|
|
39
|
+
}
|
|
40
|
+
const existing2 = {
|
|
41
|
+
id: 2,
|
|
42
|
+
title: 'Original Book 2',
|
|
43
|
+
author: 'Original Author 2',
|
|
44
|
+
publishedDate: new Date('2023-02-01'),
|
|
45
|
+
}
|
|
46
|
+
await repository.create(existing1)
|
|
47
|
+
await repository.create(existing2)
|
|
48
|
+
|
|
49
|
+
// Bulk upsert with mix of updates and new items
|
|
50
|
+
const upsertInputs = [
|
|
51
|
+
{ id: 1, title: 'Updated Book 1', author: 'Updated Author 1', publishedDate: new Date('2023-06-01') }, // Update
|
|
52
|
+
{ id: 2, title: 'Updated Book 2', author: 'Updated Author 2', publishedDate: new Date('2023-07-01') }, // Update
|
|
53
|
+
{ id: 3, title: 'New Book 3', author: 'New Author 3', publishedDate: new Date('2023-08-01') }, // Create
|
|
54
|
+
{ id: 4, title: 'New Book 4', author: 'New Author 4', publishedDate: new Date('2023-09-01') }, // Create
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
const upsertedItems = await repository.bulkUpsert(upsertInputs)
|
|
58
|
+
|
|
59
|
+
expect(upsertedItems).toHaveLength(4)
|
|
60
|
+
expect(upsertedItems).toEqual(upsertInputs)
|
|
61
|
+
|
|
62
|
+
// Verify all items have the updated/new values
|
|
63
|
+
for (const input of upsertInputs) {
|
|
64
|
+
expect(await repository.load({ id: input.id })).toEqual(input)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should handle bulk upsert with items without primary keys', async () => {
|
|
69
|
+
const inputs = [
|
|
70
|
+
{ title: 'Book Without ID 1', author: 'Author 1', publishedDate: new Date() },
|
|
71
|
+
{ title: 'Book Without ID 2', author: 'Author 2', publishedDate: new Date() },
|
|
72
|
+
{ title: 'Book Without ID 3', author: 'Author 3', publishedDate: new Date() },
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
const upsertedItems = await repository.bulkUpsert(inputs)
|
|
76
|
+
|
|
77
|
+
expect(upsertedItems).toHaveLength(3)
|
|
78
|
+
|
|
79
|
+
// All items should have generated IDs
|
|
80
|
+
expect(upsertedItems[0].id).toBe(1)
|
|
81
|
+
expect(upsertedItems[1].id).toBe(2)
|
|
82
|
+
expect(upsertedItems[2].id).toBe(3)
|
|
83
|
+
|
|
84
|
+
// Verify content is preserved
|
|
85
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
86
|
+
expect(upsertedItems[i].title).toBe(inputs[i].title)
|
|
87
|
+
expect(upsertedItems[i].author).toBe(inputs[i].author)
|
|
88
|
+
expect(await repository.load({ id: upsertedItems[i].id })).toEqual(upsertedItems[i])
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('should handle empty bulk upsert', async () => {
|
|
93
|
+
const upsertedItems = await repository.bulkUpsert([])
|
|
94
|
+
|
|
95
|
+
expect(upsertedItems).toEqual([])
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('should handle bulk upsert with partial updates', async () => {
|
|
99
|
+
// Create initial items
|
|
100
|
+
const initial1 = {
|
|
101
|
+
id: 1,
|
|
102
|
+
title: 'Original Book 1',
|
|
103
|
+
author: 'Original Author 1',
|
|
104
|
+
publishedDate: new Date('2023-01-01'),
|
|
105
|
+
}
|
|
106
|
+
const initial2 = {
|
|
107
|
+
id: 2,
|
|
108
|
+
title: 'Original Book 2',
|
|
109
|
+
author: 'Original Author 2',
|
|
110
|
+
publishedDate: new Date('2023-02-01'),
|
|
111
|
+
}
|
|
112
|
+
await repository.create(initial1)
|
|
113
|
+
await repository.create(initial2)
|
|
114
|
+
|
|
115
|
+
// Updates with all required fields but only changing title
|
|
116
|
+
const partialUpdates = [
|
|
117
|
+
{ id: 1, title: 'Updated Title 1', author: 'Original Author 1', publishedDate: new Date('2023-01-01') },
|
|
118
|
+
{ id: 2, title: 'Updated Title 2', author: 'Original Author 2', publishedDate: new Date('2023-02-01') },
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
const upsertedItems = await repository.bulkUpsert(partialUpdates)
|
|
122
|
+
|
|
123
|
+
expect(upsertedItems).toHaveLength(2)
|
|
124
|
+
|
|
125
|
+
// Should have updated titles but kept other properties
|
|
126
|
+
expect(upsertedItems[0].title).toBe('Updated Title 1')
|
|
127
|
+
expect(upsertedItems[0].author).toBe('Original Author 1')
|
|
128
|
+
expect(upsertedItems[0].publishedDate).toEqual(initial1.publishedDate)
|
|
129
|
+
|
|
130
|
+
expect(upsertedItems[1].title).toBe('Updated Title 2')
|
|
131
|
+
expect(upsertedItems[1].author).toBe('Original Author 2')
|
|
132
|
+
expect(upsertedItems[1].publishedDate).toEqual(initial2.publishedDate)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('should handle large bulk operations efficiently', async () => {
|
|
136
|
+
// Create a large number of items to test performance
|
|
137
|
+
const inputs: Array<{ id: number; title: string; author: string; publishedDate: Date }> = []
|
|
138
|
+
for (let i = 1; i <= 100; i++) {
|
|
139
|
+
inputs.push({
|
|
140
|
+
id: i,
|
|
141
|
+
title: `Book ${i}`,
|
|
142
|
+
author: `Author ${i}`,
|
|
143
|
+
publishedDate: new Date(`2023-${String((i % 12) + 1).padStart(2, '0')}-01`),
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const startTime = Date.now()
|
|
148
|
+
const upsertedItems = await repository.bulkUpsert(inputs)
|
|
149
|
+
const endTime = Date.now()
|
|
150
|
+
|
|
151
|
+
expect(upsertedItems).toHaveLength(100)
|
|
152
|
+
expect(endTime - startTime).toBeLessThan(1000) // Should complete in under 1 second
|
|
153
|
+
|
|
154
|
+
// Verify a few random items
|
|
155
|
+
expect(await repository.load({ id: 1 })).toEqual(inputs[0])
|
|
156
|
+
expect(await repository.load({ id: 50 })).toEqual(inputs[49])
|
|
157
|
+
expect(await repository.load({ id: 100 })).toEqual(inputs[99])
|
|
158
|
+
})
|
|
159
|
+
})
|
|
@@ -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
|