Skip to content

Commit

Permalink
chore: add repository-factory unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Jun 11, 2018
1 parent 9c0524b commit 572dc85
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/repository/src/repositories/constraint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {Filter, WhereBuilder, Where, FilterBuilder} from '../query';
import {AnyObject, DataObject} from '../common-types';
import {cloneDeep, isArray} from 'lodash';
import {cloneDeep} from 'lodash';
import {Entity} from '../model';

/**
Expand Down
13 changes: 5 additions & 8 deletions packages/repository/src/repositories/relation.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// License text available at https://opensource.org/licenses/MIT

import {EntityCrudRepository} from './repository';
import {Class} from '../common-types';
import {RelationType} from '../decorators/relation.decorator';
import {Entity} from '../model';
import {
Expand All @@ -14,9 +13,7 @@ import {

export interface RelationDefinitionBase {
type: RelationType;
modelFrom: Class<Entity> | string;
keyTo: string;
keyFrom: string;
}

export interface HasManyDefinition extends RelationDefinitionBase {
Expand All @@ -38,7 +35,7 @@ export interface HasManyDefinition extends RelationDefinitionBase {
* relation attached to a datasource.
*
*/
export function hasManyRepositoryFactory<SourceID, T extends Entity, ID>(
export function relationRepositoryFactory<SourceID, T extends Entity, ID>(
sourceModelId: SourceID,
relationMetadata: HasManyDefinition,
targetRepository: EntityCrudRepository<T, ID>,
Expand All @@ -47,9 +44,9 @@ export function hasManyRepositoryFactory<SourceID, T extends Entity, ID>(
case RelationType.hasMany:
const fkConstraint = {[relationMetadata.keyTo]: sourceModelId};

return new DefaultHasManyEntityCrudRepository(
targetRepository,
fkConstraint,
);
return new DefaultHasManyEntityCrudRepository<
T,
EntityCrudRepository<T, ID>
>(targetRepository, fkConstraint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DefaultCrudRepository,
juggler,
EntityCrudRepository,
hasManyRepositoryFactory,
relationRepositoryFactory,
HasManyDefinition,
RelationType,
} from '../..';
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('HasMany relation', () => {
// The initial "involved" implementation is below

//FIXME: should be automagically instantiated via DI or other means
const customerOrders = hasManyRepositoryFactory(
const customerOrders = relationRepositoryFactory(
customerId,
customerHasManyOrdersRelationMeta,
orderRepo,
Expand Down Expand Up @@ -117,8 +117,6 @@ describe('HasMany relation', () => {

function givenHasManyRelationMetadata(): HasManyDefinition {
return {
modelFrom: Customer,
keyFrom: 'id',
keyTo: 'customerId',
type: RelationType.hasMany,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
relationRepositoryFactory,
juggler,
DefaultCrudRepository,
Entity,
RelationDefinitionBase,
RelationType,
ModelDefinition,
HasManyDefinition,
} from '../../../src';
import * as RelationFactoryNamespace from '../../../src/repositories/relation.repository'; // needed to spy on the factory function
import {sinon} from '@loopback/testlab';

describe('relationRepositoryFactory', () => {
const instanceId = 4;
let meta: RelationDefinitionBase;
let relationTestRepo: DefaultCrudRepository<Entity, number>;
let db: juggler.DataSource;

before(givenDataSource);
before(givenTestRepository);

it('calls DefaultHasManyEntityCrudRepository constructor when given HasMany metadata', () => {
meta = {
type: RelationType.hasMany,
keyTo: 'hasManyId',
};
const spy = sinon.spy(
RelationFactoryNamespace,
'DefaultHasManyEntityCrudRepository',
);

relationRepositoryFactory(
instanceId,
meta as HasManyDefinition,
relationTestRepo,
);

sinon.assert.calledWithNew(spy);
sinon.assert.calledWith(spy, relationTestRepo, {hasManyId: instanceId});
});

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

function givenTestRepository() {
relationTestRepo = new DefaultCrudRepository(
class TestModel extends Entity {
static definition = new ModelDefinition({
name: 'TestModel',
properties: {
testId: {type: 'string', id: true},
},
});
},
db,
);
}
});

0 comments on commit 572dc85

Please sign in to comment.