Skip to content

Commit

Permalink
test(repository): hasMany relation to same entity
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Mar 15, 2019
1 parent d63ac43 commit bff588f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
repository,
RepositoryMixin,
} from '../..';
import {Order} from '../fixtures/models';
import {Customer, Order} from '../fixtures/models';
import {CustomerRepository, OrderRepository} from '../fixtures/repositories';

describe('HasMany relation', () => {
Expand Down Expand Up @@ -152,6 +152,35 @@ describe('HasMany relation', () => {
).to.be.rejectedWith(/`orders` is not defined/);
});

// The following tests test hasMany Entity to itself

it('gets the parent entity through the child entity', async () => {
const parent = await customerRepo.create({name: 'parent customer'});
const child = await customerRepo.create({
name: 'child customer',
parentId: parent.id,
});

const childsParent = await controller.getParentCustomer(child.id);

expect(_.pick(childsParent, ['id', 'name'])).to.eql(
_.pick(parent, ['id', 'name']),
);
});

it('creates a child entity through the parent entity', async () => {
const parent = await customerRepo.create({
name: 'parent customer',
});
const child = await controller.createCustomerChildren(parent.id, {
name: 'child customer',
});
expect(child.parentId).to.equal(parent.id);

const children = await controller.findCustomerChildren(parent.id);
expect(children).to.containEql(child);
});

// This should be enforced by the database to avoid race conditions
it.skip('reject create request when the customer does not exist');

Expand Down Expand Up @@ -179,6 +208,23 @@ describe('HasMany relation', () => {
async deleteCustomerOrders(customerId: number) {
return await this.customerRepository.orders(customerId).delete();
}

async getParentCustomer(customerId: number) {
return await this.customerRepository.parent(customerId);
}

async createCustomerChildren(
customerId: number,
customerData: Partial<Customer>,
) {
return await this.customerRepository
.customers(customerId)
.create(customerData);
}

async findCustomerChildren(customerId: number) {
return await this.customerRepository.customers(customerId).find();
}
}

function givenApplicationWithMemoryDB() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Entity, hasMany, model, property, hasOne} from '../../..';
import {Entity, hasMany, model, property, hasOne, belongsTo} from '../../..';
import {Order} from './order.model';
import {Address} from './address.model';

Expand All @@ -25,4 +25,10 @@ export class Customer extends Entity {

@hasOne(() => Address)
address: Address;

@hasMany(() => Customer, {keyTo: 'parentId'})
customers?: Customer[];

@belongsTo(() => Customer)
parentId?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
HasManyRepositoryFactory,
juggler,
repository,
BelongsToAccessor,
} from '../../..';
import {Customer, Order, Address} from '../models';
import {OrderRepository} from './order.repository';
Expand All @@ -27,6 +28,15 @@ export class CustomerRepository extends DefaultCrudRepository<
Address,
typeof Customer.prototype.id
>;
public readonly customers: HasManyRepositoryFactory<
Customer,
typeof Customer.prototype.id
>;
public readonly parent: BelongsToAccessor<
Customer,
typeof Customer.prototype.id
>;

constructor(
@inject('datasources.db') protected db: juggler.DataSource,
@repository.getter('OrderRepository')
Expand All @@ -43,5 +53,13 @@ export class CustomerRepository extends DefaultCrudRepository<
'address',
addressRepositoryGetter,
);
this.customers = this.createHasManyRepositoryFactoryFor(
'customers',
Getter.fromValue(this),
);
this.parent = this.createBelongsToAccessorFor(
'parent',
Getter.fromValue(this),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class OrderRepository extends DefaultCrudRepository<
customerRepositoryGetter: Getter<CustomerRepository>,
) {
super(Order, db);
this.customer = this._createBelongsToAccessorFor(
this.customer = this.createBelongsToAccessorFor(
'customer',
customerRepositoryGetter,
);
Expand Down

0 comments on commit bff588f

Please sign in to comment.