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/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/generate/generator.py
Functions¶
bool() -> Generator
¶
Create a random boolean generator.
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random boolean values. |
boolean() -> Generator
¶
Alias for generate.bool
.
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random boolean values. |
date(format: builtins.str = '%Y-%m-%d', *, disable_conversion: builtins.bool = False) -> Generator
¶
Create a date generator.
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 |
---|---|
format
|
Expected format of the date. If not provided, an ISO 8601 date format is used:
TYPE:
|
disable_conversion
|
If True, the conversion from Python's
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces dates in the specified format. |
Source code in src/pact/generate/__init__.py
datetime(format: builtins.str, *, disable_conversion: builtins.bool = False) -> Generator
¶
Create a datetime generator.
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 |
---|---|
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 |
---|---|
Generator
|
A generator that produces datetimes in the specified format. |
Source code in src/pact/generate/__init__.py
decimal(precision: builtins.int | None = None) -> Generator
¶
Alias for generate.float
.
PARAMETER | DESCRIPTION |
---|---|
precision
|
The number of digits to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random decimal values. |
Source code in src/pact/generate/__init__.py
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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random decimal values. |
Source code in src/pact/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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random hexadecimal values. |
Source code in src/pact/generate/__init__.py
hexadecimal(digits: builtins.int | None = None) -> Generator
¶
Alias for generate.hex
.
PARAMETER | DESCRIPTION |
---|---|
digits
|
The number of digits to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random hexadecimal values. |
Source code in src/pact/generate/__init__.py
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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random integers. |
Source code in src/pact/generate/__init__.py
integer(*, min: builtins.int | None = None, max: builtins.int | None = None) -> Generator
¶
Alias for generate.int
.
PARAMETER | DESCRIPTION |
---|---|
min
|
The minimum value for the integer.
TYPE:
|
max
|
The maximum value for the integer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random integers. |
Source code in src/pact/generate/__init__.py
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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces mock server URLs. |
Source code in src/pact/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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces values from the provider state context. |
Source code in src/pact/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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces strings matching the given regex pattern. |
Source code in src/pact/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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random strings. |
Source code in src/pact/generate/__init__.py
string(size: builtins.int | None = None) -> Generator
¶
Alias for generate.str
.
PARAMETER | DESCRIPTION |
---|---|
size
|
The size of the string to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces random strings. |
Source code in src/pact/generate/__init__.py
time(format: builtins.str = '%H:%M:%S', *, disable_conversion: builtins.bool = False) -> Generator
¶
Create a time generator.
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 |
---|---|
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 |
---|---|
Generator
|
A generator that produces times in the specified format. |
Source code in src/pact/generate/__init__.py
timestamp(format: builtins.str, *, disable_conversion: builtins.bool = False) -> Generator
¶
Alias for generate.datetime
.
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces datetimes in the specified format. |
Source code in src/pact/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:
|
RETURNS | DESCRIPTION |
---|---|
Generator
|
A generator that produces UUIDs in the specified format. |