Skip to content

Commit e86b7eb

Browse files
committed
chore(example): avoid changing python path
Initially, when having the tests completely separate, the examples were scoped to their own space and required setting `PYTHONPATH` to work. This is not needed if we change `import src.{mod}` to `import examples.src.{mod}`. Signed-off-by: JP-Ellis <[email protected]>
1 parent 045083b commit e86b7eb

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

examples/tests/test_00_consumer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
import pytest
1919
import requests
20+
from examples.src.consumer import User, UserConsumer
2021
from pact import Consumer, Format, Like, Provider
2122
from yarl import URL
2223

23-
from src.consumer import User, UserConsumer
24-
2524
if TYPE_CHECKING:
2625
from pathlib import Path
2726

examples/tests/test_01_provider_fastapi.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626

2727
import pytest
2828
import uvicorn
29+
from examples.src.fastapi import app
2930
from pact import Verifier
3031
from pydantic import BaseModel
3132
from yarl import URL
3233

33-
from src.fastapi import app
34-
3534
PROVIDER_URL = URL("http://localhost:8080")
3635

3736

@@ -91,10 +90,10 @@ def verifier() -> Generator[Verifier, Any, None]:
9190

9291
def mock_user_123_doesnt_exist() -> None:
9392
"""Mock the database for the user 123 doesn't exist state."""
94-
import src.fastapi
93+
import examples.src.fastapi
9594

96-
src.fastapi.FAKE_DB = MagicMock()
97-
src.fastapi.FAKE_DB.get.return_value = None
95+
examples.src.fastapi.FAKE_DB = MagicMock()
96+
examples.src.fastapi.FAKE_DB.get.return_value = None
9897

9998

10099
def mock_user_123_exists() -> None:
@@ -108,10 +107,10 @@ def mock_user_123_exists() -> None:
108107
By using consumer-driven contracts and testing the provider against the
109108
consumer's contract, we can ensure that the provider is only providing what
110109
"""
111-
import src.fastapi
110+
import examples.src.fastapi
112111

113-
src.fastapi.FAKE_DB = MagicMock()
114-
src.fastapi.FAKE_DB.get.return_value = {
112+
examples.src.fastapi.FAKE_DB = MagicMock()
113+
examples.src.fastapi.FAKE_DB.get.return_value = {
115114
"id": 123,
116115
"name": "Verna Hampton",
117116
"created_on": "2016-12-15T20:16:01",

examples/tests/test_01_provider_flask.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
from unittest.mock import MagicMock
2727

2828
import pytest
29+
from examples.src.flask import app
2930
from flask import request
3031
from pact import Verifier
3132
from yarl import URL
3233

33-
from src.flask import app
34-
3534
PROVIDER_URL = URL("http://localhost:8080")
3635

3736

@@ -84,10 +83,10 @@ def verifier() -> Generator[Verifier, Any, None]:
8483

8584
def mock_user_123_doesnt_exist() -> None:
8685
"""Mock the database for the user 123 doesn't exist state."""
87-
import src.flask
86+
import examples.src.flask
8887

89-
src.flask.FAKE_DB = MagicMock()
90-
src.flask.FAKE_DB.get.return_value = None
88+
examples.src.flask.FAKE_DB = MagicMock()
89+
examples.src.flask.FAKE_DB.get.return_value = None
9190

9291

9392
def mock_user_123_exists() -> None:
@@ -101,10 +100,10 @@ def mock_user_123_exists() -> None:
101100
By using consumer-driven contracts and testing the provider against the
102101
consumer's contract, we can ensure that the provider is only providing what
103102
"""
104-
import src.flask
103+
import examples.src.flask
105104

106-
src.flask.FAKE_DB = MagicMock()
107-
src.flask.FAKE_DB.get.return_value = {
105+
examples.src.flask.FAKE_DB = MagicMock()
106+
examples.src.flask.FAKE_DB.get.return_value = {
108107
"id": 123,
109108
"name": "Verna Hampton",
110109
"created_on": "2016-12-15T20:16:01",

examples/tests/test_02_message_consumer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
from unittest.mock import MagicMock
2929

3030
import pytest
31+
from examples.src.message import Handler
3132
from pact import MessageConsumer, MessagePact, Provider
3233

33-
from src.message import Handler
34-
3534
if TYPE_CHECKING:
3635
from pathlib import Path
3736

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extra-dependencies = ["hatchling", "packaging", "requests"]
105105
[tool.hatch.envs.default.scripts]
106106
lint = ["black --check --diff {args:.}", "ruff {args:.}", "mypy {args:.}"]
107107
test = "pytest --cov-config=pyproject.toml --cov=pact --cov=tests tests/"
108-
example = "PYTHONPATH=examples pytest examples/ {args}"
108+
example = "pytest examples/ {args}"
109109
all = ["lint", "tests"]
110110

111111
# Test environment for running unit tests. This automatically tests against all

0 commit comments

Comments
 (0)