Skip to content

Commit

Permalink
Add test code
Browse files Browse the repository at this point in the history
  • Loading branch information
mirromutth committed Oct 9, 2019
1 parent 2001797 commit 82ee48a
Show file tree
Hide file tree
Showing 8 changed files with 1,908 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"env": {
"es6": true,
"node": true
},
"extends": [
"airbnb-base"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"no-undef": "off"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".ts"],
"paths": ["./test"]
}
}
}
}
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: test

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-18.04

strategy:
matrix:
node-version: ['10.x']

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Set up MySQL Latest
uses: mirromutth/mysql-action@master
with:
mysql version: latest
mysql database: test
mysql root password: ${{ secrets.DatabasePassword }}
- name: npm install, build, and test
run: |
npm install
npm run lint
npm test -- --password=${{ secrets.DatabasePassword }}
env:
CI: true
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### MacOS

.DS_Store

### NPM (using YARN, sorry NPM)

node_modules/
package-lock.json
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "mysql-action",
"version": "1.1.1",
"description": "MySQL Github Action",
"main": "build/app.js",
"directories": {
"test": "test"
},
"scripts": {
"lint": "eslint . --ext .ts",
"build": "node_modules/.bin/tsc",
"test": "mocha -r ts-node/register test/**/*-test.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mirromutth/mysql-action.git"
},
"keywords": [
"MySQL",
"Action",
"Github-Actions"
],
"author": "Mirro Mutth",
"license": "MIT",
"bugs": {
"url": "https://github.com/mirromutth/mysql-action/issues"
},
"homepage": "https://github.com/mirromutth/mysql-action#readme",
"devDependencies": {
"@types/chai": "^4.2.3",
"@types/lodash": "^4.14.142",
"@types/mocha": "^5.2.7",
"@types/mysql": "^2.15.7",
"@typescript-eslint/eslint-plugin": "^2.3.3",
"@typescript-eslint/parser": "^2.3.3",
"chai": "^4.2.0",
"eslint": "^6.5.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2",
"mocha": "^6.2.1",
"ts-node": "^8.4.1",
"typescript": "^3.6.3"
},
"dependencies": {
"lodash": "^4.17.15",
"mysql": "^2.17.1"
}
}
33 changes: 33 additions & 0 deletions test/arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as _ from 'lodash/fp';

const args: { [key: string]: string } = _.pipe(
_.map((value: string) => {
if (!value.startsWith('--')) {
return undefined;
}

const index = value.indexOf('=', 2);

if (index < 0) {
return undefined;
}

return [value.substring(2, index), value.substring(index + 1)];
}),
_.filter((value) => value !== undefined),
_.fromPairs,
)(process.argv);

export function required(key: string): string {
const result = args[key];

if (result === undefined) {
throw new TypeError(`Argument ${key} not found`);
}

return result;
}

export function optional(key: string): string | undefined {
return args[key];
}
43 changes: 43 additions & 0 deletions test/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { promisify } from 'util';
import { createConnection } from 'mysql';

import { required } from './arguments';

export interface Queryer {
init(): Promise<void>;
query(sql: string): Promise<any>;
close(): Promise<void>;
}

function createQueryer(): Queryer {
const connection = createConnection({
host: 'localhost',
user: 'root',
password: required('password'),
database: 'test',
});

return {
init: () => new Promise<void>((resolve, reject) => {
connection.connect((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}),
query: promisify(connection.query.bind(connection)),
close: () => new Promise<void>((resolve, reject) => {
connection.end((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}),
};
}

export default createQueryer;
21 changes: 21 additions & 0 deletions test/database-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai';

import createQueryer from './connection';

describe('Database', () => {
const queryer = createQueryer();

before('Connect', queryer.init);
after('Disconnect', queryer.close);

describe('#select', () => {
it('should return tables for current database', async () => {
const results = await queryer.query('SHOW TABLES');
expect(results).to.be.a('array');
});
it('should return informations for other database', async () => {
const results = await queryer.query('SELECT * FROM information_schema.innodb_trx');
expect(results).to.be.a('array');
});
});
});
Loading

0 comments on commit 82ee48a

Please sign in to comment.