mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-04 23:17:20 +08:00
refactor: make model sessions explicit
This commit is contained in:
@@ -11,6 +11,7 @@ import pytest
|
||||
|
||||
from app.db.models.systemconfig import SystemConfig
|
||||
from app.db.models.userconfig import UserConfig
|
||||
from app.db.uow import run_async_transaction
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -25,10 +26,13 @@ def test_create_persists_and_get_reads_back(db):
|
||||
"""
|
||||
row = SystemConfig(key="base-create", value={"n": 1})
|
||||
row.create(db.session)
|
||||
db.session.commit()
|
||||
|
||||
assert row.id is not None
|
||||
assert SystemConfig.get(db.session, row.id).key == "base-create"
|
||||
assert asyncio.run(SystemConfig.async_get(rid=row.id)).key == "base-create"
|
||||
assert db.run_async_session(
|
||||
lambda session: SystemConfig.async_get(session, rid=row.id)
|
||||
).key == "base-create"
|
||||
|
||||
|
||||
def test_get_returns_none_for_missing_id(db):
|
||||
@@ -36,7 +40,9 @@ def test_get_returns_none_for_missing_id(db):
|
||||
主键不存在时返回 None,而不是抛异常或返回任意一行。
|
||||
"""
|
||||
assert SystemConfig.get(db.session, -1) is None
|
||||
assert asyncio.run(SystemConfig.async_get(rid=-1)) is None
|
||||
assert db.run_async_session(
|
||||
lambda session: SystemConfig.async_get(session, rid=-1)
|
||||
) is None
|
||||
|
||||
|
||||
def test_async_create_flushes_and_assigns_primary_key(db):
|
||||
@@ -45,7 +51,11 @@ def test_async_create_flushes_and_assigns_primary_key(db):
|
||||
|
||||
异步路径的调用方常常紧接着用 id 建立关联,拿到 None 会让关联静默丢失。
|
||||
"""
|
||||
created = asyncio.run(SystemConfig(key="base-async-create", value={"n": 2}).async_create())
|
||||
created = asyncio.run(run_async_transaction(
|
||||
lambda session: SystemConfig(
|
||||
key="base-async-create", value={"n": 2}
|
||||
).async_create(session)
|
||||
))
|
||||
|
||||
assert created.id is not None
|
||||
assert SystemConfig.get(db.session, created.id).value == {"n": 2}
|
||||
@@ -57,11 +67,20 @@ def test_update_writes_payload_fields(db):
|
||||
"""
|
||||
row = SystemConfig(key="base-update", value={"n": 1})
|
||||
row.create(db.session)
|
||||
db.session.flush()
|
||||
|
||||
row.update(db.session, {"value": {"n": 9}})
|
||||
assert SystemConfig.get(db.session, row.id).value == {"n": 9}
|
||||
db.session.commit()
|
||||
|
||||
asyncio.run(row.async_update(payload={"value": {"n": 10}}))
|
||||
async def update_in_owned_transaction(session) -> None:
|
||||
"""在同一异步事务中读取并更新目标行。"""
|
||||
async_row = await SystemConfig.async_get(session, row.id)
|
||||
assert async_row is not None
|
||||
await async_row.async_update(session, payload={"value": {"n": 10}})
|
||||
|
||||
asyncio.run(run_async_transaction(update_in_owned_transaction))
|
||||
db.session.expire_all()
|
||||
assert SystemConfig.get(db.session, row.id).value == {"n": 10}
|
||||
|
||||
|
||||
@@ -85,7 +104,9 @@ def test_async_delete_removes_only_the_given_row(db):
|
||||
dropped = db.add(SystemConfig(key="base-async-del", value={"n": 1}))
|
||||
kept = db.add(SystemConfig(key="base-async-keep", value={"n": 2}))
|
||||
|
||||
asyncio.run(SystemConfig.async_delete(rid=dropped.id))
|
||||
asyncio.run(run_async_transaction(
|
||||
lambda session: SystemConfig.async_delete(session, rid=dropped.id)
|
||||
))
|
||||
|
||||
assert SystemConfig.get(db.session, dropped.id) is None
|
||||
assert SystemConfig.get(db.session, kept.id) is not None
|
||||
@@ -95,7 +116,9 @@ def test_async_delete_tolerates_missing_row(db):
|
||||
"""
|
||||
删除不存在的行不抛异常,保持调用方的幂等语义。
|
||||
"""
|
||||
asyncio.run(SystemConfig.async_delete(rid=-1))
|
||||
asyncio.run(run_async_transaction(
|
||||
lambda session: SystemConfig.async_delete(session, rid=-1)
|
||||
))
|
||||
|
||||
|
||||
def test_list_returns_every_row_of_that_model_only(db):
|
||||
@@ -117,7 +140,7 @@ def test_async_list_matches_sync_list(db):
|
||||
db.add(UserConfig(username="base-list", key="k", value="v"))
|
||||
|
||||
sync_ids = sorted(item.id for item in UserConfig.list(db.session))
|
||||
async_ids = sorted(item.id for item in asyncio.run(UserConfig.async_list()))
|
||||
async_ids = sorted(item.id for item in db.run_async_session(UserConfig.async_list))
|
||||
|
||||
assert sync_ids == async_ids
|
||||
|
||||
@@ -132,7 +155,7 @@ def test_truncate_empties_the_table(db):
|
||||
assert UserConfig.list(db.session) == []
|
||||
|
||||
db.add(UserConfig(username="base-truncate-async", key="k", value="v"))
|
||||
asyncio.run(UserConfig.async_truncate())
|
||||
asyncio.run(run_async_transaction(UserConfig.async_truncate))
|
||||
assert UserConfig.list(db.session) == []
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user