Skip to content

Commit dd07515

Browse files
committed
remove try-finally blocks from tests
1 parent e40389f commit dd07515

2 files changed

Lines changed: 66 additions & 83 deletions

File tree

tests/server/tasks/test_database_push_notification_config_store.py

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -844,51 +844,41 @@ async def test_core_to_0_3_model_conversion(
844844
core_to_compat_push_notification_config_model
845845
)
846846

847-
try:
848-
task_id = 'v03-persistence-task'
849-
config_id = 'c1'
850-
original_config = TaskPushNotificationConfig(
851-
id=config_id,
852-
url='https://example.com/push',
853-
token='legacy-token',
854-
)
855-
856-
# 1. Save the config (will use core_to_compat_push_notification_config_model)
857-
await store.set_info(task_id, original_config, MINIMAL_CALL_CONTEXT)
858-
859-
# 2. Verify it's stored in v0.3 format directly in DB
860-
async with store.async_session_maker() as session:
861-
db_model = await session.get(
862-
store.config_model, (task_id, config_id)
863-
)
864-
assert db_model is not None
865-
assert db_model.protocol_version == '0.3'
866-
867-
# v0.3 JSON structure for PushNotificationConfig (unwrapped)
868-
import json
869-
870-
raw_data = db_model.config_data
871-
if store._fernet:
872-
raw_data = store._fernet.decrypt(raw_data)
873-
data = json.loads(raw_data.decode('utf-8'))
874-
assert data['url'] == 'https://example.com/push'
875-
assert data['id'] == 'c1'
876-
assert data['token'] == 'legacy-token'
877-
assert 'taskId' not in data
878-
879-
# 3. Retrieve the config (will use compat_push_notification_config_model_to_core)
880-
retrieved_configs = await store.get_info(task_id, MINIMAL_CALL_CONTEXT)
881-
assert len(retrieved_configs) == 1
882-
retrieved = retrieved_configs[0]
883-
assert retrieved.id == original_config.id
884-
assert retrieved.url == original_config.url
885-
assert retrieved.token == original_config.token
886-
887-
finally:
888-
# Reset conversion attributes
889-
if assignment_type == 'class':
890-
DatabasePushNotificationConfigStore.core_to_model_conversion = None
891-
else:
892-
store.core_to_model_conversion = None
893-
894-
await store.delete_info(task_id, MINIMAL_CALL_CONTEXT)
847+
task_id = 'v03-persistence-task'
848+
config_id = 'c1'
849+
original_config = TaskPushNotificationConfig(
850+
id=config_id,
851+
url='https://example.com/push',
852+
token='legacy-token',
853+
)
854+
# 1. Save the config (will use core_to_compat_push_notification_config_model)
855+
await store.set_info(task_id, original_config, MINIMAL_CALL_CONTEXT)
856+
# 2. Verify it's stored in v0.3 format directly in DB
857+
async with store.async_session_maker() as session:
858+
db_model = await session.get(store.config_model, (task_id, config_id))
859+
assert db_model is not None
860+
assert db_model.protocol_version == '0.3'
861+
# v0.3 JSON structure for PushNotificationConfig (unwrapped)
862+
import json
863+
864+
raw_data = db_model.config_data
865+
if store._fernet:
866+
raw_data = store._fernet.decrypt(raw_data)
867+
data = json.loads(raw_data.decode('utf-8'))
868+
assert data['url'] == 'https://example.com/push'
869+
assert data['id'] == 'c1'
870+
assert data['token'] == 'legacy-token'
871+
assert 'taskId' not in data
872+
# 3. Retrieve the config (will use compat_push_notification_config_model_to_core)
873+
retrieved_configs = await store.get_info(task_id, MINIMAL_CALL_CONTEXT)
874+
assert len(retrieved_configs) == 1
875+
retrieved = retrieved_configs[0]
876+
assert retrieved.id == original_config.id
877+
assert retrieved.url == original_config.url
878+
assert retrieved.token == original_config.token
879+
# Reset conversion attributes
880+
if assignment_type == 'class':
881+
DatabasePushNotificationConfigStore.core_to_model_conversion = None
882+
else:
883+
store.core_to_model_conversion = None
884+
await store.delete_info(task_id, MINIMAL_CALL_CONTEXT)

tests/server/tasks/test_database_task_store.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -880,41 +880,34 @@ async def test_core_to_0_3_model_conversion(
880880
else:
881881
store.core_to_model_conversion = core_to_compat_task_model
882882

883-
try:
884-
task_id = 'v03-persistence-task'
885-
original_task = Task(
886-
id=task_id,
887-
context_id='v03-context',
888-
status=TaskStatus(state=TaskState.TASK_STATE_WORKING),
889-
metadata={'key': 'value'},
890-
)
891-
892-
# 1. Save the task (will use core_to_compat_task_model)
893-
await store.save(original_task)
894-
895-
# 2. Verify it's stored in v0.3 format directly in DB
896-
async with store.async_session_maker() as session:
897-
db_task = await session.get(TaskModel, task_id)
898-
assert db_task is not None
899-
assert db_task.protocol_version == '0.3'
900-
# v0.3 status JSON uses string for state
901-
assert db_task.status['state'] == 'working'
902-
903-
# 3. Retrieve the task (will use compat_task_model_to_core)
904-
retrieved_task = await store.get(task_id)
905-
assert retrieved_task is not None
906-
assert retrieved_task.id == original_task.id
907-
assert retrieved_task.status.state == TaskState.TASK_STATE_WORKING
908-
assert dict(retrieved_task.metadata) == {'key': 'value'}
909-
910-
finally:
911-
# Reset conversion attributes
912-
if assignment_type == 'class':
913-
DatabaseTaskStore.core_to_model_conversion = None
914-
else:
915-
store.core_to_model_conversion = None
916-
917-
await store.delete('v03-persistence-task')
883+
task_id = 'v03-persistence-task'
884+
original_task = Task(
885+
id=task_id,
886+
context_id='v03-context',
887+
status=TaskStatus(state=TaskState.TASK_STATE_WORKING),
888+
metadata={'key': 'value'},
889+
)
890+
# 1. Save the task (will use core_to_compat_task_model)
891+
await store.save(original_task)
892+
# 2. Verify it's stored in v0.3 format directly in DB
893+
async with store.async_session_maker() as session:
894+
db_task = await session.get(TaskModel, task_id)
895+
assert db_task is not None
896+
assert db_task.protocol_version == '0.3'
897+
# v0.3 status JSON uses string for state
898+
assert db_task.status['state'] == 'working'
899+
# 3. Retrieve the task (will use compat_task_model_to_core)
900+
retrieved_task = await store.get(task_id)
901+
assert retrieved_task is not None
902+
assert retrieved_task.id == original_task.id
903+
assert retrieved_task.status.state == TaskState.TASK_STATE_WORKING
904+
assert dict(retrieved_task.metadata) == {'key': 'value'}
905+
# Reset conversion attributes
906+
if assignment_type == 'class':
907+
DatabaseTaskStore.core_to_model_conversion = None
908+
else:
909+
store.core_to_model_conversion = None
910+
await store.delete('v03-persistence-task')
918911

919912

920913
# Ensure aiosqlite, asyncpg, and aiomysql are installed in the test environment (added to pyproject.toml).

0 commit comments

Comments
 (0)