0

The below test will pass correctly, but if I post a body of {"a different key" : 4.56} it will fail as "key" is expected. In other words, the dictionary key is not flexible, only the float value.

How can I define a pact where only the dictionary types matter i.e. keys must be strings, value must be floats? The docs don't make this clear: https://github.com/pact-foundation/pact-python

def test_case_1(pact, client):
   (
      pact.given("object does not exist")
      .upon_receiving("a new post request")
      .with_request(
         "post", 
         "/url/post/endpoint",
         body=Like({"key": 1.23})
      .will_respond_with(200, body={})
   )
   with pact:
      client.post(body={"key": 4.56})
Conor
  • 535
  • 2
  • 8
  • 16
  • Have you tried using an empty JSON object / dictionary for contract? That sounds like a weak contract though but if that's what you want. – Gaël J Mar 30 '23 at 17:27
  • so my actual use case is more complicated: I have a load of nested key, values. It's just for this case, I have key: {"key": 1.234} and I don't care what the values in that dictionary are (just the types). But yes, I tried passing {}, but that does assert that {} is passed (it doesn't ignore the keys/attributes in the dictionary that are passed in. I guess Matthew Fellow's answer below is the way to go – Conor Mar 31 '23 at 08:31
  • I don't know how to do that in Python but in other languages you can define a contract to not match exact values but only the type. – Gaël J Mar 31 '23 at 09:08

1 Answers1

0

I think you want to say "I don't care if that key exists or not". If that's the case, then you don't include the keys you aren't interested in.

If you need it in some cases, but not in others, you're best to write a test case for each of the scenarios you actually need, rather than defining what the provider API can do (test your code, not the provider)

Community member Tim Jones has a really great way to think about matchers:

Think of the matchers as saying “this test covers all cases that pass this matcher”, not “this is the response schema”

I'd also have a read of this article to understand the dangers in what you might be asking for: https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18