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

Commits on Jul 13, 2015

  1. Copy the full SHA
    f1c152d View commit details

Commits on Jul 22, 2015

  1. Copy the full SHA
    67754d9 View commit details

Commits on Jul 28, 2015

  1. Copy the full SHA
    e25b6c6 View commit details

Commits on Aug 16, 2015

  1. Copy the full SHA
    5a48288 View commit details

Commits on Oct 21, 2015

  1. Copy the full SHA
    d4aa61c View commit details
  2. Merge pull request #1 from paperhive/greenkeeper-update-all

    Update all dependencies 🌴
    andrenarchy committed Oct 21, 2015
    Copy the full SHA
    3864e53 View commit details
  3. version bump to 2.0.8

    andrenarchy committed Oct 21, 2015
    Copy the full SHA
    2224707 View commit details

Commits on Nov 19, 2015

  1. Copy the full SHA
    186e1ef View commit details

Commits on Dec 8, 2015

  1. Copy the full SHA
    cbfdba1 View commit details
  2. Merge pull request #2 from paperhive/greenkeeper-should-8.0.0

    Update should to version 8.0.0 🚀
    andrenarchy committed Dec 8, 2015
    Copy the full SHA
    1dcccf7 View commit details

Commits on Dec 25, 2015

  1. Copy the full SHA
    467e9ff View commit details
  2. fix eslint complaint

    nschloe committed Dec 25, 2015
    Copy the full SHA
    4ecc26d View commit details
  3. add missing dependency

    nschloe committed Dec 25, 2015
    Copy the full SHA
    e2e6ba7 View commit details
  4. Copy the full SHA
    e962b65 View commit details
  5. dependencies update

    nschloe committed Dec 25, 2015
    Copy the full SHA
    9b8117f View commit details
  6. Copy the full SHA
    3901fb3 View commit details
  7. yieldification

    nschloe committed Dec 25, 2015
    Copy the full SHA
    e50b137 View commit details
  8. remove debug lol

    nschloe committed Dec 25, 2015
    Copy the full SHA
    7d1272f View commit details
  9. add .babelrc

    nschloe committed Dec 25, 2015
    Copy the full SHA
    875a5b7 View commit details

Commits on Dec 26, 2015

  1. travis: lint, test

    nschloe committed Dec 26, 2015
    Copy the full SHA
    07713b0 View commit details
  2. fix linter

    nschloe committed Dec 26, 2015
    Copy the full SHA
    330c517 View commit details

Commits on Dec 31, 2015

  1. Merge pull request #3 from paperhive/tests

    modernization
    andrenarchy committed Dec 31, 2015
    Copy the full SHA
    2ec1395 View commit details

Commits on Mar 28, 2016

  1. fix linting errors

    andrenarchy committed Mar 28, 2016
    Copy the full SHA
    0f05773 View commit details

Commits on Apr 11, 2016

  1. Copy the full SHA
    4079458 View commit details
  2. Merge pull request #31 from paperhive/greenkeeper-eslint-config-airbn…

    …b-7.0.0
    
    Update eslint-config-airbnb to version 7.0.0 🚀
    andrenarchy committed Apr 11, 2016
    Copy the full SHA
    ab98388 View commit details

Commits on Apr 22, 2016

  1. Copy the full SHA
    b7d3aa6 View commit details

Commits on Apr 24, 2016

  1. Copy the full SHA
    cb26eb7 View commit details
  2. make eslint happier :)

    andrenarchy committed Apr 24, 2016
    Copy the full SHA
    dc2bc02 View commit details
  3. Merge pull request #32 from paperhive/greenkeeper-eslint-config-airbn…

    …b-8.0.0
    
    Update eslint-config-airbnb to version 8.0.0 🚀
    andrenarchy committed Apr 24, 2016
    Copy the full SHA
    9231fed View commit details

Commits on May 8, 2016

  1. Copy the full SHA
    6d52b93 View commit details

Commits on May 10, 2016

  1. Merge pull request #35 from paperhive/greenkeeper-eslint-config-airbn…

    …b-9.0.1
    
    Update eslint-config-airbnb to version 9.0.1 🚀
    andrenarchy committed May 10, 2016
    Copy the full SHA
    9b0baaf View commit details

Commits on May 30, 2016

  1. Copy the full SHA
    2b655c1 View commit details

Commits on May 31, 2016

  1. Merge pull request #38 from paperhive/greenkeeper-should-9.0.0

    Update should to version 9.0.0 🚀
    andrenarchy committed May 31, 2016
    Copy the full SHA
    030cf8e View commit details

Commits on Jul 1, 2016

  1. Copy the full SHA
    e78e5a5 View commit details

Commits on Jul 2, 2016

  1. Merge pull request #41 from paperhive/greenkeeper-eslint-3.0.0

    Update eslint to version 3.0.0 🚀
    andrenarchy authored Jul 2, 2016
    Copy the full SHA
    7f4e7ed View commit details

Commits on Jul 18, 2016

  1. Copy the full SHA
    c8bd55a View commit details
  2. Merge pull request #43 from paperhive/greenkeeper-should-10.0.0

    Update should to version 10.0.0 🚀
    andrenarchy authored Jul 18, 2016
    Copy the full SHA
    4e348d3 View commit details

Commits on Aug 1, 2016

  1. Copy the full SHA
    bdeec15 View commit details
  2. Merge pull request #45 from paperhive/greenkeeper-mocha-3.0.0

    Update mocha to version 3.0.0 🚀
    andrenarchy authored Aug 1, 2016
    Copy the full SHA
    48027c3 View commit details
  3. Copy the full SHA
    caef3f5 View commit details

Commits on Aug 5, 2016

  1. fix dependencies

    andrenarchy committed Aug 5, 2016
    Copy the full SHA
    41a8577 View commit details
  2. Merge pull request #47 from paperhive/greenkeeper-eslint-config-airbn…

    …b-10.0.0
    
    Update eslint-config-airbnb to version 10.0.0 🚀
    andrenarchy authored Aug 5, 2016
    Copy the full SHA
    41bf7e0 View commit details

Commits on Aug 10, 2016

  1. Copy the full SHA
    c497e5d View commit details
  2. Merge pull request #54 from paperhive/greenkeeper-should-11.0.0

    Update should to version 11.0.0 🚀
    andrenarchy authored Aug 10, 2016
    Copy the full SHA
    346669b View commit details
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