Generate¶
Generator module.
Classes¶
Generator
¶
Bases: ABC
Abstract generator.
In Pact, a generator is used by Pact to generate data on-the-fly during the contract verification process. Generators are used in combination with matchers to provide more flexible matching of data.
This class is abstract and should not be used directly. Instead, use one of the concrete generator classes. Alternatively, you can create your own generator 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_generator_json() -> dict[str, Any]
abstractmethod
¶
Convert the generator to a generator JSON object.
This method is used internally to convert the generator 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://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 generator as a generator JSON object. |
Source code in src/pact/v3/generate/generator.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 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/generate/generator.py
Functions¶
bool() -> Generator
¶
boolean() -> Generator
¶
date(format: builtins.str = '%Y-%m-%d', *, disable_conversion: builtins.bool = False) -> Generator
¶
Create a date generator.
PARAMETER | DESCRIPTION |
---|---|
format
|
Expected format of the date. This uses Python's Pact internally uses the Java
SimpleDateFormat
and the conversion from Python's If not provided, an ISO 8601 date format is used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
datetime(format: builtins.str, *, disable_conversion: builtins.bool = False) -> Generator
¶
Create a date-time generator.
PARAMETER | DESCRIPTION |
---|---|
format
|
Expected format of the timestamp. This uses Python's Pact internally uses the Java
SimpleDateFormat
and the conversion from Python's If not provided, an ISO 8601 timestamp format will be used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
decimal(precision: builtins.int | None = None) -> Generator
¶
float(precision: builtins.int | None = None) -> Generator
¶
Create a random decimal generator.
Note that the precision is the number of digits to generate in total, not
the number of decimal places. Therefore a precision of 3
will generate
numbers like 0.123
and 12.3
.
PARAMETER | DESCRIPTION |
---|---|
precision
|
The number of digits to generate.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
hex(digits: builtins.int | None = None) -> Generator
¶
Create a random hexadecimal generator.
PARAMETER | DESCRIPTION |
---|---|
digits
|
The number of digits to generate.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
hexadecimal(digits: builtins.int | None = None) -> Generator
¶
int(*, min: builtins.int | None = None, max: builtins.int | None = None) -> Generator
¶
Create a random integer generator.
PARAMETER | DESCRIPTION |
---|---|
min
|
The minimum value for the integer.
TYPE:
|
max
|
The maximum value for the integer.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
integer(*, min: builtins.int | None = None, max: builtins.int | None = None) -> Generator
¶
mock_server_url(regex: builtins.str | None = None, example: builtins.str | None = None) -> Generator
¶
Create a mock server URL generator.
Generates a URL with the mock server as the base URL.
PARAMETER | DESCRIPTION |
---|---|
regex
|
The regex pattern to match.
TYPE:
|
example
|
An example URL to use.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
provider_state(expression: builtins.str | None = None) -> Generator
¶
Create a provider state generator.
Generates a value that is looked up from the provider state context using the given expression.
PARAMETER | DESCRIPTION |
---|---|
expression
|
The expression to use to look up the provider state.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
regex(regex: builtins.str) -> Generator
¶
Create a regex generator.
The generator will generate a string that matches the given regex pattern.
PARAMETER | DESCRIPTION |
---|---|
regex
|
The regex pattern to match.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
str(size: builtins.int | None = None) -> Generator
¶
Create a random string generator.
PARAMETER | DESCRIPTION |
---|---|
size
|
The size of the string to generate.
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
string(size: builtins.int | None = None) -> Generator
¶
time(format: builtins.str = '%H:%M:%S', *, disable_conversion: builtins.bool = False) -> Generator
¶
Create a time generator.
PARAMETER | DESCRIPTION |
---|---|
format
|
Expected format of the time. This uses Python's Pact internally uses the Java
SimpleDateFormat
and the conversion from Python's If not provided, an ISO 8601 time format will be used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
Source code in src/pact/v3/generate/__init__.py
timestamp(format: builtins.str, *, disable_conversion: builtins.bool = False) -> Generator
¶
Alias for generate.datetime
.
Source code in src/pact/v3/generate/__init__.py
uuid(format: Literal['simple', 'lowercase', 'uppercase', 'urn'] = 'lowercase') -> Generator
¶
Create a UUID generator.
PARAMETER | DESCRIPTION |
---|---|
format
|
The format of the UUID to generate. This parameter is only supported under the V4 specification.
TYPE:
|