@jskit-ai/database-runtime 0.1.274 → 0.1.276
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/CHANGELOG.md +1 -0
- package/package.json +2 -2
- package/src/server/testDatabases.js +8 -7
- package/test/testDatabases.test.js +16 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- **Breaking:** test databases are refused in staging and production (`APP_ENV`), not under `NODE_ENV=production`: `Test databases are never prepared when APP_ENV is staging.` and `Package data tests never run when APP_ENV is production.`; a missing `APP_ENV` throws.
|
|
5
6
|
- **Behaviour change.** `jskit test --databases` gives every test file the snapshot, rows included: a test database an earlier file of the run used is restored before the next file takes it. It was restored only when its tables or migration ledger had changed (a migration test), so rows an earlier file wrote stayed, and a file's result could depend on which files ran before it on the same copy (for example an app's first account, which only an empty database admits). The run's first file on each copy still takes it as the run restored it; each later file costs one restore, a template copy on PostgreSQL and a table rebuild on MySQL and MariaDB.
|
|
6
7
|
- `countQueries(knex, work)` and `countRuntimeQueries(work)` count only the statements `work` sends, with those of the async work it starts (they run it in an `AsyncLocalStorage` scope): another test, or a background loop, sending through the same Knex or runtime meanwhile is left out, where its statements counted too and could make a flat path look as if it grew. A loop started before `work` that sends on its behalf is left out as well. A count nested in another counts for both, as before.
|
|
7
8
|
- `./server/testDatabases` exports `createPackageTestKnex(t, { environment })`, the Knex a package's own data test runs on when it creates its tables in the test: an in-memory SQLite database, or on a `DB_CLIENT=pg` run a schema of the test's own in `TEST_DB_NAME` (on DB_HOST, DB_PORT, DB_USER and DB_PASSWORD), in UTC, with a pool of up to eight connections for concurrent transactions, dropped with everything in it after the test. It refuses `NODE_ENV=production`, and a pg run without `TEST_DB_NAME` fails (`TestDatabaseMissingError`).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/database-runtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.276",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -82,6 +82,6 @@
|
|
|
82
82
|
}
|
|
83
83
|
},
|
|
84
84
|
"peerDependencies": {
|
|
85
|
-
"@jskit-ai/kernel": "0.2.
|
|
85
|
+
"@jskit-ai/kernel": "0.2.27"
|
|
86
86
|
}
|
|
87
87
|
}
|
|
@@ -29,6 +29,7 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, w
|
|
|
29
29
|
import os from "node:os";
|
|
30
30
|
import path from "node:path";
|
|
31
31
|
import { fileURLToPath } from "node:url";
|
|
32
|
+
import { appEnvironment, isDeployed } from "@jskit-ai/kernel/shared/support/appEnvironment";
|
|
32
33
|
import { parseDatabaseUrl, resolveKnexConnectionFromEnvironment, resolveKnexPool } from "../shared/databaseConnection.js";
|
|
33
34
|
import { detectDialectFromClient } from "../shared/dialect.js";
|
|
34
35
|
import { RUNTIME_QUERY_CHANNEL } from "./database.js";
|
|
@@ -91,17 +92,17 @@ function connectedDatabaseName(environment) {
|
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
/**
|
|
94
|
-
* The disposable database `variable` names, checked with its first `count - 1` copies: never
|
|
95
|
-
* application's database (DB_NAME, DATABASE_URL's, PROD_DB_NAME, and `connected`, the
|
|
96
|
-
* names), and never the other test database or its copies.
|
|
95
|
+
* The disposable database `variable` names, checked with its first `count - 1` copies: never in staging or
|
|
96
|
+
* production (APP_ENV), never the application's database (DB_NAME, DATABASE_URL's, PROD_DB_NAME, and `connected`, the
|
|
97
|
+
* one its Knex configuration names), and never the other test database or its copies.
|
|
97
98
|
* @param {Record<string, string | undefined>} environment
|
|
98
99
|
* @param {"TEST_DB_NAME" | "BROWSER_TEST_DB_NAME"} variable
|
|
99
100
|
* @param {number} [count]
|
|
100
101
|
* @param {{ connected?: string }} [options]
|
|
101
102
|
*/
|
|
102
103
|
function requireDisposableDatabase(environment, variable, count = 1, { connected = "" } = {}) {
|
|
103
|
-
if (environment
|
|
104
|
-
throw new Error(
|
|
104
|
+
if (isDeployed(environment)) {
|
|
105
|
+
throw new Error(`Test databases are never prepared when APP_ENV is ${appEnvironment(environment)}.`);
|
|
105
106
|
}
|
|
106
107
|
const name = String(environment[variable] || "").trim();
|
|
107
108
|
if (!DISPOSABLE_NAME.test(name)) {
|
|
@@ -634,8 +635,8 @@ async function createPackageTestKnex(context, { environment }) {
|
|
|
634
635
|
context.after(() => knex.destroy());
|
|
635
636
|
return knex;
|
|
636
637
|
}
|
|
637
|
-
if (environment
|
|
638
|
-
throw new Error(
|
|
638
|
+
if (isDeployed(environment)) {
|
|
639
|
+
throw new Error(`Package data tests never run when APP_ENV is ${appEnvironment(environment)}.`);
|
|
639
640
|
}
|
|
640
641
|
const connection = {
|
|
641
642
|
...resolveKnexConnectionFromEnvironment(environment, { client: "pg", defaultPort: 5432 }),
|
|
@@ -7,6 +7,7 @@ import knexFactory from "knex";
|
|
|
7
7
|
import { createKnexMigrationConfig } from "../src/server/knexMigrationConfig.js";
|
|
8
8
|
import {
|
|
9
9
|
TestDatabaseMissingError,
|
|
10
|
+
createPackageTestKnex,
|
|
10
11
|
countQueries,
|
|
11
12
|
requireDisposableDatabase,
|
|
12
13
|
requireTestDatabase,
|
|
@@ -17,6 +18,7 @@ import {
|
|
|
17
18
|
} from "../src/server/testDatabases.js";
|
|
18
19
|
|
|
19
20
|
const connectionEnvironment = Object.freeze({
|
|
21
|
+
APP_ENV: "test",
|
|
20
22
|
DB_HOST: "127.0.0.1",
|
|
21
23
|
DB_NAME: "catalogue",
|
|
22
24
|
DB_USER: "catalogue_user",
|
|
@@ -29,7 +31,7 @@ test("a run's test databases are the designated one, then its numbered copies",
|
|
|
29
31
|
assert.deepEqual(testDatabaseNames("catalogue_test", 3), ["catalogue_test", "catalogue_test_1", "catalogue_test_2"]);
|
|
30
32
|
});
|
|
31
33
|
|
|
32
|
-
test("a test database and its copies are never the application's database, production or the other test database", () => {
|
|
34
|
+
test("a test database and its copies are never the application's database, staging, production or the other test database", () => {
|
|
33
35
|
const environment = { ...connectionEnvironment, TEST_DB_NAME: "catalogue_test", BROWSER_TEST_DB_NAME: "catalogue_browser" };
|
|
34
36
|
assert.equal(requireDisposableDatabase(environment, "TEST_DB_NAME", 4), "catalogue_test");
|
|
35
37
|
assert.equal(requireDisposableDatabase(environment, "BROWSER_TEST_DB_NAME", 4), "catalogue_browser");
|
|
@@ -42,9 +44,11 @@ test("a test database and its copies are never the application's database, produ
|
|
|
42
44
|
[{ BROWSER_TEST_DB_NAME: "catalogue_test" }, "TEST_DB_NAME", 1, /the other test database/u],
|
|
43
45
|
[{ BROWSER_TEST_DB_NAME: "catalogue_test_1" }, "TEST_DB_NAME", 2, /the other test database/u],
|
|
44
46
|
[{ TEST_DB_NAME: "catalogue_browser_2" }, "BROWSER_TEST_DB_NAME", 1, /the other test database/u],
|
|
45
|
-
[{ NODE_ENV: "production" }, "TEST_DB_NAME", 1,
|
|
47
|
+
[{ APP_ENV: "staging", NODE_ENV: "production" }, "TEST_DB_NAME", 1, /^Test databases are never prepared when APP_ENV is staging\.$/u],
|
|
48
|
+
[{ APP_ENV: "production", NODE_ENV: "production" }, "TEST_DB_NAME", 1, /when APP_ENV is production/u],
|
|
49
|
+
[{ APP_ENV: "" }, "TEST_DB_NAME", 1, /^APP_ENV is not set/u]
|
|
46
50
|
]) {
|
|
47
|
-
assert.throws(() => requireDisposableDatabase({ ...environment, ...overrides }, variable, count), message,
|
|
51
|
+
assert.throws(() => requireDisposableDatabase({ ...environment, ...overrides }, variable, count), { message },
|
|
48
52
|
JSON.stringify(overrides));
|
|
49
53
|
}
|
|
50
54
|
// The database the Knex configuration connects to, whatever the environment says.
|
|
@@ -52,6 +56,15 @@ test("a test database and its copies are never the application's database, produ
|
|
|
52
56
|
/PROD_DB_NAME \(catalogue_test_1\)/u);
|
|
53
57
|
});
|
|
54
58
|
|
|
59
|
+
test("package data tests on PostgreSQL never run in staging or production, before any connection", async (context) => {
|
|
60
|
+
for (const APP_ENV of ["staging", "production"]) {
|
|
61
|
+
await assert.rejects(
|
|
62
|
+
createPackageTestKnex(context, { environment: { ...connectionEnvironment, DB_CLIENT: "pg", APP_ENV, NODE_ENV: "production" } }),
|
|
63
|
+
{ message: `Package data tests never run when APP_ENV is ${APP_ENV}.` }
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
55
68
|
test("the snapshot key changes when a migration changes, is added or is renamed, and only then", async (context) => {
|
|
56
69
|
const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-test-databases-"));
|
|
57
70
|
context.after(() => rm(appRoot, { recursive: true, force: true }));
|