1 | juggling = require('jugglingdb')
|
2 | Schema = juggling.Schema
|
3 | Text = Schema.Text
|
4 |
|
5 | DBNAME = 'myapp_test'
|
6 | DBUSER = 'root'
|
7 | DBPASS = ''
|
8 | DBENGINE = 'mysql'
|
9 |
|
10 | schema = new Schema __dirname + '/..', database: '', username: DBUSER, password: DBPASS
|
11 | schema.log = (q) -> console.log q
|
12 |
|
13 | query = (sql, cb) ->
|
14 | schema.adapter.query sql, cb
|
15 |
|
16 | User = schema.define 'User',
|
17 | email: { type: String, null: false, index: true }
|
18 | name: String
|
19 | bio: Text
|
20 | password: String
|
21 | birthDate: Date
|
22 | pendingPeriod: Number
|
23 | createdByAdmin: Boolean
|
24 | , indexes:
|
25 | index1:
|
26 | columns: 'email, createdByAdmin'
|
27 |
|
28 | withBlankDatabase = (cb) ->
|
29 | db = schema.settings.database = DBNAME
|
30 | query 'DROP DATABASE IF EXISTS ' + db, (err) ->
|
31 | query 'CREATE DATABASE ' + db, (err) ->
|
32 | query 'USE '+ db, cb
|
33 |
|
34 | getFields = (model, cb) ->
|
35 | query 'SHOW FIELDS FROM ' + model, (err, res) ->
|
36 | if err
|
37 | cb err
|
38 | else
|
39 | fields = {}
|
40 | res.forEach (field) -> fields[field.Field] = field
|
41 | cb err, fields
|
42 |
|
43 | getIndexes = (model, cb) ->
|
44 | query 'SHOW INDEXES FROM ' + model, (err, res) ->
|
45 | if err
|
46 | console.log err
|
47 | cb err
|
48 | else
|
49 | indexes = {}
|
50 |
|
51 | res.forEach (index) ->
|
52 | indexes[index.Key_name] = index if parseInt(index.Seq_in_index, 10) == 1
|
53 | cb err, indexes
|
54 |
|
55 | it = (name, testCases) ->
|
56 | module.exports[name] = testCases
|
57 |
|
58 | it 'should run migration', (test) ->
|
59 | withBlankDatabase (err) ->
|
60 | schema.automigrate ->
|
61 | getFields 'User', (err, fields) ->
|
62 | test.deepEqual fields,
|
63 | id:
|
64 | Field: 'id'
|
65 | Type: 'int(11)'
|
66 | Null: 'NO'
|
67 | Key: 'PRI'
|
68 | Default: null
|
69 | Extra: 'auto_increment'
|
70 | email:
|
71 | Field: 'email'
|
72 | Type: 'varchar(255)'
|
73 | Null: 'NO'
|
74 | Key: 'MUL'
|
75 | Default: null
|
76 | Extra: ''
|
77 | name:
|
78 | Field: 'name'
|
79 | Type: 'varchar(255)'
|
80 | Null: 'YES'
|
81 | Key: ''
|
82 | Default: null
|
83 | Extra: ''
|
84 | bio:
|
85 | Field: 'bio'
|
86 | Type: 'text'
|
87 | Null: 'YES'
|
88 | Key: ''
|
89 | Default: null
|
90 | Extra: ''
|
91 | password:
|
92 | Field: 'password'
|
93 | Type: 'varchar(255)'
|
94 | Null: 'YES'
|
95 | Key: ''
|
96 | Default: null
|
97 | Extra: ''
|
98 | birthDate:
|
99 | Field: 'birthDate'
|
100 | Type: 'datetime'
|
101 | Null: 'YES'
|
102 | Key: ''
|
103 | Default: null
|
104 | Extra: ''
|
105 | pendingPeriod:
|
106 | Field: 'pendingPeriod'
|
107 | Type: 'int(11)'
|
108 | Null: 'YES'
|
109 | Key: ''
|
110 | Default: null
|
111 | Extra: ''
|
112 | createdByAdmin:
|
113 | Field: 'createdByAdmin'
|
114 | Type: 'tinyint(1)'
|
115 | Null: 'YES'
|
116 | Key: ''
|
117 | Default: null
|
118 | Extra: ''
|
119 |
|
120 | getIndexes 'User', (err, fields) ->
|
121 | test.deepEqual fields,
|
122 | PRIMARY:
|
123 | Table: 'User'
|
124 | Non_unique: 0
|
125 | Key_name: 'PRIMARY'
|
126 | Seq_in_index: 1
|
127 | Column_name: 'id'
|
128 | Collation: 'A'
|
129 | Cardinality: 0
|
130 | Sub_part: null
|
131 | Packed: null
|
132 | Null: ''
|
133 | Index_type: 'BTREE'
|
134 | Comment: ''
|
135 | Index_comment: ''
|
136 | email:
|
137 | Table: 'User'
|
138 | Non_unique: 1
|
139 | Key_name: 'email'
|
140 | Seq_in_index: 1
|
141 | Column_name: 'email'
|
142 | Collation: 'A'
|
143 | Cardinality: 0
|
144 | Sub_part: null
|
145 | Packed: null
|
146 | Null: ''
|
147 | Index_type: 'BTREE'
|
148 | Comment: ''
|
149 | Index_comment: ''
|
150 | index1:
|
151 | Table: 'User'
|
152 | Non_unique: 1
|
153 | Key_name: 'index1'
|
154 | Seq_in_index: 1
|
155 | Column_name: 'email'
|
156 | Collation: 'A'
|
157 | Cardinality: 0
|
158 | Sub_part: null
|
159 | Packed: null
|
160 | Null: ''
|
161 | Index_type: 'BTREE'
|
162 | Comment: ''
|
163 | Index_comment: ''
|
164 | test.done()
|
165 |
|
166 | it 'should autoupgrade', (test) ->
|
167 | userExists = (cb) ->
|
168 | query 'SELECT * FROM User', (err, res) ->
|
169 | cb(not err and res[0].email == 'test@example.com')
|
170 |
|
171 | User.create email: 'test@example.com', (err, user) ->
|
172 | test.ok not err
|
173 | userExists (yep) ->
|
174 | test.ok yep
|
175 | User.defineProperty 'email', type: String
|
176 | User.defineProperty 'name', type: String, limit: 50
|
177 | User.defineProperty 'newProperty', type: Number
|
178 | User.defineProperty 'pendingPeriod', false
|
179 | schema.autoupdate (err) ->
|
180 | getFields 'User', (err, fields) ->
|
181 |
|
182 | test.equal fields.email.Null, 'YES', 'Email is not null'
|
183 |
|
184 | test.equal fields.name.Type, 'varchar(50)', 'Name is not varchar(50)'
|
185 |
|
186 | test.ok fields.newProperty, 'New column was not added'
|
187 | if fields.newProperty
|
188 | test.equal fields.newProperty.Type, 'int(11)', 'New column type is not int(11)'
|
189 |
|
190 | test.ok not fields.pendingPeriod, 'drop column'
|
191 |
|
192 |
|
193 | userExists (yep) ->
|
194 | test.ok yep
|
195 | test.done()
|
196 |
|
197 | it "record should be updated", (done) ->
|
198 | userExists = (cb) ->
|
199 | query "SELECT * FROM UserData", (err, res) ->
|
200 | cb not err and res[0].email is "yourname@newname.com"
|
201 |
|
202 | UserData.update
|
203 | where:
|
204 | id: "1"
|
205 |
|
206 | update:
|
207 | email: "yourname@newname.com"
|
208 | , (err, o) ->
|
209 | userExists (yep) ->
|
210 | assert.ok yep, "Email has changed"
|
211 |
|
212 |
|
213 | UserData.update
|
214 | update:
|
215 | email: "yourname@newname.com"
|
216 | , (err, o) ->
|
217 | assert.equal err, "Where or Update fields are missing", " no error when where field is missing "
|
218 |
|
219 |
|
220 | UserData.update
|
221 | where:
|
222 | id: "1"
|
223 | , (err, o) ->
|
224 | assert.equal err, "Where or Update fields are missing", " no error when update field is missing "
|
225 | done()
|
226 |
|
227 | it 'should check actuality of schema', (test) ->
|
228 |
|
229 | User.schema.isActual (err, ok) ->
|
230 | test.ok ok, 'schema is actual'
|
231 | User.defineProperty 'email', false
|
232 | User.schema.isActual (err, ok) ->
|
233 | test.ok not ok, 'schema is not actual'
|
234 | test.done()
|
235 |
|
236 | it 'should add single-column index', (test) ->
|
237 | User.defineProperty 'email', type: String, index: { kind: 'FULLTEXT', type: 'HASH'}
|
238 | User.schema.autoupdate (err) ->
|
239 | return console.log(err) if err
|
240 | getIndexes 'User', (err, ixs) ->
|
241 | test.ok ixs.email && ixs.email.Column_name == 'email'
|
242 | console.log(ixs)
|
243 | test.equal ixs.email.Index_type, 'BTREE', 'default index type'
|
244 | test.done()
|
245 |
|
246 | it 'should change type of single-column index', (test) ->
|
247 | User.defineProperty 'email', type: String, index: { type: 'BTREE' }
|
248 | User.schema.isActual (err, ok) ->
|
249 | test.ok ok, 'schema is actual'
|
250 | User.schema.autoupdate (err) ->
|
251 | return console.log(err) if err
|
252 | getIndexes 'User', (err, ixs) ->
|
253 | test.ok ixs.email && ixs.email.Column_name == 'email'
|
254 | test.equal ixs.email.Index_type, 'BTREE'
|
255 | test.done()
|
256 |
|
257 | it 'should remove single-column index', (test) ->
|
258 | User.defineProperty 'email', type: String, index: false
|
259 | User.schema.autoupdate (err) ->
|
260 | return console.log(err) if err
|
261 | getIndexes 'User', (err, ixs) ->
|
262 | test.ok !ixs.email
|
263 | test.done()
|
264 |
|
265 | it 'should update multi-column index when order of columns changed', (test) ->
|
266 | User.schema.adapter._models.User.settings.indexes.index1.columns = 'createdByAdmin, email'
|
267 | User.schema.isActual (err, ok) ->
|
268 | test.ok not ok, 'schema is not actual'
|
269 | User.schema.autoupdate (err) ->
|
270 | return console.log(err) if err
|
271 | getIndexes 'User', (err, ixs) ->
|
272 | test.equals ixs.index1.Column_name, 'createdByAdmin'
|
273 | test.done()
|
274 |
|
275 |
|
276 | it 'test', (test) ->
|
277 | User.defineProperty 'email', type: String, index: true
|
278 | User.schema.autoupdate (err) ->
|
279 | User.schema.autoupdate (err) ->
|
280 | User.schema.autoupdate (err) ->
|
281 | test.done()
|
282 |
|
283 | it 'should disconnect when done', (test) ->
|
284 | schema.disconnect()
|
285 | test.done()
|
286 |
|