text: "use strict";
  
  // IN TRAVIS-CI ONLY TEST WITH REAL DB IN ONE VERSION 0.12
  if(process.versions.node.substr(0,4)!=="0.12" && process.env.TRAVIS) return;
  
  // WHEN COVER with NO DB NO DB
  if(process.env.COVER==="ndb") return;
  
  var _ = require('lodash');
  var expect = require('expect.js');
  var pg0 = require('pg');
  var pg = require('..');
  var Promises = require('best-promise');
  var colors = require('colors'); 
  
  console.warn(pg.poolBalanceControl());
  
  describe('pg-promise-strict with real database', function(){
      var connectParams = {
          user: 'test_user',
          password: 'test_pass',
          database: 'test_db',
          host: 'localhost',
          port: 5432
      }
      var expectedTable1Data = [
          {id:1, text1:'one'},
          {id:2, text1:'two'},
      ];
      describe('pool connections', function(){
          it('failed connection', function(done){
              Promises.start(function(){
                  return pg.connect({
                      user: 'test_user',
                      password: 'bad_pass',
                      database: 'test_db',
                      host: 'localhost',
                      port: 5432
                  });
              }).then(function(client){
                  if(process.env.TRAVIS){
                      console.log('**************** MAY BE AN ERROR. I MUST STUDY MORE THIS ISSUE ************** ');
                      client.query("SELECT 1").execute().then(function(result){
                          console.log('** SOMETHING IS WRONG IN TRAVIS WITH PG PASSs ***',result);
                          client.done();
                          done();
                      }).catch(function(err){
                          console.log("ok. error detected when execute");
                          done();
                      });
                  }else{
                      done(new Error('must raise error'));
                  }
              }).catch(function(err){
                  expect(err).to.be.a(Error);
                  expect(err.code).to.be('28P01');
                  expect(err.message).to.match(/(aut.*|pass.*){2}/);
                  done();
              }).catch(done).then(function(){
              });
          });
          it('successful connection', function(done){
              pg.debug.Client=true;
              pg.debug.pool=true;
              Promises.start().then(function(){
                  return pg.connect(connectParams);
              }).then(function(client){
                  expect(client).to.be.a(pg.Client);
                  expect(client.internals.client).to.be.a(pg0.Client);
                  expect(pg.poolBalanceControl().length>0).to.be.ok();
                  client.done();
                  expect(pg.poolBalanceControl().length==0).to.be.ok();
                  done();
              }).catch(function(err){
                  console.log('Check your postgresql 9.3 instalation. Then be sure to create the user and db with:');
                  console.log("create user test_user password 'test_pass';".cyan);
                  console.log("create database test_db owner test_user;".cyan);
                  done(err);
              }).then(function(){
                  pg.debug.Client=false;
              });
          });
      });
      describe('call queries', function(){
          var client;
          var poolLog;
          before(function(done){
              pg.connect(connectParams).then(function(returnedClient){
                  // if(pg.poolBalanceControl().length>0) done(new Error("There are UNEXPECTED unbalanced conections"));
                  client = returnedClient;
                  done();
              });
          });
          after(function(){
              client.done();
          });
          it("successful query that doesn't return rows", function(done){
              pg.easy=true;
              pg.debug.Query=true;
              client.query("drop schema if exists test_pgps cascade;").then(function(result){
                  expect(result.command).to.be("DROP");
                  expect(result.rowCount).to.not.be.ok();
                  done();
              }).catch(done).then(function(){
                  pg.debug.Query=false;
              });
          });
          function tipicalExecuteWay(queryText,done,commandExpected,resultExpected,functionName,params){
              pg.easy=false;
              client.query(queryText,params)[functionName||"execute"]().then(function(result){
                  if(resultExpected){
                      for(var attr in resultExpected){
                          expect([attr,result[attr]]).to.eql([attr,resultExpected[attr]]);
                      }
                  }else{
                      expect(result.rowCount).to.not.be.ok();
                  }
                  expect(result.command).to.be(commandExpected);
                  done();
              }).catch(done).then(function(){
                  pg.debug.Query=false;
              });
          }
          it("call execute directly", function(done){
              this.timeout(5000);
              if(process.env.APPVEYOR){
                  done();
                  return;
              }
              tipicalExecuteWay("create schema test_pgps;",done,'CREATE');
          });
          function tipicalFail(textQuery,done,reason,code,msg,functionName){
              client.query(textQuery)[functionName||"execute"]().then(function(result){
                  console.log("EXPECT FAIL BUT OBTAINS",result);
                  done(new Error("Must fail because "+reason));
              }).catch(function(err){
                  expect(err).to.be.a(Error);
                  expect(err.code).to.be(code);
                  expect(err).to.match(msg);
                  done();
              }).catch(done).then(function(){
              });
          }
          it("failed call", function(done){
              tipicalFail("create schema test_pgps;",done,"the schema exists",'42P06',/(exist.*|test_pgps.*){2}/);
          });
          it("call a compound", function(done){
              tipicalExecuteWay(
                  "do $$ begin "+
                  "create table test_pgps.table1(id integer primary key, text1 text); "+
                  "create table test_pgps.table2(text2 text primary key, int2 integer); "+
                  "end$$;",
                  done,
                  "DO"
              )
          });
          it("call multiple insert with returning clause", function(done){
              tipicalExecuteWay("insert into test_pgps.table1 values (1,'one'), (2,'two');",done,"INSERT",{
                  rowCount:2
              })
          });
          it("query unique value", function(done){
              tipicalExecuteWay("select 3+$1",done,"SELECT",{
                  value:8
              },"fetchUniqueValue",[5])
          });
          it("fail to query unique value", function(done){
              tipicalFail("select 1, 2",done,"returns 2 columns","54U11!",/query expects.*one field.*and obtains 2/,"fetchUniqueValue")
          });
          it("query unique row", function(done){
              tipicalExecuteWay("select * from test_pgps.table1 order by id limit 1",done,"SELECT",{
                  row:expectedTable1Data[0]
              },"fetchUniqueRow")
          });
          it("fail to query unique row", function(done){
              tipicalFail("select * from test_pgps.table1",done,"returns 2 rows","54011!",/query expects.*one row.*and obtains 2/,"fetchUniqueRow")
          });
          it("query row by row", function(done){
              var accumulate=[];
              client.query("select * from test_pgps.table1 order by id").onRow(function(row){
                  accumulate.unshift(row);
              }).then(function(){
                  accumulate.reverse();
                  expect(accumulate).to.eql(expectedTable1Data);
                  done();
              }).catch(done);
          });
          it("control not call query row by row without callback", function(done){
              tipicalFail("select 1, 2",done,"no callback provide","39004!",/fetchRowByRow must recive a callback/,"fetchRowByRow")
          });
      });
      describe('pool-less connections', function(){
          describe('call queries', function(){
              var client;
              before(function(){
                  client = new pg.Client(connectParams);
              });
              it("successful query", function(done){
                  pg.easy=true;
                  client.connect().then(function(){
                      return client.query("select * from test_pgps.table1 order by id;");
                  }).then(function(result){
                      expect(result.rows).to.eql(expectedTable1Data);
                      result.client.end();
                      done();
                  }).catch(done).then(function(){
                  });
              });
              it("unsuccessful query", function(done){
                  this.timeout(5000);
                  pg.easy=true;
                  pg.debug.Client=true;
                  client = new pg.Client("this_user@xxxx");
                  expect(client).to.be.a(pg.Client);
                  expect(client.internals.client).to.be.a(pg0.Client);
                  client.connect().then(function(){
                      done(new Error("must raise error"));
                  }).catch(function(err){
                      expect(err.message).to.match(/(aut.*|pass.*){2}|database.*does not exist/);
                      done();
                  }).catch(done).then(function(){
                  });
              });
              it("connect with extra parameter", function(done){
                  this.timeout(5000);
                  pg.easy=true;
                  pg.debug.Client=true;
                  client = new pg.Client("this_user@xxxx");
                  expect(client).to.be.a(pg.Client);
                  expect(client.internals.client).to.be.a(pg0.Client);
                  client.connect("extra parameter").then(function(){
                      done(new Error("must raise error because must not have parameters"));
                  }).catch(function(err){
                      expect(err.message).to.match(/must no recive parameters/);
                      done();
                  }).catch(done).then(function(){
                  });
              });
          });
      });
  });
  
  