From 1705f50b2814f1e998153a2359d6404066ba1a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 05:14:34 -0300 Subject: [PATCH 01/11] Changes to use glob function :+1: --- mongoose_fixtures.js | 79 +++++++++------------------------ package.json | 5 ++- tests/mongoose_fixtures.test.js | 17 ++++--- 3 files changed, 34 insertions(+), 67 deletions(-) diff --git a/mongoose_fixtures.js b/mongoose_fixtures.js index f821b46..a1605f8 100644 --- a/mongoose_fixtures.js +++ b/mongoose_fixtures.js @@ -2,6 +2,8 @@ var fs = require('fs'); var mongoose = require('mongoose'); var async = require('async'); +var path = require('path'); +var glob = require('glob'); /** @@ -26,23 +28,7 @@ var load = exports.load = function(data, db, callback) { } else if (typeof data == 'string') { - //Get the absolute dir path if a relative path was given - if (data.substr(0, 1) !== '/') { - var parentPath = module.parent.filename.split('/'); - parentPath.pop(); - data = parentPath.join('/') + '/' + data; - } - - //Determine if data is pointing to a file or directory - fs.stat(data, function(err, stats) { - if (err) throw err; - - if (stats.isDirectory()) { - loadDir(data, db, callback); - } else { //File - loadFile(data, db, callback); - } - }); + loadFiles(data, db, callback); } else { //Unsupported type @@ -124,55 +110,30 @@ function loadObject(data, db, callback) { async.forEach(Object.keys(data), iterator, callback); } - /** - * Loads fixtures from one file + * Loads fixtures from matches of glob search filesystem pattern * * TODO: Add callback option * - * @param {String} The full path to the file to load + * @param {String} The directory path to load e.g. 'data/fixtures' or '../data' * @param {Connection} The mongoose connection to use * @param {Function} Callback */ -function loadFile(file, db, callback) { - callback = callback || function() {}; - - if (file.substr(0, 1) !== '/') { - var parentPath = module.parent.filename.split('/'); - parentPath.pop(); - file = parentPath.join('/') + '/' + file; - } - - load(require(file), db, callback); -} +function loadFiles(data, db, callback) { + var __cwd = module.parent.filename.split('/'); + __cwd.pop(); + __cwd = __cwd.join('/'); + data = path.join(__cwd, data); + if (!/\*|\.js*/.test(data)) + data = data + '/*'; -/** - * Loads fixtures from all files in a directory - * - * TODO: Add callback option - * - * @param {String} The directory path to load e.g. 'data/fixtures' or '../data' - * @param {Connection} The mongoose connection to use - * @param {Function} Callback - */ -function loadDir(dir, db, callback) { - callback = callback || function() {}; - - //Get the absolute dir path if a relative path was given - if (dir.substr(0, 1) !== '/') { - var parentPath = module.parent.filename.split('/'); - parentPath.pop(); - dir = parentPath.join('/') + '/' + dir; - } - - //Load each file in directory - fs.readdir(dir, function(err, files){ - if (err) return callback(err); - - var iterator = function(file, next){ - loadFile(dir + '/' + file, db, next); - }; - async.forEach(files, iterator, callback); - }); + glob(data, { cwd: __cwd }, function(err, files) { + if (err) return callback(err); + + var iterator = function(file, next) { + load(require(file), db, callback); + }; + async.forEach(files, iterator, callback); + }); }; diff --git a/package.json b/package.json index 89b3e0a..d81df4c 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "url": "https://github.com/jackdbernier" } ], - "version": "0.1.0", + "version": "0.1.1", "repository": { "type": "git", "url": "http://github.com/powmedia/mongoose-fixtures.git" @@ -34,7 +34,8 @@ ], "dependencies": { "mongoose": "3.2.x", - "async": "0.1.x" + "async": "0.1.x", + "glob": "3.1.x" }, "devDependencies": { "mocha": "1.6.x", diff --git a/tests/mongoose_fixtures.test.js b/tests/mongoose_fixtures.test.js index be77e20..b6423ec 100644 --- a/tests/mongoose_fixtures.test.js +++ b/tests/mongoose_fixtures.test.js @@ -10,13 +10,17 @@ var MongooseInitializer = require('openifyit-commons').MongooseInitializer; describe('mongoose-fixtures test', function(){ before(function(done){ - this.mongooseInitializer = new MongooseInitializer(process.env.MONGODB_URL, path.join(__dirname, './models')); + mongoose.connect(process.env.MONGODB_URL); + require('./models/country.coffee'); + done(); + + // this.mongooseInitializer = new MongooseInitializer(process.env.MONGODB_URL, path.join(__dirname, './models')); - var functions = [ - this.mongooseInitializer.openConnection, - this.mongooseInitializer.loadModels - ]; - async.series(functions, done); + // var functions = [ + // this.mongooseInitializer.openConnection, + // this.mongooseInitializer.loadModels + // ]; + // async.series(functions, done); }); after(function(done){ @@ -25,6 +29,7 @@ describe('mongoose-fixtures test', function(){ it('should load fixtures from a directory', function(done){ fixturesLoader.load('./fixtures', function(err){ + console.log(err) expect(err).not.to.be.ok(); var CountrySchema = mongoose.connection.model('Country'); CountrySchema.find({}, function(err, countries){ From 08696c1bf956eb4a93ffcea01e089dea5b2b3673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 05:20:02 -0300 Subject: [PATCH 02/11] Added .travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c1ffcb3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - 0.8 From 1e396c1db91b9d4dd90f12100726df17afe37d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 05:23:36 -0300 Subject: [PATCH 03/11] Added mongodb services on .travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c1ffcb3..d3a84a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ language: node_js +services: mongodb node_js: - 0.8 From 3533f0951be52eca5a9ab406f7966778ceb86265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 07:01:28 -0300 Subject: [PATCH 04/11] Fix iterator callback to next --- mongoose_fixtures.js | 2 +- tests/mongoose_fixtures.test.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mongoose_fixtures.js b/mongoose_fixtures.js index a1605f8..eaf162d 100644 --- a/mongoose_fixtures.js +++ b/mongoose_fixtures.js @@ -132,7 +132,7 @@ function loadFiles(data, db, callback) { if (err) return callback(err); var iterator = function(file, next) { - load(require(file), db, callback); + load(require(file), db, next); }; async.forEach(files, iterator, callback); }); diff --git a/tests/mongoose_fixtures.test.js b/tests/mongoose_fixtures.test.js index b6423ec..9401174 100644 --- a/tests/mongoose_fixtures.test.js +++ b/tests/mongoose_fixtures.test.js @@ -29,7 +29,6 @@ describe('mongoose-fixtures test', function(){ it('should load fixtures from a directory', function(done){ fixturesLoader.load('./fixtures', function(err){ - console.log(err) expect(err).not.to.be.ok(); var CountrySchema = mongoose.connection.model('Country'); CountrySchema.find({}, function(err, countries){ From f2c725799d726f4a4ae1fcfed78a4ec4e555baac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 15:17:17 -0300 Subject: [PATCH 05/11] Added falback to save fixture by pass in hooks --- mongoose_fixtures.js | 50 +++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/mongoose_fixtures.js b/mongoose_fixtures.js index eaf162d..2416bb3 100644 --- a/mongoose_fixtures.js +++ b/mongoose_fixtures.js @@ -4,7 +4,7 @@ var mongoose = require('mongoose'); var async = require('async'); var path = require('path'); var glob = require('glob'); - + /** * Clears a collection and inserts the given data as new documents @@ -37,7 +37,7 @@ var load = exports.load = function(data, db, callback) { } } - + /** * Clears a collection and inserts the given data as new documents * @@ -51,17 +51,14 @@ var load = exports.load = function(data, db, callback) { */ function insertCollection(modelName, data, db, callback) { callback = callback || {}; - - //Counters for managing callbacks - var tasks = { total: 0, done: 0 }; - + //Load model var Model = db.model(modelName); - + //Clear existing collection Model.collection.remove(function(err) { if (err) return callback(err); - + //Convert object to array var items = []; if (Array.isArray(data)) { @@ -71,24 +68,29 @@ function insertCollection(modelName, data, db, callback) { items.push(data[i]); } } - + //Check number of tasks to run if (items.length == 0) { return callback(); - } else { - tasks.total = items.length; } - - //Insert each item individually so we get Mongoose validation etc. - items.forEach(function(item) { + + var iterator = function(item, next) { var doc = new Model(item); - doc.save(function(err) { - if (err) return callback(err); - - //Check if task queue is complete - tasks.done++; - if (tasks.done == tasks.total) callback(); - }); + //Insert each item individually so we get Mongoose validation etc. + doc.save(function (err) { + if (err) { + //Or Fallback Deep insert + Model.collection.insert(doc.toObject(), function (err, res) { + if (err) return next(err); + next(); + }); + } + else + next(); + }) + }; + async.forEach(items, iterator, function (){ + callback(); }); }); } @@ -96,7 +98,7 @@ function insertCollection(modelName, data, db, callback) { /** * Loads fixtures from object data - * + * * @param {Object} The data to load, keyed by the Mongoose model name e.g.: * { User: [{name: 'Alex'}, {name: 'Bob'}] } * @param {Connection} The mongoose connection to use @@ -112,9 +114,9 @@ function loadObject(data, db, callback) { /** * Loads fixtures from matches of glob search filesystem pattern - * + * * TODO: Add callback option - * + * * @param {String} The directory path to load e.g. 'data/fixtures' or '../data' * @param {Connection} The mongoose connection to use * @param {Function} Callback From 17fdf137cf198579034c2a1079d64f27a8f263ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 15:54:29 -0300 Subject: [PATCH 06/11] Added tests for glob paterns --- tests/fixtures/globfolder/clients.js | 7 +++++++ tests/fixtures/globfolder/index.js | 0 tests/fixtures/globfolder/index.txt | 1 + tests/models/client.js | 15 +++++++++++++++ tests/mongoose_fixtures.test.js | 17 +++++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 tests/fixtures/globfolder/clients.js create mode 100644 tests/fixtures/globfolder/index.js create mode 100644 tests/fixtures/globfolder/index.txt create mode 100644 tests/models/client.js diff --git a/tests/fixtures/globfolder/clients.js b/tests/fixtures/globfolder/clients.js new file mode 100644 index 0000000..7252e26 --- /dev/null +++ b/tests/fixtures/globfolder/clients.js @@ -0,0 +1,7 @@ +exports.Client = { + client1: { + _id: "516b515f4448521a66d1e551", + user_id: "5113ddd5f9bd2f0c06000003", + secret: "secret" + } +} \ No newline at end of file diff --git a/tests/fixtures/globfolder/index.js b/tests/fixtures/globfolder/index.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/globfolder/index.txt b/tests/fixtures/globfolder/index.txt new file mode 100644 index 0000000..7b0348a --- /dev/null +++ b/tests/fixtures/globfolder/index.txt @@ -0,0 +1 @@ +This is not a fixture! \ No newline at end of file diff --git a/tests/models/client.js b/tests/models/client.js new file mode 100644 index 0000000..ce56f66 --- /dev/null +++ b/tests/models/client.js @@ -0,0 +1,15 @@ +var mongoose = require('mongoose'); + +var ClientSchema = new mongoose.Schema({ + user_id: { type: mongoose.Schema.ObjectId, ref: 'User', index: { unique: true }, required: true }, + secret: { type: String, required: true }, + redirect_uri: { type: String } +}); + +// Methods +ClientSchema.methods.getClientByUserId = function (user_id, next) { + this.findOne({ user_id: user_id }, next); +}; + +mongoose.model('Client', ClientSchema); + diff --git a/tests/mongoose_fixtures.test.js b/tests/mongoose_fixtures.test.js index 9401174..9b9d94a 100644 --- a/tests/mongoose_fixtures.test.js +++ b/tests/mongoose_fixtures.test.js @@ -10,7 +10,9 @@ var MongooseInitializer = require('openifyit-commons').MongooseInitializer; describe('mongoose-fixtures test', function(){ before(function(done){ + // @todo Fix connection and model loader mongoose.connect(process.env.MONGODB_URL); + require('./models/client'); require('./models/country.coffee'); done(); @@ -69,4 +71,19 @@ describe('mongoose-fixtures test', function(){ }); }); }); + + it('should load fixtures from glob pattern', function(done){ + var pattern = './fixtures/globfolder/!(index.*)(.*)'; + fixturesLoader.load(pattern, function(err){ + expect(err).not.to.be.ok(); + var ClientSchema = mongoose.connection.model('Client'); + ClientSchema.find({}, function(err, clients){ + expect(err).not.to.be.ok(); + expect(clients).to.be.ok(); + expect(clients).to.be.an(Array); + expect(clients.length).to.be.eql(1); + done(); + }); + }); + }); }); From 30fb8556c1c08822663479b8c8ad6e13ffacbefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 16:14:23 -0300 Subject: [PATCH 07/11] Added tests for fallback save to prevent paranoid hooks --- mongoose_fixtures.js | 4 ++-- tests/fixtures/paranoidhooks/paranoids.js | 6 ++++++ tests/models/paranoid.js | 11 +++++++++++ tests/mongoose_fixtures.test.js | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/paranoidhooks/paranoids.js create mode 100644 tests/models/paranoid.js diff --git a/mongoose_fixtures.js b/mongoose_fixtures.js index 2416bb3..57ef310 100644 --- a/mongoose_fixtures.js +++ b/mongoose_fixtures.js @@ -127,8 +127,8 @@ function loadFiles(data, db, callback) { __cwd = __cwd.join('/'); data = path.join(__cwd, data); - if (!/\*|\.js*/.test(data)) - data = data + '/*'; + if (!/(\/\.\*|\.js*)$/.test(data)) + data = data + '/.*'; glob(data, { cwd: __cwd }, function(err, files) { if (err) return callback(err); diff --git a/tests/fixtures/paranoidhooks/paranoids.js b/tests/fixtures/paranoidhooks/paranoids.js new file mode 100644 index 0000000..84e2c25 --- /dev/null +++ b/tests/fixtures/paranoidhooks/paranoids.js @@ -0,0 +1,6 @@ +exports.Paranoid = { + paranoid1: { + _id: "516b515f4448521a66d1e551", + name: "Foo bar" + } +} \ No newline at end of file diff --git a/tests/models/paranoid.js b/tests/models/paranoid.js new file mode 100644 index 0000000..2bc34c2 --- /dev/null +++ b/tests/models/paranoid.js @@ -0,0 +1,11 @@ +var mongoose = require('mongoose'); + +var ParanoidSchema = new mongoose.Schema({ + name: { type: String, required: true }, +}); + +ParanoidSchema.pre('save', function (next) { + return next(new Error('You can\'t do this!')); +}); + +mongoose.model('Paranoid', ParanoidSchema); \ No newline at end of file diff --git a/tests/mongoose_fixtures.test.js b/tests/mongoose_fixtures.test.js index 9b9d94a..40f99ef 100644 --- a/tests/mongoose_fixtures.test.js +++ b/tests/mongoose_fixtures.test.js @@ -13,6 +13,7 @@ describe('mongoose-fixtures test', function(){ // @todo Fix connection and model loader mongoose.connect(process.env.MONGODB_URL); require('./models/client'); + require('./models/paranoid'); require('./models/country.coffee'); done(); @@ -86,4 +87,19 @@ describe('mongoose-fixtures test', function(){ }); }); }); + + it('should load fixtures from a paranid model', function(done){ + var pattern = './fixtures/paranoidhooks/paranoids.js'; + fixturesLoader.load(pattern, function(err){ + expect(err).not.to.be.ok(); + var ParanoidSchema = mongoose.connection.model('Paranoid'); + ParanoidSchema.find({}, function(err, paranoids){ + expect(err).not.to.be.ok(); + expect(paranoids).to.be.ok(); + expect(paranoids).to.be.an(Array); + expect(paranoids.length).to.be.eql(1); + done(); + }); + }); + }); }); From 9e8c5c89661821ee41c9e4fbe8aff90f91117da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Sun, 21 Apr 2013 16:25:09 -0300 Subject: [PATCH 08/11] Update README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index d545e77..7d2049e 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,22 @@ Usage //Directories (loads all files in the directory) fixtures.load(__dirname + '/fixtures'); + + //You can use patterns to match files + //this match all, excluding all index.* + fixturesLoader.load('./fixtures/!(index.*)(.*)'); + + //This match /fixture/user/* and /fixture/user/* + fixture.load('./fixtures/{user,blog}/*.js'); + +See: + +* `man sh` +* `man bash` +* `man 3 fnmatch` +* `man 5 gitignore` +* [node-glob](https://github.com/isaacs/node-glob) +* [minimatch documentation](https://github.com/isaacs/minimatch) Installation ------------ From de9f1da8edd3a0e947c7176dc39c17dc07dfd10e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Mon, 22 Apr 2013 12:01:57 -0300 Subject: [PATCH 09/11] Fix linux glob patern --- mongoose_fixtures.js | 4 ++-- tests/mongoose_fixtures.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mongoose_fixtures.js b/mongoose_fixtures.js index 57ef310..63866d0 100644 --- a/mongoose_fixtures.js +++ b/mongoose_fixtures.js @@ -127,8 +127,8 @@ function loadFiles(data, db, callback) { __cwd = __cwd.join('/'); data = path.join(__cwd, data); - if (!/(\/\.\*|\.js*)$/.test(data)) - data = data + '/.*'; + if (!/(\..*|\/\.\*|\.js*)$|[\(\)\!]/.test(data)) + data = data + '/*.*'; glob(data, { cwd: __cwd }, function(err, files) { if (err) return callback(err); diff --git a/tests/mongoose_fixtures.test.js b/tests/mongoose_fixtures.test.js index 40f99ef..4a50ba9 100644 --- a/tests/mongoose_fixtures.test.js +++ b/tests/mongoose_fixtures.test.js @@ -74,7 +74,7 @@ describe('mongoose-fixtures test', function(){ }); it('should load fixtures from glob pattern', function(done){ - var pattern = './fixtures/globfolder/!(index.*)(.*)'; + var pattern = './fixtures/globfolder/!(index.*)(*.*)'; fixturesLoader.load(pattern, function(err){ expect(err).not.to.be.ok(); var ClientSchema = mongoose.connection.model('Client'); From 48221a67745a99dc7872b587ca2085e12582f088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Mon, 22 Apr 2013 12:19:55 -0300 Subject: [PATCH 10/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d2049e..f7f2275 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -mongoose-fixtures +mongoose-fixtures [![Build Status](https://travis-ci.org/joaoneto/mongoose-fixtures.png?branch=master)](https://travis-ci.org/joaoneto/mongoose-fixtures) ================= Simple fixture loader for Mongoose on NodeJS. From 8e4502be2b8a178a3b2d7b1459bf9d7b17652d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Neto?= Date: Fri, 3 May 2013 15:57:29 -0300 Subject: [PATCH 11/11] Fix callback null and mongoose conection --- mongoose_fixtures.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mongoose_fixtures.js b/mongoose_fixtures.js index 63866d0..bf914a8 100644 --- a/mongoose_fixtures.js +++ b/mongoose_fixtures.js @@ -20,6 +20,8 @@ var load = exports.load = function(data, db, callback) { if (typeof db === 'function') { callback = db; db = mongoose.connection; + } else { + db = db || mongoose.connection; } if (typeof data == 'object') { @@ -122,6 +124,7 @@ function loadObject(data, db, callback) { * @param {Function} Callback */ function loadFiles(data, db, callback) { + callback = callback || function() {}; var __cwd = module.parent.filename.split('/'); __cwd.pop(); __cwd = __cwd.join('/');