3
3
// This file is licensed under the MIT License.
4
4
// License text available at https://opensource.org/licenses/MIT
5
5
6
+ 'use strict' ;
7
+
6
8
require ( './init.js' ) ;
7
- var ds = getDataSource ( ) ;
9
+ var ds = global . getDataSource ( ) ;
8
10
9
11
describe ( 'mongodb custom id name' , function ( ) {
10
12
var Customer = ds . createModel (
11
13
'customer' ,
12
- { seq : { type : Number , id : true } , name : String , emails : [ String ] , age : Number } ,
13
- { forceId : false } ) ;
14
+ {
15
+ seq : { type : Number , id : true } ,
16
+ name : String ,
17
+ emails : [ String ] ,
18
+ age : Number ,
19
+ } ,
20
+ { forceId : false }
21
+ ) ;
14
22
before ( function ( done ) {
15
23
Customer . deleteAll ( done ) ;
16
24
} ) ;
17
25
18
26
it ( 'should allow custom name for the id property for create' , function ( done ) {
19
- Customer . create ( {
20
- seq : 1 ,
21
- name : 'John1' ,
22
- emails : [ 'john@x.com' , 'john@y.com' ] ,
23
- age : 30 ,
24
- } , function ( err , customer ) {
25
- customer . seq . should . equal ( 1 ) ;
26
- Customer . create ( {
27
- seq : 2 ,
28
- name : 'John2' ,
29
- emails : [ 'john2@x.com' , 'john2@y.com' ] ,
30
- age : 40 ,
31
- } , function ( err , customer ) {
32
- customer . seq . should . equal ( 2 ) ;
33
- done ( err , customer ) ;
34
- } ) ;
35
- } ) ;
27
+ Customer . create (
28
+ {
29
+ seq : 1 ,
30
+ name : 'John1' ,
31
+ emails : [ 'john@x.com' , 'john@y.com' ] ,
32
+ age : 30 ,
33
+ } ,
34
+ function ( err , customer ) {
35
+ customer . seq . should . equal ( 1 ) ;
36
+ Customer . create (
37
+ {
38
+ seq : 2 ,
39
+ name : 'John2' ,
40
+ emails : [ 'john2@x.com' , 'john2@y.com' ] ,
41
+ age : 40 ,
42
+ } ,
43
+ function ( err , customer ) {
44
+ customer . seq . should . equal ( 2 ) ;
45
+ done ( err , customer ) ;
46
+ }
47
+ ) ;
48
+ }
49
+ ) ;
36
50
} ) ;
37
51
38
52
it ( 'should allow custom name for the id property for findById' , function ( done ) {
@@ -43,7 +57,7 @@ describe('mongodb custom id name', function() {
43
57
} ) ;
44
58
45
59
it ( 'should allow inq with find' , function ( done ) {
46
- Customer . find ( { where : { seq : { inq : [ 1 ] } } } , function ( err , customers ) {
60
+ Customer . find ( { where : { seq : { inq : [ 1 ] } } } , function ( err , customers ) {
47
61
customers . length . should . equal ( 1 ) ;
48
62
customers [ 0 ] . seq . should . equal ( 1 ) ;
49
63
done ( err ) ;
@@ -54,35 +68,47 @@ describe('mongodb custom id name', function() {
54
68
describe ( 'mongodb string id' , function ( ) {
55
69
var Customer = ds . createModel (
56
70
'customer2' ,
57
- { seq : { type : String , id : true } , name : String , emails : [ String ] , age : Number } ,
58
- { forceId : false } ) ;
71
+ {
72
+ seq : { type : String , id : true } ,
73
+ name : String ,
74
+ emails : [ String ] ,
75
+ age : Number ,
76
+ } ,
77
+ { forceId : false }
78
+ ) ;
59
79
var customer1 , customer2 ;
60
80
61
81
before ( function ( done ) {
62
82
Customer . deleteAll ( done ) ;
63
83
} ) ;
64
84
65
85
it ( 'should allow custom name for the id property for create' , function ( done ) {
66
- Customer . create ( {
67
- seq : '1' ,
68
- name : 'John1' ,
69
- emails : [ 'john@x.com' , 'john@y.com' ] ,
70
- age : 30 ,
71
- } , function ( err , customer ) {
72
- customer . seq . should . equal ( '1' ) ;
73
- customer1 = customer ;
74
- var customer2Id = new ds . ObjectID ( ) . toString ( ) ;
75
- Customer . create ( {
76
- seq : customer2Id ,
77
- name : 'John2' ,
78
- emails : [ 'john2@x.com' , 'john2@y.com' ] ,
79
- age : 40 ,
80
- } , function ( err , customer ) {
81
- customer2 = customer ;
82
- customer . seq . toString ( ) . should . eql ( customer2Id ) ;
83
- done ( err , customer ) ;
84
- } ) ;
85
- } ) ;
86
+ Customer . create (
87
+ {
88
+ seq : '1' ,
89
+ name : 'John1' ,
90
+ emails : [ 'john@x.com' , 'john@y.com' ] ,
91
+ age : 30 ,
92
+ } ,
93
+ function ( err , customer ) {
94
+ customer . seq . should . equal ( '1' ) ;
95
+ customer1 = customer ;
96
+ var customer2Id = new ds . ObjectID ( ) . toString ( ) ;
97
+ Customer . create (
98
+ {
99
+ seq : customer2Id ,
100
+ name : 'John2' ,
101
+ emails : [ 'john2@x.com' , 'john2@y.com' ] ,
102
+ age : 40 ,
103
+ } ,
104
+ function ( err , customer ) {
105
+ customer2 = customer ;
106
+ customer . seq . toString ( ) . should . eql ( customer2Id ) ;
107
+ done ( err , customer ) ;
108
+ }
109
+ ) ;
110
+ }
111
+ ) ;
86
112
} ) ;
87
113
88
114
it ( 'should allow custom name for the id property for findById' , function ( done ) {
@@ -93,15 +119,18 @@ describe('mongodb string id', function() {
93
119
} ) ;
94
120
95
121
it ( 'should allow inq with find' , function ( done ) {
96
- Customer . find ( { where : { seq : { inq : [ 1 ] } } } , function ( err , customers ) {
122
+ Customer . find ( { where : { seq : { inq : [ 1 ] } } } , function ( err , customers ) {
97
123
customers . length . should . equal ( 1 ) ;
98
124
customers [ 0 ] . seq . should . equal ( '1' ) ;
99
125
done ( err ) ;
100
126
} ) ;
101
127
} ) ;
102
128
103
- it ( 'should allow inq with find' , function ( done ) {
104
- Customer . find ( { where : { seq : { inq : [ customer2 . seq ] } } } , function ( err , customers ) {
129
+ it ( 'should allow inq with find - test 2' , function ( done ) {
130
+ Customer . find ( { where : { seq : { inq : [ customer2 . seq ] } } } , function (
131
+ err ,
132
+ customers
133
+ ) {
105
134
customers . length . should . equal ( 1 ) ;
106
135
// seq is now a string
107
136
customers [ 0 ] . seq . should . eql ( customer2 . seq . toString ( ) ) ;
@@ -113,30 +142,39 @@ describe('mongodb string id', function() {
113
142
describe ( 'mongodb default id type' , function ( ) {
114
143
var Account = ds . createModel (
115
144
'account' ,
116
- { seq : { id : true , generated : true } , name : String , emails : [ String ] , age : Number } ,
117
- { forceId : false } ) ;
145
+ {
146
+ seq : { id : true , generated : true } ,
147
+ name : String ,
148
+ emails : [ String ] ,
149
+ age : Number ,
150
+ } ,
151
+ { forceId : false }
152
+ ) ;
118
153
119
154
before ( function ( done ) {
120
155
Account . deleteAll ( done ) ;
121
156
} ) ;
122
157
123
158
var id ;
124
159
it ( 'should generate id value for create' , function ( done ) {
125
- Account . create ( {
126
- name : 'John1' ,
127
- emails : [ 'john@x.com' , 'john@y.com' ] ,
128
- age : 30 ,
129
- } , function ( err , account ) {
130
- if ( err ) return done ( err ) ;
131
- account . should . have . property ( 'seq' ) ;
132
- id = account . seq ;
133
- Account . findById ( id , function ( err , account1 ) {
160
+ Account . create (
161
+ {
162
+ name : 'John1' ,
163
+ emails : [ 'john@x.com' , 'john@y.com' ] ,
164
+ age : 30 ,
165
+ } ,
166
+ function ( err , account ) {
134
167
if ( err ) return done ( err ) ;
135
- account1 . seq . should . eql ( account . seq ) ;
136
168
account . should . have . property ( 'seq' ) ;
137
- done ( err , account1 ) ;
138
- } ) ;
139
- } ) ;
169
+ id = account . seq ;
170
+ Account . findById ( id , function ( err , account1 ) {
171
+ if ( err ) return done ( err ) ;
172
+ account1 . seq . should . eql ( account . seq ) ;
173
+ account . should . have . property ( 'seq' ) ;
174
+ done ( err , account1 ) ;
175
+ } ) ;
176
+ }
177
+ ) ;
140
178
} ) ;
141
179
142
180
it ( 'should be able to find by string id' , function ( done ) {
@@ -161,23 +199,27 @@ describe('mongodb default id type', function() {
161
199
describe ( 'mongodb default id name' , function ( ) {
162
200
var Customer1 = ds . createModel (
163
201
'customer1' ,
164
- { name : String , emails : [ String ] , age : Number } ,
165
- { forceId : false } ) ;
202
+ { name : String , emails : [ String ] , age : Number } ,
203
+ { forceId : false }
204
+ ) ;
166
205
167
206
before ( function ( done ) {
168
207
Customer1 . deleteAll ( done ) ;
169
208
} ) ;
170
209
171
210
it ( 'should allow value for the id property for create' , function ( done ) {
172
- Customer1 . create ( {
173
- id : 1 ,
174
- name : 'John1' ,
175
- emails : [ 'john@x.com' , 'john@y.com' ] ,
176
- age : 30 ,
177
- } , function ( err , customer ) {
178
- customer . id . should . equal ( 1 ) ;
179
- done ( err , customer ) ;
180
- } ) ;
211
+ Customer1 . create (
212
+ {
213
+ id : 1 ,
214
+ name : 'John1' ,
215
+ emails : [ 'john@x.com' , 'john@y.com' ] ,
216
+ age : 30 ,
217
+ } ,
218
+ function ( err , customer ) {
219
+ customer . id . should . equal ( 1 ) ;
220
+ done ( err , customer ) ;
221
+ }
222
+ ) ;
181
223
} ) ;
182
224
183
225
it ( 'should allow value the id property for findById' , function ( done ) {
@@ -188,18 +230,20 @@ describe('mongodb default id name', function() {
188
230
} ) ;
189
231
190
232
it ( 'should generate id value for create' , function ( done ) {
191
- Customer1 . create ( {
192
- name : 'John1' ,
193
- emails : [ 'john@x.com' , 'john@y.com' ] ,
194
- age : 30 ,
195
- } , function ( err , customer ) {
196
- // console.log(customer);
197
- customer . should . have . property ( 'id' ) ;
198
- Customer1 . findById ( customer . id , function ( err , customer1 ) {
199
- customer1 . id . should . eql ( customer . id ) ;
200
- done ( err , customer ) ;
201
- } ) ;
202
- } ) ;
233
+ Customer1 . create (
234
+ {
235
+ name : 'John1' ,
236
+ emails : [ 'john@x.com' , 'john@y.com' ] ,
237
+ age : 30 ,
238
+ } ,
239
+ function ( err , customer ) {
240
+ // console.log(customer);
241
+ customer . should . have . property ( 'id' ) ;
242
+ Customer1 . findById ( customer . id , function ( err , customer1 ) {
243
+ customer1 . id . should . eql ( customer . id ) ;
244
+ done ( err , customer ) ;
245
+ } ) ;
246
+ }
247
+ ) ;
203
248
} ) ;
204
249
} ) ;
205
-
0 commit comments