Skip to content

Commit 40cb6c9

Browse files
committed
add test.mjs
1 parent 8884f80 commit 40cb6c9

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ deps:
2424
lint:
2525
GOOS=js GOARCH=wasm go vet .
2626
.PHONY: lint
27+
28+
test: sqldef.wasm
29+
node --test test.mjs
30+
.PHONY: test

app.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sqldef, getFullVersion } from "./sqldef_browser.mjs";
1+
import { sqldef, getFullVersion } from "./sqldef.mjs";
22
import { schemaExamples } from "./schema_examples.mjs";
33

44
const dbType = document.getElementById("dbType");
@@ -11,7 +11,6 @@ const outputDown = document.getElementById("outputDown");
1111
const errorDown = document.getElementById("errorDown");
1212
const versionEl = document.getElementById("version");
1313

14-
1514
async function runDiff() {
1615
// Run up diff (current -> desired)
1716
errorUp.classList.add("hidden");

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
<body>
116116
<h1>sqldef</h1>
117117
<p>sqldef is a <a href="https://github.com/sqldef/sqldef">CLI tool</a> for diffing two SQL schemas. You can use it to
118-
manage the migration of PostgreSQL, MySQL, SQLite3, and SQL Server databases, using regular SQL DDLs.</p>
118+
manage the migration of RDBMSs using regular SQL DDLs.</p>
119+
<p>Supported databases: MySQL, MariaDB, TiDB, PostgreSQL, SQL Server, and SQLite3.</p>
119120
<h2>Online Demo</h2>
120121
<div>
121122
<select id="dbType">

sqldef_browser.mjs renamed to sqldef.mjs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ if (typeof Go === "undefined") {
88
let SQLDEF = null;
99
let isLoading = false;
1010

11-
const getInstance = async () => {
11+
function formatMs(ms) {
12+
return ms.toFixed(4).replace(/\.?0+$/, "");
13+
}
14+
15+
async function getInstance() {
1216
while (isLoading) {
1317
await new Promise((resolve) => setTimeout(resolve));
1418
}
@@ -30,10 +34,9 @@ const getInstance = async () => {
3034
const t3 = performance.now();
3135

3236
console.debug(
33-
"sqldef.wasm loading time: fetch: %dms, instantiate: %dms, start: %dms",
34-
t1 - t0,
35-
t2 - t1,
36-
t3 - t2
37+
`sqldef.wasm loading time: fetch: ${formatMs(
38+
t1 - t0
39+
)}ms, instantiate: ${formatMs(t2 - t1)}ms, start: ${formatMs(t3 - t2)}ms`
3740
);
3841

3942
SQLDEF = globalThis._SQLDEF;
@@ -42,7 +45,7 @@ const getInstance = async () => {
4245
}
4346

4447
return SQLDEF;
45-
};
48+
}
4649

4750
export async function sqldef(
4851
dbType,

sqldef.wasm

0 Bytes
Binary file not shown.

test.mjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert";
3+
import { readFile } from "node:fs/promises";
4+
import { fileURLToPath } from "node:url";
5+
import { dirname, join } from "node:path";
6+
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = dirname(__filename);
9+
10+
// Mock fetch for Node.js to load local wasm file
11+
globalThis.fetch = async (url) => {
12+
const filePath = join(__dirname, url);
13+
const buffer = await readFile(filePath);
14+
return {
15+
arrayBuffer: async () => buffer.buffer,
16+
};
17+
};
18+
19+
// Now import the modules
20+
const { sqldef } = await import("./sqldef.mjs");
21+
const { schemaExamples } = await import("./schema_examples.mjs");
22+
23+
for (const [dbType, example] of Object.entries(schemaExamples)) {
24+
test(`sqldef returns non-empty string for ${dbType} (enableDrop=false)`, async () => {
25+
const result = await sqldef(dbType, example.desired, example.current, false);
26+
assert.strictEqual(typeof result, "string");
27+
assert.ok(result.length > 0, `Result should be non-empty for ${dbType}`);
28+
});
29+
30+
test(`sqldef returns non-empty string for ${dbType} (enableDrop=true)`, async () => {
31+
const result = await sqldef(dbType, example.desired, example.current, true);
32+
assert.strictEqual(typeof result, "string");
33+
assert.ok(result.length > 0, `Result should be non-empty for ${dbType}`);
34+
});
35+
}

0 commit comments

Comments
 (0)