63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
"""Runs against a real MariaDB when THOR_TEST_DB_HOST is set (the CI integration job)."""
|
|||
|
|
import os
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from thor.config import DatabaseSettings
|
||
|
|
from thor.db import MIGRATIONS, Database, DatabaseError, Store, from_dt, to_dt
|
||
|
|
|
||
|
|
pytestmark = pytest.mark.skipif(not os.environ.get('THOR_TEST_DB_HOST'), reason='needs THOR_TEST_DB_HOST')
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def db():
|
||
|
|
d = Database(DatabaseSettings(
|
||
|
|
host=os.environ['THOR_TEST_DB_HOST'], port=int(os.environ.get('THOR_TEST_DB_PORT', '3306')),
|
||
|
|
name=os.environ.get('THOR_TEST_DB_NAME', 'thor'), user=os.environ.get('THOR_TEST_DB_USER', 'thor'),
|
||
|
|
password=os.environ.get('THOR_TEST_DB_PASSWORD', '')))
|
||
|
|
for t in ('state', 'cycles', 'source_status', 'blind_periods', 'schema_version'):
|
||
|
|
d.execute(f'DROP TABLE IF EXISTS {t}') # nosec B608 - fixed table names
|
||
|
|
return d
|
||
|
|
|
||
|
|
|
||
|
|
def test_migrate_is_idempotent(db):
|
||
|
|
assert db.migrate() == len(MIGRATIONS)
|
||
|
|
assert db.migrate() == len(MIGRATIONS)
|
||
|
|
assert len(db.query('SELECT * FROM schema_version')) == len(MIGRATIONS)
|
||
|
|
|
||
|
|
|
||
|
|
def test_store_round_trips(db):
|
||
|
|
db.migrate()
|
||
|
|
s = Store(db)
|
||
|
|
t = 1795575600.123
|
||
|
|
s.set_state('last_cycle', {'blind': ['loki'], 'ok': True}, t)
|
||
|
|
s.set_state('last_cycle', {'blind': [], 'ok': True}, t + 1)
|
||
|
|
assert s.get_state('last_cycle') == {'blind': [], 'ok': True}
|
||
|
|
assert s.get_state('missing', 'dflt') == 'dflt'
|
||
|
|
s.record_cycle(started=t, finished=t + 2, window_start=t - 120, window_end=t, ok=True, blind=['loki'])
|
||
|
|
s.record_cycle(started=t - 100000, finished=t, window_start=t, window_end=t, ok=False, blind=[], error='x')
|
||
|
|
assert s.prune_cycles(t - 1) == 1
|
||
|
|
s.save_source_status('loki', False, t, None, 'HTTP 500', t)
|
||
|
|
s.save_source_status('loki', True, t + 5, t + 5, '', t + 5)
|
||
|
|
loki = s.source_statuses()['loki']
|
||
|
|
assert loki == {'up': True, 'since': pytest.approx(t + 5, abs=0.001),
|
||
|
|
'last_ok': pytest.approx(t + 5, abs=0.001), 'last_error': ''}
|
||
|
|
s.open_blind_period('loki', t, 'HTTP 500')
|
||
|
|
assert s.blind_periods(t - 10)[0]['ended'] is None
|
||
|
|
s.close_blind_period('loki', t + 60)
|
||
|
|
periods = s.blind_periods(t - 10)
|
||
|
|
assert periods[0]['ended'] == pytest.approx(t + 60, abs=0.001)
|
||
|
|
assert s.blind_periods(t + 61) == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_time_conversion_is_utc():
|
||
|
|
t = 1795575600.5
|
||
|
|
assert from_dt(to_dt(t)) == t
|
||
|
|
assert to_dt(t).isoformat() == '2026-11-25T03:00:00.500000'
|
||
|
|
assert to_dt(None) is None and from_dt(None) is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_unreachable_database_raises_database_error():
|
||
|
|
d = Database(DatabaseSettings(host='127.0.0.1', port=1, user='x', password='y'))
|
||
|
|
with pytest.raises(DatabaseError):
|
||
|
|
d.query('SELECT 1')
|