Add Debian deployment, MariaDB storage and randomized proxy routing
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
"""Real MariaDB contract tests; run only against an explicitly supplied test database."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from studio.domain import normalize_catalog, select_score_lines
|
||||
from studio.database import Database
|
||||
from tests.test_data import raw_catalog
|
||||
|
||||
pytestmark = pytest.mark.skipif(not os.environ.get('STUDIO_MYSQL_TEST_CONFIG'), reason='requires an isolated MariaDB test database')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mysql():
|
||||
from studio.mysql_database import MySQLDatabase
|
||||
config = json.loads(Path(os.environ['STUDIO_MYSQL_TEST_CONFIG']).read_text())
|
||||
assert config['database'].endswith('_test'), 'refusing to clear a production database'
|
||||
db = MySQLDatabase(config)
|
||||
with db.connection() as conn:
|
||||
for table in ('catalog', 'scores', 'schools', 'jobs', 'job_units', 'job_logs'):
|
||||
conn.execute(f'DELETE FROM {table}')
|
||||
return db
|
||||
|
||||
|
||||
def test_mysql_preserves_upserts_favorites_and_history(mysql):
|
||||
mysql.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
|
||||
first = mysql.query_catalog({})['items'][0]
|
||||
mysql.favorite(first['id'], True)
|
||||
mysql.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
|
||||
mysql.upsert_catalog(normalize_catalog(raw_catalog(), 2026))
|
||||
mysql.upsert_scores(select_score_lines([
|
||||
{'code': '081200', 'total': 320, 'depart_name': '计算机学院'},
|
||||
{'code': '081200', 'total': 260, 'depart_name': '计算机学院', 'note': '少干专项计划'},
|
||||
{'code': '081200', 'total': 390, 'depart_name': '其他学院'},
|
||||
], '081200', '10001', '北京大学', 2026))
|
||||
result = mysql.query_catalog({'year': 2027, 'direction': '人工', 'english': '203'})
|
||||
assert result['total'] == 1
|
||||
assert result['items'][0]['score_total'] == 320
|
||||
assert mysql.query_catalog({'favorites': True})['total'] == 1
|
||||
assert mysql.overview()['records'] == 4
|
||||
assert len(mysql.history(first['id'])['plans']) == 4
|
||||
assert len(mysql.history(first['id'])['scores']) == 2
|
||||
|
||||
|
||||
def test_mysql_score_unique_key_preserves_long_notes_and_normalized_department(mysql):
|
||||
raw = raw_catalog()
|
||||
raw['yxsmc'] = '计算机 ( 学院 )'
|
||||
mysql.upsert_catalog(normalize_catalog(raw, 2027))
|
||||
scores = select_score_lines([
|
||||
{'code': '081200', 'total': 320, 'depart_name': '计算机(学院)', 'note': '普通计划' * 300},
|
||||
{'code': '081200', 'total': 321, 'depart_name': '计算机(学院)', 'note': '普通计划' * 300 + 'B'},
|
||||
], '081200', '10001', '北京大学', 2026)
|
||||
mysql.upsert_scores(scores)
|
||||
mysql.upsert_scores(scores)
|
||||
assert mysql.overview()['scores'] == 2
|
||||
assert mysql.query_catalog({})['items'][0]['score_total'] == 321
|
||||
|
||||
|
||||
def test_mysql_tasks_logs_and_checkpoints(mysql):
|
||||
mysql.job_create('contract-job', {'year': 2027})
|
||||
mysql.job_update('contract-job', status='running', records=7)
|
||||
mysql.complete_unit('contract-job', 'catalog:11:10001:081200:1')
|
||||
mysql.complete_unit('contract-job', 'catalog:11:10001:081200:1')
|
||||
mysql.log('contract-job', 'info', '数据库测试')
|
||||
assert mysql.unit_done('contract-job', 'catalog:11:10001:081200:1')
|
||||
assert mysql.job_get('contract-job')['completed_units'] == 1
|
||||
assert mysql.job_get('contract-job')['records'] == 7
|
||||
assert mysql.logs('contract-job')[0]['message'] == '数据库测试'
|
||||
|
||||
|
||||
def test_sqlite_migration_preserves_ids_and_refuses_nonempty_target(mysql, tmp_path):
|
||||
from studio.migrate import migrate_sqlite
|
||||
source = Database(tmp_path / 'snapshot.db')
|
||||
source.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
|
||||
source.upsert_scores(select_score_lines([{'code': '081200', 'total': 320}], '081200', '10001', '北京大学', 2026))
|
||||
source.favorite(1, True)
|
||||
source.job_create('migrated-job', {'year': 2027})
|
||||
source.complete_unit('migrated-job', 'catalog:11:10001:081200:1')
|
||||
source.log('migrated-job', 'info', 'preserve log')
|
||||
counts = migrate_sqlite(tmp_path / 'snapshot.db', mysql)
|
||||
assert counts['catalog'] == 2 and counts['scores'] == 1
|
||||
assert mysql.record(1)['favorite']
|
||||
assert mysql.query_catalog({})['items'][0]['score_total'] == 320
|
||||
assert mysql.job_get('migrated-job')['completed_units'] == 1
|
||||
assert mysql.logs('migrated-job')[0]['id'] == 1
|
||||
with pytest.raises(ValueError, match='非空'):
|
||||
migrate_sqlite(tmp_path / 'snapshot.db', mysql)
|
||||
|
||||
|
||||
def test_migration_rolls_back_every_table_on_bad_source(mysql, tmp_path):
|
||||
from studio.migrate import migrate_sqlite
|
||||
source = Database(tmp_path / 'bad.db')
|
||||
source.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
|
||||
with source.connection() as conn:
|
||||
conn.execute("INSERT INTO scores(school_code,major_code,year,scope,data) VALUES('10001','081200',2026,'major','not json')")
|
||||
with pytest.raises(ValueError):
|
||||
migrate_sqlite(tmp_path / 'bad.db', mysql)
|
||||
assert mysql.overview()['records'] == 0
|
||||
assert mysql.overview()['scores'] == 0
|
||||
Reference in New Issue
Block a user