From d6df2334bce4530759d1bef94700c3408cadaf45 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira <321.bruno@gmail.com> Date: Thu, 22 Dec 2016 23:27:34 -0200 Subject: [PATCH 1/5] Add suport search files for pattern --- README.md | 6 ++++++ package.json | 1 + src/index.js | 24 ++++++++++++++++++++++++ test/index.test.js | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ab1b820..791324c 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,9 @@ Adds documents to the relevant collection. If the collection doesn't exist it wi //Directories (loads all files in the directory) fixtures.load(__dirname + '/fixtures', callback); + + //Pattern (loads all files with a patter) + fixtures.load(__dirname + '/fixtures/*.js', callback); clear(callback) @@ -192,6 +195,9 @@ Installation Changelog --------- +###0.15.0 +- Add suport search files for pattern + ###0.14.0 - Update mongodb to 2.2.x diff --git a/package.json b/package.json index d6abd37..4b40d1c 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ }, "dependencies": { "async": "0.1.15", + "glob": "^7.1.1", "mongodb": "~2.2.x", "nodeunit": "^0.9.1", "optimist": "0.3.5", diff --git a/src/index.js b/src/index.js index 4df8172..a7a9c72 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ var fs = require('fs'), mongo = require('mongodb'), ObjectID = mongo.ObjectId, async = require('async'), + glob = require('glob'), _ = require('underscore'), basePath = path.dirname(module.parent.filename); @@ -390,6 +391,11 @@ var _mixedToObject = function(fixtures, cb) { // Resolve relative paths if necessary. fixtures = path.resolve(basePath, fixtures); + // check if is a pattern + if(glob.hasMagic(fixtures)){ + return _patternToObject(glob.sync(fixtures), cb); + } + //Determine if fixtures is pointing to a file or directory fs.stat(fixtures, function(err, stats) { if (err) return cb(err); @@ -402,6 +408,24 @@ var _mixedToObject = function(fixtures, cb) { }); } +/** + * Get data from pattern file as an object + * + * @param {Array} List of full path to the file to load + * @param {Function} Optional callback(err, data) + * @api private + */ +var _patternToObject = function(files, cb) { + cb = cb || noop; + + var data = {}; + + files.map(function(file){ + Object.assign(data, require(file)); + }); + + cb(null, data); +} /** * Get data from one file as an object diff --git a/test/index.test.js b/test/index.test.js index 4f978e5..fd5963d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -272,7 +272,40 @@ exports['load'] = { }); }); }, - + + 'pattern': function(test) { + test.expect(2); + + loader.load('./fixtures/*.js', function(err) { + if (err) return test.done(err); + + async.parallel([ + function(next) { + loadCollection('archer', function(err, docs) { + if (err) return next(err); + + var names = _.pluck(docs, 'name'); + + test.same(names.sort(), ['Sterling', 'Lana', 'Cheryl'].sort()); + + next(); + }); + }, + function(next) { + loadCollection('southpark', function(err, docs) { + if (err) return next(err); + + var names = _.pluck(docs, 'name'); + + test.same(names.sort(), ['Stan', 'Towelie'].sort()); + + next(); + }); + } + ], test.done); + }); + }, + 'directory': { 'default' : function(test) { loader.load('./fixtures', function(err) { From 926e16a38a375c65443e1002566c8e145ff23794 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira <321.bruno@gmail.com> Date: Thu, 22 Dec 2016 23:32:08 -0200 Subject: [PATCH 2/5] ajust indentation --- test/index.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index fd5963d..65954f1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -276,8 +276,8 @@ exports['load'] = { 'pattern': function(test) { test.expect(2); - loader.load('./fixtures/*.js', function(err) { - if (err) return test.done(err); + loader.load('./fixtures/*.js', function(err) { + if (err) return test.done(err); async.parallel([ function(next) { @@ -303,7 +303,7 @@ exports['load'] = { }); } ], test.done); - }); + }); }, 'directory': { From 2b3bd3154fedb7335e9a773b23b4fbfe84e2f2da Mon Sep 17 00:00:00 2001 From: "bruno.oliveira" Date: Mon, 9 Jan 2017 09:00:29 -0200 Subject: [PATCH 3/5] ajust for old version node --- src/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a7a9c72..70c132d 100644 --- a/src/index.js +++ b/src/index.js @@ -75,7 +75,7 @@ var Loader = exports.Loader = function(dbOrUri, options) { safe: true }, options); } - + this.options = options; this.modifiers = []; }; @@ -421,7 +421,11 @@ var _patternToObject = function(files, cb) { var data = {}; files.map(function(file){ - Object.assign(data, require(file)); + var filePath = require(file); + + Object.keys(filePath).map(function(key){ + data[key] = filePath[key]; + }); }); cb(null, data); @@ -538,4 +542,4 @@ var _buildConnectionUri = function(options) { parts.push(options.db); return parts.join(''); -} \ No newline at end of file +} From 9b5d087d6558424d8af002c414e68c8eec216814 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira <321.bruno@gmail.com> Date: Mon, 9 Jan 2017 09:11:51 -0200 Subject: [PATCH 4/5] ajust of space --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 70c132d..24e7004 100644 --- a/src/index.js +++ b/src/index.js @@ -423,7 +423,7 @@ var _patternToObject = function(files, cb) { files.map(function(file){ var filePath = require(file); - Object.keys(filePath).map(function(key){ + Object.keys(filePath).map(function(key) { data[key] = filePath[key]; }); }); From 47396cd361ceee29e106b4e0e76fc899692c823d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira <321.bruno@gmail.com> Date: Mon, 9 Jan 2017 09:20:18 -0200 Subject: [PATCH 5/5] ajust of space --- test/index.test.js | 142 ++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 65954f1..5d91869 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,7 +3,7 @@ var fixtures = require('../src/index.js'), id = fixtures.createObjectId, mongo = require('mongodb'), - fs = require('fs'), + fs = require('fs'), async = require('async'), _ = require('underscore'); @@ -11,18 +11,18 @@ var dbName = 'pow-mongodb-fixtures-test', loader = fixtures.connect(dbName), server = new mongo.Db(dbName, new mongo.Server('127.0.0.1', 27017, {})), db; - - + + function loadCollection(name, cb) { db.collection(name, function(err, collection) { if (err) return cb(err); - + collection.find(function(err, cursor) { if (err) return cb(err); - + cursor.toArray(function(err, docs) { if (err) return cb(err); - + cb(null, docs); }); }); @@ -32,9 +32,9 @@ function loadCollection(name, cb) { exports['init'] = function(test) { server.open(function(err, openDb) { if (err) return test.done(err); - + db = openDb; - + db.dropDatabase(test.done); }); }; @@ -43,30 +43,30 @@ exports['init'] = function(test) { exports['createObjectId'] = { 'with ID as string': function(test) { var objId = id('4eca80fae4af59f55d000020'); - + test.same(objId.constructor.name, 'ObjectID'); test.same(objId.toString(), '4eca80fae4af59f55d000020'); - + test.done(); }, - + 'with existing ID': function(test) { var id1 = id(); - + var id2 = id(id1); - + test.same(id2.constructor.name, 'ObjectID'); test.same(id2.toString(), id1.toString()); - + test.done(); }, - + 'without ID': function(test) { var objId = id(); - + test.same(objId.constructor.name, 'ObjectID'); test.same(objId.toString().length, 24); - + test.done(); } }; @@ -109,21 +109,21 @@ exports['works when referencing an objectID in different scope'] = { var todo = 'TODO: Havent been able to replicate error yet:'; todo += 'Sometimes when referencing an ID that was generated elsewhere, it gets encoded incorrectly.'; todo += 'Test needs to fail first so it can be fixed.'; - + console.log(todo); return test.done(); - + var self = this; - + var data = {}; - + var users = data.users = { sterling: { - _id: id(), + _id: id(), name: 'Sterling' } }; - + var posts = data.posts = [ { _id: id(), @@ -131,10 +131,10 @@ exports['works when referencing an objectID in different scope'] = { text: 'Danger Zone!' } ]; - + loader.load(data, function(err) { if (err) return test.done(err); - + //TODO: Try to replicate problem from before. Maybe have to load setup fixture into DB test.same(users.sterling._id.toString(), posts[0].author.toString()); @@ -164,10 +164,10 @@ exports['load'] = { setUp: function(done) { db.dropDatabase(done); }, - + 'array': function(test) { test.expect(2); - + var data = { southpark: [ { name: 'Eric' }, @@ -180,28 +180,28 @@ exports['load'] = { { name: 'George' } ] }; - + loader.load(data, function(err) { if (err) return test.done(err); - + async.parallel([ function(next) { loadCollection('southpark', function(err, docs) { if (err) return next(err); - + var names = _.pluck(docs, 'name'); - + test.same(names.sort(), ['Eric', 'Butters', 'Kenny'].sort()); - + next(); }); }, function(next) { loadCollection('boredToDeath', function(err, docs) { if (err) return next(err); - + var names = _.pluck(docs, 'name'); - + test.same(names.sort(), ['Jonathan', 'Ray', 'George'].sort()); next(); @@ -210,10 +210,10 @@ exports['load'] = { ], test.done); }); }, - + 'object': function(test) { test.expect(2); - + var data = { southpark: { eric: { name: 'Eric' }, @@ -226,7 +226,7 @@ exports['load'] = { george: { name: 'George' } } }; - + loader.load(data, function(err) { if (err) return test.done(err); @@ -234,20 +234,20 @@ exports['load'] = { function(next) { loadCollection('southpark', function(err, docs) { if (err) return next(err); - + var names = _.pluck(docs, 'name'); - + test.same(names.sort(), ['Eric', 'Butters', 'Kenny'].sort()); - + next(); }); }, function(next) { loadCollection('boredToDeath', function(err, docs) { if (err) return next(err); - + var names = _.pluck(docs, 'name'); - + test.same(names.sort(), ['Jonathan', 'Ray', 'George'].sort()); next(); @@ -256,18 +256,18 @@ exports['load'] = { ], test.done); }); }, - + 'file': function(test) { loader.load('./fixtures/archer.js', function(err) { if (err) return test.done(err); - + loadCollection('archer', function(err, docs) { if (err) return next(err); - + var names = _.pluck(docs, 'name'); - + test.same(names.sort(), ['Sterling', 'Lana', 'Cheryl'].sort()); - + test.done(); }); }); @@ -399,7 +399,7 @@ exports['clear'] = { setUp: function(done) { db.dropDatabase(function(err) { if (err) return done(err); - + loader.load('./fixtures', done); }); }, @@ -409,7 +409,7 @@ exports['clear'] = { function(cb) { loader.clear(cb); }, - + function(cb) { loadCollection('archer', function(err, docs) { if (err) return cb(err); @@ -419,7 +419,7 @@ exports['clear'] = { cb(); }); }, - + function(cb) { loadCollection('southpark', function(err, docs) { if (err) return cb(err); @@ -437,7 +437,7 @@ exports['clear'] = { function(cb) { loader.clear('archer', cb); }, - + function(cb) { loadCollection('archer', function(err, docs) { if (err) return cb(err); @@ -447,7 +447,7 @@ exports['clear'] = { cb(); }); }, - + function(cb) { loadCollection('southpark', function(err, docs) { if (err) return cb(err); @@ -459,13 +459,13 @@ exports['clear'] = { } ], test.done); }, - + 'clears multiple collections if called with an array': function(test) { async.series([ function(cb) { loader.clear(['archer', 'southpark'], cb); }, - + function(cb) { loadCollection('archer', function(err, docs) { if (err) return cb(err); @@ -475,7 +475,7 @@ exports['clear'] = { cb(); }); }, - + function(cb) { loadCollection('southpark', function(err, docs) { if (err) return cb(err); @@ -487,11 +487,11 @@ exports['clear'] = { } ], test.done); }, - + 'clearing non-existent collections shouldn\'t error': function(test) { loader.clear('fheruas', function(err) { test.ifError(err); - + test.done(); }) } @@ -502,7 +502,7 @@ exports['clearAllAndLoad'] = { setUp: function(done) { db.dropDatabase(function(err) { if (err) return done(err); - + loader.load('./fixtures', done); }); }, @@ -512,12 +512,12 @@ exports['clearAllAndLoad'] = { data.southpark = [ { name: 'Kyle' } ]; - + async.series([ function(cb) { loader.clearAllAndLoad(data, cb); }, - + function(cb) { loadCollection('archer', function(err, docs) { if (err) return cb(err); @@ -527,7 +527,7 @@ exports['clearAllAndLoad'] = { cb(); }); }, - + function(cb) { loadCollection('southpark', function(err, docs) { if (err) return cb(err); @@ -546,22 +546,22 @@ exports['clearAndLoad'] = { setUp: function(done) { db.dropDatabase(function(err) { if (err) return done(err); - + loader.load('./fixtures', done); }); }, - + 'drops only referenced collections; with object data': function(test) { var data = {}; data.southpark = [ { name: 'Kyle' } ]; - + async.series([ function(cb) { loader.clearAndLoad(data, cb); }, - + function(cb) { loadCollection('archer', function(err, docs) { if (err) return cb(err); @@ -571,7 +571,7 @@ exports['clearAndLoad'] = { cb(); }); }, - + function(cb) { loadCollection('southpark', function(err, docs) { if (err) return cb(err); @@ -583,13 +583,13 @@ exports['clearAndLoad'] = { } ], test.done); }, - + 'drops only referenced collections; with a file': function(test) { async.series([ function(cb) { loader.clearAndLoad(__dirname + '/fixtures/southpark2.js', cb); }, - + function(cb) { loadCollection('archer', function(err, docs) { if (err) return cb(err); @@ -599,13 +599,13 @@ exports['clearAndLoad'] = { cb(); }); }, - + function(cb) { loadCollection('southpark', function(err, docs) { if (err) return cb(err); - + var names = _.pluck(docs, 'name'); - + test.same(names, ['Stan', 'Towelie']); cb();