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', () => {
  let mongo = new MongoDBIntegration(mongomock.MongoClient).as('test');
  let executeSuccess = (flow, done) => {
    flow.execute(err => {
      if (err) throw err;
      done();
    });
  }

  it('should insert', (done) => {
    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);
              });
    executeSuccess(flow, done);
  })

  it('should find dogs', done => {
    let flow = new datafire.Flow('test_flow');
    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');
              });
    executeSuccess(flow, done);
  });

  it('should find Grumpy', done => {
    let flow = new datafire.Flow('test_flow');
    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');
              })
    executeSuccess(flow, done);
  })

  it('should update Lucy', done => {
    let flow = new datafire.Flow('test_flow');
    flow.step('update', mongo.update('Pet'), {query: {name: "Lucy"}, update: {$set: {age: 2}}});
    flow.step('update_result',
              data => {
                expect(data.update.n).to.equal(1);
              });
    flow.step('lucy', mongo.findOne('Pet'), {query: {name: "Lucy"}});
    flow.step('find_result',
              data => {
                expect(data.lucy.name).to.equal("Lucy");
                expect(data.lucy.age).to.equal(2);
              })
    executeSuccess(flow, done);
  })

  it('should remove Grumpy', done => {
    let flow = new datafire.Flow('test_flow');
    flow.step('remove', mongo.remove('Pet'), {query: {name: "Grumpy"}})
    flow.step('remove_result',
              data => {
                expect(data.remove.n).to.equal(1);
              })
    flow.step('find_grumpy', mongo.find('Pet'), {query: {name: "Grumpy"}})
    flow.step('find_result',
              data => {
                expect(data.find_grumpy.length).to.equal(0);
              })
    executeSuccess(flow, done);
  })
})

@@ -824,16 +824,35 @@
 insert',
+ %7B%0A      operation:
  mongo.i
@@ -864,16 +864,30 @@
 ('Pet'),
+%0A      params:
  %7Bdocume
@@ -900,54 +900,23 @@
 ets%7D
-)%0A 
+,%0A
       
- .step('insert_result',%0A             
+finish:
  dat
@@ -918,32 +918,24 @@
 : data =%3E %7B%0A
-        
         expe
@@ -970,32 +970,24 @@
 1);%0A        
-  
-      
 expect(data.
@@ -1017,36 +1017,34 @@
 .length);%0A      
-    
+%7D%0A
     %7D);%0A    exec

use new step API