|
16 | 16 | from __future__ import annotations
|
17 | 17 |
|
18 | 18 | import logging
|
| 19 | +from datetime import UTC, datetime, timedelta |
19 | 20 | from http import HTTPStatus
|
20 | 21 | from typing import TYPE_CHECKING, Any, Dict, Generator
|
21 | 22 |
|
|
24 | 25 | from yarl import URL
|
25 | 26 |
|
26 | 27 | from examples.src.consumer import User, UserConsumer
|
27 |
| -from pact import Consumer, Format, Like, Provider |
| 28 | +from pact import Consumer, Format, Like, Provider # type: ignore[attr-defined] |
28 | 29 |
|
29 | 30 | if TYPE_CHECKING:
|
30 | 31 | from pathlib import Path
|
31 | 32 |
|
32 |
| - from pact.pact import Pact |
| 33 | + from pact.pact import Pact # type: ignore[import-untyped] |
33 | 34 |
|
34 |
| -log = logging.getLogger(__name__) |
| 35 | +logger = logging.getLogger(__name__) |
35 | 36 |
|
36 | 37 | MOCK_URL = URL("http://localhost:8080")
|
37 | 38 |
|
38 | 39 |
|
| 40 | +@pytest.fixture(scope="session", autouse=True) |
| 41 | +def _setup_pact_logging() -> None: |
| 42 | + """ |
| 43 | + Set up logging for the pact package. |
| 44 | + """ |
| 45 | + from pact.v3 import ffi |
| 46 | + |
| 47 | + ffi.log_to_stderr("INFO") |
| 48 | + |
| 49 | + |
39 | 50 | @pytest.fixture
|
40 | 51 | def user_consumer() -> UserConsumer:
|
41 | 52 | """
|
@@ -78,7 +89,7 @@ def pact(broker: URL, pact_dir: Path) -> Generator[Pact, Any, None]:
|
78 | 89 | pact = consumer.has_pact_with(
|
79 | 90 | Provider("UserProvider"),
|
80 | 91 | pact_dir=pact_dir,
|
81 |
| - publish_to_broker=True, |
| 92 | + publish_to_broker=False, |
82 | 93 | # Mock service configuration
|
83 | 94 | host_name=MOCK_URL.host,
|
84 | 95 | port=MOCK_URL.port,
|
@@ -142,3 +153,66 @@ def test_get_unknown_user(pact: Pact, user_consumer: UserConsumer) -> None:
|
142 | 153 | assert excinfo.value.response is not None
|
143 | 154 | assert excinfo.value.response.status_code == HTTPStatus.NOT_FOUND
|
144 | 155 | pact.verify()
|
| 156 | + |
| 157 | + |
| 158 | +def test_post_request_to_create_user(pact: Pact, user_consumer: UserConsumer) -> None: |
| 159 | + """ |
| 160 | + Test the POST request for creating a new user. |
| 161 | +
|
| 162 | + This test defines the expected interaction for a POST request to create |
| 163 | + a new user. It sets up the expected request and response from the provider, |
| 164 | + including the request body and headers, and verifies that the response |
| 165 | + status code is 200 and the response body matches the expected user data. |
| 166 | + """ |
| 167 | + expected: Dict[str, Any] = { |
| 168 | + "id": 124, |
| 169 | + "name": "Jane Doe", |
| 170 | + "email": "jane@example.com", |
| 171 | + "created_on": Format().iso_8601_datetime(), |
| 172 | + } |
| 173 | + header = {"Content-Type": "application/json"} |
| 174 | + payload: dict[str, str] = { |
| 175 | + "name": "Jane Doe", |
| 176 | + "email": "jane@example.com", |
| 177 | + "created_on": (datetime.now(tz=UTC) - timedelta(days=318)).isoformat(), |
| 178 | + } |
| 179 | + expected_response_code: int = 200 |
| 180 | + |
| 181 | + ( |
| 182 | + pact.given("create user 124") |
| 183 | + .upon_receiving("A request to create a new user") |
| 184 | + .with_request(method="POST", path="/users/", headers=header, body=payload) |
| 185 | + .will_respond_with(status=200, headers=header, body=Like(expected)) |
| 186 | + ) |
| 187 | + |
| 188 | + with pact: |
| 189 | + response = user_consumer.create_user(user=payload, header=header) |
| 190 | + assert response[0] == expected_response_code |
| 191 | + assert response[1].id == 124 |
| 192 | + assert response[1].name == "Jane Doe" |
| 193 | + |
| 194 | + pact.verify() |
| 195 | + |
| 196 | + |
| 197 | +def test_delete_request_to_delete_user(pact: Pact, user_consumer: UserConsumer) -> None: |
| 198 | + """ |
| 199 | + Test the DELETE request for deleting a user. |
| 200 | +
|
| 201 | + This test defines the expected interaction for a DELETE request to delete |
| 202 | + a user. It sets up the expected request and response from the provider, |
| 203 | + including the request body and headers, and verifies that the response |
| 204 | + status code is 200 and the response body matches the expected user data. |
| 205 | + """ |
| 206 | + expected_response_code: int = 204 |
| 207 | + ( |
| 208 | + pact.given("delete the user 124") |
| 209 | + .upon_receiving("a request for deleting user") |
| 210 | + .with_request(method="DELETE", path="/users/124") |
| 211 | + .will_respond_with(204) |
| 212 | + ) |
| 213 | + |
| 214 | + with pact: |
| 215 | + response_status_code = user_consumer.delete_user(124) |
| 216 | + assert response_status_code == expected_response_code |
| 217 | + |
| 218 | + pact.verify() |
0 commit comments