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¶
AbstractMatcher
¶
flowchart TD
pact.match.matcher.AbstractMatcher[AbstractMatcher]
click pact.match.matcher.AbstractMatcher href "" "pact.match.matcher.AbstractMatcher"
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¶
has_value() -> bool
¶
Check if the matcher has a value.
If a value is present, it must be accessible via the value
attribute.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the matcher has a value, otherwise False. |
Source code in src/pact/match/matcher.py
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 integration JSON docs.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The matcher as an integration JSON object. |
Source code in src/pact/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, refer to the Pact specification and the matchers section
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The matcher as a matching rule. |
Source code in src/pact/match/matcher.py
AndMatcher(*matchers: AbstractMatcher[Any], value: _T_co | Unset = UNSET)
¶
Bases: AbstractMatcher[_T_co]
flowchart TD
pact.match.matcher.AndMatcher[AndMatcher]
pact.match.matcher.AbstractMatcher[AbstractMatcher]
pact.match.matcher.AbstractMatcher --> pact.match.matcher.AndMatcher
click pact.match.matcher.AndMatcher href "" "pact.match.matcher.AndMatcher"
click pact.match.matcher.AbstractMatcher href "" "pact.match.matcher.AbstractMatcher"
And matcher.
A matcher that combines multiple matchers using a logical AND.
It is best practice to provide a value. This may be set when creating
the AndMatcher, or it may be inferred from one of the constituent
matchers. In the latter case, the value from the first matcher that has
a value will be used.
| PARAMETER | DESCRIPTION |
|---|---|
matchers
|
List of matchers to combine.
TYPE:
|
value
|
Example value to match against. If not provided, the value from the first matcher that has a value will be used. |
Source code in src/pact/match/matcher.py
Functions¶
has_value() -> bool
¶
Check if the matcher has a value.
If a value is present, it must be accessible via the value
attribute.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the matcher has a value, otherwise False. |
Source code in src/pact/match/matcher.py
to_integration_json() -> dict[str, Any]
¶
Convert the matcher to an integration JSON object.
See
[AbstractMatcher.to_integration_json][pact.pact.match.matcher.AbstractMatcher.to_integration_json]
for more information.
Source code in src/pact/match/matcher.py
to_matching_rule() -> dict[str, Any]
¶
Convert the matcher to a matching rule.
See
[AbstractMatcher.to_matching_rule][pact.pact.match.matcher.AbstractMatcher.to_matching_rule]
for more information.
Source code in src/pact/match/matcher.py
ArrayContainsMatcher(variants: Sequence[_T_co | AbstractMatcher[_T_co]])
¶
Bases: AbstractMatcher[Sequence[_T_co]]
flowchart TD
pact.match.matcher.ArrayContainsMatcher[ArrayContainsMatcher]
pact.match.matcher.AbstractMatcher[AbstractMatcher]
pact.match.matcher.AbstractMatcher --> pact.match.matcher.ArrayContainsMatcher
click pact.match.matcher.ArrayContainsMatcher href "" "pact.match.matcher.ArrayContainsMatcher"
click pact.match.matcher.AbstractMatcher href "" "pact.match.matcher.AbstractMatcher"
Array contains matcher.
A matcher that checks if an array contains a value.
| PARAMETER | DESCRIPTION |
|---|---|
variants
|
List of possible values to match against.
TYPE:
|
Source code in src/pact/match/matcher.py
Functions¶
has_value() -> bool
¶
Check if the matcher has a value.
If a value is present, it must be accessible via the value
attribute.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the matcher has a value, otherwise False. |
Source code in src/pact/match/matcher.py
to_integration_json() -> dict[str, Any]
¶
Convert the matcher to an integration JSON object.
See
[AbstractMatcher.to_integration_json][pact.pact.match.matcher.AbstractMatcher.to_integration_json]
for more information.
Source code in src/pact/match/matcher.py
to_matching_rule() -> dict[str, Any]
¶
Convert the matcher to a matching rule.
See
[AbstractMatcher.to_matching_rule][pact.pact.match.matcher.AbstractMatcher.to_matching_rule]
for more information.
Source code in src/pact/match/matcher.py
EachKeyMatcher(value: Mapping[_T, Matchable], rules: list[AbstractMatcher[_T]] | None = None)
¶
Bases: AbstractMatcher[Mapping[_T, Matchable]]
flowchart TD
pact.match.matcher.EachKeyMatcher[EachKeyMatcher]
pact.match.matcher.AbstractMatcher[AbstractMatcher]
pact.match.matcher.AbstractMatcher --> pact.match.matcher.EachKeyMatcher
click pact.match.matcher.EachKeyMatcher href "" "pact.match.matcher.EachKeyMatcher"
click pact.match.matcher.AbstractMatcher href "" "pact.match.matcher.AbstractMatcher"
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.
TYPE:
|
Source code in src/pact/match/matcher.py
Functions¶
has_value() -> bool
¶
Check if the matcher has a value.
If a value is present, it must be accessible via the value
attribute.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the matcher has a value, otherwise False. |
Source code in src/pact/match/matcher.py
to_integration_json() -> dict[str, Any]
¶
Convert the matcher to an integration JSON object.
See
[AbstractMatcher.to_integration_json][pact.pact.match.matcher.AbstractMatcher.to_integration_json]
for more information.
Source code in src/pact/match/matcher.py
to_matching_rule() -> dict[str, Any]
¶
Convert the matcher to a matching rule.
See
[AbstractMatcher.to_matching_rule][pact.pact.match.matcher.AbstractMatcher.to_matching_rule]
for more information.
Source code in src/pact/match/matcher.py
EachValueMatcher(value: Mapping[Matchable, _T_co], rules: list[AbstractMatcher[_T_co]] | None = None)
¶
Bases: AbstractMatcher[Mapping[Matchable, _T_co]]
flowchart TD
pact.match.matcher.EachValueMatcher[EachValueMatcher]
pact.match.matcher.AbstractMatcher[AbstractMatcher]
pact.match.matcher.AbstractMatcher --> pact.match.matcher.EachValueMatcher
click pact.match.matcher.EachValueMatcher href "" "pact.match.matcher.EachValueMatcher"
click pact.match.matcher.AbstractMatcher href "" "pact.match.matcher.AbstractMatcher"
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.
TYPE:
|
Source code in src/pact/match/matcher.py
Functions¶
has_value() -> bool
¶
Check if the matcher has a value.
If a value is present, it must be accessible via the value
attribute.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the matcher has a value, otherwise False. |
Source code in src/pact/match/matcher.py
to_integration_json() -> dict[str, Any]
¶
Convert the matcher to an integration JSON object.
See
[AbstractMatcher.to_integration_json][pact.pact.match.matcher.AbstractMatcher.to_integration_json]
for more information.
Source code in src/pact/match/matcher.py
to_matching_rule() -> dict[str, Any]
¶
Convert the matcher to a matching rule.
See
[AbstractMatcher.to_matching_rule][pact.pact.match.matcher.AbstractMatcher.to_matching_rule]
for more information.
Source code in src/pact/match/matcher.py
GenericMatcher(type: MatcherType, /, value: _T_co | Unset = UNSET, generator: AbstractGenerator | None = None, extra_fields: Mapping[str, Any] | None = None, **kwargs: Matchable)
¶
Bases: AbstractMatcher[_T_co]
flowchart TD
pact.match.matcher.GenericMatcher[GenericMatcher]
pact.match.matcher.AbstractMatcher[AbstractMatcher]
pact.match.matcher.AbstractMatcher --> pact.match.matcher.GenericMatcher
click pact.match.matcher.GenericMatcher href "" "pact.match.matcher.GenericMatcher"
click pact.match.matcher.AbstractMatcher href "" "pact.match.matcher.AbstractMatcher"
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/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_co | Unset = value
instance-attribute
¶
Default value used by Pact when executing tests.
Functions¶
has_value() -> bool
¶
Check if the matcher has a value.
If a value is present, it must be accessible via the value
attribute.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the matcher has a value, otherwise False. |
Source code in src/pact/match/matcher.py
to_integration_json() -> dict[str, Any]
¶
Convert the matcher to an integration JSON object.
See
[AbstractMatcher.to_integration_json][pact.pact.match.matcher.AbstractMatcher.to_integration_json]
for more information.
Source code in src/pact/match/matcher.py
to_matching_rule() -> dict[str, Any]
¶
Convert the matcher to a matching rule.
See
[AbstractMatcher.to_matching_rule][pact.pact.match.matcher.AbstractMatcher.to_matching_rule]
for more information.
Source code in src/pact/match/matcher.py
IntegrationJSONEncoder
¶
Bases: JSONEncoder
flowchart TD
pact.match.matcher.IntegrationJSONEncoder[IntegrationJSONEncoder]
click pact.match.matcher.IntegrationJSONEncoder href "" "pact.match.matcher.IntegrationJSONEncoder"
JSON encoder class for integration JSON objects.
This class is used to encode integration JSON objects to JSON.
Functions¶
default(o: Any) -> Any
¶
Encode the object to JSON.
| PARAMETER | DESCRIPTION |
|---|---|
o
|
The object to encode.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The encoded object. |
Source code in src/pact/match/matcher.py
MatchingRuleJSONEncoder
¶
Bases: JSONEncoder
flowchart TD
pact.match.matcher.MatchingRuleJSONEncoder[MatchingRuleJSONEncoder]
click pact.match.matcher.MatchingRuleJSONEncoder href "" "pact.match.matcher.MatchingRuleJSONEncoder"
JSON encoder class for matching rules.
This class is used to encode matching rules to JSON.
Functions¶
default(o: Any) -> Any
¶
Encode the object to JSON.
| PARAMETER | DESCRIPTION |
|---|---|
o
|
The object to encode.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The encoded object. |