Skip to content

Commit

Permalink
feat(v3): add specification attribute to pacts
Browse files Browse the repository at this point in the history
Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Mar 1, 2024
1 parent 94e2db7 commit 1082b62
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
23 changes: 22 additions & 1 deletion src/pact/v3/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,27 @@ class PactSpecification(Enum):
V3 = lib.PactSpecification_V3
V4 = lib.PactSpecification_V4

@classmethod
def from_str(cls, version: str) -> PactSpecification:
"""
Instantiate a Pact Specification from a string.
This method is case-insensitive, and allows for the version to be
specified with or without a leading "V", and with either a dot or an
underscore as the separator.
Args:
version:
The version of the Pact Specification.
Returns:
The Pact Specification.
"""
version = version.upper().replace(".", "_")
if version.startswith("V"):
return cls[version]
return cls["V" + version]

def __str__(self) -> str:
"""
Informal string representation of the Pact Specification.
Expand Down Expand Up @@ -5280,7 +5301,7 @@ def handle_get_pact_spec_version(handle: PactHandle) -> PactSpecification:
Returns:
The spec version for the Pact model.
"""
raise NotImplementedError
return PactSpecification(lib.pactffi_handle_get_pact_spec_version(handle._ref))


def with_pact_metadata(
Expand Down
13 changes: 8 additions & 5 deletions src/pact/v3/pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def provider(self) -> str:
"""
return self._provider

@property
def specification(self) -> pact.v3.ffi.PactSpecification:
"""
Pact specification version.
"""
return pact.v3.ffi.handle_get_pact_spec_version(self._handle)

def with_specification(
self,
version: str | pact.v3.ffi.PactSpecification,
Expand All @@ -128,11 +135,7 @@ def with_specification(
prefix.
"""
if isinstance(version, str):
version = version.upper().replace(".", "_")
if version.startswith("V"):
version = pact.v3.ffi.PactSpecification[version]
else:
version = pact.v3.ffi.PactSpecification["V" + version]
version = pact.v3.ffi.PactSpecification.from_str(version)
pact.v3.ffi.with_specification(self._handle, version)
return self

Expand Down
2 changes: 2 additions & 0 deletions tests/v3/test_pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest

from pact.v3 import Pact
from pact.v3.ffi import PactSpecification

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -131,6 +132,7 @@ def test_write_file(pact: Pact, temp_dir: Path) -> None:
)
def test_specification(pact: Pact, version: str) -> None:
pact.with_specification(version)
assert pact.specification == PactSpecification.from_str(version)


def test_server_log(pact: Pact) -> None:
Expand Down

0 comments on commit 1082b62

Please sign in to comment.