Skip to content

Commit 8a795ce

Browse files
add connectable tests
1 parent bdb7c1b commit 8a795ce

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

test/connectable/create-manager.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Connectable ::', function() {
5+
describe('Create Manager', function() {
6+
it('should validate the connection string has a protocol', function(done) {
7+
Pack.createManager({
8+
connectionString: 'localhost:5432/mppg'
9+
})
10+
.exec(function(err) {
11+
assert(err);
12+
assert.equal(err.exit, 'malformed');
13+
14+
return done();
15+
});
16+
});
17+
18+
it('should successfully return a Pool', function(done) {
19+
Pack.createManager({
20+
connectionString: 'mysql://mp:mp@localhost:5432/mppg'
21+
})
22+
.exec(function(err, report) {
23+
if (err) {
24+
return done(err);
25+
}
26+
27+
// Assert that the manager has a pool object
28+
assert(report.manager.pool);
29+
30+
// Assert that the manager has a getConnection function
31+
assert(report.manager.pool.getConnection);
32+
33+
return done();
34+
});
35+
});
36+
});
37+
});

test/connectable/destroy-manager.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Connectable ::', function() {
5+
describe('Destroy Manager', function() {
6+
var manager;
7+
8+
// Create a manager
9+
before(function(done) {
10+
Pack.createManager({
11+
connectionString: 'mysql://mp:mp@localhost:3306/mppg'
12+
})
13+
.exec(function(err, report) {
14+
if (err) {
15+
return done(err);
16+
}
17+
18+
manager = report.manager;
19+
return done();
20+
});
21+
});
22+
23+
24+
it('should successfully destroy the manager', function(done) {
25+
Pack.destroyManager({
26+
manager: manager
27+
})
28+
.exec(function(err) {
29+
assert(!err);
30+
return done();
31+
});
32+
});
33+
});
34+
});

test/connectable/get-connection.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Connectable ::', function() {
5+
describe('Get Connection', function() {
6+
var manager;
7+
8+
// Create a manager
9+
before(function(done) {
10+
// Needed to dynamically get the host using the docker container
11+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
12+
13+
Pack.createManager({
14+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
15+
})
16+
.exec(function(err, report) {
17+
if (err) {
18+
return done(err);
19+
}
20+
21+
manager = report.manager;
22+
return done();
23+
});
24+
});
25+
26+
it('should successfully return a connection instance', function(done) {
27+
Pack.getConnection({
28+
manager: manager
29+
})
30+
.exec(function(err, report) {
31+
if (err) {
32+
return done(err);
33+
}
34+
35+
// Assert that the report has a client object
36+
assert(report.connection);
37+
38+
// Assert that the connection has a release function
39+
assert(report.connection.release);
40+
41+
return done();
42+
});
43+
});
44+
});
45+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Connectable ::', function() {
5+
describe('Release Connection', function() {
6+
var manager;
7+
var connection;
8+
9+
// Create a manager and connection
10+
before(function(done) {
11+
// Needed to dynamically get the host using the docker container
12+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
13+
14+
Pack.createManager({
15+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
16+
})
17+
.exec(function(err, report) {
18+
if (err) {
19+
return done(err);
20+
}
21+
22+
manager = report.manager;
23+
24+
Pack.getConnection({
25+
manager: manager
26+
})
27+
.exec(function(err, report) {
28+
if (err) {
29+
return done(err);
30+
}
31+
32+
connection = report.connection;
33+
return done();
34+
});
35+
});
36+
});
37+
38+
it('should successfully release a connection', function(done) {
39+
// Grab the number of free connections before releasing the current one
40+
var freeConnectionsPreRelease = manager.pool._freeConnections.length;
41+
42+
// Release the connection
43+
Pack.releaseConnection({
44+
connection: connection
45+
})
46+
.exec(function(err) {
47+
if (err) {
48+
return done(err);
49+
}
50+
51+
// If the connection was successfully released the _allConnections and the
52+
// _freeConnections should be equal.
53+
// https://github.com/mysqljs/mysql/blob/master/lib/Pool.js
54+
var poolSize = manager.pool._allConnections.length;
55+
var freeConnectionsPostRelease = manager.pool._freeConnections.length;
56+
57+
// Ensure we end up with different counts after releasing the connection
58+
assert.notEqual(freeConnectionsPostRelease, freeConnectionsPreRelease);
59+
60+
// Ensure that all the available connections are free
61+
assert.equal(poolSize, freeConnectionsPostRelease);
62+
63+
return done();
64+
});
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)