Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 209 scope symbols #210

Merged
merged 2 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ export function deepAssign(target: any, ...sources: any[]): any {
.getOwnPropertyNames(source)
.forEach(key => assign(key, target, source))
;
/* istanbul ignore next */
if (Object.getOwnPropertySymbols) {
Object
.getOwnPropertySymbols(source)
.forEach(key => assign(key, target, source))
;
}
});

return target;

function assign(key: string | number, _target: any, _source: any): void {
function assign(key: string | number | symbol, _target: any, _source: any): void {
const sourceValue = _source[key];

if (sourceValue !== void 0) {
Expand Down
97 changes: 78 additions & 19 deletions test/specs/scopes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {useFakeTimers} from 'sinon';
import {Op} from 'sequelize';
import {createSequelize} from "../utils/sequelize";
import {getScopeOptions} from "../../lib/services/scopes";
import {ShoeWithScopes, SHOE_DEFAULT_SCOPE, SHOE_SCOPES} from "../models/ShoeWithScopes";
import {Manufacturer} from "../models/Manufacturer";
import {Person} from "../models/Person";
import {Model} from '../../lib/models/Model';
import {Table} from '../../lib/annotations/Table';
import {Scopes} from '../../lib/annotations/Scopes';
import {majorVersion} from '../../lib/utils/versioning';
import {Column} from '../../lib/annotations/Column';
import {UpdatedAt} from '../../lib/annotations/UpdatedAt';
import chaiDatetime = require('chai-datetime');

use(chaiAsPromised);
use(chaiDatetime);

describe('scopes', () => {

Expand Down Expand Up @@ -92,30 +102,30 @@ describe('scopes', () => {
it('should consider scopes and additional included model (object)', () =>
expect(
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
.findOne({
include: [{
model: Person,
}]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer').which.is.not.null;
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
expect(shoe).to.have.property('owner').which.is.not.null;
})
.findOne({
include: [{
model: Person,
}]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer').which.is.not.null;
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
expect(shoe).to.have.property('owner').which.is.not.null;
})
).not.to.be.rejected
);

it('should consider scopes and additional included model (model)', () =>
expect(
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
.findOne({
include: [Person]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer').which.is.not.null;
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
expect(shoe).to.have.property('owner').which.is.not.null;
})
.findOne({
include: [Person]
})
.then(shoe => {
expect(shoe).to.have.property('manufacturer').which.is.not.null;
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
expect(shoe).to.have.property('owner').which.is.not.null;
})
).not.to.be.rejected
);

Expand All @@ -134,7 +144,7 @@ describe('scopes', () => {
it('should not consider default scope due to unscoped call, but additonal includes (model)', () =>

(ShoeWithScopes
.unscoped() as typeof ShoeWithScopes)
.unscoped() as typeof ShoeWithScopes)
.findOne({
include: [Person]
})
Expand Down Expand Up @@ -219,6 +229,55 @@ describe('scopes', () => {

});

if (majorVersion > 3) {

describe('with symbols', () => {
const _sequelize = createSequelize(false);

@Scopes({
bob: {where: {name: {[Op.like]: '%bob%'}}},
updated: {where: {updated: {[Op.gt]: new Date(2000, 1)}}},
})
@Table
class Person extends Model<Person> {

@Column
name: string;

@UpdatedAt
updated: Date;
}

_sequelize.addModels([Person]);

beforeEach(() => _sequelize.sync({force: true}));

it('should consider symbols while finding elements', () => {
return Person
.create({name: '1bob2'})
.then(() => Person.create({name: 'bob'}))
.then(() => Person.create({name: 'bobby'}))
.then(() => Person.create({name: 'robert'}))
.then(() => (Person.scope('bob') as typeof Person).findAll())
.then(persons => expect(persons).to.have.property('length', 3))
;
});

it('should consider symbols on timestamp column while finding elements', () => {
const clock = useFakeTimers(+new Date());
return Person
.create({name: 'test'})
.then(() => (Person.scope('updated') as typeof Person).findAll())
.then(() => Person.findAll())
.then(persons => expect(persons).to.have.property('length', 1))
.then(() => clock.restore())
;
});

});
}


});

});
23 changes: 21 additions & 2 deletions test/specs/utils/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ describe('utils', () => {
const childSourceA = {f: childSourceF};
const childSourceB = {};
const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim, notNull: null};
const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden', nullable: null, notNull: 'notNull'};
const source2 = {
e: 'für elisa',
g: () => null,
arr: [{h: 1}, {}, 'e'],
over: 'ridden',
nullable: null,
notNull: 'notNull'
};
const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2));

it('should not be undefined', () => {
Expand Down Expand Up @@ -110,15 +117,27 @@ describe('utils', () => {

it('should keep prototype chain', () => {
class Test {
protoFn(): any {}
protoFn(): any {
}
}

const copy = deepAssign({}, {test: new Test()});

expect(copy.test)
.to.have.property('protoFn')
.that.is.a('function');
});

if (Object.getOwnPropertySymbols) {
it('should copy symbol based objects', () => {
const symbol = Symbol('test');
const value = 'test';
const copy = deepAssign({}, {[symbol]: value});

expect(copy[symbol]).to.equal(value);
});
}

});

});
Expand Down