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
@@ -92,30 +102,30 @@ describe('scopes', () => {
92
102
it ( 'should consider scopes and additional included model (object)' , ( ) =>
93
103
expect (
94
104
( ShoeWithScopes . scope ( 'full' ) as typeof ShoeWithScopes )
95
- . findOne ( {
96
- include : [ {
97
- model : Person ,
98
- } ]
99
- } )
100
- . then ( shoe => {
101
- expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . is . not . null ;
102
- expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . have . property ( 'brand' , BRAND ) ;
103
- expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
104
- } )
105
+ . findOne ( {
106
+ include : [ {
107
+ model : Person ,
108
+ } ]
109
+ } )
110
+ . then ( shoe => {
111
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . is . not . null ;
112
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . have . property ( 'brand' , BRAND ) ;
113
+ expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
114
+ } )
105
115
) . not . to . be . rejected
106
116
) ;
107
117
108
118
it ( 'should consider scopes and additional included model (model)' , ( ) =>
109
119
expect (
110
120
( ShoeWithScopes . scope ( 'full' ) as typeof ShoeWithScopes )
111
- . findOne ( {
112
- include : [ Person ]
113
- } )
114
- . then ( shoe => {
115
- expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . is . not . null ;
116
- expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . have . property ( 'brand' , BRAND ) ;
117
- expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
118
- } )
121
+ . findOne ( {
122
+ include : [ Person ]
123
+ } )
124
+ . then ( shoe => {
125
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . is . not . null ;
126
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . have . property ( 'brand' , BRAND ) ;
127
+ expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
128
+ } )
119
129
) . not . to . be . rejected
120
130
) ;
121
131
@@ -134,7 +144,7 @@ describe('scopes', () => {
134
144
it ( 'should not consider default scope due to unscoped call, but additonal includes (model)' , ( ) =>
135
145
136
146
( ShoeWithScopes
137
- . unscoped ( ) as typeof ShoeWithScopes )
147
+ . unscoped ( ) as typeof ShoeWithScopes )
138
148
. findOne ( {
139
149
include : [ Person ]
140
150
} )
@@ -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