Skip to content

Commit

Permalink
fix: fixup!
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Sep 20, 2019
1 parent 196e4f6 commit f71c96f
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,183 +24,160 @@ import {
} from '../fixtures/models';
import {givenBoundCrudRepositories} from '../helpers';

export function hasManyRelationAcceptance(
export function belongsToInclusionResolverAcceptance(
dataSourceOptions: DataSourceOptions,
repositoryClass: CrudRepositoryCtor,
features: CrudFeatures,
) {
skipIf<[(this: Suite) => void], void>(
!features.supportsInclusionResolvers,
describe,
'retrieve models including relations',
() => {
describe('BelongsTo inclusion resolvers - acceptance', () => {
before(deleteAllModelsInDefaultDataSource);
let customerRepo: CustomerRepository;
let orderRepo: OrderRepository;
let existingCustomerId: MixedIdType;

before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
// when running the test suite on MongoDB, we don't really need to setup
// this config for mongo connector to pass the test.
// however real-world applications might have such config for MongoDB
// setting it up to check if it works fine as well
Order.definition.properties.customerId.type = features.idType;
Order.definition.properties.customerId.mongodb = {
dataType: 'ObjectID',
};
// this helper should create the inclusion resolvers for us
({customerRepo, orderRepo} = givenBoundCrudRepositories(
ctx.dataSource,
repositoryClass,
));
// inclusionResolvers should be defined. And resolver for each
// relation should be created by the belongsToFactory at this point.
expect(customerRepo.inclusionResolvers).to.not.be.undefined();
expect(orderRepo.inclusionResolvers).to.not.be.undefined();
//a relation should have its own inclusionResolver built
expect(orderRepo.customer.inclusionResolver).to.not.be.undefined();

// inclusionResolvers shouldn't setup yet at this point
expect(customerRepo.inclusionResolvers).to.deepEqual(new Map());

await ctx.dataSource.automigrate([Customer.name, Order.name]);
}),
);

beforeEach(async () => {
orderRepo.inclusionResolvers.set(
'customer',
orderRepo.customer.inclusionResolver,
);
await customerRepo.deleteAll();
await orderRepo.deleteAll();
});

it('throws an error if it tries to query not-exists relation names', async () => {
await orderRepo.create({
description: 'shiba',
customerId: existingCustomerId,
});
await expect(
orderRepo.find({include: [{relation: 'shipment'}]}),
).to.be.rejectedWith(
`Invalid "filter.include" entries: {"relation":"shipment"}`,
);
});

it('throws error if the target repository does not have the registered resolver', async () => {
await orderRepo.create({
description: 'shiba',
customerId: existingCustomerId,
});
// unregister the resolver
orderRepo.inclusionResolvers.delete('customer');

await expect(
orderRepo.find({include: [{relation: 'customer'}]}),
).to.be.rejectedWith(
`Invalid "filter.include" entries: {"relation":"customer"}`,
);
});

it('simple belongs-to relation retrieve via find() method', async () => {
const thor = await customerRepo.create({name: 'Thor'});
const order = await orderRepo.create({
description: 'Mjolnir',
customerId: thor.id,
});
const result = await orderRepo.find({
include: [{relation: 'customer'}],
});

const expected = {
...order,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
id: thor.id,
name: 'Thor',
parentId: features.emptyValue,
},
};
expect(toJSON(result)).to.deepEqual([toJSON(expected)]);
});

it('returns related instances to target models via find() method', async () => {
const thor = await customerRepo.create({name: 'Thor'});
const odin = await customerRepo.create({name: 'Odin'});
const order1 = await orderRepo.create({
description: 'Mjolnir',
customerId: thor.id,
});
const order2 = await orderRepo.create({
description: 'Coffee maker',
customerId: odin.id,
});
'BelongsTo inclusion resolvers - acceptance',
suite,
);
function suite() {
before(deleteAllModelsInDefaultDataSource);
let customerRepo: CustomerRepository;
let orderRepo: OrderRepository;
let existingCustomerId: MixedIdType;

before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
// this helper should create the inclusion resolvers and also
// register inclusion resolvers for us
({customerRepo, orderRepo} = givenBoundCrudRepositories(
ctx.dataSource,
repositoryClass,
features,
));
expect(orderRepo.customer.inclusionResolver).to.be.Function();

await ctx.dataSource.automigrate([Customer.name, Order.name]);
}),
);

beforeEach(async () => {
await customerRepo.deleteAll();
await orderRepo.deleteAll();
});

it('throws an error if it tries to query not-exists relation names', async () => {
await orderRepo.create({
description: 'shiba',
customerId: existingCustomerId,
});
await expect(
orderRepo.find({include: [{relation: 'shipment'}]}),
).to.be.rejectedWith(
`Invalid "filter.include" entries: {"relation":"shipment"}`,
);
});

it('returns single model instance including single related instance', async () => {
const thor = await customerRepo.create({name: 'Thor'});
const order = await orderRepo.create({
description: 'Mjolnir',
customerId: thor.id,
});
const result = await orderRepo.find({
include: [{relation: 'customer'}],
});

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

it('returns multiple model instances including related instances', async () => {
const thor = await customerRepo.create({name: 'Thor'});
const odin = await customerRepo.create({name: 'Odin'});
const thorOrder = await orderRepo.create({
description: "Thor's Mjolnir",
customerId: thor.id,
});
const odinOrder = await orderRepo.create({
description: "Odin's Coffee Maker",
customerId: odin.id,
});

const expected = [
{
...order1,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
id: thor.id,
name: 'Thor',
parentId: features.emptyValue,
},
},
{
...order2,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
id: odin.id,
name: 'Odin',
parentId: features.emptyValue,
},
},
];
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});
const result = await orderRepo.find({
include: [{relation: 'customer'}],
});

it('returns related instances to target models via findById() method', async () => {
const thor = await customerRepo.create({name: 'Thor'});
const odin = await customerRepo.create({name: 'Odin'});
await orderRepo.create({
description: 'Mjolnir',
customerId: thor.id,
});
const order2 = await orderRepo.create({
description: 'Coffee maker',
customerId: odin.id,
});
const expected = [
{
...thorOrder,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
...thor,
parentId: features.emptyValue,
},
},
{
...odinOrder,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
...odin,
parentId: features.emptyValue,
},
},
];
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

it('returns a specified instance including its related model instances', async () => {
const thor = await customerRepo.create({name: 'Thor'});
const odin = await customerRepo.create({name: 'Odin'});
await orderRepo.create({
description: "Thor's Mjolnir",
customerId: thor.id,
});
const odinOrder = await orderRepo.create({
description: "Odin's Coffee Maker",
customerId: odin.id,
});

const result = await orderRepo.findById(order2.id, {
include: [{relation: 'customer'}],
});
const expected = {
...order2,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
id: odin.id,
name: 'Odin',
parentId: features.emptyValue,
},
};
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});
const result = await orderRepo.findById(odinOrder.id, {
include: [{relation: 'customer'}],
});
},
);
const expected = {
...odinOrder,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
customer: {
...odin,
parentId: features.emptyValue,
},
};
expect(toJSON(result)).to.deepEqual(toJSON(expected));
});

it('throws error if the target repository does not have the registered resolver', async () => {
await orderRepo.create({
description: 'shiba',
customerId: existingCustomerId,
});
// unregister the resolver
orderRepo.inclusionResolvers.delete('customer');

await expect(
orderRepo.find({include: [{relation: 'customer'}]}),
).to.be.rejectedWith(
`Invalid "filter.include" entries: {"relation":"customer"}`,
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function belongsToRelationAcceptance(
({customerRepo, orderRepo, shipmentRepo} = givenBoundCrudRepositories(
ctx.dataSource,
repositoryClass,
features,
));
const models = [Customer, Order, Shipment];
await ctx.dataSource.automigrate(models.map(m => m.name));
Expand Down Expand Up @@ -106,6 +107,7 @@ export function belongsToRelationAcceptance(
EntityNotFoundError,
);
});

// helpers
function givenAccessor() {
findCustomerOfOrder = createBelongsToAccessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function hasManyRelationAcceptance(
({customerRepo, orderRepo} = givenBoundCrudRepositories(
ctx.dataSource,
repositoryClass,
features,
));
await ctx.dataSource.automigrate([Customer.name, Order.name]);
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function hasOneRelationAcceptance(
({customerRepo, addressRepo} = givenBoundCrudRepositories(
ctx.dataSource,
repositoryClass,
features,
));
const models = [Customer, Address];
await ctx.dataSource.automigrate(models.map(m => m.name));
Expand Down
Loading

0 comments on commit f71c96f

Please sign in to comment.