Skip to content

Commit 2b95aad

Browse files
authored
added hello-activity-test (temporalio#20)
1 parent 8ccd571 commit 2b95aad

File tree

9 files changed

+119
-3
lines changed

9 files changed

+119
-3
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ jobs:
2727
- run: python -m pip install --upgrade wheel poetry poethepoet
2828
- run: poetry install
2929
- run: poe lint
30-
# TODO: Re-enable when tests appear
31-
# - run: poe test -s -o log_cli_level=DEBUG
30+
- run: poe test -s -o log_cli_level=DEBUG
31+
- run: poe test -s -o log_cli_level=DEBUG --workflow-environment time-skipping
3232

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ Some examples require extra dependencies. See each sample's directory for specif
5656
* [custom_converter](custom_converter) - Use a custom payload converter to handle custom types.
5757
* [custom_decorator](custom_decorator) - Custom decorator to auto-heartbeat a long-running activity.
5858
* [encryption](encryption) - Apply end-to-end encryption for all input/output.
59+
60+
61+
## Test
62+
63+
Running the tests requires `poe` to be installed.
64+
65+
python -m pip install poethepoet
66+
67+
Once you have `poe` installed you can run:
68+
69+
poe test

encryption/__init__.py

Whitespace-only changes.

hello/__init__.py

Whitespace-only changes.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies = { cryptography = "^38.0.1", aiohttp = "^3.8.1" }
3636
[tool.poe.tasks]
3737
format = [{cmd = "black ."}, {cmd = "isort ."}]
3838
lint = [{cmd = "black --check ."}, {cmd = "isort --check-only ."}, {ref = "lint-types" }]
39-
lint-types = "mypy --no-silence-site-packages --check-untyped-defs ."
39+
lint-types = "mypy --check-untyped-defs ."
4040
test = "pytest"
4141

4242
[build-system]
@@ -55,6 +55,7 @@ skip_gitignore = true
5555

5656
[tool.mypy]
5757
ignore_missing_imports = true
58+
namespace_packages = true
5859

5960
[[tool.mypy.overrides]]
6061
module = "aiohttp.*"

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import asyncio
2+
import multiprocessing
3+
import sys
4+
from typing import AsyncGenerator
5+
6+
import pytest
7+
import pytest_asyncio
8+
from temporalio.client import Client
9+
from temporalio.testing import WorkflowEnvironment
10+
11+
# Due to https://github.com/python/cpython/issues/77906, multiprocessing on
12+
# macOS starting with Python 3.8 has changed from "fork" to "spawn". For
13+
# pre-3.8, we are changing it for them.
14+
if sys.version_info < (3, 8) and sys.platform.startswith("darwin"):
15+
multiprocessing.set_start_method("spawn", True)
16+
17+
18+
def pytest_addoption(parser):
19+
parser.addoption(
20+
"--workflow-environment",
21+
default="local",
22+
help="Which workflow environment to use ('local', 'time-skipping', or target to existing server)",
23+
)
24+
25+
26+
@pytest.fixture(scope="session")
27+
def event_loop():
28+
# See https://github.com/pytest-dev/pytest-asyncio/issues/68
29+
# See https://github.com/pytest-dev/pytest-asyncio/issues/257
30+
# Also need ProactorEventLoop on older versions of Python with Windows so
31+
# that asyncio subprocess works properly
32+
if sys.version_info < (3, 8) and sys.platform == "win32":
33+
loop = asyncio.ProactorEventLoop()
34+
else:
35+
loop = asyncio.get_event_loop_policy().new_event_loop()
36+
yield loop
37+
loop.close()
38+
39+
40+
@pytest_asyncio.fixture(scope="session")
41+
async def env(request) -> AsyncGenerator[WorkflowEnvironment, None]:
42+
env_type = request.config.getoption("--workflow-environment")
43+
if env_type == "local":
44+
env = await WorkflowEnvironment.start_local()
45+
elif env_type == "time-skipping":
46+
env = await WorkflowEnvironment.start_time_skipping()
47+
else:
48+
env = WorkflowEnvironment.from_client(await Client.connect(env_type))
49+
yield env
50+
await env.shutdown()
51+
52+
53+
@pytest_asyncio.fixture
54+
async def client(env: WorkflowEnvironment) -> Client:
55+
return env.client

tests/hello/__init__.py

Whitespace-only changes.

tests/hello/hello_activity_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import uuid
2+
3+
from temporalio import activity
4+
from temporalio.client import Client
5+
from temporalio.worker import Worker
6+
7+
from hello.hello_activity import (
8+
ComposeGreetingInput,
9+
GreetingWorkflow,
10+
compose_greeting,
11+
)
12+
13+
14+
async def test_execute_workflow(client: Client):
15+
task_queue_name = str(uuid.uuid4())
16+
17+
async with Worker(
18+
client,
19+
task_queue=task_queue_name,
20+
workflows=[GreetingWorkflow],
21+
activities=[compose_greeting],
22+
):
23+
assert "Hello, World!" == await client.execute_workflow(
24+
GreetingWorkflow.run,
25+
"World",
26+
id=str(uuid.uuid4()),
27+
task_queue=task_queue_name,
28+
)
29+
30+
31+
@activity.defn(name="compose_greeting")
32+
async def compose_greeting_mocked(input: ComposeGreetingInput) -> str:
33+
return f"{input.greeting}, {input.name} from mocked activity!"
34+
35+
36+
async def test_mock_activity(client: Client):
37+
task_queue_name = str(uuid.uuid4())
38+
async with Worker(
39+
client,
40+
task_queue=task_queue_name,
41+
workflows=[GreetingWorkflow],
42+
activities=[compose_greeting_mocked],
43+
):
44+
assert "Hello, World from mocked activity!" == await client.execute_workflow(
45+
GreetingWorkflow.run,
46+
"World",
47+
id=str(uuid.uuid4()),
48+
task_queue=task_queue_name,
49+
)

0 commit comments

Comments
 (0)