Matcher¶
Matching functionality for Pact.
Matchers are used in Pact to allow for more flexible matching of data. While the consumer defines the expected request and response, there are circumstances where the provider may return dynamically generated data. In these cases, the consumer should use a matcher to define the expected data.
Attributes¶
Classes¶
ArrayContainsMatcher(variants: Sequence[_T | Matcher[_T]])
¶
EachKeyMatcher(value: Mapping[_T, Matchable], rules: list[Matcher[_T]] | None = None)
¶
Bases: Matcher[Mapping[_T, Matchable]]
Each key matcher.
A matcher that applies a matcher to each key in a mapping.
PARAMETER | DESCRIPTION |
---|---|
value
|
Example value to match against. |
rules
|
List of matchers to apply to each key in the mapping. |
Source code in src/pact/v3/match/matcher.py
EachValueMatcher(value: Mapping[Matchable, _T], rules: list[Matcher[_T]] | None = None)
¶
Bases: Matcher[Mapping[Matchable, _T]]
Each value matcher.
A matcher that applies a matcher to each value in a mapping.
PARAMETER | DESCRIPTION |
---|---|
value
|
Example value to match against. |
rules
|
List of matchers to apply to each value in the mapping. |
Source code in src/pact/v3/match/matcher.py
GenericMatcher(type: MatcherType, /, value: _T | Unset = UNSET, generator: Generator | None = None, extra_fields: Mapping[str, Any] | None = None, **kwargs: Matchable)
¶
Bases: Matcher[_T]
Generic matcher.
A generic matcher, with the ability to define arbitrary additional fields for inclusion in the integration JSON object and matching rule.
PARAMETER | DESCRIPTION |
---|---|
type
|
The type of the matcher.
TYPE:
|
value
|
The value to match. If not provided, the Pact library will generate a value based on the matcher type (or use the generator if provided). To ensure reproducibility, it is highly recommended to provide a value when creating a matcher. |
generator
|
The generator to use when generating the value. The generator will generally only be used if value is not provided.
TYPE:
|
extra_fields
|
Additional configuration elements to pass to the matcher. These fields will be used when converting the matcher to both an integration JSON object and a matching rule. |
**kwargs
|
Alternative way to define extra fields. See the
TYPE:
|
Source code in src/pact/v3/match/matcher.py
Attributes¶
generator = generator
instance-attribute
¶
Generator used to generate a value when the value is not provided.
type = type
instance-attribute
¶
The type of the matcher.
value: _T | Unset = value
instance-attribute
¶
Default value used by Pact when executing tests.
Functions¶
has_value() -> bool
¶
to_integration_json() -> dict[str, Any]
¶
Convert the matcher to an integration JSON object.
This method is used internally to convert the matcher to a JSON object which can be embedded directly in a number of places in the Pact FFI.
For more information about this format, see the docs:
https://docs.pact.io/implementation_guides/rust/pact_ffi/integrationjson
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
dict[str, Any]: The matcher as an integration JSON object. |
Source code in src/pact/v3/match/matcher.py
to_matching_rule() -> dict[str, Any]
¶
Convert the matcher to a matching rule.
This method is used internally to convert the matcher to a matching rule which can be embedded directly in a Pact file.
For more information about this format, see the docs:
https://github.com/pact-foundation/pact-specification/tree/version-4
and
https://github.com/pact-foundation/pact-specification/tree/version-2?tab=readme-ov-file#matchers
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
dict[str, Any]: The matcher as a matching rule. |
Source code in src/pact/v3/match/matcher.py
IntegrationJSONEncoder
¶
Bases: JSONEncoder
JSON encoder class for integration JSON objects.
This class is used to encode integration JSON objects to JSON.
Matcher
¶
Abstract matcher.
In Pact, a matcher is used to define how a value should be compared. This allows for more flexible matching of data, especially when the provider returns dynamically generated data.
This class is abstract and should not be used directly. Instead, use one of the concrete matcher classes. Alternatively, you can create your own matcher by subclassing this class.
The matcher provides methods to convert into an integration JSON object and a matching rule. These methods are used internally by the Pact library when communicating with the FFI and generating the Pact file.
Functions¶
to_integration_json() -> dict[str, Any]
abstractmethod
¶
Convert the matcher to an integration JSON object.
This method is used internally to convert the matcher to a JSON object which can be embedded directly in a number of places in the Pact FFI.
For more information about this format, see the docs:
https://docs.pact.io/implementation_guides/rust/pact_ffi/integrationjson
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The matcher as an integration JSON object. |
Source code in src/pact/v3/match/matcher.py
to_matching_rule() -> dict[str, Any]
abstractmethod
¶
Convert the matcher to a matching rule.
This method is used internally to convert the matcher to a matching rule which can be embedded directly in a Pact file.
For more information about this format, see the docs:
https://github.com/pact-foundation/pact-specification/tree/version-4
and
https://github.com/pact-foundation/pact-specification/tree/version-2?tab=readme-ov-file#matchers
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The matcher as a matching rule. |
Source code in src/pact/v3/match/matcher.py
MatchingRuleJSONEncoder
¶
Bases: JSONEncoder
JSON encoder class for matching rules.
This class is used to encode matching rules to JSON.