Skip to content

Add integration tests #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ jobs:
npm test
env:
CI: true
integration_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: npm ci
- name: Integration test with MySQL image
uses: docker://givery/track-mysql:2.3.2
with:
entrypoint: mocha
args: integration_test/mysql.test.js
60 changes: 60 additions & 0 deletions integration_test/mysql.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require("fs").promises;
const os = require("os");
const expect = require("chai").expect;
const u = require("./util");

describe("MySQL test runner", function () {
this.timeout(30000);

let dir
beforeEach(async () => {
dir = await fs.mkdtemp(os.tmpdir() + "/");
});
afterEach(async () => {
await fs.rm(dir, { recursive: true });
});

it("should work", async () => {
await u.write(dir, {
"package.json": u.trimTextBlock(`
{
"scripts": {
"test": "mocha -R track-reporter"
},
"dependencies": {
"track-db-test-library": "${process.cwd()}",
"mysql": "2.18"
}
}
`),
"test": {
"test.js": u.trimTextBlock(`
const dblib = require("track-db-test-library");
const runner = new dblib.TestRunner("ja", "test/test.yml");
runner.runAll();
`),
"test.yml": u.trimTextBlock(`
client: mysql
testcases:
- title: "[Basic] テスト"
exec:
- CREATE TABLE points(x INTEGER, y INTEGER)
- INSERT INTO points (x, y) VALUES (1, 2)
- SELECT x, y FROM points
check:
equal_to: test/01.csv
`),
"01.csv": u.trimTextBlock(`
x,y
1,2
`),
}
});

await u.exec("npm install", { cwd: dir });
const r = await u.exec("npm test", { cwd: dir });

expect(r.ok).to.be.true;
expect(r.stdout).to.include("# pass 1");
});
});
9 changes: 9 additions & 0 deletions integration_test/run-locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

IMAGE_VERSION=2.3.2
ROOT_DIR=`cd $(dirname $0)/.. && pwd`

docker run --rm -it \
-v ${ROOT_DIR}:/root/src \
givery/track-mysql:${IMAGE_VERSION} \
mocha integration_test/mysql.test.js
49 changes: 49 additions & 0 deletions integration_test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require("fs");
const cp = require("child_process");
const util = require("util");

async function write(dir, files) {
await fs.promises.mkdir(dir, { recursive: true });
for (const [path, content] of Object.entries(files)) {
if (typeof content === "string") {
await fs.promises.writeFile(`${dir}/${path}`, content, "utf-8");
} else {
await write(`${dir}/${path}`, content);
}
}
}

function trimTextBlock(text) {
const lines = text.split("\n");
const firstLine = lines.findIndex(s => s.trim().length > 0);
const indent = (/^\s*/.exec(lines[firstLine])[0] || { length: 0 }).length;
return lines.slice(firstLine).map(s => s.substring(indent)).join("\n");
}

async function exec(command, options) {
try {
const { stdout, stderr } = await util.promisify(cp.exec)(command, options);
// stdout && console.log(stdout);
// stderr && console.error(stderr);
return {
ok: true,
stdout,
stderr,
};
} catch (err) {
// err.stdout && console.error(err.stdout);
// err.stderr && console.error(err.stderr);
return {
ok: false,
stdout: err.stdout,
stderr: err.stderr,
...err,
};
}
}

module.exports = {
write,
trimTextBlock,
exec,
}
67 changes: 67 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Test utility for Track database challenges",
"main": "index.js",
"scripts": {
"test": "gulp"
"test": "gulp",
"integration_test": "mocha integration_test"
},
"bin": {
"track-db": "bin/track-db.js",
Expand Down Expand Up @@ -32,7 +33,8 @@
"yaml": "^1.6.0"
},
"peerDependencies": {
"sqlite3": "^5.0.11"
"sqlite3": "^5.0.11",
"mysql": "2.18"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
Loading