Skip to content

Commit 29e69fd

Browse files
author
MIYASHITA, Akihiro
committed
Add integration tests
1 parent 97f5328 commit 29e69fd

File tree

6 files changed

+196
-2
lines changed

6 files changed

+196
-2
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ jobs:
2323
npm test
2424
env:
2525
CI: true
26+
integration_test:
27+
steps:
28+
- name: Integration test with MySQL image
29+
uses: docker://givery/track-mysql:2.3.2
30+
with:
31+
entrypoint: mocha
32+
args: integration_test/mysql.test.js

integration_test/mysql.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const fs = require("fs").promises;
2+
const os = require("os");
3+
const expect = require("chai").expect;
4+
const u = require("./util");
5+
6+
describe("MySQL test runner", function () {
7+
this.timeout(30000);
8+
9+
let dir
10+
beforeEach(async () => {
11+
dir = await fs.mkdtemp(os.tmpdir() + "/");
12+
});
13+
afterEach(async () => {
14+
await fs.rm(dir, { recursive: true });
15+
});
16+
17+
it("should work", async () => {
18+
await u.write(dir, {
19+
"package.json": u.trimTextBlock(`
20+
{
21+
"scripts": {
22+
"test": "mocha -R track-reporter"
23+
},
24+
"dependencies": {
25+
"track-db-test-library": "${process.cwd()}",
26+
"mysql": "2.18"
27+
}
28+
}
29+
`),
30+
"test": {
31+
"test.js": u.trimTextBlock(`
32+
const dblib = require("track-db-test-library");
33+
const runner = new dblib.TestRunner("ja", "test/test.yml");
34+
runner.runAll();
35+
`),
36+
"test.yml": u.trimTextBlock(`
37+
client: mysql
38+
testcases:
39+
- title: "[Basic] テスト"
40+
exec:
41+
- CREATE TABLE points(x INTEGER, y INTEGER)
42+
- INSERT INTO points (x, y) VALUES (1, 2)
43+
- SELECT x, y FROM points
44+
check:
45+
equal_to: test/01.csv
46+
`),
47+
"01.csv": u.trimTextBlock(`
48+
x,y
49+
1,2
50+
`),
51+
}
52+
});
53+
54+
await u.exec("npm install", { cwd: dir });
55+
const r = await u.exec("npm test", { cwd: dir });
56+
57+
expect(r.ok).to.be.true;
58+
expect(r.stdout).to.include("# pass 1");
59+
});
60+
});

integration_test/run-locally.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
IMAGE_VERSION=2.3.2
4+
ROOT_DIR=`cd $(dirname $0)/.. && pwd`
5+
6+
docker run --rm -it \
7+
-v ${ROOT_DIR}:/root/src \
8+
givery/track-mysql:${IMAGE_VERSION} \
9+
mocha integration_test/mysql.test.js

integration_test/util.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require("fs");
2+
const cp = require("child_process");
3+
const util = require("util");
4+
5+
async function write(dir, files) {
6+
await fs.promises.mkdir(dir, { recursive: true });
7+
for (const [path, content] of Object.entries(files)) {
8+
if (typeof content === "string") {
9+
await fs.promises.writeFile(`${dir}/${path}`, content, "utf-8");
10+
} else {
11+
await write(`${dir}/${path}`, content);
12+
}
13+
}
14+
}
15+
16+
function trimTextBlock(text) {
17+
const lines = text.split("\n");
18+
const firstLine = lines.findIndex(s => s.trim().length > 0);
19+
const indent = (/^\s*/.exec(lines[firstLine])[0] || { length: 0 }).length;
20+
return lines.slice(firstLine).map(s => s.substring(indent)).join("\n");
21+
}
22+
23+
async function exec(command, options) {
24+
try {
25+
const { stdout, stderr } = await util.promisify(cp.exec)(command, options);
26+
// stdout && console.log(stdout);
27+
// stderr && console.error(stderr);
28+
return {
29+
ok: true,
30+
stdout,
31+
stderr,
32+
};
33+
} catch (err) {
34+
// err.stdout && console.error(err.stdout);
35+
// err.stderr && console.error(err.stderr);
36+
return {
37+
ok: false,
38+
stdout: err.stdout,
39+
stderr: err.stderr,
40+
...err,
41+
};
42+
}
43+
}
44+
45+
module.exports = {
46+
write,
47+
trimTextBlock,
48+
exec,
49+
}

package-lock.json

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Test utility for Track database challenges",
55
"main": "index.js",
66
"scripts": {
7-
"test": "gulp"
7+
"test": "gulp",
8+
"integration_test": "mocha integration_test"
89
},
910
"bin": {
1011
"track-db": "bin/track-db.js",
@@ -32,7 +33,8 @@
3233
"yaml": "^1.6.0"
3334
},
3435
"peerDependencies": {
35-
"sqlite3": "^5.0.11"
36+
"sqlite3": "^5.0.11",
37+
"mysql": "2.18"
3638
},
3739
"devDependencies": {
3840
"chai": "^4.2.0",

0 commit comments

Comments
 (0)