|
1
|
import { ModulesSdkTypes } from "@medusajs/framework/types" |
|
2
|
import { toMikroORMEntity } from "@medusajs/framework/utils" |
|
3
|
import { IndexData } from "@models" |
|
4
|
import { buildSchemaObjectRepresentation, Configuration } from "@utils" |
|
5
|
import { baseGraphqlSchema } from "../../src/utils/base-graphql-schema" |
|
6
|
import { DataSynchronizer } from "../../src/services/data-synchronizer" |
|
7
|
import { |
|
8
|
createIndexTestBed, |
|
9
|
IndexModuleTestBed, |
|
10
|
schema, |
|
11
|
} from "../__fixtures__" |
|
12
|
import { updateRemovedSchema } from "../__fixtures__/update-removed-schema" |
|
13
|
import { updatedSchema } from "../__fixtures__/updated-schema" |
|
14
|
|
|
15
|
jest.setTimeout(300000) |
|
16
|
|
|
17
|
const buildRepresentation = (moduleSchema: string) => { |
|
18
|
return buildSchemaObjectRepresentation(baseGraphqlSchema + moduleSchema) |
|
19
|
.objectRepresentation |
|
20
|
} |
|
21
|
|
|
22
|
describe("IndexModuleService syncIndexConfig", function () { |
|
23
|
let testBed: IndexModuleTestBed |
|
24
|
let indexMetadataService: ModulesSdkTypes.IMedusaInternalService<any> |
|
25
|
let indexSyncService: ModulesSdkTypes.IMedusaInternalService<any> |
|
26
|
let dataSynchronizer: DataSynchronizer |
|
27
|
|
|
28
|
beforeAll(async () => { |
|
29
|
// Starting the module in worker mode runs the initial configuration |
|
30
|
// sync against the configured schema |
|
31
|
testBed = await createIndexTestBed({ schema }) |
|
32
|
|
|
33
|
indexMetadataService = testBed.container.resolve("indexMetadataService") |
|
34
|
indexSyncService = testBed.container.resolve("indexSyncService") |
|
35
|
dataSynchronizer = testBed.container.resolve("dataSynchronizer") |
|
36
|
}) |
|
37
|
|
|
38
|
afterAll(async () => { |
|
39
|
await testBed.teardown() |
|
40
|
}) |
|
41
|
|
|
42
|
it("should full sync all entities when the config has changed", async () => { |
|
43
|
const currentMetadata = await indexMetadataService.list() |
|
44
|
|
|
45
|
expect(currentMetadata).toHaveLength(7) |
|
46
|
expect(currentMetadata).toEqual( |
|
47
|
expect.arrayContaining([ |
|
48
|
expect.objectContaining({ |
|
49
|
entity: "InternalObject", |
|
50
|
fields: "b", |
|
51
|
status: "done", |
|
52
|
}), |
|
53
|
expect.objectContaining({ |
|
54
|
entity: "Product", |
|
55
|
fields: "created_at,id,title", |
|
56
|
status: "done", |
|
57
|
}), |
|
58
|
expect.objectContaining({ |
|
59
|
entity: "InternalNested", |
|
60
|
fields: "a", |
|
61
|
status: "done", |
|
62
|
}), |
|
63
|
expect.objectContaining({ |
|
64
|
entity: "PriceSet", |
|
65
|
fields: "id", |
|
66
|
status: "done", |
|
67
|
}), |
|
68
|
expect.objectContaining({ |
|
69
|
entity: "Price", |
|
70
|
fields: "amount,price_set.id", |
|
71
|
status: "done", |
|
72
|
}), |
|
73
|
expect.objectContaining({ |
|
74
|
entity: "ProductVariant", |
|
75
|
fields: "id,product.id,product_id,sku", |
|
76
|
status: "done", |
|
77
|
}), |
|
78
|
expect.objectContaining({ |
|
79
|
entity: "LinkProductVariantPriceSet", |
|
80
|
fields: "id,price_set_id,variant_id", |
|
81
|
status: "done", |
|
82
|
}), |
|
83
|
]) |
|
84
|
) |
|
85
|
|
|
86
|
let indexSync = await indexSyncService.list({ |
|
87
|
last_key: null, |
|
88
|
}) |
|
89
|
expect(indexSync).toHaveLength(7) |
|
90
|
|
|
91
|
// The module restarts with an updated schema config |
|
92
|
let configurationChecker = new Configuration({ |
|
93
|
logger: testBed.logger.asLogger(), |
|
94
|
schemaObjectRepresentation: buildRepresentation(updatedSchema), |
|
95
|
indexMetadataService, |
|
96
|
indexSyncService, |
|
97
|
dataSynchronizer, |
|
98
|
}) |
|
99
|
|
|
100
|
const syncRequired = await configurationChecker.checkChanges() |
|
101
|
|
|
102
|
expect(syncRequired).toHaveLength(2) |
|
103
|
expect(syncRequired).toEqual( |
|
104
|
expect.arrayContaining([ |
|
105
|
expect.objectContaining({ |
|
106
|
entity: "Product", |
|
107
|
fields: "handle,id,title", |
|
108
|
status: "pending", |
|
109
|
}), |
|
110
|
expect.objectContaining({ |
|
111
|
entity: "Price", |
|
112
|
fields: "amount,currency_code,price_set.id", |
|
113
|
status: "pending", |
|
114
|
}), |
|
115
|
]) |
|
116
|
) |
|
117
|
|
|
118
|
indexSync = await indexSyncService.list({ |
|
119
|
last_key: null, |
|
120
|
}) |
|
121
|
expect(indexSync).toHaveLength(7) |
|
122
|
|
|
123
|
const updatedMetadata = await indexMetadataService.list() |
|
124
|
|
|
125
|
expect(updatedMetadata).toHaveLength(7) |
|
126
|
expect(updatedMetadata).toEqual( |
|
127
|
expect.arrayContaining([ |
|
128
|
expect.objectContaining({ |
|
129
|
entity: "InternalObject", |
|
130
|
fields: "b", |
|
131
|
status: "done", |
|
132
|
}), |
|
133
|
expect.objectContaining({ |
|
134
|
entity: "Product", |
|
135
|
fields: "handle,id,title", |
|
136
|
status: "pending", |
|
137
|
}), |
|
138
|
expect.objectContaining({ |
|
139
|
entity: "InternalNested", |
|
140
|
fields: "a", |
|
141
|
status: "done", |
|
142
|
}), |
|
143
|
expect.objectContaining({ |
|
144
|
entity: "PriceSet", |
|
145
|
fields: "id", |
|
146
|
status: "done", |
|
147
|
}), |
|
148
|
expect.objectContaining({ |
|
149
|
entity: "Price", |
|
150
|
fields: "amount,currency_code,price_set.id", |
|
151
|
status: "pending", |
|
152
|
}), |
|
153
|
expect.objectContaining({ |
|
154
|
entity: "ProductVariant", |
|
155
|
fields: "id,product.id,product_id,sku", |
|
156
|
status: "done", |
|
157
|
}), |
|
158
|
expect.objectContaining({ |
|
159
|
entity: "LinkProductVariantPriceSet", |
|
160
|
fields: "id,price_set_id,variant_id", |
|
161
|
status: "done", |
|
162
|
}), |
|
163
|
]) |
|
164
|
) |
|
165
|
|
|
166
|
await dataSynchronizer.syncEntities(syncRequired) |
|
167
|
|
|
168
|
// Seed an index entry for an entity that is about to be removed from |
|
169
|
// the schema to assert its data is cleaned up |
|
170
|
const manager = testBed.forkManager() |
|
171
|
await manager |
|
172
|
.getRepository(toMikroORMEntity(IndexData)) |
|
173
|
.upsertMany([ |
|
174
|
{ id: "price_set_1", name: "PriceSet", data: { id: "price_set_1" } }, |
|
175
|
]) |
|
176
|
|
|
177
|
// Sync again removing entities not linked |
|
178
|
configurationChecker = new Configuration({ |
|
179
|
logger: testBed.logger.asLogger(), |
|
180
|
schemaObjectRepresentation: buildRepresentation(updateRemovedSchema), |
|
181
|
indexMetadataService, |
|
182
|
indexSyncService, |
|
183
|
dataSynchronizer, |
|
184
|
}) |
|
185
|
|
|
186
|
const syncRequired2 = await configurationChecker.checkChanges() |
|
187
|
expect(syncRequired2).toHaveLength(1) |
|
188
|
expect(syncRequired2).toEqual( |
|
189
|
expect.arrayContaining([ |
|
190
|
expect.objectContaining({ |
|
191
|
entity: "ProductVariant", |
|
192
|
fields: "description,id,product.id,product_id,sku", |
|
193
|
status: "pending", |
|
194
|
}), |
|
195
|
]) |
|
196
|
) |
|
197
|
|
|
198
|
const updatedMetadata2 = await indexMetadataService.list() |
|
199
|
expect(updatedMetadata2).toHaveLength(2) |
|
200
|
expect(updatedMetadata2).toEqual( |
|
201
|
expect.arrayContaining([ |
|
202
|
expect.objectContaining({ |
|
203
|
entity: "Product", |
|
204
|
fields: "handle,id,title", |
|
205
|
status: "done", |
|
206
|
}), |
|
207
|
expect.objectContaining({ |
|
208
|
entity: "ProductVariant", |
|
209
|
fields: "description,id,product.id,product_id,sku", |
|
210
|
status: "pending", |
|
211
|
}), |
|
212
|
]) |
|
213
|
) |
|
214
|
|
|
215
|
// The index data of the removed entities has been deleted |
|
216
|
const priceSetEntries = await testBed |
|
217
|
.forkManager() |
|
218
|
.find(toMikroORMEntity(IndexData), { name: "PriceSet" }) |
|
219
|
expect(priceSetEntries).toHaveLength(0) |
|
220
|
}) |
|
221
|
}) |