Skip to content

Commit

Permalink
fix: update tests to use template string
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Oct 8, 2019
1 parent 34fba34 commit a77b297
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 60 deletions.
35 changes: 13 additions & 22 deletions packages/cli/generators/relation/belongs-to-relation.generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = class BelongsToRelationGenerator extends BaseRelationGenerator

getBelongsTo(className, relationName, fktype) {
return {
decorators: [{name: 'belongsTo', arguments: ['() => ' + className]}],
decorators: [{name: 'belongsTo', arguments: [`() => ${className}`]}],
name: relationName,
type: fktype,
};
Expand Down Expand Up @@ -132,39 +132,30 @@ module.exports = class BelongsToRelationGenerator extends BaseRelationGenerator

_getRepositoryRelationPropertyType() {
return (
'BelongsToAccessor<' +
utils.toClassName(this.artifactInfo.dstModelClass) +
', typeof ' +
utils.toClassName(this.artifactInfo.srcModelClass) +
'.prototype.' +
this.artifactInfo.srcModelPrimaryKey +
'>'
`BelongsToAccessor<` +
`${utils.toClassName(this.artifactInfo.dstModelClass)}` +
`, typeof ${utils.toClassName(this.artifactInfo.srcModelClass)}` +
`.prototype.${this.artifactInfo.srcModelPrimaryKey}>`
);
}

_addCreatorToRepositoryConstructor(classConstructor) {
const relationPropertyName = this._getRepositoryRelationPropertyName();
const statement =
'this.' +
this._getRepositoryRelationPropertyName() +
' = ' +
"this.createBelongsToAccessorFor('" +
this.artifactInfo.relationName.replace(/Id$/, '') +
"', " +
utils.camelCase(this.artifactInfo.dstRepositoryClassName) +
'Getter,);';
`this.${relationPropertyName} = ` +
`this.createBelongsToAccessorFor('` +
`${this.artifactInfo.relationName.replace(/Id$/, '')}',` +
` ${utils.camelCase(this.artifactInfo.dstRepositoryClassName)}` +
`Getter,);`;
classConstructor.insertStatements(1, statement);
}

_registerInclusionResolverForRelation(classConstructor, options) {
const relationPropertyName = this._getRepositoryRelationPropertyName();
if (options.registerInclusionResolver) {
const statement =
"this.registerInclusionResolver('" +
relationPropertyName +
"', " +
'this.' +
relationPropertyName +
'.inclusionResolver);';
`this.registerInclusionResolver(` +
`'${relationPropertyName}', this.${relationPropertyName}.inclusionResolver);`;
classConstructor.insertStatements(2, statement);
}
}
Expand Down
39 changes: 12 additions & 27 deletions packages/cli/generators/relation/has-many-relation.generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ module.exports = class HasManyRelationGenerator extends BaseRelationGenerator {
let relationDecorator = [
{
name: 'hasMany',
arguments: [
'() => ' + className + " ,{keyTo: '" + foreignKeyName + "'}",
],
arguments: [`() => ${className}, {keyTo: '${foreignKeyName}'}`],
},
];
if (isDefaultForeignKey) {
relationDecorator = [
{name: 'hasMany', arguments: ['() => ' + className]},
{name: 'hasMany', arguments: [`() => ${className}`]},
];
}

Expand Down Expand Up @@ -172,41 +170,28 @@ module.exports = class HasManyRelationGenerator extends BaseRelationGenerator {
}

_getRepositoryRelationPropertyType() {
return (
'HasManyRepositoryFactory<' +
utils.toClassName(this.artifactInfo.dstModelClass) +
', typeof ' +
utils.toClassName(this.artifactInfo.srcModelClass) +
'.prototype.' +
this.artifactInfo.srcModelPrimaryKey +
'>'
);
return `HasManyRepositoryFactory<${utils.toClassName(
this.artifactInfo.dstModelClass,
)}, typeof ${utils.toClassName(
this.artifactInfo.srcModelClass,
)}.prototype.${this.artifactInfo.srcModelPrimaryKey}>`;
}

_addCreatorToRepositoryConstructor(classConstructor) {
const relationPropertyName = this._getRepositoryRelationPropertyName();
const statement =
'this.' +
relationPropertyName +
' = ' +
"this.createHasManyRepositoryFactoryFor('" +
relationPropertyName +
"', " +
utils.camelCase(this.artifactInfo.dstRepositoryClassName) +
'Getter,);';
`this.${relationPropertyName} = ` +
`this.createHasManyRepositoryFactoryFor('${relationPropertyName}', ` +
`${utils.camelCase(this.artifactInfo.dstRepositoryClassName)}Getter,);`;
classConstructor.insertStatements(1, statement);
}

_registerInclusionResolverForRelation(classConstructor, options) {
const relationPropertyName = this._getRepositoryRelationPropertyName();
if (options.registerInclusionResolver) {
const statement =
"this.registerInclusionResolver('" +
relationPropertyName +
"', " +
'this.' +
relationPropertyName +
'.inclusionResolver);';
`this.registerInclusionResolver(` +
`'${relationPropertyName}', this.${relationPropertyName}.inclusionResolver);`;
classConstructor.insertStatements(2, statement);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class OrderRepository extends DefaultCrudRepository<
`;

exports[
`lb4 relation checks generated source class repository answers {"relationType":"belongsTo","sourceModel":"OrderClass","destinationModel":"CustomerClass","registerInclusionResolver":false} generates OrderClass repository file with different inputs 1`
`lb4 relation checks generated source class repository answers {"relationType":"belongsTo","sourceModel":"OrderClass","destinationModel":"CustomerClass","registerInclusionResolver":true} generates OrderClass repository file with different inputs 1`
] = `
import {DefaultCrudRepository, repository, BelongsToAccessor} from '@loopback/repository';
import {OrderClass, CustomerClass} from '../models';
Expand All @@ -51,6 +51,7 @@ export class OrderClassRepository extends DefaultCrudRepository<
constructor(@inject('datasources.myDB') dataSource: MyDBDataSource, @repository.getter('CustomerClassRepository') protected customerClassRepositoryGetter: Getter<CustomerClassRepository>,) {
super(OrderClass, dataSource);
this.customerClass = this.createBelongsToAccessorFor('customerClassCustNumber', customerClassRepositoryGetter,);
this.registerInclusionResolver('customerClass', this.customerClass.inclusionResolver);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class CustomerRepository extends DefaultCrudRepository<
`;

exports[
`lb4 relation HasMany checks generated source class repository answers {"relationType":"hasMany","sourceModel":"CustomerClass","destinationModel":"OrderClass","registerInclusionResolver":false} generates CustomerClass repository file with different inputs 1`
`lb4 relation HasMany checks generated source class repository answers {"relationType":"hasMany","sourceModel":"CustomerClass","destinationModel":"OrderClass","registerInclusionResolver":true} generates CustomerClass repository file with different inputs 1`
] = `
import {DefaultCrudRepository, repository, HasManyRepositoryFactory} from '@loopback/repository';
import {CustomerClass, OrderClass} from '../models';
Expand All @@ -51,6 +51,7 @@ export class CustomerClassRepository extends DefaultCrudRepository<
constructor(@inject('datasources.myDB') dataSource: MyDBDataSource, @repository.getter('OrderClassRepository') protected orderClassRepositoryGetter: Getter<OrderClassRepository>,) {
super(CustomerClass, dataSource);
this.orderClasses = this.createHasManyRepositoryFactoryFor('orderClasses', orderClassRepositoryGetter,);
this.registerInclusionResolver('orderClasses', this.orderClasses.inclusionResolver);
}
}
Expand Down Expand Up @@ -448,7 +449,7 @@ export class Customer extends Entity {
})
name?: string;
@hasMany(() => Order ,{keyTo: 'mykey'})
@hasMany(() => Order, {keyTo: 'mykey'})
orders: Order[];
constructor(data?: Partial<Customer>) {
Expand Down Expand Up @@ -508,7 +509,7 @@ export class CustomerClass extends Entity {
})
name?: string;
@hasMany(() => OrderClass ,{keyTo: 'mykey'})
@hasMany(() => OrderClass, {keyTo: 'mykey'})
orderClasses: OrderClass[];
constructor(data?: Partial<CustomerClass>) {
Expand Down Expand Up @@ -567,7 +568,7 @@ export class CustomerClassType extends Entity {
})
name?: string;
@hasMany(() => OrderClassType ,{keyTo: 'mykey'})
@hasMany(() => OrderClassType, {keyTo: 'mykey'})
orderClassTypes: OrderClassType[];
constructor(data?: Partial<CustomerClassType>) {
Expand Down Expand Up @@ -687,7 +688,7 @@ export class CustomerClass extends Entity {
})
name?: string;
@hasMany(() => OrderClass ,{keyTo: 'customerClassCustNumber'})
@hasMany(() => OrderClass, {keyTo: 'customerClassCustNumber'})
myOrders: OrderClass[];
constructor(data?: Partial<CustomerClass>) {
Expand Down Expand Up @@ -746,7 +747,7 @@ export class CustomerClassType extends Entity {
})
name?: string;
@hasMany(() => OrderClassType ,{keyTo: 'customerClassTypeCustNumber'})
@hasMany(() => OrderClassType, {keyTo: 'customerClassTypeCustNumber'})
myOrders: OrderClassType[];
constructor(data?: Partial<CustomerClassType>) {
Expand Down Expand Up @@ -835,7 +836,7 @@ export class CustomerClass extends Entity {
})
name?: string;
@hasMany(() => OrderClass ,{keyTo: 'customerClassCustNumber'})
@hasMany(() => OrderClass, {keyTo: 'customerClassCustNumber'})
orderClasses: OrderClass[];
constructor(data?: Partial<CustomerClass>) {
Expand Down Expand Up @@ -864,7 +865,7 @@ export class CustomerClassType extends Entity {
})
name?: string;
@hasMany(() => OrderClassType ,{keyTo: 'customerClassTypeCustNumber'})
@hasMany(() => OrderClassType, {keyTo: 'customerClassTypeCustNumber'})
orderClassTypes: OrderClassType[];
constructor(data?: Partial<CustomerClassType>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ describe('lb4 relation', function() {
relationType: 'belongsTo',
sourceModel: 'OrderClass',
destinationModel: 'CustomerClass',
registerInclusionResolver: false,
registerInclusionResolver: true,
},
{
relationType: 'belongsTo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ describe('lb4 relation HasMany', function() {
relationType: 'hasMany',
sourceModel: 'CustomerClass',
destinationModel: 'OrderClass',
registerInclusionResolver: false,
registerInclusionResolver: true,
},
{
relationType: 'hasMany',
Expand Down

0 comments on commit a77b297

Please sign in to comment.