Skip to content

Commit a476fbe

Browse files
author
Agnes Lin
committed
fix: could not find a workaround for mongo replacement
1 parent a55152e commit a476fbe

File tree

2 files changed

+95
-51
lines changed

2 files changed

+95
-51
lines changed

packages/repository-tests/src/crud/relations/acceptance/has-many-inclusion-resolver.relation.acceptance.ts

+95-45
Original file line numberDiff line numberDiff line change
@@ -187,65 +187,111 @@ export function hasManyInclusionResolverAcceptance(
187187
expect(toJSON(result)).to.deepEqual(toJSON(expected));
188188
});
189189

190-
it('returns inclusions after running save() operation', async () => {
191-
// this shows save() works well with func ensurePersistable and ObjectId
192-
// the test skips for Cloudant as it needs the _rev property for replacement.
193-
// see replace-by-id.suite.ts
194-
const thor = await customerRepo.create({name: 'Thor'});
195-
const odin = await customerRepo.create({name: 'Odin'});
196-
197-
const thorOrder = await orderRepo.create({
198-
customerId: thor.id,
199-
description: 'Pizza',
200-
});
201-
202-
const pizza = await orderRepo.findById(thorOrder.id);
203-
pizza.customerId = odin.id;
204-
205-
await orderRepo.save(pizza);
206-
const odinPizza = await orderRepo.findById(thorOrder.id);
190+
skipIf(
191+
features.hasRevisionToken,
192+
it,
193+
'returns inclusions after running save() operation',
194+
async () => {
195+
// this shows save() works well with func ensurePersistable and ObjectId
196+
// the test skips for Cloudant as it needs the _rev property for replacement.
197+
// see replace-by-id.suite.ts
198+
const thor = await customerRepo.create({name: 'Thor'});
199+
const odin = await customerRepo.create({name: 'Odin'});
200+
201+
const thorOrder = await orderRepo.create({
202+
customerId: thor.id,
203+
description: 'Pizza',
204+
});
205+
206+
const pizza = await orderRepo.findById(thorOrder.id);
207+
pizza.customerId = odin.id;
208+
209+
await orderRepo.save(pizza);
210+
const odinPizza = await orderRepo.findById(thorOrder.id);
211+
212+
const result = await customerRepo.findById(odin.id, {
213+
include: [{relation: 'orders'}],
214+
});
215+
const expected = {
216+
...odin,
217+
parentId: features.emptyValue,
218+
orders: [
219+
{
220+
...odinPizza,
221+
isShipped: features.emptyValue,
222+
// eslint-disable-next-line @typescript-eslint/camelcase
223+
shipment_id: features.emptyValue,
224+
},
225+
],
226+
};
227+
expect(toJSON(result)).to.containEql(toJSON(expected));
228+
},
229+
);
207230

208-
const result = await customerRepo.findById(odin.id, {
209-
include: [{relation: 'orders'}],
210-
});
211-
const expected = {
212-
...odin,
213-
parentId: features.emptyValue,
214-
orders: [
231+
skipIf(
232+
features.hasRevisionToken,
233+
it,
234+
'returns inclusions after running replaceById() operation',
235+
async () => {
236+
// this shows replaceById() works well with func ensurePersistable and ObjectId
237+
// the test skips for Cloudant as it needs the _rev property for replacement.
238+
// see replace-by-id.suite.ts
239+
const thor = await customerRepo.create({name: 'Thor'});
240+
const odin = await customerRepo.create({name: 'Odin'});
241+
242+
const thorOrder = await orderRepo.create({
243+
customerId: thor.id,
244+
description: 'Pizza',
245+
});
246+
247+
const pizza = await orderRepo.findById(thorOrder.id.toString());
248+
pizza.customerId = odin.id;
249+
// FIXME: [mongo] if pizza obj is converted to JSON obj, it would get an error
250+
// because it tries to convert ObjectId to string type.
251+
// should test with JSON obj once it's fixed.
252+
253+
await orderRepo.replaceById(pizza.id, pizza);
254+
const odinPizza = await orderRepo.findById(thorOrder.id);
255+
256+
const result = await customerRepo.find({
257+
include: [{relation: 'orders'}],
258+
});
259+
const expected = [
215260
{
216-
...odinPizza,
217-
isShipped: features.emptyValue,
218-
// eslint-disable-next-line @typescript-eslint/camelcase
219-
shipment_id: features.emptyValue,
261+
...thor,
262+
parentId: features.emptyValue,
220263
},
221-
],
222-
};
223-
expect(toJSON(result)).to.containEql(toJSON(expected));
224-
});
264+
{
265+
...odin,
266+
parentId: features.emptyValue,
267+
orders: [
268+
{
269+
...odinPizza,
270+
isShipped: features.emptyValue,
271+
// eslint-disable-next-line @typescript-eslint/camelcase
272+
shipment_id: features.emptyValue,
273+
},
274+
],
275+
},
276+
];
277+
expect(toJSON(result)).to.deepEqual(toJSON(expected));
278+
},
279+
);
225280

226-
it('returns inclusions after running replaceById() operation', async () => {
227-
// this shows replaceById() works well with func ensurePersistable and ObjectId
228-
// the test skips for Cloudant as it needs the _rev property for replacement.
229-
// see replace-by-id.suite.ts
281+
it('returns inclusions after running updateById() operation', async () => {
230282
const thor = await customerRepo.create({name: 'Thor'});
283+
const odin = await customerRepo.create({name: 'Odin'});
231284

232285
const thorOrder = await orderRepo.create({
233286
customerId: thor.id,
234287
description: 'Pizza',
235288
});
236289

237290
const pizza = await orderRepo.findById(thorOrder.id.toString());
238-
pizza.description = 'Reheated pizza';
291+
pizza.customerId = odin.id;
239292
const reheatedPizza = toJSON(pizza);
240293

241-
// coerce the id for mongodb connector to pass the id equality check
242-
// in juggler's replaceById function
243-
const coercedId =
244-
typeof thorOrder.id === 'number'
245-
? thorOrder.id
246-
: thorOrder.id.toString();
247-
248-
await orderRepo.replaceById(coercedId, reheatedPizza);
294+
await orderRepo.updateById(pizza.id, reheatedPizza);
249295
const odinPizza = await orderRepo.findById(thorOrder.id);
250296

251297
const result = await customerRepo.find({
@@ -255,6 +301,10 @@ export function hasManyInclusionResolverAcceptance(
255301
{
256302
...thor,
257303
parentId: features.emptyValue,
304+
},
305+
{
306+
...odin,
307+
parentId: features.emptyValue,
258308
orders: [
259309
{
260310
...odinPizza,

packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts

-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export class Customer extends Entity {
3232
})
3333
name: string;
3434

35-
@property({
36-
type: 'string',
37-
required: false,
38-
})
39-
_rev?: string;
40-
4135
@hasMany(() => Order)
4236
orders: Order[];
4337

0 commit comments

Comments
 (0)