pact-foundation / pact-python

Python version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
http://pact.io
MIT License
568 stars 137 forks source link

Implement a `Generator` interface #747

Open JP-Ellis opened 1 month ago

JP-Ellis commented 1 month ago

Summary

Provide a Pythonic interface for creating generators.

Motivation

The Pact specification supports generators for most aspects of an interaction. These generators allow for values to be dynamic which can be particularly useful for values such as unique IDs, or datetimes.

At present, functions such as with_body or with_matching_rules only take strings or byte arrays which are passed as-is to the underlying FFI. As a result, while generators are technically supported, the end user is responsible for writing out the generators in a JSON string. This is clearly undesirable.

Proposed Implementation

General Considerations

The proposed changes would apply to all functions that can support generators, whether it be the body, headers, metadata, etc. The specific might change slightly based on context, but the general idea should remain the same.

There should also be consistency between using with_body(...) with a generators, and with_generators(...). That is, a user should be able to straightforward refactor from one to the other. Similarly, adapting a generator from with_body to with_header should be equally straightforward.

Generator Constructor

The specific implementation might depend a bit on what is done for the matching rules in #746, but I would want to follow a similar pattern. That is:

Types

Unlike the matching rules, the types supported by generators is a bit more restricted and needs to be more properly informed by the underlying capabilities of the Rust library.

References

[!NOTE]

To be filled out.

JP-Ellis commented 1 month ago

If possible, I would like to first get some eyes across the proposed implementation to hopefully "get it right" the first time around.

Tagging @YOU54F, @mefellows, @valkolovos