Skip to content

Commit 4c47843

Browse files
committed
chore(example): migrate consumer example
Migrate the consumer example to the new example structure. This should help reduce the redundancy between the examples and make it easier for users to test them out and see how they work. Ultimately, the idea will be that the consumer tests run first, and then the provider tests run against the generated pact file. Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent 8ee2eb0 commit 4c47843

File tree

16 files changed

+330
-352
lines changed

16 files changed

+330
-352
lines changed

.cirrus.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ TEST_TEMPLATE: &TEST_TEMPLATE
55
- python --version
66
# TODO: Fix lints before enabling
77
- echo hatch run lint
8-
# TODO: Implement the examples to work in hatch
9-
- echo hatch run example
108
- hatch run test
119

1210
linux_arm64_task:

.github/workflows/test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ jobs:
5252
if: matrix.python-version == env.STABLE_PYTHON_VERSION && runner.os == 'Linux'
5353
run: echo hatch run lint
5454

55-
- # TODO: Implement the examples to work in hatch
56-
name: Examples
55+
- name: Examples
5756
if: matrix.python-version == env.STABLE_PYTHON_VERSION && runner.os == 'Linux'
58-
run: echo hatch run example
57+
run: hatch run example --color=yes --capture=no
5958

6059
- name: Run tests and track code coverage
6160
run: hatch run test

examples/.ruff.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extend = "../pyproject.toml"
2+
3+
ignore = [
4+
"S101", # Forbid assert statements
5+
"D103", # Require docstring in public function
6+
]
7+
8+
[per-file-ignores]
9+
"tests/**.py" = [
10+
"INP001", # Forbid implicit namespaces
11+
]

examples/common/sharedfixtures.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

examples/conftest.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Shared PyTest configuration.
3+
4+
In order to run the examples, we need to run the Pact broker. In order to avoid
5+
having to run the Pact broker manually, or repeating the same code in each
6+
example, we define a PyTest fixture to run the Pact broker.
7+
8+
We also define a `pact_dir` fixture to define the directory where the generated
9+
Pact files will be stored. You are encouraged to have a look at these files
10+
after the examples have been run.
11+
"""
12+
from __future__ import annotations
13+
14+
from pathlib import Path
15+
from typing import Any, Generator
16+
17+
import pytest
18+
from testcontainers.compose import DockerCompose
19+
from yarl import URL
20+
21+
EXAMPLE_DIR = Path(__file__).parent.resolve()
22+
23+
24+
def pytest_addoption(parser: pytest.Parser) -> None:
25+
"""Define additional command lines to customise the examples."""
26+
parser.addoption(
27+
"--broker-url",
28+
help=(
29+
"The URL of the broker to use. If this option has been given, the container"
30+
" will _not_ be started."
31+
),
32+
type=str,
33+
)
34+
35+
36+
@pytest.fixture(scope="session")
37+
def broker(request: pytest.FixtureRequest) -> Generator[URL, Any, None]:
38+
"""
39+
Fixture to run the Pact broker.
40+
41+
This inspects whether the `--broker-url` option has been given. If it has,
42+
it is assumed that the broker is already running and simply returns the
43+
given URL.
44+
45+
Otherwise, the Pact broker is started in a container. The URL of the
46+
containerised broker is then returned.
47+
"""
48+
broker_url: str | None = request.config.getoption("--broker-url")
49+
50+
# If we have been given a broker URL, there's nothing more to do here and we
51+
# can return early.
52+
if broker_url:
53+
yield URL(broker_url)
54+
return
55+
56+
with DockerCompose(
57+
EXAMPLE_DIR,
58+
compose_file_name=["docker-compose.yml"],
59+
pull=True,
60+
) as _:
61+
yield URL("http://pactbroker:pactbroker@localhost:9292")
62+
return
63+
64+
65+
@pytest.fixture(scope="session")
66+
def pact_dir() -> Path:
67+
"""Fixture for the Pact directory."""
68+
return EXAMPLE_DIR / "pacts"

examples/consumer/conftest.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/consumer/requirements.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/consumer/run_pytest.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/consumer/src/consumer.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

examples/consumer/tests/consumer/test_user_consumer.py

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)