Skip to content

Commit

Permalink
Updated package to now include jest and coverall
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Dabrowski authored and Andrew Dabrowski committed Sep 30, 2017
1 parent be59953 commit 213fa34
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 22 deletions.
9 changes: 8 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ examples
.eslintrc.yml

# Travis
.travis.yml
.travis.yml

# Coverage
coverage

# Tests
jest.config.json
tests
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ node_js:
- stable

install:
- npm install
- npm install

script:
- npm run test-ci

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# MySql Framework
[![Build Status](https://travis-ci.org/afdabro/mysql-framework.svg?branch=master)](https://travis-ci.org/afdabro/mysql-framework)
[![Coverage Status](https://coveralls.io/repos/github/afdabro/mysql-framework/badge.svg?branch=master)](https://coveralls.io/github/afdabro/mysql-framework?branch=master)


Simplify [MySQL pure js client](https://github.com/mysqljs/mysql) code complexity and reduce code duplication with the MySQL framework.

## Installation
Expand Down
5 changes: 5 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"collectCoverageFrom": [
"lib/*.js"
]
}
38 changes: 19 additions & 19 deletions lib/SqlConnectionFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ const mysql = require('mysql');

/** Class representing a sql connection factory. */
module.exports = class SqlConnectionFactory {
/**
* Initializes a new instance of SqlConnectionFactory.
* @param {ConnectionConfig} options default connection options
* @see {@link https://github.com/mysqljs/mysql#connection-options}
*/
constructor(options) {
this.defaultConnectionOptions = options;
}
/**
* Initializes a new instance of SqlConnectionFactory.
* @param {ConnectionConfig} options default connection options
* @see {@link https://github.com/mysqljs/mysql#connection-options}
*/
constructor(options) {
this.defaultConnectionOptions = options;
}

/**
* Creates Connection instance by merging default options with argument options.
* @param {ConnectionConfig} options - Custom connection options.
* @see {@link https://github.com/mysqljs/mysql#connection-options}
* @return {Connection} A connection instance.
*/
createInstance(options) {
const mergedOptions = Object.assign({},
this.defaultConnectionOptions, options);
return mysql.createConnection(mergedOptions);
}
/**
* Creates Connection by merging default options with argument options.
* @param {ConnectionConfig} options - Custom connection options.
* @see {@link https://github.com/mysqljs/mysql#connection-options}
* @return {Connection} A connection instance.
*/
createInstance(options) {
const mergedOptions = Object.assign({},
this.defaultConnectionOptions, options);
return mysql.createConnection(mergedOptions);
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.1",
"description": "A MySQL Framework using the MySQL node.js driver.",
"scripts": {
"test": "jest"
"test-ci": "jest --config jest.config.json --coverage"
},
"author": "andrew dabrowski <andrewfdabrowski@gmail.com>",
"license": "MIT",
Expand All @@ -16,6 +16,7 @@
"promise": "^8.0.1"
},
"devDependencies": {
"coveralls": "^3.0.0",
"eslint": "^4.6.1",
"eslint-config-google": "^0.9.1",
"jest": "^21.0.2"
Expand Down
11 changes: 11 additions & 0 deletions tests/SqlConnectionFactory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const SqlConnectionFactory = require('../lib/SqlConnectionFactory.js');

describe('SqlConnectionFactory tests', () => {
test('create simple connection', () => {
const sqlConnectionFactory = new SqlConnectionFactory();
const connection = sqlConnectionFactory.createInstance();
expect(connection).toBeDefined();
});
});
41 changes: 41 additions & 0 deletions tests/SqlQueryWorker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const SqlQueryWorker = require('../lib/SqlQueryWorker.js');

describe('SqlQueryWorker tests', () => {
test('query failed', () => {
const connection = {
query: jest.fn((query, callback) => callback('error')),
};
const sqlQueryWorker = new SqlQueryWorker();
const myQuery = 'SELECT * FROM table';

return sqlQueryWorker.queryAsync(connection, myQuery)
.then((result) => {
expect(result).toBe();
})
.catch((err) => {
expect(connection.query).toBeCalledWith(myQuery,
expect.anything());
expect(err).toBe('error');
});
});

test('query success', () => {
const connection = {
query: jest.fn((query, callback) => callback(undefined, 'success')),
};
const sqlQueryWorker = new SqlQueryWorker();
const myQuery = 'SELECT * FROM table';

return sqlQueryWorker.queryAsync(connection, myQuery)
.then((result) => {
expect(connection.query).toBeCalledWith(myQuery,
expect.anything());
expect(result).toBe('success');
})
.catch((err) => {
expect(err).toBe();
});
});
});

0 comments on commit 213fa34

Please sign in to comment.