Skip to content

Commit

Permalink
fix: modify customer model
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Dec 2, 2019
1 parent f136c61 commit 4311a04
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,66 @@ export function hasManyInclusionResolverAcceptance(
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

skipIf(
features.hasRevisionToken,
it,
'returns inclusions after running save() operation',
async () => {
// this shows save() works well with func ensurePersistable and ObjectId
// the test skips for Cloudant as it needs the _rev property for replacement.
// see replace-by-id.suite.ts
const thor = await customerRepo.create({name: 'Thor'});
const odin = await customerRepo.create({name: 'Odin'});

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

const pizza = await orderRepo.findById(thorOrder.id);
pizza.customerId = odin.id;

await orderRepo.save(pizza);
const odinPizza = await orderRepo.findById(thorOrder.id);

const result = await customerRepo.findById(odin.id, {
include: [{relation: 'orders'}],
});
const expected = {
...odin,
it('returns inclusions after running save() operation', async () => {
// this shows save() works well with func ensurePersistable and ObjectId
// the test skips for Cloudant as it needs the _rev property for replacement.
// see replace-by-id.suite.ts
const thor = await customerRepo.create({name: 'Thor'});
const odin = await customerRepo.create({name: 'Odin'});

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

const pizza = await orderRepo.findById(thorOrder.id);
pizza.customerId = odin.id;

await orderRepo.save(pizza);
const odinPizza = await orderRepo.findById(thorOrder.id);

const result = await customerRepo.findById(odin.id, {
include: [{relation: 'orders'}],
});
const expected = {
...odin,
parentId: features.emptyValue,
orders: [
{
...odinPizza,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
},
],
};
expect(toJSON(result)).to.containEql(toJSON(expected));
});

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

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

const pizza = await orderRepo.findById(thorOrder.id.toString());
const reheatedPizza = {...pizza};
reheatedPizza.description = 'Reheated pizza';

await orderRepo.replaceById(thorOrder.id, reheatedPizza);
const odinPizza = await orderRepo.findById(thorOrder.id);

const result = await customerRepo.find({
include: [{relation: 'orders'}],
});
const expected = [
{
...thor,
parentId: features.emptyValue,
orders: [
{
Expand All @@ -223,53 +256,10 @@ export function hasManyInclusionResolverAcceptance(
shipment_id: features.emptyValue,
},
],
};
expect(toJSON(result)).to.containEql(toJSON(expected));
},
);

skipIf(
features.hasRevisionToken,
it,
'returns inclusions after running replaceById() operation',
async () => {
// this shows replaceById() works well with func ensurePersistable and ObjectId
// the test skips for Cloudant as it needs the _rev property for replacement.
// see replace-by-id.suite.ts
const thor = await customerRepo.create({name: 'Thor'});

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

const pizza = await orderRepo.findById(thorOrder.id.toString());
const reheatedPizza = {...pizza};
reheatedPizza.description = 'Reheated pizza';

await orderRepo.replaceById(thorOrder.id, reheatedPizza);
const odinPizza = await orderRepo.findById(thorOrder.id);

const result = await customerRepo.find({
include: [{relation: 'orders'}],
});
const expected = [
{
...thor,
parentId: features.emptyValue,
orders: [
{
...odinPizza,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
},
],
},
];
expect(toJSON(result)).to.deepEqual(toJSON(expected));
},
);
},
];
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

it('throws when navigational properties are present when updating model instance with save()', async () => {
// save() calls replaceById so the result will be the same for replaceById
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export class Customer extends Entity {
})
name: string;

@property({
type: 'string',
required: false,
})
_rev?: string;

@hasMany(() => Order)
orders: Order[];

Expand Down

0 comments on commit 4311a04

Please sign in to comment.