Skip to content

Commit e409d32

Browse files
authored
Use minimal scope with pytest.raises (temporalio#582)
* Fix indentation bug in test * Use pytest.raises() with minimal scope This is a style fix; none of these are incorrect AFAICS * Fix assertions about client-side cancellation / failure errors
1 parent c57df81 commit e409d32

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

tests/worker/test_workflow.py

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,37 +1113,37 @@ async def cancel_child(self) -> None:
11131113
@pytest.mark.parametrize("use_execute", [True, False])
11141114
async def test_workflow_cancel_child_started(client: Client, use_execute: bool):
11151115
async with new_worker(client, CancelChildWorkflow, LongSleepWorkflow) as worker:
1116-
with pytest.raises(WorkflowFailureError) as err:
1117-
# Start workflow
1118-
handle = await client.start_workflow(
1119-
CancelChildWorkflow.run,
1120-
use_execute,
1121-
id=f"workflow-{uuid.uuid4()}",
1122-
task_queue=worker.task_queue,
1123-
)
1116+
# Start workflow
1117+
handle = await client.start_workflow(
1118+
CancelChildWorkflow.run,
1119+
use_execute,
1120+
id=f"workflow-{uuid.uuid4()}",
1121+
task_queue=worker.task_queue,
1122+
)
11241123

1125-
# Wait until child started
1126-
async def child_started() -> bool:
1127-
try:
1128-
return await handle.query(
1129-
CancelChildWorkflow.ready
1130-
) and await client.get_workflow_handle_for(
1131-
LongSleepWorkflow.run, # type: ignore[arg-type]
1132-
workflow_id=f"{handle.id}_child",
1133-
).query(LongSleepWorkflow.started)
1134-
except RPCError as err:
1135-
# Ignore not-found or failed precondition because child may
1136-
# not have started yet
1137-
if (
1138-
err.status == RPCStatusCode.NOT_FOUND
1139-
or err.status == RPCStatusCode.FAILED_PRECONDITION
1140-
):
1141-
return False
1142-
raise
1124+
# Wait until child started
1125+
async def child_started() -> bool:
1126+
try:
1127+
return await handle.query(
1128+
CancelChildWorkflow.ready
1129+
) and await client.get_workflow_handle_for(
1130+
LongSleepWorkflow.run, # type: ignore[arg-type]
1131+
workflow_id=f"{handle.id}_child",
1132+
).query(LongSleepWorkflow.started)
1133+
except RPCError as err:
1134+
# Ignore not-found or failed precondition because child may
1135+
# not have started yet
1136+
if (
1137+
err.status == RPCStatusCode.NOT_FOUND
1138+
or err.status == RPCStatusCode.FAILED_PRECONDITION
1139+
):
1140+
return False
1141+
raise
11431142

1144-
await assert_eq_eventually(True, child_started)
1145-
# Send cancel signal and wait on the handle
1146-
await handle.signal(CancelChildWorkflow.cancel_child)
1143+
await assert_eq_eventually(True, child_started)
1144+
# Send cancel signal and wait on the handle
1145+
await handle.signal(CancelChildWorkflow.cancel_child)
1146+
with pytest.raises(WorkflowFailureError) as err:
11471147
await handle.result()
11481148
assert isinstance(err.value.cause, ChildWorkflowError)
11491149
assert isinstance(err.value.cause.cause, CancelledError)
@@ -2374,17 +2374,17 @@ async def test_workflow_already_started(client: Client, env: WorkflowEnvironment
23742374
async with new_worker(client, LongSleepWorkflow) as worker:
23752375
id = f"workflow-{uuid.uuid4()}"
23762376
# Try to start it twice
2377+
await client.start_workflow(
2378+
LongSleepWorkflow.run,
2379+
id=id,
2380+
task_queue=worker.task_queue,
2381+
)
23772382
with pytest.raises(WorkflowAlreadyStartedError):
23782383
await client.start_workflow(
23792384
LongSleepWorkflow.run,
23802385
id=id,
23812386
task_queue=worker.task_queue,
23822387
)
2383-
await client.start_workflow(
2384-
LongSleepWorkflow.run,
2385-
id=id,
2386-
task_queue=worker.task_queue,
2387-
)
23882388

23892389

23902390
@workflow.defn
@@ -3263,15 +3263,15 @@ async def test_workflow_custom_failure_converter(client: Client):
32633263
client = Client(**config)
32643264

32653265
# Run workflow and confirm error
3266-
with pytest.raises(WorkflowFailureError) as err:
3267-
async with new_worker(
3268-
client, CustomErrorWorkflow, activities=[custom_error_activity]
3269-
) as worker:
3270-
handle = await client.start_workflow(
3271-
CustomErrorWorkflow.run,
3272-
id=f"workflow-{uuid.uuid4()}",
3273-
task_queue=worker.task_queue,
3274-
)
3266+
async with new_worker(
3267+
client, CustomErrorWorkflow, activities=[custom_error_activity]
3268+
) as worker:
3269+
handle = await client.start_workflow(
3270+
CustomErrorWorkflow.run,
3271+
id=f"workflow-{uuid.uuid4()}",
3272+
task_queue=worker.task_queue,
3273+
)
3274+
with pytest.raises(WorkflowFailureError) as err:
32753275
await handle.result()
32763276

32773277
# Check error is as expected
@@ -4606,13 +4606,13 @@ async def test_workflow_timeout_support(client: Client, approach: str):
46064606
client, TimeoutSupportWorkflow, activities=[wait_cancel]
46074607
) as worker:
46084608
# Run and confirm activity gets cancelled
4609+
handle = await client.start_workflow(
4610+
TimeoutSupportWorkflow.run,
4611+
approach,
4612+
id=f"workflow-{uuid.uuid4()}",
4613+
task_queue=worker.task_queue,
4614+
)
46094615
with pytest.raises(WorkflowFailureError) as err:
4610-
handle = await client.start_workflow(
4611-
TimeoutSupportWorkflow.run,
4612-
approach,
4613-
id=f"workflow-{uuid.uuid4()}",
4614-
task_queue=worker.task_queue,
4615-
)
46164616
await handle.result()
46174617
assert isinstance(err.value.cause, ActivityError)
46184618
assert isinstance(err.value.cause.cause, CancelledError)
@@ -4946,8 +4946,8 @@ async def run(self, param: str) -> None:
49464946

49474947

49484948
async def test_workflow_fail_on_bad_input(client: Client):
4949-
with pytest.raises(WorkflowFailureError) as err:
4950-
async with new_worker(client, FailOnBadInputWorkflow) as worker:
4949+
async with new_worker(client, FailOnBadInputWorkflow) as worker:
4950+
with pytest.raises(WorkflowFailureError) as err:
49514951
await client.execute_workflow(
49524952
"FailOnBadInputWorkflow",
49534953
123,
@@ -5484,15 +5484,19 @@ async def _run_workflow_and_get_warning(self) -> bool:
54845484
assert update_task
54855485
with pytest.raises(RPCError) as update_err:
54865486
await update_task
5487-
assert (
5488-
update_err.value.status == RPCStatusCode.NOT_FOUND
5489-
and "workflow execution already completed"
5490-
in str(update_err.value).lower()
5491-
)
5487+
assert update_err.value.status == RPCStatusCode.NOT_FOUND and (
5488+
str(update_err.value).lower()
5489+
== "workflow execution already completed"
5490+
)
54925491

54935492
with pytest.raises(WorkflowFailureError) as err:
54945493
await handle.result()
5495-
assert "workflow execution failed" in str(err.value).lower()
5494+
assert isinstance(
5495+
err.value.cause,
5496+
{"cancellation": CancelledError, "failure": ApplicationError}[
5497+
self.workflow_termination_type
5498+
],
5499+
)
54965500

54975501
unfinished_handler_warning_emitted = any(
54985502
issubclass(w.category, self._unfinished_handler_warning_cls)

0 commit comments

Comments
 (0)