'use strict';

var Database = require('../lib/database');
var drivers = require('./drivers');
var should = require('should');
var fs = require('fs');

var getDriver = function(name, opts) {
  return new Database(name, opts);
};

var driver = drivers[1];
var db = getDriver(driver.name, driver.opts);

var path = 'test/sql/';
var options = {encoding:'utf8'};
var script;

before(function(done) {
  script = fs.readFileSync(path + 'dropSP1.sql',options);
  db.execute(driver.dropSP1)
    .then(function(){
      script = fs.readFileSync(path + 'createSP1.sql',options);
      db.execute(script);
    })
    .then(function(){
      script = fs.readFileSync(path + 'dropSP2.sql',options);
      db.execute(script);
    })
    .then(function(){
      script = fs.readFileSync(path + 'createSP2.sql',options);
      db.execute(script);
    })
    .then(function(){
      script = fs.readFileSync(path + 'dropSP3.sql',options);
      db.execute(script);
    })
    .then(function(){
      script = fs.readFileSync(path + 'createSP3.sql',options);
      db.execute(script);
      done();
    });
});

after(function(done) {
  script = fs.readFileSync(path + 'dropSP1.sql',options);
  db.execute(script)
    .then(function(){
      script = fs.readFileSync(path + 'dropSP2.sql',options);
      db.execute(driver.dropSP2);
    })
    .then(function(){
      script = fs.readFileSync(path + 'dropSP3.sql',options);
      db.execute(driver.dropSP3);
        done();
    });
});


describe('Happy Stored Procedure', function() {

  it('should return record set', function(done) {
      db.procedure.execute('SP_TEST1')
        .then(function(recordSet){
          should.exist(recordSet);
          done();
        });
  });  

  it('should return record for the given id', function(done) {
      db.procedure.input('id', 'Int', 5);
      db.procedure.execute('SP_TEST2')
        .then(function(recordSet){
          recordSet[0].name.should.equal('test5');
          done();
        });
  });  

  it('should return output for the given id', function(done) {
      db.procedure.input('id', 'Int', 7);
      db.procedure.output('result', 'NVarChar');
      db.procedure.execute('SP_TEST3')
        .then(function(recordSet){
          recordSet.returnValue.should.equal(0);
          done();
        });
  });    
});



describe('Unhappy Stored Procedure', function() {

  it('should throw error if not a valid stored procedure', function(done) {
      db.procedure.execute('SP_TEST')
        .catch(function(err){
            err.message.should.match(/Could not find stored procedure/);
            done();
        });
    });  

  it('should throw error if not a valid input parameter', function(done) {
      db.procedure.input('invalid', 'Int', 5);
      db.procedure.execute('SP_TEST2')
        .catch(function(err){
            err.message.should.match(/expects parameter '@id', which was not supplied./);
            done();
        });
  });  

  it('should throw error if not a valid data type in input parameter', function(done) {
      db.procedure.input('id', 'IntV', 5);
      db.procedure.execute('SP_TEST2')
        .catch(function(err){
            err.message.should.match(/expects parameter '@id', which was not supplied./);
            done();
        });
  });     

  it('should throw error if less number of parameter passed', function(done) {
      db.procedure.input('id', 'Int', 5);
      db.procedure.execute('SP_TEST3')
        .catch(function(err){
            err.message.should.match(/expects parameter '@result', which was not supplied./);
            done();
        });
  });      

  it('should throw error if more number of parameter passed', function(done) {
      db.procedure.input('id1', 'Int', 5);
      db.procedure.input('id2', 'Int', 5);
      db.procedure.execute('SP_TEST2')
        .catch(function(err){
            err.message.should.match(/SP_TEST2 has too many arguments specified/);
            done();
        });
  });    

});
@@ -2601,18 +2601,16 @@
     %7D);%0A
-  
   %7D);  %0A
@@ -3004,36 +3004,32 @@
 eter', function(
-done
 ) %7B%0A      db.pro
@@ -3014,32 +3014,46 @@
 ction() %7B%0A      
+(function()%7B%0A%09
 db.procedure.inp
@@ -3069,30 +3069,25 @@
 'IntV', 5);%0A
-      
+%09
 db.procedure
@@ -3110,158 +3110,63 @@
 T2')
+;
 %0A      
-  .catch(function(err)%7B%0A            err.message.should.match(/expects parameter '@id', which was not supplied./);%0A            done();%0A        %7D
+%7D).should.throw(/IntV is not a valid data type/
 );%0A 
@@ -3864,12 +3864,13 @@
 %7D);    %0A%0A%7D);
+%0A

Test case modified to assert with thrown error on invalid data type