Skip to content

Commit

Permalink
test: modify the test, all should be passin
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Aug 28, 2019
1 parent dc6e1fa commit 2e522d7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 60 deletions.
Expand Up @@ -106,7 +106,6 @@ export function hasManyWithoutDIRelationAcceptance(

//--- HELPERS ---//

// use strictObjectIDCoercion here to make sure mongo's happy
@model()
class Order extends Entity {
@property({
Expand Down
Expand Up @@ -16,8 +16,9 @@ import {
withCrudCtx,
} from '../../../helpers.repository-tests';
import {Customer, Order} from '../fixtures/models';
import {CustomerRepository, OrderRepository} from '../fixtures/repositories';
import {CustomerRepository, OrderRepository, createOrderRepo} from '../fixtures/repositories';
import {givenBoundCrudRepositories} from '../helpers';
import { Shipment } from '../fixtures/models/shipment.model';

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

before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
({customerRepo, orderRepo} = givenBoundCrudRepositories(
({customerRepo} = givenBoundCrudRepositories(
ctx.dataSource,
));
// ANGUS: try create orderRepo by the repo that inherit from CrudRepositoryCtor
// but how can we get the class OrderRepository if it's created in createOrderRepo();
orderRepo = createOrderRepo(repositoryClass);
const models = [Customer, Order];
await ctx.dataSource.automigrate(models.map(m => m.name));
}),
);

beforeEach(async () => {
await customerRepo.deleteAll();
// ANGUS: we don't have deleteAll, findById methods in CrudRepositoryCtor ...
await orderRepo.deleteAll();
});

Expand Down Expand Up @@ -125,13 +130,13 @@ export function hasManyRelationAcceptance(
]);
});

// it('throws error when query tries to change the foreignKey', async () => {
// await expect(
// patchCustomerOrders(existingCustomerId, {
// customerId: existingCustomerId + 1,
// }),
// ).to.be.rejectedWith(/Property "customerId" cannot be changed!/);
// });
it('throws error when query tries to change the foreignKey', async () => {
await expect(
patchCustomerOrders(existingCustomerId, {
customerId: existingCustomerId + 1,
}),
).to.be.rejectedWith(/Property "customerId" cannot be changed!/);
});

it('can delete many instances', async () => {
await createCustomerOrders(existingCustomerId, {
Expand All @@ -146,17 +151,18 @@ export function hasManyRelationAcceptance(
expect(relatedOrders).to.be.empty();
});

// it("does not delete instances that don't belong to the constrained instance", async () => {
// const newOrder = {
// customerId: existingCustomerId + 1,
// description: 'another order',
// };
// await orderRepo.create(newOrder);
// await deleteCustomerOrders(existingCustomerId);
// const orders = await orderRepo.find();
// expect(orders).to.have.length(1);
// expect(_.pick(orders[0], ['customerId', 'description'])).to.eql(newOrder);
// });
it("does not delete instances that don't belong to the constrained instance", async () => {
const newId = (await givenPersistedCustomerInstance()).id;
const newOrder = {
customerId: newId,
description: 'another order',
};
await orderRepo.create(newOrder);
await deleteCustomerOrders(existingCustomerId);
const orders = await orderRepo.find();
expect(orders).to.have.length(1);
expect(toJSON(_.pick(orders[0], ['customerId', 'description']))).to.eql(toJSON(newOrder));
});

it('does not create an array of the related model', async () => {
await expect(
Expand Down
Expand Up @@ -27,7 +27,7 @@ export function hasOneRelationAcceptance(
describe('hasOne relation (acceptance)', () => {
let customerRepo: CustomerRepository;
let addressRepo: AddressRepository;
let existingCustomerId: string | number;
let existingCustomerId: any;

before(deleteAllModelsInDefaultDataSource);

Expand Down Expand Up @@ -187,13 +187,13 @@ export function hasOneRelationAcceptance(
expect(toJSON(found)).to.containDeep({city: 'Paris'});
});

// it('throws an error when PATCH tries to change the foreignKey', async () => {
// await expect(
// patchCustomerAddress(existingCustomerId, {
// customerId: existingCustomerId + 1,
// }),
// ).to.be.rejectedWith(/Property "customerId" cannot be changed!/);
// });
it('throws an error when PATCH tries to change the foreignKey', async () => {
await expect(
patchCustomerAddress(existingCustomerId, {
customerId: existingCustomerId + 1,
}),
).to.be.rejectedWith(/Property "customerId" cannot be changed!/);
});

it('can DELETE hasOne relation instances', async () => {
await createCustomerAddress(existingCustomerId, {
Expand Down
Expand Up @@ -16,7 +16,7 @@ export class Shipment extends Entity {
id: string | number;

@property({type: 'string'})
name: string;
name: string | number;

@hasMany(() => Order, {keyTo: 'shipment_id'})
shipmentOrders: Order[];
Expand Down
Expand Up @@ -3,49 +3,64 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Getter, inject} from '@loopback/context';
import {Getter} from '@loopback/context';
import {
BelongsToAccessor,
DefaultCrudRepository,
juggler,
repository,
createBelongsToAccessor,
BelongsToDefinition,
} from '@loopback/repository';
import {Customer, Order, OrderRelations, Shipment} from '../models';
import {CustomerRepository, ShipmentRepository} from '../repositories';
import { CrudRepositoryCtor } from '../../../..';

// export function createOrderRepo(repoClass: CrudRepositoryCtor) {
// return
export class OrderRepository extends DefaultCrudRepository<
// repoClass<
export function createOrderRepo(repoClass: CrudRepositoryCtor) {
return class OrderRepository extends repoClass<
Order,
typeof Order.prototype.id,
OrderRelations
> {
public readonly customer: BelongsToAccessor<
Customer,
typeof Order.prototype.id
>;
public readonly shipment: BelongsToAccessor<
Shipment,
typeof Order.prototype.id
>;
> {
public readonly customer: BelongsToAccessor<
Customer,
typeof Order.prototype.id
>;
public readonly shipment: BelongsToAccessor<
Shipment,
typeof Order.prototype.id
>;

constructor(
@inject('datasources.db') db: juggler.DataSource,
@repository.getter('CustomerRepository')
customerRepositoryGetter: Getter<CustomerRepository>,
@repository.getter('ShipmentRepository')
shipmentRepositoryGetter: Getter<ShipmentRepository>,
) {
super(Order, db);
this.customer = this.createBelongsToAccessorFor(
'customer',
constructor(
db: juggler.DataSource,
customerRepositoryGetter: Getter<CustomerRepository>,
shipmentRepositoryGetter: Getter<ShipmentRepository>,
) {
super(Order, db);
// this.customer = this.createBelongsToAccessorFor(
// 'customer',
// customerRepositoryGetter,
// );
const customerMeta = this.entityClass.definition.relations['customer'];
// customerMeta = {type: RelationType.belongsTo,
// targetsMany: false,
// keyFrom: string,
// keyTo: string
// } As BelongsToDefinition;]
// call createBelongsToAccessor directly since createBelongsToAccessorFor is protected method of defaultCrud
this.customer = createBelongsToAccessor(customerMeta as BelongsToDefinition,
customerRepositoryGetter,
);
this);

this.shipment = this.createBelongsToAccessorFor(
'shipment',
shipmentRepositoryGetter,
);
// this.shipment = this.createBelongsToAccessorFor(
// 'shipment',
// shipmentRepositoryGetter,
// );
// call createBelongsToAccessor directly since createBelongsToAccessorFor is protected method of defaultCrud
const shipmentrMeta = this.entityClass.definition.relations['shipment'];
this.shipment = createBelongsToAccessor(shipmentrMeta as BelongsToDefinition,
shipmentRepositoryGetter,
this);
}
}
}
Expand Up @@ -12,7 +12,7 @@ export class Product extends Entity {
id: true,
description: 'The unique identifier for a product',
})
id: number;
id: string | number;

@property({type: 'string'})
name: string;
Expand Down

0 comments on commit 2e522d7

Please sign in to comment.