From 19ec7d259b47762187ff3602b168318ded24b547 Mon Sep 17 00:00:00 2001 From: RobinBuschmann Date: Sun, 16 Jul 2017 15:48:14 +0200 Subject: [PATCH 1/3] fixes #43 --- lib/services/association.ts | 3 +- test/models/Manufacturer.ts | 13 ++++++-- test/models/ShoeWithScopes.ts | 6 ++++ test/specs/scopes.spec.ts | 60 +++++++++++++++++++++++++++++++++-- 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/lib/services/association.ts b/lib/services/association.ts index 956e7b6e..7dd1823a 100644 --- a/lib/services/association.ts +++ b/lib/services/association.ts @@ -144,7 +144,8 @@ export function getAssociationsByRelation(target: any, relatedClass: any): ISequ const associations = getAssociations(target); - return (associations || []).filter(association => association.relatedClassGetter() === relatedClass); + return (associations || []).filter(association => + association.relatedClassGetter().prototype === relatedClass.prototype); } /** diff --git a/test/models/Manufacturer.ts b/test/models/Manufacturer.ts index 27750c57..f8e8e49f 100644 --- a/test/models/Manufacturer.ts +++ b/test/models/Manufacturer.ts @@ -1,13 +1,22 @@ -import {Table, Model, Column, HasMany} from "../../index"; +import {Table, Model, Column, HasMany, Scopes} from "../../index"; import {ShoeWithScopes} from "./ShoeWithScopes"; - +@Scopes({ + brandOnly: { + attributes: { + exclude: ['notInScopeBrandOnly'] + } + } +}) @Table export class Manufacturer extends Model { @Column brand: string; + @Column + notInScopeBrandOnly: string; + @HasMany(() => ShoeWithScopes) shoes: ShoeWithScopes[]; } diff --git a/test/models/ShoeWithScopes.ts b/test/models/ShoeWithScopes.ts index 57d25980..a892082f 100644 --- a/test/models/ShoeWithScopes.ts +++ b/test/models/ShoeWithScopes.ts @@ -12,6 +12,12 @@ export const SHOE_SCOPES = { }, yellow: { where: {primaryColor: 'yellow'} + }, + red: { + where: {primaryColor: 'red'} + }, + manufacturerWithScope: { + include: [() => Manufacturer.scope('brandOnly')] } }; diff --git a/test/specs/scopes.spec.ts b/test/specs/scopes.spec.ts index 79df58bc..f9769873 100644 --- a/test/specs/scopes.spec.ts +++ b/test/specs/scopes.spec.ts @@ -41,7 +41,8 @@ describe('scopes', () => { secondaryColor: 'blue', producedAt: new Date(), manufacturer: { - brand: BRAND + brand: BRAND, + notInScopeBrandOnly: 'invisible :)', }, owner: { name: OWNER @@ -78,7 +79,7 @@ describe('scopes', () => { .unscoped() .findOne() .then(shoe => { - expect(shoe).to.have.property('secretKey').which.is.not.null; + expect(shoe).to.have.property('secretKey').which.is.a('string'); }) ); @@ -140,6 +141,61 @@ describe('scopes', () => { }) ); + describe('with using scoped included model', () => { + + it('should consider scope of included model (without own scope)', () => + ShoeWithScopes + .findOne({ + include: [Manufacturer.scope('brandOnly')] + }) + .then(shoe => { + expect(shoe).to.have.property('manufacturer') + .that.have.property('notInScopeBrandOnly') + .which.is.undefined; + }) + ); + + it('should consider scope of included model (with own scope)', () => + ShoeWithScopes + .scope('red') + .findOne({ + include: [Manufacturer.scope('brandOnly')] + }) + .then(shoe => { + expect(shoe).to.have.property('manufacturer') + .that.have.property('notInScopeBrandOnly') + .which.is.undefined; + }) + ); + + }); + + }); + + describe('with nested scope', () => { + + it('should consider nested scope', () => + ShoeWithScopes + .scope('manufacturerWithScope') + .findOne() + .then(shoe => { + expect(shoe).to.have.property('manufacturer') + .that.have.property('notInScopeBrandOnly') + .which.is.undefined; + }) + ); + + it('should not consider nested scope', () => + ShoeWithScopes + .scope('full') + .findOne() + .then(shoe => { + expect(shoe).to.have.property('manufacturer') + .that.have.property('notInScopeBrandOnly') + .which.is.a('string'); + }) + ); + }); }); From 1531d7edc59ab6e45187e4192d36cabdc805f6ea Mon Sep 17 00:00:00 2001 From: RobinBuschmann Date: Sun, 16 Jul 2017 16:45:45 +0200 Subject: [PATCH 2/3] fixed for v4 --- lib/services/association.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/services/association.ts b/lib/services/association.ts index 7dd1823a..4e9d6ab9 100644 --- a/lib/services/association.ts +++ b/lib/services/association.ts @@ -141,11 +141,15 @@ export function setAssociations(target: any, associations: ISequelizeAssociation } export function getAssociationsByRelation(target: any, relatedClass: any): ISequelizeAssociation[] { - const associations = getAssociations(target); - return (associations || []).filter(association => - association.relatedClassGetter().prototype === relatedClass.prototype); + return (associations || []).filter(association => { + const _relatedClass = association.relatedClassGetter(); + return ( + _relatedClass.prototype === relatedClass.prototype || // v3 + v4 + relatedClass.prototype instanceof _relatedClass // v4 (for child classes) + ); + }); } /** From 3102c69a5bf6c6cb6beae92b777540bb2da9ddd5 Mon Sep 17 00:00:00 2001 From: RobinBuschmann Date: Sun, 16 Jul 2017 17:38:21 +0200 Subject: [PATCH 3/3] make istanbul happy --- lib/services/association.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/services/association.ts b/lib/services/association.ts index 4e9d6ab9..522d82b6 100644 --- a/lib/services/association.ts +++ b/lib/services/association.ts @@ -147,6 +147,7 @@ export function getAssociationsByRelation(target: any, relatedClass: any): ISequ const _relatedClass = association.relatedClassGetter(); return ( _relatedClass.prototype === relatedClass.prototype || // v3 + v4 + /* istanbul ignore next */ relatedClass.prototype instanceof _relatedClass // v4 (for child classes) ); });