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');
});
import test from 'ava';
const helper = require('think-helper');
const mock = require('mock-require');


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');
});

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

  await instance.execute(
    "insert into `think_test`.`books` (`name`, `author`) values ('thinkjs best practice', 'David')"
  );

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

  t.is(books[0].name,'thinkjs best practice');
});

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

  await instance.close();
  t.is(instance.pool._closed,true);
});

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

  await instance.close();
  t.is(instance.pool._closed,true);
});
add test case