File tree 3 files changed +32
-6
lines changed
3 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -675,6 +675,27 @@ class PactSpecification(Enum):
675
675
V3 = lib .PactSpecification_V3
676
676
V4 = lib .PactSpecification_V4
677
677
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
+
678
699
def __str__ (self ) -> str :
679
700
"""
680
701
Informal string representation of the Pact Specification.
@@ -5280,7 +5301,7 @@ def handle_get_pact_spec_version(handle: PactHandle) -> PactSpecification:
5280
5301
Returns:
5281
5302
The spec version for the Pact model.
5282
5303
"""
5283
- raise NotImplementedError
5304
+ return PactSpecification ( lib . pactffi_handle_get_pact_spec_version ( handle . _ref ))
5284
5305
5285
5306
5286
5307
def with_pact_metadata (
Original file line number Diff line number Diff line change @@ -109,6 +109,13 @@ def provider(self) -> str:
109
109
"""
110
110
return self ._provider
111
111
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
+
112
119
def with_specification (
113
120
self ,
114
121
version : str | pact .v3 .ffi .PactSpecification ,
@@ -128,11 +135,7 @@ def with_specification(
128
135
prefix.
129
136
"""
130
137
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 )
136
139
pact .v3 .ffi .with_specification (self ._handle , version )
137
140
return self
138
141
Original file line number Diff line number Diff line change 10
10
import pytest
11
11
12
12
from pact .v3 import Pact
13
+ from pact .v3 .ffi import PactSpecification
13
14
14
15
if TYPE_CHECKING :
15
16
from pathlib import Path
@@ -131,6 +132,7 @@ def test_write_file(pact: Pact, temp_dir: Path) -> None:
131
132
)
132
133
def test_specification (pact : Pact , version : str ) -> None :
133
134
pact .with_specification (version )
135
+ assert pact .specification == PactSpecification .from_str (version )
134
136
135
137
136
138
def test_server_log (pact : Pact ) -> None :
You can’t perform that action at this time.
0 commit comments