Skip to content

Commit

Permalink
fixup! merge and add hasOne and repository acceptance
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Aug 16, 2019
1 parent c2285f6 commit def69c1
Show file tree
Hide file tree
Showing 14 changed files with 491 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {juggler} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {
deleteAllModelsInDefaultDataSource,
Expand All @@ -21,6 +20,7 @@ import {
OrderRepository,
ShipmentRepository,
} from '../fixtures/repositories';
import {givenBoundCrudRepositories} from '../helpers';

export function belongsToRelationAcceptance(
dataSourceOptions: DataSourceOptions,
Expand All @@ -46,10 +46,11 @@ export function belongsToRelationAcceptance(
}
});

await givenBoundCrudRepositories(ctx.dataSource);
await ctx.dataSource.automigrate(Customer.name);
await ctx.dataSource.automigrate(Order.name);
await ctx.dataSource.automigrate(Shipment.name);
({customerRepo, orderRepo, shipmentRepo} = givenBoundCrudRepositories(
ctx.dataSource,
));

await ctx.dataSource.automigrate(models.map(m => m.name));
}),
);

Expand Down Expand Up @@ -83,17 +84,5 @@ export function belongsToRelationAcceptance(
const result = await orderRepo.shipment(order.id);
expect(result).to.deepEqual(shipment);
});

//--- HELPERS ---//

async function givenBoundCrudRepositories(db: juggler.DataSource) {
orderRepo = new OrderRepository(
db,
async () => customerRepo,
async () => shipmentRepo,
);
customerRepo = new repositoryClass(Customer, db) as CustomerRepository;
shipmentRepo = new repositoryClass(Shipment, db) as ShipmentRepository;
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,99 +34,88 @@ export function hasManyWithoutDIRelationAcceptance(
describe('HasMany relation without di (acceptance)', () => {
before(deleteAllModelsInDefaultDataSource);
// Given a Customer and Order models - see definitions at the bottom

// let existingCustomerId: number;
let existingCustomerId: string;
let ds: juggler.DataSource;
let customerRepo: CustomerRepository;
let orderRepo: OrderRepository;

before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
givenDataSource(ctx.dataSource);
ds = ctx.dataSource;
givenOrderRepository();
givenCustomerRepository();
await ctx.dataSource.automigrate(Customer.name);
await ctx.dataSource.automigrate(Order.name);
await ctx.dataSource.automigrate([Customer.name, Order.name]);
}),
);

// before(givenDataSource(ctx.dataSource));
// before(givenOrderRepository);
// before(givenCustomerRepository);
beforeEach(async () => {
await orderRepo.deleteAll();
//existingCustomerId = (await givenPersistedCustomerInstance()).id;
existingCustomerId = (await givenPersistedCustomerInstance()).id;
});

it('can create an instance of the related model (acceptance)', async () => {
it('can create an instance of the related model', async () => {
async function createCustomerOrders(
customerId: number,
customerId: string,
orderData: Partial<Order>,
): Promise<Order> {
return customerRepo.orders(customerId).create(orderData);
}
const customerA = await customerRepo.create({name: 'customer A'});
const order = await createCustomerOrders(customerA.id, {
description: 'order 1',
});
expect({
customerId: order.id.valueOf(),
description: order.description,
}).to.eql({
customerId: customerA.id,
const order = await createCustomerOrders(existingCustomerId, {
description: 'order 1',
});
// do this to avoid type problems of BSON type of mongodb
expect(order.customerId.toString()).eql(existingCustomerId.toString());
expect(order.description).to.eql('order 1');

const persisted = await orderRepo.findById(order.id);
console.log(persisted);

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

it('can find instances of the related model (acceptance)', async () => {
async function createCustomerOrders(
customerId: number,
customerId: string,
orderData: Partial<Order>,
): Promise<Order> {
return customerRepo.orders(customerId).create(orderData);
}
async function findCustomerOrders(customerId: number) {
async function findCustomerOrders(customerId: string) {
return customerRepo.orders(customerId).find();
}
const customerB = await customerRepo.create({name: 'customer B'});
const customerC = await customerRepo.create({name: 'customer c'});

const order = await createCustomerOrders(customerB.id, {
const order = await createCustomerOrders(existingCustomerId, {
description: 'order 1',
});
console.log(typeof customerB.id);
console.log(customerB);
console.log(typeof order.id);
console.log(order);

const notMyOrder = await createCustomerOrders(customerC.id, {
const notMyOrder = await createCustomerOrders(existingCustomerId + 1, {
description: 'order 2',
});
const orders = await findCustomerOrders(customerB.id);
const orders = await findCustomerOrders(existingCustomerId);

expect(orders).to.containEql(order);
expect(orders).to.not.containEql(notMyOrder);

const persisted = await orderRepo.find({
where: {customerId: customerB.id},
where: {customerId: existingCustomerId},
});
expect(persisted).to.deepEqual(orders);
});

//--- HELPERS ---//

@model()
// use strictObjectIDCoercion here to make sure mongo's happy
@model({
settings: {
strictObjectIDCoercion: true,
},
})
class Order extends Entity {
@property({
type: features.idType,
id: true,
generated: true,
})
id: number;
id: string;

@property({
type: 'string',
Expand All @@ -138,17 +127,21 @@ export function hasManyWithoutDIRelationAcceptance(
type: features.idType,
required: true,
})
customerId: number;
customerId: string;
}

@model()
@model({
settings: {
strictObjectIDCoercion: true,
},
})
class Customer extends Entity {
@property({
type: features.idType,
id: true,
generated: true,
})
id: number;
id: string;

@property({
type: 'string',
Expand Down Expand Up @@ -191,10 +184,6 @@ export function hasManyWithoutDIRelationAcceptance(
}
}

function givenDataSource(db: juggler.DataSource) {
ds = db;
}

function givenOrderRepository() {
orderRepo = new OrderRepository(ds);
}
Expand All @@ -203,8 +192,8 @@ export function hasManyWithoutDIRelationAcceptance(
customerRepo = new CustomerRepository(ds, Getter.fromValue(orderRepo));
}

// async function givenPersistedCustomerInstance() {
// return customerRepo.create({name: 'a customer'});
// }
async function givenPersistedCustomerInstance() {
return customerRepo.create({name: 'a customer'});
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {juggler} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import * as _ from 'lodash';
import {
Expand All @@ -18,6 +17,7 @@ import {
} from '../../helpers.repository-tests';
import {Customer, Order} from '../fixtures/models';
import {CustomerRepository, OrderRepository} from '../fixtures/repositories';
import {givenBoundCrudRepositories} from '../helpers';

export function hasManyRelationAcceptance(
dataSourceOptions: DataSourceOptions,
Expand All @@ -35,18 +35,20 @@ export function hasManyRelationAcceptance(

before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
({customerRepo, orderRepo} = givenBoundCrudRepositories(
ctx.dataSource,
));

const models = [Customer, Order];
models.forEach(model => {
models.forEach(async model => {
model.definition.properties.id.type = features.idType;
if (model === Order) {
model.definition.properties.customerId.type = features.idType;
model.definition.properties.shipment_id.type = features.idType;
}
});

await givenBoundCrudRepositoriesForCustomerAndOrder(ctx.dataSource);
await ctx.dataSource.automigrate(Customer.name);
await ctx.dataSource.automigrate(Order.name);
await ctx.dataSource.automigrate(models.map(m => m.name));
}),
);

Expand All @@ -58,14 +60,16 @@ export function hasManyRelationAcceptance(
existingCustomerId = (await givenPersistedCustomerInstance()).id;
});

it.only('can create an instance of the related model', async () => {
it('can create an instance of the related model', async () => {
const order = await customerRepo.orders(existingCustomerId).create({
description: 'order 1',
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: 1,
});
expect(order.toObject()).to.containDeep({
customerId: existingCustomerId,
description: 'order 1',
});

// do this to avoid type problems of BSON type of mongodb
expect(order.customerId.toString()).to.eql(existingCustomerId.toString());
expect(order.description).to.eql('order 1');

const persisted = await orderRepo.findById(order.id);
expect(persisted.toObject()).to.deepEqual(order.toObject());
Expand All @@ -74,9 +78,13 @@ export function hasManyRelationAcceptance(
it('can find instances of the related model', async () => {
const order = await createCustomerOrders(existingCustomerId, {
description: 'order 1',
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: 1,
});
const notMyOrder = await createCustomerOrders(existingCustomerId + 1, {
description: 'order 2',
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: 1,
});
const foundOrders = await findCustomerOrders(existingCustomerId);
expect(foundOrders).to.containEql(order);
Expand Down Expand Up @@ -107,6 +115,11 @@ export function hasManyRelationAcceptance(
await findCustomerOrders(existingCustomerId),
d => _.pick(d, ['customerId', 'description', 'isShipped']),
);
// convert the id type for mongo
if (features.idType === 'string') {
// eslint-disable-next-line require-atomic-updates
existingCustomerId = existingCustomerId.toString();
}
expect(patchedData).to.eql([
{
customerId: existingCustomerId,
Expand Down Expand Up @@ -237,13 +250,6 @@ export function hasManyRelationAcceptance(
return customerRepo.customers(customerId).find();
}

async function givenBoundCrudRepositoriesForCustomerAndOrder(
db: juggler.DataSource,
) {
customerRepo = new CustomerRepository(db, async () => orderRepo);
orderRepo = new OrderRepository(db, async () => customerRepo);
}

async function givenPersistedCustomerInstance() {
return customerRepo.create({name: 'a customer'});
}
Expand Down
Loading

0 comments on commit def69c1

Please sign in to comment.