Skip to content

Commit

Permalink
test: hasmany datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Aug 14, 2019
1 parent c8f2d45 commit f551ade
Showing 1 changed file with 139 additions and 118 deletions.
Expand Up @@ -17,137 +17,160 @@ import {
RelationType,
} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {
CrudFeatures,
CrudRepositoryCtor,
CrudTestContext,
DataSourceOptions,
} from '../..';
import {withCrudCtx} from '../../helpers.repository-tests';

// Given a Customer and Order models - see definitions at the bottom
let db: juggler.DataSource;
let customerRepo: EntityCrudRepository<Customer, typeof Customer.prototype.id>;
let orderRepo: EntityCrudRepository<Order, typeof Order.prototype.id>;
let reviewRepo: EntityCrudRepository<Review, typeof Review.prototype.id>;

describe('HasMany relation', () => {
let existingCustomerId: number;

let customerOrderRepo: HasManyRepository<Order>;
let customerAuthoredReviewFactoryFn: HasManyRepositoryFactory<
Review,
typeof Customer.prototype.id
>;
let customerApprovedReviewFactoryFn: HasManyRepositoryFactory<
Review,
typeof Customer.prototype.id
>;

before(givenCrudRepositories);
before(givenPersistedCustomerInstance);
before(givenConstrainedRepositories);
before(givenRepositoryFactoryFunctions);

beforeEach(async function resetDatabase() {
await orderRepo.deleteAll();
await reviewRepo.deleteAll();
});
export function hasManyFactorySuite(
dataSourceOptions: DataSourceOptions,
repositoryClass: CrudRepositoryCtor,
features: CrudFeatures,
) {
describe('HasMany relation', () => {
let existingCustomerId: number;

let customerOrderRepo: HasManyRepository<Order>;
let customerAuthoredReviewFactoryFn: HasManyRepositoryFactory<
Review,
typeof Customer.prototype.id
>;
let customerApprovedReviewFactoryFn: HasManyRepositoryFactory<
Review,
typeof Customer.prototype.id
>;

before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
db = ctx.dataSource;
givenCrudRepositories(db);
await givenPersistedCustomerInstance();
givenConstrainedRepositories();
givenRepositoryFactoryFunctions();
await ctx.dataSource.automigrate(Review.name);
await ctx.dataSource.automigrate(Customer.name);
await ctx.dataSource.automigrate(Order.name);
}),
);

it('can create an instance of the related model', async () => {
const order = await customerOrderRepo.create({
description: 'an order desc',
beforeEach(async function resetDatabase() {
await orderRepo.deleteAll();
await reviewRepo.deleteAll();
});
const persisted = await orderRepo.findById(order.id);

expect(order).to.deepEqual(persisted);
});

it('can find an instance of the related model', async () => {
const order = await customerOrderRepo.create({
description: 'an order desc',
});
const notMyOrder = await orderRepo.create({
description: "someone else's order desc",
customerId: existingCustomerId + 1, // a different customerId,
});
const persistedOrders = await orderRepo.find({
where: {
it('can create an instance of the related model', async () => {
const order = await customerOrderRepo.create({
description: 'an order desc',
customerId: existingCustomerId,
},
});

const orders = await customerOrderRepo.find();

expect(orders).to.containEql(order);
expect(orders).to.not.containEql(notMyOrder);
expect(orders).to.deepEqual(persistedOrders);
});
});
const persisted = await orderRepo.findById(order.id);

it('finds appropriate related model instances for multiple relations', async () => {
// note(shimks): roundabout way of creating reviews with 'approves'
// ideally, the review repository should have a approve function
// which should 'approve' a review
// On another note, this test should be separated for 'create' and 'find'
await customerAuthoredReviewFactoryFn(existingCustomerId).create({
description: 'my wonderful review',
approvedId: existingCustomerId + 1,
});
await customerAuthoredReviewFactoryFn(existingCustomerId + 1).create({
description: 'smash that progenitor loving approve button',
approvedId: existingCustomerId,
expect(order).to.deepEqual(persisted);
});

const reviewsApprovedByCustomerOne = await customerApprovedReviewFactoryFn(
existingCustomerId,
).find();
const reviewsApprovedByCustomerTwo = await customerApprovedReviewFactoryFn(
existingCustomerId + 1,
).find();

const persistedReviewsApprovedByCustomerOne = await reviewRepo.find({
where: {
approvedId: existingCustomerId,
},
it('can find an instance of the related model', async () => {
const order = await customerOrderRepo.create({
description: 'an order desc',
customerId: existingCustomerId,
});
const notMyOrder = await orderRepo.create({
description: "someone else's order desc",
customerId: existingCustomerId + 1, // a different customerId,
});
const persistedOrders = await orderRepo.find({
where: {
customerId: existingCustomerId,
},
});

const orders = await customerOrderRepo.find();

expect(orders).to.containEql(order);
expect(orders).to.not.containEql(notMyOrder);
expect(orders).to.deepEqual(persistedOrders);
});
const persistedReviewsApprovedByCustomerTwo = await reviewRepo.find({
where: {

it('finds appropriate related model instances for multiple relations', async () => {
// note(shimks): roundabout way of creating reviews with 'approves'
// ideally, the review repository should have a approve function
// which should 'approve' a review
// On another note, this test should be separated for 'create' and 'find'
await customerAuthoredReviewFactoryFn(existingCustomerId).create({
description: 'my wonderful review',
approvedId: existingCustomerId + 1,
},
});
await customerAuthoredReviewFactoryFn(existingCustomerId + 1).create({
description: 'smash that progenitor loving approve button',
approvedId: existingCustomerId,
});

const reviewsApprovedByCustomerOne = await customerApprovedReviewFactoryFn(
existingCustomerId,
).find();
const reviewsApprovedByCustomerTwo = await customerApprovedReviewFactoryFn(
existingCustomerId + 1,
).find();

const persistedReviewsApprovedByCustomerOne = await reviewRepo.find({
where: {
approvedId: existingCustomerId,
},
});
const persistedReviewsApprovedByCustomerTwo = await reviewRepo.find({
where: {
approvedId: existingCustomerId + 1,
},
});

expect(reviewsApprovedByCustomerOne).to.eql(
persistedReviewsApprovedByCustomerOne,
);
expect(reviewsApprovedByCustomerTwo).to.eql(
persistedReviewsApprovedByCustomerTwo,
);
});

expect(reviewsApprovedByCustomerOne).to.eql(
persistedReviewsApprovedByCustomerOne,
);
expect(reviewsApprovedByCustomerTwo).to.eql(
persistedReviewsApprovedByCustomerTwo,
);
//--- HELPERS ---//

async function givenPersistedCustomerInstance() {
const customer = await customerRepo.create({name: 'a customer'});
existingCustomerId = customer.id;
}

function givenConstrainedRepositories() {
const orderFactoryFn = createHasManyRepositoryFactory<
Order,
typeof Order.prototype.id,
typeof Customer.prototype.id
>(
Customer.definition.relations.orders as HasManyDefinition,
Getter.fromValue(orderRepo),
);

customerOrderRepo = orderFactoryFn(existingCustomerId);
}

function givenRepositoryFactoryFunctions() {
customerAuthoredReviewFactoryFn = createHasManyRepositoryFactory(
Customer.definition.relations.reviewsAuthored as HasManyDefinition,
Getter.fromValue(reviewRepo),
);
customerApprovedReviewFactoryFn = createHasManyRepositoryFactory(
Customer.definition.relations.reviewsApproved as HasManyDefinition,
Getter.fromValue(reviewRepo),
);
}
});

//--- HELPERS ---//

async function givenPersistedCustomerInstance() {
const customer = await customerRepo.create({name: 'a customer'});
existingCustomerId = customer.id;
}

function givenConstrainedRepositories() {
const orderFactoryFn = createHasManyRepositoryFactory<
Order,
typeof Order.prototype.id,
typeof Customer.prototype.id
>(
Customer.definition.relations.orders as HasManyDefinition,
Getter.fromValue(orderRepo),
);

customerOrderRepo = orderFactoryFn(existingCustomerId);
}

function givenRepositoryFactoryFunctions() {
customerAuthoredReviewFactoryFn = createHasManyRepositoryFactory(
Customer.definition.relations.reviewsAuthored as HasManyDefinition,
Getter.fromValue(reviewRepo),
);
customerApprovedReviewFactoryFn = createHasManyRepositoryFactory(
Customer.definition.relations.reviewsApproved as HasManyDefinition,
Getter.fromValue(reviewRepo),
);
}
});
}

//--- HELPERS ---//

Expand Down Expand Up @@ -222,10 +245,8 @@ class Customer extends Entity {
});
}

function givenCrudRepositories() {
db = new juggler.DataSource({connector: 'memory'});

customerRepo = new DefaultCrudRepository(Customer, db);
orderRepo = new DefaultCrudRepository(Order, db);
reviewRepo = new DefaultCrudRepository(Review, db);
function givenCrudRepositories(ds: juggler.DataSource) {
customerRepo = new DefaultCrudRepository(Customer, ds);
orderRepo = new DefaultCrudRepository(Order, ds);
reviewRepo = new DefaultCrudRepository(Review, ds);
}

0 comments on commit f551ade

Please sign in to comment.