Created
November 11, 2013 15:16
-
-
Save leegee/7414693 to your computer and use it in GitHub Desktop.
Express and node-mysql connection pool test with nodeunit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mysql = require('mysql'); | |
| var express = require('express'); | |
| var app = express(); | |
| var controllers = module.exports.controllers = {}; | |
| var models = module.exports.models = {}; | |
| if (!process.env.dbpass){ | |
| app.use(express.logger('dev')); | |
| } | |
| app.set('port', process.env.PORT || 3000); | |
| var dbConfig = module.exports.dbConfig = { | |
| host : 'localhost', | |
| user : process.env.dbuser || 'root', | |
| password : process.env.dbpass || 'password', | |
| database : process.env.dbname || 'mysql', | |
| insecureAuth : process.env.dbpass? false : true, | |
| connectionLimit : 20, | |
| supportBigNumbers : true | |
| }; | |
| var pool = module.exports.pool = mysql.createPool( dbConfig ); | |
| app.get('/', function(req, res){ | |
| models.default( function( rows ){ | |
| res.send( rows ); | |
| } ); | |
| }); | |
| process.on('exit', function () { | |
| console.log('Express server exiting.'); | |
| }); | |
| module.exports.server = app.listen( app.get('port') ); | |
| console.log('Express server listening on port ' + app.get('port')); | |
| models.default = function(next){ | |
| pool.getConnection(function(err, dbh) { | |
| if (err) throw err; | |
| dbh.query('SELECT * FROM user LIMIT 1', function(err, rows, fields) { | |
| if (err) throw err; | |
| dbh.release(); | |
| if (next) next( rows ); | |
| }); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "express-mysql-nodeunit-experiment", | |
| "preferGlobal": false, | |
| "version": "0.0.1", | |
| "author": "Lee Goddard <[email protected]>", | |
| "description": "A quick test of mysql connection pooling in Express with nodeunit", | |
| "scripts": { | |
| "start": "node lib/app.js", | |
| "test": "nodeunit tests.js" | |
| }, | |
| "main": "./lib/app.js", | |
| "dependencies" : { | |
| "express" : "3.4.4", | |
| "mysql" : "2.0.0-alpha9" | |
| }, | |
| "analyze": false, | |
| "devDependencies": { | |
| "nodeunit" : "0.8.2" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var nodeunit = require('nodeunit'); | |
| var app; | |
| exports['The DB'] = nodeunit.testCase({ | |
| setUp: function(cb){ | |
| app = require('./app'); | |
| cb(); | |
| }, | |
| tearDown: function(cb){ | |
| app.server.close(); | |
| app.pool.end( function(){ | |
| console.log('MySQL cx pool terminating'); | |
| cb(); | |
| }); | |
| }, | |
| 'scripted user': function (test){ | |
| test.expect(1); | |
| app.models.default( function( rows ){ | |
| test.ok( | |
| rows[0].User == app.dbConfig.user, | |
| "is in mysql.user" | |
| ); | |
| test.done(); | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment