renew schema and tests for new dtabases
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import test from 'node:test';
|
||||
|
||||
const root = path.resolve(import.meta.dirname, '..');
|
||||
|
||||
test('prestart validates a real database, GPX bounds and nullable station coordinates', () => {
|
||||
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'wegwichtel-prestart-'));
|
||||
const dataDir = path.join(temporary, 'data');
|
||||
const storageDir = path.join(temporary, 'storage');
|
||||
fs.mkdirSync(dataDir);
|
||||
const routeDir = path.join(storageDir, 'active/routes/1');
|
||||
fs.mkdirSync(routeDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(routeDir, 'route.gpx'), '<gpx><rte><rtept lat="50" lon="10"/></rte></gpx>');
|
||||
const db = new DatabaseSync(path.join(dataDir, 'wegwichtel.sqlite'));
|
||||
const environment = { ...process.env, DATA_DIR: dataDir, STORAGE_DIR: storageDir };
|
||||
delete environment.NODE_TEST_CONTEXT;
|
||||
const check = () => spawnSync(process.execPath, ['--test', 'test/runtime-database-integrity.test.js'], {
|
||||
cwd: root, encoding: 'utf8', env: environment
|
||||
});
|
||||
try {
|
||||
db.exec(fs.readFileSync(path.join(root, 'database/schema.sql'), 'utf8'));
|
||||
db.prepare(`INSERT INTO routes (name, slug, gpx_path, min_lat, min_lon, max_lat, max_lon)
|
||||
VALUES ('Test', 'test', 'active/routes/1/route.gpx', 50, 10, 50, 10)`).run();
|
||||
db.prepare("INSERT INTO pois (route_id, title, lat, lon) VALUES (1, 'Ohne GPS', NULL, NULL)").run();
|
||||
let result = check();
|
||||
assert.equal(result.status, 0, result.stdout + result.stderr);
|
||||
db.exec('UPDATE routes SET min_lat=49');
|
||||
result = check();
|
||||
assert.notEqual(result.status, 0, result.stdout + result.stderr);
|
||||
assert.match(result.stdout + result.stderr, /min_lat stimmt nicht mit der GPX-Datei/);
|
||||
db.exec('UPDATE routes SET min_lat=50');
|
||||
fs.writeFileSync(path.join(routeDir, 'route.gpx'), '<gpx><rte>');
|
||||
result = check();
|
||||
assert.notEqual(result.status, 0);
|
||||
assert.match(result.stdout + result.stderr, /kein gültiges XML/);
|
||||
db.exec('DROP TABLE pois; CREATE TABLE pois (id INTEGER PRIMARY KEY, route_id INTEGER NOT NULL, title TEXT NOT NULL, lat REAL NOT NULL, lon REAL NOT NULL) STRICT');
|
||||
result = check();
|
||||
assert.notEqual(result.status, 0);
|
||||
assert.match(result.stdout + result.stderr, /Falsche NOT NULL-Eigenschaft für pois.lat/);
|
||||
} finally {
|
||||
db.close();
|
||||
fs.rmSync(temporary, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user