forked from shakefu/live-mongodb-fixtures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixture.js
360 lines (307 loc) · 10.6 KB
/
fixture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"use strict"
/**
* Test fixtures.
*/
// System
const fs = require('fs')
const path = require('path')
const assert = require('assert')
// 3rd party
const _ = require('lodash')
const async = require('async')
const { EJSON } = require('bson')
const debug = require('debug')('fixtures')
/**
* Fixture class itself.
*
* See the README for usage documentation.
*/
class Fixture {
constructor (options) {
// This is the fixture's name, it's just an identifier
assert(_.isString(options.name), "Fixture name must be String")
this.name = options.name
// Create our debug logger
this.debug = require('debug')(`fixtures:${this.name}`)
// We parse the collection options to prep it for use
assert(_.isObject(options.collections),
"Fixture collections must be Object")
this.fixtures = this.parse(options.collections)
// Get our list of keys for this
assert(_.isArray(options.keys), "Fixture keys must be Array")
this.keys = options.keys
// Default path is unset
this.path = options.path || null
}
static init (module) {
let fixtures = Fixture.getFixtures(module)
// Get the path to the fixture file, which will be the default path
if (module.filename) {
let dir = path.dirname(module.filename)
// Update all the fixtures that don't specify a path with the one
// given
_.each(fixtures, (fixture) => {
if (fixture.path) return
// Assign the directory we found if we don't have a path already
fixture.path = dir
})
}
// Debug output of the directory used for each Fixture
_.each(fixtures, (fixture) => {
debug(`${fixture.name}: directory: ${fixture.path}`)
})
if (module.parent) {
debug("Fixtures ready.")
return
}
debug("Starting fixture CLI")
// TODO: Pass in exports?
Fixture.main(module)
}
static getFixtures (module) {
let exports = module.exports
// Get all the exported fixtures in this module
let names = {}
let fixtures = []
_.each(exports, (fixture) => {
if (!(fixture instanceof Fixture)) return
if (names[fixture.name]) {
throw new Error(`Duplicate fixture name: ${fixture.name}`)
}
fixtures.push(fixture)
names[fixture.name] = true
})
return fixtures
}
parse (fixtures) {
let found = []
this.debug(`Parsing ${this.name}`)
// Update all the fixture values to be array, to handle single-item
// syntax seamlessly
fixtures = _.mapValues(fixtures, (value) => {
if (!_.isArray(value)) return [value]
return value
})
// this.debug(fixtures)
_.each(fixtures, (collections, key) => {
_.each(collections, (collection, i) => {
// This name will always be unique
let name = this.parseName(collection,
`${this.name}.${key}[${i}]`)
// Validate that our collecitons have a compatible API,
// including find, remove, and save
assert(_.isFunction(collection.find),
`Fixture collection ${name} missing find method`)
assert(_.isFunction(collection.remove),
`Fixture collection ${name} missing remove method`)
assert(_.isFunction(collection.save),
`Fixture collection ${name} missing save method`)
found.push({
name: name,
collection: collection,
key: key
})
this.debug(`Fixture collection ${name}: OK`)
})
})
this.debug(`Found ${found.length} fixture collections`)
return found
}
/**
* Return a name based on the available properties on the collection.
*/
parseName (collection, name) {
// Try to get a better name, but it may not be unique
// TODO: Figure out if this breaks in other non-HumbleJS ODMs
if (collection.name) {
name = `${this.name}.${collection.name}`
}
// Try again to get a better name, checking for uniqueness
if (collection._name) {
name = `${this.name}.${collection._name}`
}
// Try one last time to get a better name
if (collection.collection &&
collection.collection._name) {
name = `${this.name}.${collection.collection._name}`
}
return name
}
/**
* Load fixtures from EJSON into the database.
*/
load (callback) {
// Build all the loaders for the collections
let loaders = _.map(this.fixtures, (fixture) =>
this.loadDocs.bind(this, fixture))
async.parallel(loaders, (err, results) => {
if (err) return callback(err)
// this.debug(results)
callback(null, results.reduce((a, b) => a + b))
})
}
/**
* Query for fixtures in a database and write them to EJSON.
*/
get (callback) {
// Build our queries
let queries = _.map(this.fixtures, (fixture) =>
this.findDocs.bind(this, fixture))
async.parallel(queries, (err, results) => {
if (err) return callback(err)
// this.debug(results)
callback(null, results.reduce((a, b) => a + b))
})
}
/**
* Remove fixture documents.
*/
clear (callback) {
this.debug(`Removing documents.`)
let queries = _.map(this.fixtures, (fixture) =>
this.clearDocs.bind(this, fixture))
async.parallel(queries, (err, results) => {
if (err) return callback(err)
results = _.map(results, 'n')
results = results.reduce((a, b) => a + b)
debug(`Removed ${results} documents`)
callback(null, results)
})
}
/**
* Query for documents for a fixture.
*/
findDocs (fixture, callback) {
this.debug(`Getting ${fixture.name}, looking for ${this.keys.length} ` +
`documents.`)
// Try to find our documents for the fixture
fixture.collection.find(
// Build the query hash
this.query(fixture.key, this.keys),
(err, docs) => {
if (err) return callback(err)
// debug(`Got ${docs.length} for ${fixture.name}`)
// Write out the docs to EJSON
this.writeDocs(this.filename(fixture.name), docs, callback)
})
}
/**
* Read documents from the filesystem and store them to the database.
*/
loadDocs (fixture, callback) {
fs.readFile(this.filename(fixture.name), (err, docs) => {
if (err) return callback(err)
// Convert the fixtures back into javascript
docs = EJSON.parse(docs)
docs = _.toArray(docs)
this.debug(`Loaded ${docs.length} from ${fixture.name}`)
// Create partial functions for saving the docs back. For whatever
// reason .save() didn't work with multiple docs, so we use
// async.parallel instead
docs = _.map(docs, (doc) =>
fixture.collection.save.bind(fixture.collection, doc))
// Save everything to the DB, overwriting anything already there
async.parallel(docs, (err, results) => {
if (err) return callback(err)
callback(null, results.filter((i) => i != null).length)
})
})
}
/**
* Serialize fixture documents to EJSON and write to the filesystem
*
* @param file {String} - Filename to write to
* @param docs {Array} - Array of documents to write out
* @param callback
*/
writeDocs (file, docs, callback) {
debug(`Writing ${docs.length} documents to ${file}`)
// Serialize the documents straight to EJSON
let data = EJSON.stringify(docs)
// And dump to the filesystem for later use
fs.writeFile(file, data, (err) => {
callback(err, docs.length)
})
}
/**
* Removes the fixture documents from the database.
*/
clearDocs (fixture, callback) {
// Removedocs
fixture.collection.remove(this.query(fixture.key, this.keys), callback)
}
/**
* Return a filename for the fixture
*
* @param name {String} - Collection name
*/
filename (name) {
// Provide a default directory to write to
let dir = this.path || './test'
return `${dir}/${name}.ejson`
}
/**
* Return a query hash for MongoDB.
*
* This uses the $in operator by default, but maybe should be overridden at
* some point.
*/
query (key, values) {
return {[key]: {$in: values}}
}
/**
* Runs the Fixture CLI
*/
static main (module) {
let fixtures = Fixture.getFixtures(module)
// Decide whether we're loading, getting, or erroring
let cmd = process.argv[2]
cmd = {
'load': Fixture.loadAll,
'get': Fixture.getAll,
}[cmd]
// Didn't match? We're done here
if (!cmd) {
debug("Invalid or missing command")
process.exit(1)
}
// Run the command
cmd(fixtures)
}
/**
* Loads all fixtures.
*/
static loadAll (fixtures) {
async.parallel(
_.map(fixtures, (fixture) => fixture.load.bind(fixture)),
(err, results) => {
if (err) {
debug(err)
throw err
}
debug(results)
results = results.reduce((a, b) => a + b)
debug(`Wrote ${results} documents to database.`)
process.exit(0)
})
}
/**
* Gets all fixtures from the active DB and writes them to EJSON files.
*/
static getAll (fixtures) {
// Get all fixtures in parallel
async.parallel(
_.map(fixtures, (fixture) => fixture.get.bind(fixture)),
(err, results) => {
if (err) {
debug(err)
throw err
}
debug(results)
results = results.reduce((a, b) => a + b)
debug(`Wrote ${results} documents to fixture files.`)
process.exit(0)
})
}
}
module.exports = Fixture