'use strict';

/**
 * Mocha hooks to always clear & drop db for each test
 * Thanks to: http://www.scotchmedia.com/tutorials/express/authentication/1/06
 **/ 

var mongoose = require('mongoose'),
	db = 'mongodb://127.0.0.1/slugherodbtest';

module.exports = function () {

	before('Clear database', function (done) {

		function clearDB() {			
			for (var i in mongoose.connection.collections) {
				mongoose.connection.collections[i].remove(function () { });
			}
			return done();
		}

		if (mongoose.connection.readyState === 0) {
			mongoose.connect(db, function (err) {
				if (err) {
					throw err;
				}
				return clearDB();
			});
		} else {
			return clearDB();
		}
		
	});

	after('Drop database', function (done) {
		
		mongoose.connection.db.dropDatabase(function (err) {
			if (err) return done(err);			
			mongoose.disconnect(done);
		});
		
	});

}
@@ -5,17 +5,16 @@
  strict'
-;
 %0A%0A/**%0A *
@@ -149,17 +149,16 @@
 /06%0A **/
- 
 %0A%0Avar mo
@@ -187,17 +187,18 @@
 oose'),%0A
-%09
+  
 db = 'mo
@@ -230,17 +230,16 @@
 odbtest'
-;
 %0A%0Amodule
@@ -263,18 +263,18 @@
 on () %7B%0A
-%0A%09
+  
 before('
@@ -308,19 +308,20 @@
 done) %7B%0A
-%0A%09%09
+    
 function
@@ -332,19 +332,20 @@
 arDB
+ 
 () %7B
-%09%09%09%0A%09%09%09
+%0A      
 for 
@@ -389,20 +389,24 @@
 ions) %7B%0A
-%09%09%09%09
+        
 mongoose
@@ -456,43 +456,97 @@
 () %7B
- 
 %7D)
-;%0A%09%09%09%7D%0A%09%09%09return done();%0A%09%09%7D%0A%0A%09%09
+%0A      %7D%0A      setTimeout(function () %7B%0A        return done()%0A      %7D, 500)%0A    %7D%0A%0A    
 if (
@@ -585,19 +585,22 @@
 == 0) %7B%0A
-%09%09%09
+      
 mongoose
@@ -629,20 +629,24 @@
 (err) %7B%0A
-%09%09%09%09
+        
 if (err)
@@ -652,34 +652,46 @@
 ) %7B%0A
-%09%09%09%09%09throw err;%0A%09%09%09%09%7D%0A%09%09%09%09
+          throw err%0A        %7D%0A        
 retu
@@ -706,19 +706,22 @@
 DB()
-;%0A%09%09%09%7D);%0A%09%09
+%0A      %7D)%0A    
 %7D el
@@ -725,19 +725,22 @@
  else %7B%0A
-%09%09%09
+      
 return c
@@ -751,24 +751,24 @@
 DB()
-;%0A%09%09%7D%0A%09%09%0A%09
+%0A    %7D%0A%0A  
 %7D)
-;
 %0A%0A
-%09
+  
 afte
@@ -804,21 +804,51 @@
 done) %7B%0A
-%09%09%0A%09%09
+    setTimeout(function () %7B%0A      
 mongoose
@@ -892,19 +892,24 @@
 (err) %7B%0A
-%09%09%09
+        
 if (err)
@@ -929,16 +929,17 @@
 err)
-;%09%09%09%0A%09%09%09
+%0A        
 mong
@@ -963,22 +963,35 @@
 one)
-;%0A%09%09%7D);%0A%09%09%0A%09
+%0A      %7D)%0A%0A    %7D, 500)%0A  
 %7D)
-;
 %0A%0A%7D
+%0A

Fix test hooks