const Connection = require('../../src/connection');
const Request = require('../../src/request');
const fs = require('fs');
const TYPES = require('../../src/data-type').typeByName;

const config = JSON.parse(
  fs.readFileSync(process.env.HOME + '/.tedious/test-connection.json', 'utf8')
).config;

config.options.debug = {
  packet: true,
  data: true,
  payload: true,
  token: true,
  log: true
};

config.options.tdsVersion = process.env.TEDIOUS_TDS_VERSION;

exports.insertBinary = function(test) {
  const connection = new Connection(config);
  connection.on('end', function() {
    test.done();
  });

  connection.on('connect', function(err) {
    test.ifError(err);

    const request = new Request(
      'CREATE TABLE #test ([data] binary(4))',
      function(err) {
        test.ifError(err);

        const request = new Request(
          'INSERT INTO #test ([data]) VALUES (@p1)',
          function(err) {
            test.ifError(err);

            const request = new Request('SELECT [data] FROM #test', function(
              err
            ) {
              test.ifError(err);
              connection.close();
            });

            request.on('row', function(columns) {
              test.strictEqual(
                columns[0].value.toString('hex'),
                new Buffer([0x12, 0x34, 0x00, 0xce]).toString('hex')
              );
            });

            connection.execSql(request);
          }
        );

        request.addParameter(
          'p1',
          TYPES.Binary,
          new Buffer([0x12, 0x34, 0x00, 0xce])
        );
        connection.execSql(request);
      }
    );

    connection.execSqlBatch(request);
  });
};

@@ -95,34 +95,8 @@
 ');%0A
-const fs = require('fs');%0A
 cons
@@ -149,16 +149,79 @@
 yName;%0A%0A
+const fs = require('fs');%0Aconst %7B assert %7D = require('chai');%0A%0A
 const co
@@ -498,30 +498,41 @@
 N;%0A%0A
-exports.
+describe('
 insert
-B
+ing b
 inary 
-=
+data',
  fun
@@ -541,24 +541,51 @@
 ion(
-test
 ) %7B%0A  
-const 
+beforeEach(function(done) %7B%0A    this.
 conn
@@ -619,16 +619,23 @@
 fig);%0A  
+  this.
 connecti
@@ -645,14 +645,44 @@
 on('
-end', 
+connect', done);%0A  %7D);%0A%0A  afterEach(
 func
@@ -686,24 +686,28 @@
 unction(
+done
 ) %7B%0A    
 test.don
@@ -702,94 +702,213 @@
     
-test.done();%0A  %7D);%0A%0A  connection.on('connect', function(err) %7B%0A    test.ifError(err);%0A
+if (!this.connection.closed) %7B%0A      this.connection.on('end', done);%0A      this.connection.close();%0A    %7D else %7B%0A      done();%0A    %7D%0A  %7D);%0A%0A  it('should correctly insert the binary data', function(done) %7B
 %0A   
@@ -936,23 +936,16 @@
 Request(
-%0A      
 'CREATE 
@@ -980,61 +980,72 @@
 ))',
-%0A      function(err) %7B%0A        test.ifError(err);%0A%0A  
+ (err) =%3E %7B%0A      if (err) %7B%0A        return done(err);%0A      %7D%0A%0A
     
@@ -1074,27 +1074,16 @@
 Request(
-%0A          
 'INSERT 
@@ -1120,250 +1120,339 @@
 1)',
-%0A          function(err) %7B%0A            test.ifError(err);%0A%0A            const request = new Request('SELECT %5Bdata%5D FROM #test', function(%0A              err%0A            ) %7B%0A              test.ifError(err);%0A              connection.close();%0A    
+ (err) =%3E %7B%0A        if (err) %7B%0A          return done(err);%0A        %7D%0A%0A        const values = %5B%5D;%0A        const request = new Request('SELECT %5Bdata%5D FROM #test', (err) =%3E %7B%0A          if (err) %7B%0A            return done(err);%0A          %7D%0A%0A          assert.deepEqual(values, %5Bnew Buffer(%5B0x12, 0x34, 0x00, 0xce%5D)%5D);%0A%0A          done();%0A
     
@@ -1452,36 +1452,32 @@
 );%0A        %7D);%0A%0A
-    
         request.
@@ -1520,163 +1520,36 @@
     
-    test.strictEqual(%0A                columns%5B0%5D.value.toString('hex'),%0A                new Buffer(%5B0x12, 0x34, 0x00, 0xce%5D).toString('hex')%0A              
+values.push(columns%5B0%5D.value
 );%0A 
@@ -1551,28 +1551,24 @@
 e);%0A        
- 
-   
 %7D);%0A%0A       
@@ -1564,28 +1564,29 @@
 );%0A%0A        
-    
+this.
 connection.e
@@ -1612,22 +1612,9 @@
     
-    %7D%0A        
+%7D
 );%0A%0A
@@ -1611,34 +1611,32 @@
      %7D);%0A%0A      
-  
 request.addParam
@@ -1644,34 +1644,13 @@
 ter(
-%0A          'p1',%0A         
+'p1',
  TYP
@@ -1659,26 +1659,16 @@
 .Binary,
-%0A         
  new Buf
@@ -1696,25 +1696,16 @@
 , 0xce%5D)
-%0A        
 );%0A     
@@ -1705,18 +1705,21 @@
 ;%0A      
-  
+this.
 connecti
@@ -1747,24 +1747,17 @@
     
-  %7D%0A    
+%7D
 );%0A%0A    
 conn
@@ -1752,16 +1752,21 @@
 );%0A%0A    
+this.
 connecti
@@ -1798,10 +1798,11 @@
 %0A  %7D);%0A%7D
+)
 ;%0A

convert the binary insert integration test to mocha