var mongoose = require('mongoose');
var userSchema = require('../../server/schemas/user.schema');
var suggestionSchema = require('../../server/schemas/suggestion.schema');
var questionSchema = require('../../server/schemas/question.schema');
var ipAnswerSchema = require('../../server/schemas/ip-answer.schema');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var restore = require('mongodb-restore');
var Promise = require('bluebird');
require('dotenv').load();

module.exports = function() {
  process.env.NODE_ENV = 'test';

  beforeEach(function(done) {
    // Remove mocha timeout
    this.timeout(0);

    // Use chaiAsPromised
    chai.use(chaiAsPromised);

    // Init 'should'
    chai.should();

    // Use bluebird promises
    mongoose.Promise = Promise;

    // If not connected connect and then populate
    if (mongoose.connection.readyState === 0) {
      mongoose.connect(process.env.MONGO_TEST_URI, function(err) {
        if (err) {
          throw err;
        }
        return populateDB(done);
      });
    } else { // If connected, populate
      return populateDB(done);
    }

    function populateDB(done) {
      mongoose.model('User', userSchema.schema);
      mongoose.model('Suggestion', suggestionSchema.schema);
      mongoose.model('Question', questionSchema.schema);
      mongoose.model('IpAnswer', ipAnswerSchema.schema);

      // Create default data in the db
      restore({
        uri: process.env.MONGO_TEST_URI,
        root: __dirname + '/../../server/data/sample',
        drop: true,
        dropCollections: true,
        metadata: true,
        callback: done
      });
    }

  });

  after(function(done) {
    mongoose.disconnect();
    done();
  });

};

@@ -558,88 +558,279 @@
 %0A%0A  
-beforeEach(function(done) %7B%0A    // Remove mocha timeout%0A    this.timeout(0
+// Init mongoose schemas%0A  mongoose.model('User', userSchema.schema);%0A  mongoose.model('Suggestion', suggestionSchema.schema);%0A  mongoose.model('Question', questionSchema.schema);%0A  mongoose.model('IpAnswer', ipAnswerSchema.schema);%0A%0A  // Init 'should'%0A  chai.should(
 );%0A%0A
-  
   //
@@ -849,18 +849,16 @@
 romised%0A
-  
   chai.u
@@ -882,51 +882,8 @@
 );%0A%0A
-    // Init 'should'%0A    chai.should();%0A%0A  
   //
@@ -905,18 +905,16 @@
 romises%0A
-  
   mongoo
@@ -936,16 +936,123 @@
 omise;%0A%0A
+  // Before each test hook%0A  beforeEach(function(done) %7B%0A    // Remove mocha timeout%0A    this.timeout(0);%0A%0A
     // I
@@ -1414,233 +1414,8 @@
 ) %7B%0A
-      mongoose.model('User', userSchema.schema);%0A      mongoose.model('Suggestion', suggestionSchema.schema);%0A      mongoose.model('Question', questionSchema.schema);%0A      mongoose.model('IpAnswer', ipAnswerSchema.schema);%0A%0A
     

Refactor pretest script