Skip to content

Commit

Permalink
fixup! add more tests, fix DELETE method
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Mar 19, 2019
1 parent cc6da26 commit 1e199e8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {Application} from '@loopback/core';
import {expect, toJSON} from '@loopback/testlab';
import {
ApplicationWithRepositories,
EntityNotFoundError,
Filter,
juggler,
repository,
RepositoryMixin,
Filter,
EntityNotFoundError,
} from '../..';
import {Address} from '../fixtures/models';
import {CustomerRepository, AddressRepository} from '../fixtures/repositories';
import {AddressRepository, CustomerRepository} from '../fixtures/repositories';

describe('hasOne relation', () => {
// Given a Customer and Address models - see definitions at the bottom
Expand Down Expand Up @@ -131,7 +131,30 @@ describe('hasOne relation', () => {

expect(arePatched).to.deepEqual({count: 1});
const patchedData = await addressRepo.findById(address.zipcode);
expect(patchedData.city).to.equal('Lyon-Genas');
expect(toJSON(patchedData)).to.deepEqual({
customerId: existingCustomerId,
street: '1 Amedee Bonnet',
zipcode: '69740',
city: 'Lyon-Genas',
province: 'Rhone',
});
});

it('patches the related instance only', async () => {
const bob = await customerRepo.create({name: 'Bob'});
await customerRepo.address(bob.id).create({city: 'Paris'});

const alice = await customerRepo.create({name: 'Alice'});
await customerRepo.address(alice.id).create({city: 'London'});

const result = await controller.patchCustomerAddress(alice.id, {
city: 'New York',
});

expect(result).to.deepEqual({count: 1});

const found = await customerRepo.address(bob.id).get();
expect(toJSON(found)).to.containDeep({city: 'Paris'});
});

it('throws an error when PATCH tries to change the foreignKey', async () => {
Expand Down Expand Up @@ -162,6 +185,21 @@ describe('hasOne relation', () => {
).to.be.rejectedWith(EntityNotFoundError);
});

it('deletes the related model instance only', async () => {
const bob = await customerRepo.create({name: 'Bob'});
await customerRepo.address(bob.id).create({city: 'Paris'});

const alice = await customerRepo.create({name: 'Alice'});
await customerRepo.address(alice.id).create({city: 'London'});

const result = await controller.deleteCustomerAddress(alice.id);

expect(result).to.deepEqual({count: 1});

const found = await addressRepo.find();
expect(found).to.have.length(1);
});

/*---------------- HELPERS -----------------*/

class CustomerController {
Expand Down
20 changes: 10 additions & 10 deletions packages/repository/src/relations/has-one/has-one.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import { Getter } from '@loopback/context';
import { DataObject, Options, Count } from '../../common-types';
import { Entity } from '../../model';
import { Filter, Where } from '../../query';
import {Getter} from '@loopback/context';
import {Count, DataObject, Options} from '../../common-types';
import {EntityNotFoundError} from '../../errors';
import {Entity} from '../../model';
import {Filter, Where} from '../../query';
import {
constrainDataObject,
constrainFilter,
EntityCrudRepository,
constrainWhere,
EntityCrudRepository,
} from '../../repositories';
import { EntityNotFoundError } from '../../errors';

/**
* CRUD operations for a target repository of a HasMany relation
Expand Down Expand Up @@ -61,7 +61,7 @@ export class DefaultHasOneRepository<
TargetEntity extends Entity,
TargetID,
TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>
> implements HasOneRepository<TargetEntity> {
> implements HasOneRepository<TargetEntity> {
/**
* Constructor of DefaultHasOneEntityCrudRepository
* @param getTargetRepository the getter of the related target model repository instance
Expand All @@ -71,7 +71,7 @@ export class DefaultHasOneRepository<
constructor(
public getTargetRepository: Getter<TargetRepository>,
public constraint: DataObject<TargetEntity>,
) { }
) {}

async create(
targetModelData: DataObject<TargetEntity>,
Expand All @@ -93,7 +93,7 @@ export class DefaultHasOneRepository<
): Promise<TargetEntity> {
const targetRepository = await this.getTargetRepository();
const found = await targetRepository.find(
Object.assign({ limit: 1 }, constrainFilter(filter, this.constraint)),
Object.assign({limit: 1}, constrainFilter(filter, this.constraint)),
options,
);
if (found.length < 1) {
Expand All @@ -105,7 +105,7 @@ export class DefaultHasOneRepository<
}
async delete(options?: Options): Promise<Count> {
const targetRepository = await this.getTargetRepository();
return targetRepository.deleteAll(options);
return targetRepository.deleteAll(this.constraint, options);
}

async patch(
Expand Down

0 comments on commit 1e199e8

Please sign in to comment.