Skip to content

Feat/interaction implementation #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
536 changes: 386 additions & 150 deletions pact/v3/ffi.py

Large diffs are not rendered by default.

633 changes: 544 additions & 89 deletions pact/v3/pact.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ addopts = [
"--import-mode=importlib",
"--cov-config=pyproject.toml",
"--cov=pact",
"--cov-report=xml",
]
filterwarnings = [
"ignore::DeprecationWarning:pact",
Expand Down
45 changes: 45 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Common fixtures for tests.
"""
import json
import shutil
import tempfile
from pathlib import Path
from typing import Any, Generator

import pytest


@pytest.fixture()
def temp_dir() -> Generator[Path, Any, None]:
"""
Create a temporary directory.

This fixture automatically handles cleanup of the temporary directory once
the test has finished.

The directory is populated with a few minimal files:

- `test.py`: A minimal hello-world Python script.
- `test.txt`: A minimal text file.
- `test.json`: A minimal JSON file.
- `test.png`: A minimal PNG image.
"""
temp_dir = Path(tempfile.mkdtemp())
with (temp_dir / "test.py").open("w") as f:
f.write('print("Hello, world!")')
with (temp_dir / "test.txt").open("w") as f:
f.write("Hello, world!")
with (temp_dir / "test.json").open("w") as f:
json.dump({"hello": "world"}, f)
with (temp_dir / "test.png").open("wb") as f:
f.write(
b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
b"\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4"
b"\x89\x00\x00\x00\x0a\x49\x44\x41\x54\x78\x9c\x63\x00\x01\x00\x00"
b"\x05\x00\x01\x0d\x0a\x2d\xb4\x00\x00\x00\x00\x49\x45\x4e\x44\xae"
b"\x42\x60\x82",
)

yield temp_dir
shutil.rmtree(temp_dir)
3 changes: 2 additions & 1 deletion tests/v3/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
directory.
"""


import pytest


Expand All @@ -15,4 +16,4 @@ def _setup_pact_logging() -> None:
"""
from pact.v3 import ffi

ffi.log_to_stderr(ffi.LevelFilter.DEBUG)
ffi.log_to_stderr("DEBUG")
34 changes: 34 additions & 0 deletions tests/v3/test_async_interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Pact Async Message Interaction unit tests.
"""

from __future__ import annotations

import re

import pytest
from pact.v3 import Pact


@pytest.fixture()
def pact() -> Pact:
"""
Fixture for a Pact instance.
"""
return Pact("consumer", "provider")


def test_str(pact: Pact) -> None:
interaction = pact.upon_receiving("a basic request", "Async")
assert str(interaction) == "AsyncMessageInteraction(a basic request)"


def test_repr(pact: Pact) -> None:
interaction = pact.upon_receiving("a basic request", "Async")
assert (
re.match(
r"^AsyncMessageInteraction\(InteractionHandle\(\d+\)\)$",
repr(interaction),
)
is not None
)
38 changes: 38 additions & 0 deletions tests/v3/test_ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,49 @@
They are not intended to test the Pact API itself, as that is handled by the
client library.
"""
import re

import pytest
from pact.v3 import ffi


def test_version() -> None:
assert isinstance(ffi.version(), str)
assert len(ffi.version()) > 0
assert ffi.version().count(".") == 2


def test_string_result_ok() -> None:
result = ffi.StringResult(ffi.lib.pactffi_generate_datetime_string(b"yyyy"))
assert result.is_ok
assert not result.is_failed
assert re.match(r"^\d{4}$", result.text)
assert str(result) == result.text
assert repr(result) == f"<StringResult: OK, {result.text!r}>"
result.raise_exception()


def test_string_result_failed() -> None:
result = ffi.StringResult(ffi.lib.pactffi_generate_datetime_string(b"t"))
assert not result.is_ok
assert result.is_failed
assert result.text.startswith("Error parsing")
with pytest.raises(RuntimeError):
result.raise_exception()


def test_datetime_valid() -> None:
ffi.validate_datetime("2023-01-01", "yyyy-MM-dd")


def test_datetime_invalid() -> None:
with pytest.raises(ValueError, match=r"Invalid datetime value.*"):
ffi.validate_datetime("01/01/2023", "yyyy-MM-dd")


def test_get_error_message() -> None:
# The first bit makes sure that an error is generated.
invalid_utf8 = b"\xc3\x28"
ret: int = ffi.lib.pactffi_validate_datetime(invalid_utf8, invalid_utf8)
assert ret == 2
assert ffi.get_error_message() == "error parsing value as UTF-8"
Loading