@jskit-ai/database-runtime-mysql 0.1.269 → 0.1.271

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/database-runtime-mysql",
3
- "version": "0.1.269",
3
+ "version": "0.1.271",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -55,7 +55,7 @@
55
55
  }
56
56
  },
57
57
  "peerDependencies": {
58
- "@jskit-ai/database-runtime": "0.1.271",
59
- "@jskit-ai/kernel": "0.2.22"
58
+ "@jskit-ai/database-runtime": "0.1.273",
59
+ "@jskit-ai/kernel": "0.2.24"
60
60
  }
61
61
  }
@@ -9,7 +9,7 @@
9
9
  "test": "jskit test --databases"
10
10
  },
11
11
  "dependencies": {
12
- "@jskit-ai/database-runtime": "0.1.271",
13
- "@jskit-ai/database-runtime-mysql": "0.1.269"
12
+ "@jskit-ai/database-runtime": "0.1.273",
13
+ "@jskit-ai/database-runtime-mysql": "0.1.271"
14
14
  }
15
15
  }
@@ -206,12 +206,33 @@ test("${name}", async () => {
206
206
  }));
207
207
  });
208
208
  `;
209
+ // Each file records the books it found, then writes one of its own.
210
+ const FIXTURE_ROWS_TEST = (name) => `import { writeFileSync } from "node:fs";
211
+ import test from "node:test";
212
+ import knex from "knex";
213
+ test("${name}", async () => {
214
+ const { DB_HOST: host, DB_PORT: port, DB_USER: user, DB_PASSWORD: password, DB_NAME: database } = process.env;
215
+ const db = knex({ client: "mysql2", connection: { host, port: Number(port), user, password, database } });
216
+ try {
217
+ writeFileSync("seen-${name}.json", JSON.stringify({ db: database, titles: await db("books").orderBy("id").pluck("title") }));
218
+ await db("books").insert({ title: "Written by ${name}" });
219
+ } finally {
220
+ await db.destroy();
221
+ }
222
+ });
223
+ `;
209
224
 
210
- test("jskit test --databases gives each test file running at once its own restored copy and its connection, and nothing else of the .env", {
211
- skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
212
- }, async (context) => {
213
- const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-test-databases-cli-"));
214
- const name = `${process.env.TEST_DB_NAME}_cli`;
225
+ /**
226
+ * A fixture application on this test's database server, with the books migration and the given test files, and the
227
+ * environment `jskit test` runs it with: `concurrency` files at a time, on databases named after `suffix`, which are
228
+ * dropped after the test.
229
+ * @param {import("node:test").TestContext} context
230
+ * @param {string} suffix
231
+ * @param {{ tests: Record<string, string>, concurrency: number }} options
232
+ */
233
+ async function fixtureApplication(context, suffix, { tests, concurrency }) {
234
+ const appRoot = await mkdtemp(path.join(os.tmpdir(), `jskit-test-databases-${suffix}-`));
235
+ const name = `${process.env.TEST_DB_NAME}_${suffix}`;
215
236
  const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT || 3306), user: process.env.DB_USER,
216
237
  password: process.env.DB_PASSWORD };
217
238
  const admin = knex({ client: "mysql2", connection: server });
@@ -223,26 +244,36 @@ test("jskit test --databases gives each test file running at once its own restor
223
244
  await mkdir(path.join(appRoot, "migrations", "constraints"), { recursive: true });
224
245
  await mkdir(path.join(appRoot, "test"), { recursive: true });
225
246
  await writeFile(path.join(appRoot, "migrations", "20200101000000_books.cjs"), BOOKS);
226
- await writeFile(path.join(appRoot, "package.json"), JSON.stringify({ name: "cli-fixture", type: "module", private: true }));
247
+ await writeFile(path.join(appRoot, "package.json"), JSON.stringify({ name: `${suffix}-fixture`, type: "module", private: true }));
227
248
  await writeFile(path.join(appRoot, "knexfile.js"), FIXTURE_KNEXFILE);
228
249
  // The knexfile loads the connection from it, which each test file gets with its database; nothing else of it.
229
250
  await writeFile(path.join(appRoot, ".env"), [
230
251
  `DB_HOST=${server.host}`, `DB_PORT=${server.port}`, `DB_USER=${server.user}`, `DB_PASSWORD=${server.password ?? ""}`,
231
252
  "FIXTURE_ONLY_IN_DOTENV=1", ""
232
253
  ].join("\n"), { mode: 0o600 });
233
- await writeFile(path.join(appRoot, "test", "a.test.js"), FIXTURE_TEST("a", "b"));
234
- await writeFile(path.join(appRoot, "test", "b.test.js"), FIXTURE_TEST("b", "a"));
235
- // The fixture's own node_modules (its snapshot cache goes there), resolving JSKIT's packages from this repository.
236
- // The whole scope is fine: the run checks a package's declared foreign keys only where its table exists, and only
237
- // the fixture's books migration runs here.
254
+ for (const [file, source] of Object.entries(tests)) await writeFile(path.join(appRoot, "test", file), source);
255
+ // The fixture's own node_modules (its snapshot cache goes there), resolving JSKIT's packages and knex from this
256
+ // repository. The whole scope is fine: the run checks a package's declared foreign keys only where its table exists,
257
+ // and only the fixture's books migration runs here.
238
258
  await mkdir(path.join(appRoot, "node_modules"));
239
259
  await symlink(path.join(REPO_ROOT, "node_modules", "@jskit-ai"), path.join(appRoot, "node_modules", "@jskit-ai"), "dir");
260
+ await symlink(path.join(REPO_ROOT, "node_modules", "knex"), path.join(appRoot, "node_modules", "knex"), "dir");
240
261
 
241
- const env = { ...process.env, V64_TEST_CONCURRENCY: "2", DB_NAME: `${process.env.TEST_DB_NAME}_cli_application`,
262
+ const env = { ...process.env, V64_TEST_CONCURRENCY: String(concurrency), DB_NAME: `${name}_application`,
242
263
  TEST_DB_NAME: name, DATABASE_URL: "" };
243
- for (const name of ["NODE_TEST_CONTEXT", "FIXTURE_ONLY_IN_DOTENV", "DB_HOST", "DB_PORT", "DB_USER", "DB_PASSWORD"]) {
244
- delete env[name];
264
+ for (const variable of ["NODE_TEST_CONTEXT", "FIXTURE_ONLY_IN_DOTENV", "DB_HOST", "DB_PORT", "DB_USER", "DB_PASSWORD"]) {
265
+ delete env[variable];
245
266
  }
267
+ return { appRoot, name, server, env };
268
+ }
269
+
270
+ test("jskit test --databases gives each test file running at once its own restored copy and its connection, and nothing else of the .env", {
271
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
272
+ }, async (context) => {
273
+ const { appRoot, name, server, env } = await fixtureApplication(context, "cli", {
274
+ tests: { "a.test.js": FIXTURE_TEST("a", "b"), "b.test.js": FIXTURE_TEST("b", "a") },
275
+ concurrency: 2
276
+ });
246
277
  const { stderr } = await execFileAsync(process.execPath, [JSKIT_CLI, "test", "--databases"], { cwd: appRoot, env });
247
278
  assert.match(stderr, /jskit test: 2 test database\(s\) from .+_cli ready in \d+ms: 1 migrations replayed once/u);
248
279
  assert.match(stderr, /jskit test: 2 test file\(s\) at a time \(test databases\)\./u);
@@ -270,6 +301,21 @@ test("jskit test --databases gives each test file running at once its own restor
270
301
  assert.match(focused.stderr, /jskit test: 1 test database\(s\) from .+_cli ready in \d+ms: stored snapshot/u);
271
302
  });
272
303
 
304
+ test("jskit test --databases gives a file that follows another on the same copy the snapshot again, rows included", {
305
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
306
+ }, async (context) => {
307
+ const { appRoot, name, env } = await fixtureApplication(context, "rows", {
308
+ tests: { "a.test.js": FIXTURE_ROWS_TEST("a"), "b.test.js": FIXTURE_ROWS_TEST("b") },
309
+ concurrency: 1
310
+ });
311
+ const { stderr } = await execFileAsync(process.execPath, [JSKIT_CLI, "test", "--databases"], { cwd: appRoot, env });
312
+ assert.match(stderr, /jskit test: 1 test file\(s\) at a time \(test databases\)\./u);
313
+ for (const file of ["a", "b"]) {
314
+ const seen = JSON.parse(await readFile(path.join(appRoot, `seen-${file}.json`), "utf8"));
315
+ assert.deepEqual(seen, { db: name, titles: ["Written by the migration"] }, `test file ${file}`);
316
+ }
317
+ });
318
+
273
319
  test("countQueries() counts a MySQL or MariaDB path's statements, its transactions' included", {
274
320
  skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
275
321
  }, async (context) => {