196 lines
9.5 KiB
Python
196 lines
9.5 KiB
Python
"""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', 'job_catalog_pages'):
|
|
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_mysql_catalog_page_cache_and_bulk_checkpoints_are_durable(mysql):
|
|
mysql.job_create('cached-job', {'year': 2027})
|
|
mysql.complete_unit('cached-job', 'catalog:11:10001:081200:1')
|
|
page = {'list': [{'dwdm': '10001'}], 'nextPageAvailable': False}
|
|
mysql.cache_catalog_page('cached-job', 'page-key', page)
|
|
from studio.mysql_database import MySQLDatabase
|
|
reopened = MySQLDatabase(mysql.config, recover_jobs=False)
|
|
assert reopened.completed_units('cached-job') == {'catalog:11:10001:081200:1'}
|
|
assert reopened.catalog_pages('cached-job') == {'page-key': page}
|
|
assert reopened.catalog_pages('another-job') == {}
|
|
|
|
|
|
def test_mysql_reuses_negotiated_tls_context_between_connections(mysql, monkeypatch):
|
|
import ssl
|
|
with mysql.connection() as conn:
|
|
cipher = conn.execute("SHOW SESSION STATUS LIKE 'Ssl_cipher'").fetchone()[1]
|
|
if not cipher:
|
|
pytest.skip('requires a TLS-enabled MariaDB test server')
|
|
creations = []
|
|
original = ssl.create_default_context
|
|
def observe(*args, **kwargs):
|
|
creations.append(True)
|
|
return original(*args, **kwargs)
|
|
monkeypatch.setattr(ssl, 'create_default_context', observe)
|
|
for _ in range(2):
|
|
with mysql.connection() as conn:
|
|
assert conn.execute('SELECT 1').fetchone()[0] == 1
|
|
assert creations == []
|
|
|
|
|
|
def test_mysql_bounded_catalog_and_history_targets_preserve_filters(mysql):
|
|
mysql.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
|
|
mysql.upsert_catalog(normalize_catalog(raw_catalog(), 2026))
|
|
scores = select_score_lines([{'code': '081200', 'total': 310, 'depart_name': '计算机学院'}], '081200', '10001', '北京大学', 2026)
|
|
mysql.upsert_scores(scores)
|
|
targets = list(mysql.score_targets({'year': 2027, 'provinces': ['北京']}, batch_size=1))
|
|
assert len(targets) == 1
|
|
assert targets[0]['major_code'] == '081200'
|
|
assert 'data' not in targets[0]
|
|
rows = list(mysql.iter_catalog({'year': 2027}, batch_size=1))
|
|
assert len(rows) == 2
|
|
assert len(list(mysql.iter_catalog({'year': 2026, 'max_score': 310}, batch_size=1))) == 2
|
|
assert len(list(mysql.iter_scores({('10001', '081200')}, batch_size=1))) == 1
|
|
|
|
|
|
def test_mysql_unit_commit_is_atomic_and_idempotent(mysql):
|
|
mysql.job_create('atomic-job', {})
|
|
rows = normalize_catalog(raw_catalog(), 2027)
|
|
scores = select_score_lines([{'code': '081200', 'total': 310}], '081200', '10001', '北京大学', 2026)
|
|
assert mysql.save_unit('atomic-job', 'catalog:test', rows, scores=scores)
|
|
assert not mysql.save_unit('atomic-job', 'catalog:test', rows, scores=scores)
|
|
assert mysql.job_get('atomic-job')['records'] == 2
|
|
assert mysql.job_get('atomic-job')['completed_units'] == 1
|
|
assert mysql.overview()['records'] == 2
|
|
assert mysql.overview()['scores'] == 1
|
|
|
|
|
|
def test_mysql_failed_unit_rolls_back_every_write(mysql, monkeypatch):
|
|
mysql.job_create('atomic-job', {})
|
|
def fail(*args):
|
|
raise RuntimeError('interrupted unit')
|
|
monkeypatch.setattr(mysql, '_school', fail)
|
|
rows = normalize_catalog(raw_catalog(), 2027)
|
|
scores = select_score_lines([{'code': '081200', 'total': 310}], '081200', '10001', '北京大学', 2026)
|
|
with pytest.raises(RuntimeError):
|
|
mysql.save_unit('atomic-job', 'catalog:test', rows, scores=scores)
|
|
assert mysql.job_get('atomic-job')['records'] == 0
|
|
assert mysql.job_get('atomic-job')['completed_units'] == 0
|
|
assert mysql.overview()['records'] == mysql.overview()['scores'] == 0
|
|
|
|
|
|
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')
|
|
source.cache_catalog_page('migrated-job', 'migration-page', {'list': [], 'nextPageAvailable': False})
|
|
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
|
|
assert counts['job_catalog_pages'] == 1
|
|
assert mysql.catalog_pages('migrated-job')['migration-page']['list'] == []
|
|
with pytest.raises(ValueError, match='非空'):
|
|
migrate_sqlite(tmp_path / 'snapshot.db', mysql)
|
|
|
|
|
|
def test_legacy_snapshot_without_catalog_page_table_can_still_be_migrated(mysql, tmp_path):
|
|
from studio.migrate import migrate_sqlite
|
|
source = Database(tmp_path / 'legacy.db')
|
|
source.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
|
|
with source.connection() as conn:
|
|
conn.execute('DROP TABLE job_catalog_pages')
|
|
counts = migrate_sqlite(source.path, mysql)
|
|
assert counts['catalog'] == 2
|
|
assert counts['job_catalog_pages'] == 0
|
|
|
|
|
|
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
|
|
|
|
|
|
def test_rejected_migration_cli_does_not_pause_destination_jobs(mysql, tmp_path):
|
|
import subprocess
|
|
import sys
|
|
source = Database(tmp_path / 'source.db')
|
|
mysql.job_create('existing-running-job', {'year': 2027})
|
|
mysql.job_update('existing-running-job', status='running')
|
|
result = subprocess.run([sys.executable, '-m', 'studio.migrate', str(source.path), '--config', os.environ['STUDIO_MYSQL_TEST_CONFIG']], capture_output=True, text=True)
|
|
assert result.returncode != 0
|
|
assert mysql.job_get('existing-running-job')['status'] == 'running'
|