Skip to content

Commit fa1e9bf

Browse files
committed
fixing minor bugs in the original sample and adding the mysterious missing tests that are in the slides but not the repo
1 parent 413bf2b commit fa1e9bf

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

samples/age-estimation/activities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import urllib.parse
22

33
import aiohttp
4-
from models import EstimatorResponse
4+
from shared import EstimatorResponse
55
from temporalio import activity
66

77

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import aiohttp
2+
import pytest
3+
from activities import AgeEstimationActivities
4+
from temporalio.testing import ActivityEnvironment
5+
6+
7+
@pytest.mark.asyncio
8+
@pytest.mark.parametrize(
9+
"input, output",
10+
[
11+
("Mason", 40),
12+
],
13+
)
14+
async def test_retrieve_estimate(input, output):
15+
async with aiohttp.ClientSession() as session:
16+
activity_environment = ActivityEnvironment()
17+
activities = AgeEstimationActivities(session)
18+
result = await activity_environment.run(activities.retrieve_estimate, input)
19+
assert output == result.age
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import aiohttp
2+
import pytest
3+
from activities import AgeEstimationActivities
4+
from temporalio.testing import WorkflowEnvironment
5+
from temporalio.worker import Worker
6+
from workflow import EstimateAgeWorkflow
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_successful_age_estimation():
11+
async with await WorkflowEnvironment.start_time_skipping() as env:
12+
tq = "age-estimation-workflow"
13+
async with aiohttp.ClientSession() as session:
14+
activities = AgeEstimationActivities(session)
15+
async with Worker(
16+
env.client,
17+
task_queue=tq,
18+
workflows=[EstimateAgeWorkflow],
19+
activities=[activities.retrieve_estimate],
20+
):
21+
input = "Mason"
22+
output = await env.client.execute_workflow(
23+
EstimateAgeWorkflow.run,
24+
input,
25+
id="test-age-estimation-workflow-mason",
26+
task_queue=tq,
27+
)
28+
assert f"{input} has an estimated age of 40" == output
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import aiohttp
2+
import pytest
3+
from activities import AgeEstimationActivities
4+
from temporalio.testing import WorkflowEnvironment
5+
from temporalio.worker import Worker
6+
from temporalio import activity
7+
from workflow import EstimateAgeWorkflow
8+
from shared import EstimatorResponse
9+
10+
11+
@activity.defn(name="retrieve_estimate")
12+
async def estimate_age_mocked(input: str) -> EstimatorResponse:
13+
if input == "Stanislav":
14+
return EstimatorResponse(name="Stanislav", count=8928, age=21)
15+
else:
16+
return EstimatorResponse(name="Mason", count=1487, age=40)
17+
18+
19+
@pytest.mark.asyncio
20+
async def test_successful_age_estimation():
21+
async with await WorkflowEnvironment.start_time_skipping() as env:
22+
tq = "age-estimation-workflow"
23+
async with aiohttp.ClientSession() as session:
24+
activities = AgeEstimationActivities(session)
25+
async with Worker(
26+
env.client,
27+
task_queue=tq,
28+
workflows=[EstimateAgeWorkflow],
29+
activities=[estimate_age_mocked],
30+
):
31+
input = "Stanislav"
32+
output = await env.client.execute_workflow(
33+
EstimateAgeWorkflow.run,
34+
input,
35+
id="test-age-estimation-workflow-mason",
36+
task_queue=tq,
37+
)
38+
assert f"{input} has an estimated age of 21" == output

samples/age-estimation/workflow.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
# Import activity, passing it through the sandbox without reloading the module
66
with workflow.unsafe.imports_passed_through():
77
from activities import AgeEstimationActivities
8-
from shared import EstimatorResponse
98

109

1110
@workflow.defn
12-
class EstimateAge:
11+
class EstimateAgeWorkflow:
1312
@workflow.run
14-
async def run(self, name: str) -> EstimatorResponse:
13+
async def run(self, name: str) -> str:
1514
age_estimate = await workflow.execute_activity_method(
1615
AgeEstimationActivities.retrieve_estimate,
1716
name,
1817
start_to_close_timeout=timedelta(seconds=5),
1918
)
2019

21-
return age_estimate
20+
return f"{name} has an estimated age of {age_estimate.age}"

0 commit comments

Comments
 (0)