Skip to content

Commit

Permalink
Remove deprecated bluebird functions.
Browse files Browse the repository at this point in the history
Added more tests.
  • Loading branch information
martinj committed Apr 5, 2016
1 parent dc4adaf commit ebcb0aa
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ script:

before_install:
- npm -g install npm@latest-2

before_script:
- mysql -e 'create database mysqlpromise;'
- mysql -u travis mysqlpromise < test/fixtures/db.sql
44 changes: 23 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,48 @@ DB.prototype.isConfigured = function () {
* @return {Promise}
*/
DB.prototype.query = function (query, params) {
var defer = Promise.defer();
var self = this;
params = params || {};

this.pool.getConnection(function (err, con) {
if (err) {
if (con) {
con.release();
}
return defer.reject(err);
}

con.query(query, params, function (err) {
return new Promise(function (resolve, reject) {
self.pool.getConnection(function (err, con) {
if (err) {
if (con) {
con.release();
}
return defer.reject(err);
return reject(err);
}
defer.resolve([].splice.call(arguments, 1));
con.release();

con.query(query, params, function (err) {
if (err) {
if (con) {
con.release();
}
return reject(err);
}
resolve([].splice.call(arguments, 1));
con.release();
});
});
});
return defer.promise;
};

/**
* End DB pool connections
* @return {Promise}
*/
DB.prototype.end = function () {
var defer = Promise.defer();
var self = this;

this.pool.end(function (err) {
if (err) {
return defer.reject(err);
}
return new Promise(function (resolve, reject) {
self.pool.end(function (err) {
if (err) {
return reject(err);
}

defer.resolve();
resolve();
});
});
return defer.promise;
};

module.exports = function (name) {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`foobar` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
88 changes: 77 additions & 11 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,100 @@
'use strict';
var db = require('../');
var mysql = require('../');

var dbConfig = {
host: 'localhost',
user: 'travis',
database: 'mysqlpromise'
};

require('should');

describe('mysql-promise', function () {
it('should return correct instance', function () {
var defaultDb = db();
var namedDb = db('foo');
namedDb.should.equal(db('foo'));
defaultDb.should.equal(db());
defaultDb.should.not.equal(namedDb);
var db = mysql();
var namedDb = mysql('foo');
namedDb.should.equal(mysql('foo'));
db.should.equal(mysql());
db.should.not.equal(namedDb);
});

describe('isConfigured()', function () {

it('should return false if it has no pool configured', function () {
var defaultDb = db();
defaultDb.isConfigured().should.be.false;
var db = mysql();
db.isConfigured().should.be.false;
});

it('should return false if it has no pool configured', function () {
var defaultDb = db();
defaultDb.configure({
var db = mysql();
db.configure({
host: 'localhost',
user: 'foo',
password: 'bar',
database: 'db'
});
defaultDb.isConfigured().should.be.true;
db.isConfigured().should.be.true;
});

});

describe('query()', function () {

beforeEach(function (done) {
var db = mysql('query');
db.configure(dbConfig);

db.query('DELETE FROM test')
.then(done.bind(null, null))
.catch(done);
});

it('should return results', function (done) {
var db = mysql('query');

db.query('INSERT INTO test SET ?', { id: 1, foobar: 'monkey' })
.spread(function (res) {
res.affectedRows.should.equal(1);
return db.query('SELECT * FROM test');
})
.spread(function (rows) {
rows.should.have.a.lengthOf(1);
rows[0].id.should.equal(1);
rows[0].foobar.should.equal('monkey');
done();
})
.catch(done);
});

it('should reject on error', function (done) {
var db = mysql('query');

db.query('SELECT * FROM non_existing_table')
.catch({ code: 'ER_NO_SUCH_TABLE' }, function (err) {
err.should.exist;
done();
})
.catch(done);

});
});

describe('end()', function () {

it('should close all connections in the pool', function (done) {
var db = mysql('end');
db.configure(dbConfig);
db.query('SELECT * FROM test')
.then(function () {
db.pool._allConnections.should.have.a.lengthOf(1);
return db.end();
})
.then(function () {
db.pool._allConnections.should.have.a.lengthOf(0);
done();
})
.catch(done);
});

});
});

0 comments on commit ebcb0aa

Please sign in to comment.