Skip to content

Commit ce1b6dc

Browse files
committedMar 1, 2024·
feat(v3): add specification attribute to pacts
Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent 94e2db7 commit ce1b6dc

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed
 

‎src/pact/v3/ffi.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,27 @@ class PactSpecification(Enum):
675675
V3 = lib.PactSpecification_V3
676676
V4 = lib.PactSpecification_V4
677677

678+
@classmethod
679+
def from_str(cls, version: str) -> PactSpecification:
680+
"""
681+
Instantiate a Pact Specification from a string.
682+
683+
This method is case-insensitive, and allows for the version to be
684+
specified with or without a leading "V", and with either a dot or an
685+
underscore as the separator.
686+
687+
Args:
688+
version:
689+
The version of the Pact Specification.
690+
691+
Returns:
692+
The Pact Specification.
693+
"""
694+
version = version.upper().replace(".", "_")
695+
if version.startswith("V"):
696+
return cls[version]
697+
return cls["V" + version]
698+
678699
def __str__(self) -> str:
679700
"""
680701
Informal string representation of the Pact Specification.
@@ -5280,7 +5301,7 @@ def handle_get_pact_spec_version(handle: PactHandle) -> PactSpecification:
52805301
Returns:
52815302
The spec version for the Pact model.
52825303
"""
5283-
raise NotImplementedError
5304+
return PactSpecification(lib.pactffi_handle_get_pact_spec_version(handle._ref))
52845305

52855306

52865307
def with_pact_metadata(

‎src/pact/v3/pact.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ def provider(self) -> str:
109109
"""
110110
return self._provider
111111

112+
@property
113+
def specification(self) -> pact.v3.ffi.PactSpecification:
114+
"""
115+
Pact specification version.
116+
"""
117+
return pact.v3.ffi.handle_get_pact_spec_version(self._handle)
118+
112119
def with_specification(
113120
self,
114121
version: str | pact.v3.ffi.PactSpecification,
@@ -128,11 +135,7 @@ def with_specification(
128135
prefix.
129136
"""
130137
if isinstance(version, str):
131-
version = version.upper().replace(".", "_")
132-
if version.startswith("V"):
133-
version = pact.v3.ffi.PactSpecification[version]
134-
else:
135-
version = pact.v3.ffi.PactSpecification["V" + version]
138+
version = pact.v3.ffi.PactSpecification.from_str(version)
136139
pact.v3.ffi.with_specification(self._handle, version)
137140
return self
138141

‎tests/v3/test_pact.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111

1212
from pact.v3 import Pact
13+
from pact.v3.ffi import PactSpecification
1314

1415
if TYPE_CHECKING:
1516
from pathlib import Path
@@ -131,6 +132,7 @@ def test_write_file(pact: Pact, temp_dir: Path) -> None:
131132
)
132133
def test_specification(pact: Pact, version: str) -> None:
133134
pact.with_specification(version)
135+
assert pact.specification == PactSpecification.from_str(version)
134136

135137

136138
def test_server_log(pact: Pact) -> None:

0 commit comments

Comments
 (0)
Please sign in to comment.