Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: paperhive/mongoose-erase
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.3
Choose a base ref
...
head repository: paperhive/mongoose-erase
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 158 additions and 114 deletions.
  1. +15 −0 .babelrc
  2. +11 −0 .eslintrc
  3. +0 −3 .jscsrc
  4. +13 −5 .travis.yml
  5. +18 −15 package.json
  6. +38 −56 src/index.js
  7. +14 −0 test/.eslintrc
  8. +46 −35 test/index.js
  9. +3 −0 test/mocha.opts
15 changes: 15 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"plugins": [
"babel-plugin-transform-es2015-classes",
"babel-plugin-transform-es2015-computed-properties",
"babel-plugin-transform-es2015-destructuring",
"babel-plugin-transform-es2015-function-name",
"babel-plugin-transform-es2015-modules-commonjs",
"babel-plugin-transform-es2015-object-super",
"babel-plugin-transform-es2015-parameters",
"babel-plugin-transform-es2015-shorthand-properties",
"babel-plugin-transform-es2015-sticky-regex",
"babel-plugin-transform-es2015-typeof-symbol",
"babel-plugin-transform-es2015-unicode-regex"
]
}
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "airbnb/base",
"ecmaFeatures": {
"generators": true
},
"rules": {
"object-curly-spacing": 0,
"no-cond-assign": [2, "except-parens"],
"no-param-reassign": [2, { "props": false }]
}
}
3 changes: 0 additions & 3 deletions .jscsrc

This file was deleted.

18 changes: 13 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
language: node_js

node_js:
- "node"
- "iojs"
- "node"

sudo: false

services:
- mongodb
- mongodb

before_script:
- "until nc -z localhost 27017 ; do echo Waiting for MongoDB; sleep 1; done"
- "until nc -z localhost 27017 ; do echo Waiting for MongoDB; sleep 1; done"

script:
- npm run lint
- npm run test

after_success:
- "npm run-script coveralls"
- "npm run-script coveralls"
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "mongoose-erase",
"version": "2.0.3",
"version": "2.0.9",
"description": "Erase collections, models and schemas for unit testing with mongoose",
"main": "src/index.js",
"scripts": {
"test": "npm run jshint && npm run jscs && mocha",
"jshint": "jshint **/*.js",
"jscs": "jscs **/*.js",
"lint": "eslint test src",
"test": "mocha test/",
"coveralls": "mocha --require blanket -R mocha-lcov-reporter | coveralls"
},
"repository": {
@@ -29,23 +28,27 @@
"url": "https://github.com/paperhive/mongoose-erase/issues"
},
"homepage": "https://github.com/paperhive/mongoose-erase",
"dependencies": {
"bluebird": "^3.1.1",
"co": "^4.6.0"
},
"devDependencies": {
"blanket": "^1.1.7",
"coveralls": "^2.11.2",
"jscs": "^1.13.1",
"jshint": "^2.8.0",
"mocha": "^2.2.5",
"mocha-lcov-reporter": "0.0.2",
"mongoose": "^4.0.6",
"should": "^7.0.1"
"babel-core": "^6.3.26",
"babel-preset-es2015": "^6.3.13",
"blanket": "^1.2.1",
"coveralls": "^2.11.6",
"eslint": "^3.0.0",
"eslint-config-airbnb": "^10.0.0",
"eslint-plugin-import": "^1.5.0",
"mocha": "^3.0.0",
"mocha-lcov-reporter": "1.2.0",
"mongoose": "^4.3.4",
"should": "^11.0.0"
},
"config": {
"blanket": {
"data-cover-only": "src",
"data-cover-never": "node_modules"
}
},
"dependencies": {
"async": "^1.3.0"
}
}
94 changes: 38 additions & 56 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
var async = require('async');
import { promisify } from 'bluebird';
import co from 'co';

// requires mongoose to be connected to the database
function erase(mongoose, done) {
async.waterfall([
// ensure a database connection is established
function(cb) {
if (!mongoose.connection.db) {
return cb(new Error('no established database connection'));
}
cb();
},
// get collections
function(cb) {
mongoose.connection.db.collections(cb);
},
// drop collections
function(collections, cb) {
async.each(collections, function(collection, eachCb) {
if (collection.collectionName.match(/^system\./)) {return eachCb();}
// drop collection
collection.drop(eachCb);
}, cb);
},
// reset mongoose models
function(cb) {
mongoose.connection.models = {};
mongoose.models = {};
mongoose.modelSchemas = {};
cb();
},
//// ensureIndexes
//_.map(models, function (model) {
// return model.ensureIndexes.bind(model);
//})
], done);
}
export const erase = co.wrap(function* erase(mongoose) {
// ensure a database connection is established
if (!mongoose.connection.db) {
throw new Error('no established database connection');
}

function connect(mongoose, dbURI, cb) {
if (mongoose.connection.db) {return cb();}
mongoose.connect(dbURI, cb);
}
// get collections
const collections = yield promisify(
mongoose.connection.db.collections,
{context: mongoose.connection.db}
);

function connectAndErase(mongoose, dbURI) {
return function(done) {
async.series([
function(cb) {
connect(mongoose, dbURI, cb);
},
function(cb) {
erase(mongoose, cb);
}
], done);
};
}
// drop collections
yield collections.map(co.wrap((collection) => {
// Older MongoDB installations featur system.* collections; leave those
// untouched.
if (!collection.collectionName.match(/^system\./)) {
// drop collection
collection.drop();
}
}));

module.exports = {
connect: connect,
erase: erase,
connectAndErase: connectAndErase
};
// reset mongoose models
mongoose.connection.models = {};
mongoose.models = {};
mongoose.modelSchemas = {};
});

export const connect = co.wrap(function* connect(mongoose, dbURI, options) {
if (mongoose.connection.db) return;
const newOptions = options || {};
yield promisify(mongoose.connect, {context: mongoose})(dbURI, newOptions);
});

export const connectAndErase = co.wrap(function* connectAndErase(mongoose, dbURI, options) {
const newOptions = options || {};
yield connect(mongoose, dbURI, newOptions);
yield erase(mongoose);
});
14 changes: 14 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../.eslintrc",
"env": {
"mocha": true
},
"globals": {
"should": true
},
"rules": {
"func-names": 0,
"import/no-extraneous-dependencies": 0,
"no-unused-expressions": 0
}
}
81 changes: 46 additions & 35 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
var dbURI = 'mongodb://localhost/mongoose-erase';
var mongoose = require('mongoose');
var async = require('async');
var should = require('should');

var erase = require('../');

describe('erase()', function() {

before(function(done) {
async.series([
// make sure that database is connected and erase it
erase.connectAndErase(mongoose, dbURI),
// add collection users
function(cb) {
var User = mongoose.model('User', new mongoose.Schema({
name: {type: String, index: true}
}));
User.create({name: 'Darth'}, cb);
},
// erase
function(cb) {
erase.erase(mongoose, cb);
}
], done);
});
import { promisify } from 'bluebird';
import co from 'co';
import mongoose from 'mongoose';

import { connectAndErase, erase } from '../src';

const dbURI = 'mongodb://localhost/test';

// generator/yield-version of the async/await-based mochaAsync
function mochaAsync(fn) {
return function (done) {
const ctx = this;
co(function* () {
yield fn(ctx);
}).then(done, done);
};
}

describe('erase()', () => {
before(mochaAsync(function* () {
yield connectAndErase(mongoose, dbURI);

it('should remove collections', function(done) {
mongoose.connection.db.collections(function(err, collections) {
if (err) {return done(err);}
const User = mongoose.model('User', new mongoose.Schema({
name: {type: String, index: true},
}));
yield User.create({name: 'Darth'});

yield erase(mongoose);
}));

it('should remove collections', mochaAsync(function* () {
// get collections
const collections = yield promisify(
mongoose.connection.db.collections,
{context: mongoose.connection.db}
);

// On older MongoDB installations, a collection "system.indexes" is
// always present.
if (collections.length > 0 &&
collections[0].collectionName.match(/^system\./)) {
collections.should.have.lengthOf(1);
done();
});
});
} else {
collections.should.have.lengthOf(0);
}
}));

it('should remove models', function() {
(function() {
it('should remove models', () => {
(() => {
mongoose.model('User');
}).should.throw();
});

});
3 changes: 3 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--compilers js:babel-core/register
--recursive
-r should