Match
Matching functionality.
This module provides the functionality to define matching rules to be used within a Pact contract. These rules define the expected content of the data being exchanged in a way that is more flexible than a simple equality check.
As an example, a contract may define how a new record is to be created through a POST request. The consumer would define the new information to be sent, and the expected response. The response may contain additional data added by the provider, such as an ID and a creation timestamp. The contract would define that the ID is of a specific format (e.g., an integer or a UUID), and that the creation timestamp is ISO 8601 formatted.
Warning
Do not import functions directly from this module. Instead, import the
match
module and use the functions from there:
A number of functions in this module are named after the types they match (e.g.,
int
, str
, bool
). These functions will have aliases as well for better
interoperability with the rest of the Pact ecosystem. It is important to note
that these functions will shadow the built-in types if imported directly from
this module. This is why we recommend importing the match
module and using the
functions from there.
Matching rules are frequently combined with generators which allow for Pact to
generate values on the fly during contract testing. As a general rule for the
functions below, if a value
is not provided, a generator will be used; and
conversely, if a value
is provided, a generator will not be used.
Attributes¶
Classes¶
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/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/match/matcher.py
Functions¶
array_containing(variants: Sequence[_T | Matcher[_T]]) -> Matcher[Sequence[_T]]
¶
Match an array that contains the given variants.
Matching is successful if each variant occurs once in the array. Variants may be objects containing matching rules.
PARAMETER | DESCRIPTION |
---|---|
variants
|
A list of variants to match against. |
RETURNS | DESCRIPTION |
---|---|
Matcher[Sequence[_T]]
|
Matcher for arrays containing the given variants. |
Source code in src/pact/match/__init__.py
bool(value: builtins.bool | Unset = UNSET) -> Matcher[builtins.bool]
¶
Match a boolean value.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
RETURNS | DESCRIPTION |
---|---|
Matcher[bool]
|
Matcher for boolean values. |
Source code in src/pact/match/__init__.py
boolean(value: builtins.bool | Unset = UNSET) -> Matcher[builtins.bool]
¶
date(value: dt.date | builtins.str | Unset = UNSET, /, format: builtins.str | None = None, *, disable_conversion: builtins.bool = False) -> Matcher[builtins.str]
¶
Match a date value.
A date value is a string that represents a date in a specific format. It does not have any time information.
Info
Pact internally uses the Java's
SimpleDateFormat
.
To ensure compatibility with the rest of the Python ecosystem, this
function accepts Python's
strftime
format and performs the conversion to Java's format internally.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
format
|
Expected format of the date. If not provided, an ISO 8601 date format will be used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for date strings. |
Source code in src/pact/match/__init__.py
datetime(value: dt.datetime | builtins.str | Unset = UNSET, /, format: builtins.str | None = None, *, disable_conversion: builtins.bool = False) -> Matcher[builtins.str]
¶
Match a datetime value.
A timestamp value is a string that represents a date and time in a specific format.
Info
Pact internally uses the Java's
SimpleDateFormat
.
To ensure compatibility with the rest of the Python ecosystem, this
function accepts Python's
strftime
format and performs the conversion to Java's format internally.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
format
|
Expected format of the timestamp. If not provided, an ISO 8601 timestamp format will be used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for datetime strings. |
Source code in src/pact/match/__init__.py
decimal(value: _NumberT | Unset = UNSET, /, *, precision: builtins.int | None = None) -> Matcher[_NumberT]
¶
Alias for match.float
.
each_key_matches(value: Mapping[_T, Any], /, *, rules: Matcher[_T] | list[Matcher[_T]]) -> Matcher[Mapping[_T, Matchable]]
¶
Match each key in a dictionary against a set of rules.
PARAMETER | DESCRIPTION |
---|---|
value
|
The value to match against. |
rules
|
The matching rules to match against each key. |
RETURNS | DESCRIPTION |
---|---|
Matcher[Mapping[_T, Matchable]]
|
Matcher for dictionaries where each key matches the given rules. |
Source code in src/pact/match/__init__.py
each_like(value: _T, /, *, min: builtins.int | None = None, max: builtins.int | None = None) -> Matcher[Sequence[_T]]
¶
Match each item in an array against a given value.
The value itself is arbitrary, and can include other matchers.
PARAMETER | DESCRIPTION |
---|---|
value
|
The value to match against.
TYPE:
|
min
|
The minimum number of items that must match the value. The minimum value is always 1, even if min is set to 0.
TYPE:
|
max
|
The maximum number of items that must match the value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[Sequence[_T]]
|
Matcher for arrays where each item matches the given value. |
Source code in src/pact/match/__init__.py
each_value_matches(value: Mapping[Any, _T], /, *, rules: Matcher[_T] | list[Matcher[_T]]) -> Matcher[Mapping[Matchable, _T]]
¶
Returns a matcher that matches each value in a dictionary against a set of rules.
PARAMETER | DESCRIPTION |
---|---|
value
|
The value to match against. |
rules
|
The matching rules to match against each value. |
RETURNS | DESCRIPTION |
---|---|
Matcher[Mapping[Matchable, _T]]
|
Matcher for dictionaries where each value matches the given rules. |
Source code in src/pact/match/__init__.py
float(value: _NumberT | Unset = UNSET, /, *, precision: builtins.int | None = None) -> Matcher[_NumberT]
¶
Match a floating point number.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
precision
|
The number of decimal precision to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[_NumberT]
|
Matcher for floating point numbers. |
Source code in src/pact/match/__init__.py
includes(value: builtins.str, /, *, generator: Generator | None = None) -> Matcher[builtins.str]
¶
Match a string that includes a given value.
PARAMETER | DESCRIPTION |
---|---|
value
|
The value to match against.
TYPE:
|
generator
|
The generator to use when generating the value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for strings that include the given value. |
Source code in src/pact/match/__init__.py
int(value: builtins.int | Unset = UNSET, /, *, min: builtins.int | None = None, max: builtins.int | None = None) -> Matcher[builtins.int]
¶
Match an integer value.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
min
|
If provided, the minimum value of the integer to generate.
TYPE:
|
max
|
If provided, the maximum value of the integer to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[int]
|
Matcher for integer values. |
Source code in src/pact/match/__init__.py
integer(value: builtins.int | Unset = UNSET, /, *, min: builtins.int | None = None, max: builtins.int | None = None) -> Matcher[builtins.int]
¶
Alias for match.int
.
Source code in src/pact/match/__init__.py
like(value: _T, /, *, min: builtins.int | None = None, max: builtins.int | None = None, generator: Generator | None = None) -> Matcher[_T]
¶
Alias for match.type
.
Source code in src/pact/match/__init__.py
none() -> Matcher[None]
¶
null() -> Matcher[None]
¶
number(value: builtins.int | builtins.float | Decimal | Unset = UNSET, /, *, min: builtins.int | None = None, max: builtins.int | None = None, precision: builtins.int | None = None) -> Matcher[builtins.int] | Matcher[builtins.float] | Matcher[Decimal]
¶
number(
value: builtins.int,
/,
*,
min: builtins.int | None = None,
max: builtins.int | None = None,
) -> Matcher[builtins.int]
Match a general number.
This matcher is a generalization of the integer
and decimal
matchers. It can be used to match any
number, whether it is an integer or a float.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
min
|
The minimum value of the number to generate. Only used when value is an integer.
TYPE:
|
max
|
The maximum value of the number to generate. Only used when value is an integer.
TYPE:
|
precision
|
The number of decimal digits to generate. Only used when value is a float.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[int] | Matcher[float] | Matcher[Decimal]
|
Matcher for numbers (integer, float, or Decimal). |
Source code in src/pact/match/__init__.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
regex(value: builtins.str | Unset = UNSET, /, *, regex: builtins.str | None = None) -> Matcher[builtins.str]
¶
Match a string against a regular expression.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
regex
|
The regular expression to match against.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for strings matching the given regular expression. |
Source code in src/pact/match/__init__.py
str(value: builtins.str | Unset = UNSET, /, *, size: builtins.int | None = None, generator: Generator | None = None) -> Matcher[builtins.str]
¶
Match a string value.
This function can be used to match a string value, merely verifying that the value is a string, possibly with a specific length.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
size
|
The size of the string to generate during a consumer test.
TYPE:
|
generator
|
Alternative generator to use when generating a consumer test.
If set, the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for string values. |
Source code in src/pact/match/__init__.py
string(value: builtins.str | Unset = UNSET, /, *, size: builtins.int | None = None, generator: Generator | None = None) -> Matcher[builtins.str]
¶
Alias for match.str
.
Source code in src/pact/match/__init__.py
time(value: dt.time | builtins.str | Unset = UNSET, /, format: builtins.str | None = None, *, disable_conversion: builtins.bool = False) -> Matcher[builtins.str]
¶
Match a time value.
A time value is a string that represents a time in a specific format. It does not have any date information.
Info
Pact internally uses the Java's
SimpleDateFormat
.
To ensure compatibility with the rest of the Python ecosystem, this
function accepts Python's strftime
format and performs the conversion to Java's format internally.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
format
|
Expected format of the time. If not provided, an ISO 8601 time format will be used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for time strings. |
Source code in src/pact/match/__init__.py
timestamp(value: dt.datetime | builtins.str | Unset = UNSET, /, format: builtins.str | None = None, *, disable_conversion: builtins.bool = False) -> Matcher[builtins.str]
¶
Alias for match.datetime
.
Source code in src/pact/match/__init__.py
type(value: _T, /, *, min: builtins.int | None = None, max: builtins.int | None = None, generator: Generator | None = None) -> Matcher[_T]
¶
Match a value by type.
PARAMETER | DESCRIPTION |
---|---|
value
|
A value to match against. This can be a primitive value, or a more complex object or array.
TYPE:
|
min
|
The minimum number of items that must match the value.
TYPE:
|
max
|
The maximum number of items that must match the value.
TYPE:
|
generator
|
The generator to use when generating the value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[_T]
|
Matcher for the given value type. |
Source code in src/pact/match/__init__.py
uuid(value: builtins.str | Unset = UNSET, /, *, format: Literal['uppercase', 'lowercase', 'urn', 'simple'] | None = None) -> Matcher[builtins.str]
¶
Match a UUID value.
This matcher internally combines the regex
matcher
with a UUID regex pattern. See RFC 4122
for details about the UUID format.
While RFC 4122 requires UUIDs to be output as lowercase, UUIDs are case
insensitive on input. Some common alternative formats can be enforced using
the format
parameter.
PARAMETER | DESCRIPTION |
---|---|
value
|
Default value to use when generating a consumer test. |
format
|
Enforce a specific UUID format. The following formats are supported:
If not provided, the matcher will accept any lowercase or uppercase.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Matcher[str]
|
Matcher for UUID strings. |