BlueGit

projects-timfalken-com verhalenboek

Files

verhalenboek / web / tests / startup.py

startup.py

feat: voeg Verhalenboek toe met dagen, leesvoortgang en schrijf-API
timfa project_87242033_bot_b167a9be22027ad5a4b431d0ad8426c6 · pushed by timfa · 6 weeks ago
799da657 History
startup.py 2.2 KiB Download Raw
1#!/usr/bin/env python3
2"""Eerste start en een tweede start bewaren bestaande dagen en configuratie."""
3from pathlib import Path
4import shutil
5import sqlite3
6import subprocess
7import tempfile
8
9WEB = Path(__file__).resolve().parents[1]
10ARTIFACTS = WEB / 'tests/artifacts'
11ARTIFACTS.mkdir(exist_ok=True)
12with tempfile.TemporaryDirectory(prefix='startup-', dir=ARTIFACTS) as tmp:
13 root = Path(tmp)
14 shutil.copytree(WEB / 'lib', root / 'lib')
15 (root / 'data').mkdir()
16
17 def startup():
18 result = subprocess.run(
19 ['php', '-r', 'require $argv[1]; db();', str(root / 'lib/bootstrap.php')],
20 capture_output=True, text=True, check=True,
21 )
22 assert not result.stderr, result.stderr
23 log = root / 'data/php-errors.log'
24 assert not log.exists() or not log.read_text(), 'Startup emitted PHP errors'
25
26 startup()
27 config = root / 'data/config.php'
28 assert config.is_file(), 'Eerste start maakt config.php'
29 original = config.read_text()
30 assert all(f"'{key}' => ''" in original for key in ['base_url', 'client_id', 'client_secret'])
31 config.write_text(original.replace("'client_id' => ''", "'client_id' => 'projects.timfalken.com'") + '\n// servercommentaar\n')
32 marked = config.read_text()
33 con = sqlite3.connect(root / 'data/verhalenboek.sqlite')
34 con.execute("INSERT INTO users (id, name, role, theme, font, created) VALUES ('auth:1','tim','admin','system','serif',1)")
35 con.execute("INSERT INTO days (date, title, created, updated) VALUES ('2026-01-01','Bewaard',1,1)")
36 con.execute("INSERT INTO stories (day_id, position, title, created, updated) VALUES (1,1,'Een',1,1)")
37 con.execute("INSERT INTO paragraphs (story_id, position, body) VALUES (1,1,'Blijft staan.')")
38 con.commit()
39 startup()
40 assert config.read_text() == marked, 'Bestaande config.php blijft byte voor byte staan'
41 row = con.execute('SELECT date, title FROM days').fetchone()
42 assert row == ('2026-01-01', 'Bewaard'), row
43 assert con.execute('SELECT body FROM paragraphs').fetchone()[0] == 'Blijft staan.'
44 assert 'email' not in [name for _, name, *_ in con.execute('PRAGMA table_info(users)')]
45 con.close()
46print('Startup: fresh database, preserved day and preserved config passed.')