import test from 'ava';
const helper = require('think-helper');
const mock = require('mock-require');
const mysql = require('../index.js');


function getMysql() {
  return mock.reRequire('../index');
}
const config = {
  user: 'root',
  password: '',
  database: 'think_test',
  logConnect: true,
  logSql: true,
  connectionLimit: 1
};

test.afterEach.always(_ => {
  // instance.close();
});

test('constructor function', async t => {
  const mysql = getMysql();
  let instance = mysql.getInstance(config);
  let conn = await instance.getConnection();
  t.is(helper.isEmpty(instance.config),false);
  t.is(helper.isEmpty(instance.pool),false);
  t.is(helper.isEmpty(conn),false);
});

test('constructor function', async t => {
  let conf = Object.assign(config,{socketPath:'/var/run/mysqld/mysqld.sock'});
  const mysql = getMysql();
  let instance = mysql.getInstance(conf);
  let conn = await instance.getConnection();
  t.is(helper.isEmpty(instance.config),false);
  t.is(helper.isEmpty(instance.pool),false);
  t.is(helper.isEmpty(conn),false);
});

test('constructor function', async t => {
  config.logConnect = false;
  const mysql = getMysql();
  let instance = mysql.getInstance(config);
  let conn = await instance.getConnection();
  t.is(helper.isEmpty(instance.config),false);
  t.is(helper.isEmpty(instance.pool),false);
  t.is(helper.isEmpty(conn),false);
});

test('query function', async t => {
  const mysql = getMysql();
  let instance = mysql.getInstance(config);

  await instance.execute({
    sql:"insert into `think_test`.`books` (`name`, `author`) values ('thinkjs best practice', ?)",
    timeout: 5000,
    values: ['David']
  });

  let books = await instance.query({
    sql:'SELECT * FROM `books` WHERE `author` = ?',
    timeout: 5000,
    values: ['David']
  });

  t.is(books[0].name,'thinkjs best practice');
});
@@ -99,46 +99,8 @@
 ');%0A
-const mysql = require('../index.js');%0A
 %0A%0Afu
@@ -1799,12 +1799,794 @@
 ctice');%0A%7D);
+%0A%0Atest('query function', async t =%3E %7B%0A  const mysql = getMysql();%0A  let instance = mysql.getInstance(config);%0A%0A  await instance.execute(%0A    %22insert into %60think_test%60.%60books%60 (%60name%60, %60author%60) values ('thinkjs best practice', 'David')%22%0A  );%0A%0A  let books = await instance.query(%7B%0A    sql:'SELECT * FROM %60books%60 WHERE %60author%60 = ?',%0A    timeout: 5000,%0A    values: %5B'David'%5D%0A  %7D);%0A%0A  t.is(books%5B0%5D.name,'thinkjs best practice');%0A%7D);%0A%0Atest('close function', async t =%3E %7B%0A  const mysql = getMysql();%0A  let instance = mysql.getInstance(config);%0A%0A  await instance.close();%0A  t.is(instance.pool._closed,true);%0A%7D);%0A%0Atest('close function', async t =%3E %7B%0A  const mysql = getMysql();%0A  let instance = mysql.getInstance(config);%0A%0A  await instance.close();%0A  t.is(instance.pool._closed,true);%0A%7D);

add test case