|
1 | 1 | import {expect, use} from 'chai'; |
2 | 2 | import * as chaiAsPromised from 'chai-as-promised'; |
| 3 | +import {useFakeTimers} from 'sinon'; |
| 4 | +import {Op} from 'sequelize'; |
3 | 5 | import {createSequelize} from "../utils/sequelize"; |
4 | 6 | import {getScopeOptions} from "../../lib/services/scopes"; |
5 | 7 | import {ShoeWithScopes, SHOE_DEFAULT_SCOPE, SHOE_SCOPES} from "../models/ShoeWithScopes"; |
6 | 8 | import {Manufacturer} from "../models/Manufacturer"; |
7 | 9 | import {Person} from "../models/Person"; |
| 10 | +import {Model} from '../../lib/models/Model'; |
| 11 | +import {Table} from '../../lib/annotations/Table'; |
| 12 | +import {Scopes} from '../../lib/annotations/Scopes'; |
| 13 | +import {majorVersion} from '../../lib/utils/versioning'; |
| 14 | +import {Column} from '../../lib/annotations/Column'; |
| 15 | +import {UpdatedAt} from '../../lib/annotations/UpdatedAt'; |
| 16 | +import chaiDatetime = require('chai-datetime'); |
8 | 17 |
|
9 | 18 | use(chaiAsPromised); |
| 19 | +use(chaiDatetime); |
10 | 20 |
|
11 | 21 | describe('scopes', () => { |
12 | 22 |
|
@@ -219,6 +229,55 @@ describe('scopes', () => { |
219 | 229 |
|
220 | 230 | }); |
221 | 231 |
|
| 232 | + if (majorVersion > 3) { |
| 233 | + |
| 234 | + describe('with symbols', () => { |
| 235 | + const _sequelize = createSequelize(false); |
| 236 | + |
| 237 | + @Scopes({ |
| 238 | + bob: {where: {name: {[Op.like]: '%bob%'}}}, |
| 239 | + updated: {where: {updated: {[Op.gt]: new Date(2000, 1)}}}, |
| 240 | + }) |
| 241 | + @Table |
| 242 | + class Person extends Model<Person> { |
| 243 | + |
| 244 | + @Column |
| 245 | + name: string; |
| 246 | + |
| 247 | + @UpdatedAt |
| 248 | + updated: Date; |
| 249 | + } |
| 250 | + |
| 251 | + _sequelize.addModels([Person]); |
| 252 | + |
| 253 | + beforeEach(() => _sequelize.sync({force: true})); |
| 254 | + |
| 255 | + it('should consider symbols while finding elements', () => { |
| 256 | + return Person |
| 257 | + .create({name: '1bob2'}) |
| 258 | + .then(() => Person.create({name: 'bob'})) |
| 259 | + .then(() => Person.create({name: 'bobby'})) |
| 260 | + .then(() => Person.create({name: 'robert'})) |
| 261 | + .then(() => (Person.scope('bob') as typeof Person).findAll()) |
| 262 | + .then(persons => expect(persons).to.have.property('length', 3)) |
| 263 | + ; |
| 264 | + }); |
| 265 | + |
| 266 | + it('should consider symbols on timestamp column while finding elements', () => { |
| 267 | + const clock = useFakeTimers(+new Date()); |
| 268 | + return Person |
| 269 | + .create({name: 'test'}) |
| 270 | + .then(() => (Person.scope('updated') as typeof Person).findAll()) |
| 271 | + .then(() => Person.findAll()) |
| 272 | + .then(persons => expect(persons).to.have.property('length', 1)) |
| 273 | + .then(() => clock.restore()) |
| 274 | + ; |
| 275 | + }); |
| 276 | + |
| 277 | + }); |
| 278 | + } |
| 279 | + |
| 280 | + |
222 | 281 | }); |
223 | 282 |
|
224 | 283 | }); |
0 commit comments