common.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var common = exports;
  2. var path = require('path');
  3. var _ = require('underscore');
  4. var FakeServer = require('./FakeServer');
  5. common.lib = path.join(__dirname, '../lib');
  6. common.fixtures = path.join(__dirname, 'fixtures');
  7. // Useful for triggering ECONNREFUSED errors on connect()
  8. common.bogusPort = 47378;
  9. // Useful for triggering ER_ACCESS_DENIED_ERROR errors on connect()
  10. common.bogusPassword = 'INVALID PASSWORD';
  11. // Used for simulating a fake mysql server
  12. common.fakeServerPort = 32893;
  13. // Used for simulating a fake mysql server
  14. common.fakeServerSocket = __dirname + '/fake_server.sock';
  15. common.testDatabase = process.env.MYSQL_DATABASE;
  16. var Mysql = require('../');
  17. common.isTravis = function() {
  18. return Boolean(process.env.CI);
  19. };
  20. common.createConnection = function(config) {
  21. config = mergeTestConfig(config);
  22. return Mysql.createConnection(config);
  23. };
  24. common.createPool = function(config) {
  25. config = mergeTestConfig(config);
  26. config.createConnection = common.createConnection;
  27. return Mysql.createPool(config);
  28. };
  29. common.createFakeServer = function(options) {
  30. return new FakeServer(_.extend({}, options));
  31. };
  32. common.useTestDb = function(connection) {
  33. var query = connection.query('CREATE DATABASE ' + common.testDatabase, function(err) {
  34. if (err && err.code !== 'ER_DB_CREATE_EXISTS') throw err;
  35. });
  36. connection.query('USE ' + common.testDatabase);
  37. }
  38. function mergeTestConfig(config) {
  39. if (common.isTravis()) {
  40. // see: http://about.travis-ci.org/docs/user/database-setup/
  41. config = _.extend({
  42. user: 'root'
  43. }, config);
  44. } else {
  45. config = _.extend({
  46. host : process.env.MYSQL_HOST,
  47. port : process.env.MYSQL_PORT,
  48. user : process.env.MYSQL_USER,
  49. password : process.env.MYSQL_PASSWORD
  50. }, config);
  51. }
  52. return config;
  53. }