let expect = require('chai').expect;
let mongomock = require('mongo-mock');
mongomock.max_delay = 0;

let datafire = require('../index');
datafire.integrationsDirectory = __dirname + '/integrations';
datafire.credentialsDirectory = __dirname + '/credentials';

let MongoDBIntegration = require('../native_integrations/mongodb');

describe('MongoDB Integration', () => {
  it('should work', (done) => {
    let mongo = new MongoDBIntegration(mongomock.MongoClient).as('test');
    let flow = new datafire.Flow('test_flow');

    let pets = [{
      name: 'Lucy',
      type: 'dog',
    }, {
      name: 'Blaney',
      type: 'dog',
    }, {
      name: 'Grumpy',
      type: 'cat',
    }]

    flow.step('insert', mongo.insert('Pet'), {documents: pets})
        .step('insert_result',
              data => {
                expect(data.insert.ok).to.equal(1);
                expect(data.insert.n).to.equal(pets.length);
              });
    flow.step('find', mongo.find('Pet'), {query: {type: 'dog'}})
        .step('find_result',
              data => {
                expect(data.find.length).to.equal(2);
                expect(data.find[0].name).to.equal('Lucy');
                expect(data.find[1].name).to.equal('Blaney');
              });
    flow.step('findOne', mongo.findOne('Pet'), {query: {name: "Grumpy"}})
    flow.step('findOne_result',
              data => {
                expect(data.findOne.name).to.equal('Grumpy');
                expect(data.findOne.type).to.equal('cat');
              })
    flow.execute(err => {
      if (err) throw err;
      done();
    });
  })
})

@@ -367,42 +367,8 @@
 %3E %7B%0A
-  it('should work', (done) =%3E %7B%0A  
   le
@@ -431,24 +431,178 @@
 as('test');%0A
+  let executeSuccess = (flow, done) =%3E %7B%0A    flow.execute(err =%3E %7B%0A      if (err) throw err;%0A      done();%0A    %7D);%0A  %7D%0A%0A  it('should insert', (done) =%3E %7B%0A
     let flow
@@ -636,17 +636,16 @@
 flow');%0A
-%0A
     let 
@@ -1043,32 +1043,152 @@
             %7D);%0A
+    executeSuccess(flow, done);%0A  %7D)%0A%0A  it('should find dogs', done =%3E %7B%0A    let flow = new datafire.Flow('test_flow');%0A
     flow.step('f
@@ -1479,24 +1479,147 @@
         %7D);%0A
+    executeSuccess(flow, done);%0A  %7D);%0A%0A  it('should find Grumpy', done =%3E %7B%0A    let flow = new datafire.Flow('test_flow');%0A
     flow.ste
@@ -1882,75 +1882,33 @@
     
-flow.
 execute
-(err =%3E %7B%0A      if (err) throw err;%0A      done();%0A    %7D
+Success(flow, done
 );%0A 

break up mongo tests