Skip to content

Commit d906bc8

Browse files
committed
[tests] Configure SQL Server the way PostgreSQL already is
The PostgreSQL URL has always been written into the test harness, so the suite finds a local instance with no setup. SQL Server was the odd one out: it read four environment variables, and its password defaulted to empty, so prePublishPackage failed with 'Login failed for user sa' unless you remembered to export them first. The local instance is now spelled out alongside the PostgreSQL one, in test/unit/common/other.ts, with the command that creates the container in a comment beside it. The password belongs to a throwaway container holding nothing but test data, and every part of the configuration can still be pointed elsewhere with its variable. The documentation examples keep reading TINYBASE_MSSQL rather than spelling out credentials a reader might copy, which is the better thing to show; the suite now supplies the local default when nothing else has, so running them needs no setup either. The whole persister and documentation suite, 3,541 tests, now passes with no environment variables set at all.
1 parent a671424 commit d906bc8

4 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
"partyserver",
173173
"partysocket",
174174
"parvovirus",
175+
"Passw",
175176
"passwordless",
176177
"Patryk",
177178
"Pearce",

‎test/unit/common/other.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ export const withServers: boolean = isBun
1515
: (vitest as {inject?: (key: 'servers') => boolean}).inject?.('servers') ==
1616
true;
1717

18+
// The local SQL Server these tests expect, given the same treatment as the
19+
// PostgreSQL URL in databases.ts. PostgreSQL can use trust authentication and
20+
// so needs no password; SQL Server cannot, so one is spelled out here. It is
21+
// the password of a throwaway container holding nothing but test data, never
22+
// a real instance:
23+
//
24+
// docker run -d --name tinybase-mssql -p 1433:1433 \
25+
// -e ACCEPT_EULA=Y -e MSSQL_PID=Developer \
26+
// -e MSSQL_SA_PASSWORD='TinyBase!Passw0rd' \
27+
// mcr.microsoft.com/mssql/server:2022-latest
28+
//
29+
// Point any part of it somewhere else with the matching variable.
30+
export const MSSQL_SERVER = process.env.TINYBASE_MSSQL_SERVER ?? 'localhost';
31+
export const MSSQL_PORT = Number(process.env.TINYBASE_MSSQL_PORT ?? 1433);
32+
export const MSSQL_USER = process.env.TINYBASE_MSSQL_USER ?? 'sa';
33+
export const MSSQL_PASSWORD =
34+
process.env.TINYBASE_MSSQL_PASSWORD ?? 'TinyBase!Passw0rd';
35+
export const MSSQL_CONNECTION_STRING =
36+
`Server=${MSSQL_SERVER},${MSSQL_PORT};Database=tinybase;` +
37+
`User Id=${MSSQL_USER};Password=${MSSQL_PASSWORD};` +
38+
`Encrypt=false;TrustServerCertificate=true`;
39+
1840
export const AsyncFunction = Object.getPrototypeOf(
1941
async () => null,
2042
).constructor;

‎test/unit/documentation.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as effectSchema from 'effect/Schema';
88
import {build, transformSync} from 'esbuild';
99
import 'fake-indexeddb/auto';
1010
import * as fs from 'fs';
11-
import {readFileSync, readdirSync} from 'fs';
11+
import {readdirSync, readFileSync} from 'fs';
1212
import {createRequire} from 'module';
1313
import * as mssql from 'mssql';
1414
import {dirname, extname, join, resolve} from 'path';
@@ -69,6 +69,7 @@ import {
6969
AsyncFunction,
7070
importBunSqlite,
7171
isBun,
72+
MSSQL_CONNECTION_STRING,
7273
pause,
7374
suppressWarnings,
7475
withServers,
@@ -769,6 +770,11 @@ ${body
769770
expect(problem).toBeUndefined();
770771
};
771772

773+
// The mssql examples deliberately read their connection string from the
774+
// environment rather than spelling out credentials a reader might copy. Point
775+
// it at the local instance when nothing else has, so the suite needs no setup.
776+
process.env.TINYBASE_MSSQL ??= MSSQL_CONNECTION_STRING;
777+
772778
describe('Documentation tests', () => {
773779
beforeAll(async () => {
774780
if (isBun) {

‎test/unit/persisters/common/databases.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wa
3535
import tmp from 'tmp';
3636
import {afterAll, expect} from 'vitest';
3737
import {
38+
MSSQL_PASSWORD,
39+
MSSQL_PORT,
40+
MSSQL_SERVER,
41+
MSSQL_USER,
3842
importBunSqlite,
3943
isBun,
4044
noop,
@@ -76,14 +80,13 @@ const pgAdmin = async (sql: string) => {
7680
await adminPool.end();
7781
};
7882

79-
// SQL Server needs credentials, so unlike the trust-authenticated PostgreSQL
80-
// above, these come from the environment rather than being hard-coded. Point
81-
// them at a scratch instance holding nothing but test data.
83+
// As with the PostgreSQL URL above, this is the local instance the tests
84+
// expect; see the constants in other.ts for what it is and how to override it.
8285
const getMsSqlConfig = (database: string) => ({
83-
server: process.env.TINYBASE_MSSQL_SERVER ?? 'localhost',
84-
port: Number(process.env.TINYBASE_MSSQL_PORT ?? 1433),
85-
user: process.env.TINYBASE_MSSQL_USER ?? 'sa',
86-
password: process.env.TINYBASE_MSSQL_PASSWORD ?? '',
86+
server: MSSQL_SERVER,
87+
port: MSSQL_PORT,
88+
user: MSSQL_USER,
89+
password: MSSQL_PASSWORD,
8790
database,
8891
pool: {max: 20},
8992
options: {encrypt: false, trustServerCertificate: true},

0 commit comments

Comments
 (0)