So Pact won't support cases where a response has o...
# documentation
d
So Pact won't support cases where a response has objects with EITHER an empty list or a list of length > 0. I cannot believe this isn't allowable , its a total deal breaker for using what is a great service. Great shame.
y
What happens in the case where your provider returns only empty lists and your validation passes and never returns a list greater than 0, and therefore cannot validator the object structure of a list with contents in it
d
You have a matcher that says its empty = OK or its got the objects I defined ? I don't see why thats a problem
The matcher validates either state
y
See

https://youtu.be/d6BXvwCoKSQ?t=687

for a video explanation as to why
if its an OR, you might never validate a list with contents
and therefore the list with contents might not match the consumers expectation
I would just test those two cases with provider states
d
They can all be empty , they can all be length > 0 or either ? I think we are miscommunicating. Anyway its a big impediment to us using the framework /PactFlow so your team seem fixed on this approach. Maybe I am misunderstanding something but it doesn't seem like a good approach and doesn't allow us to use this.
I cannot test with provider states as the response has objects with empty lists and objects with populated lists , writing a test for each would fail ?
y
Hmm, maybe we are 🙂 This is what I am thinking, rather than having one test Given there are no products (setup state to return empty list) When I make a request to get all products Then I am returned an empty list Given there are products (setup state to return populated list) When I make a request to get all products Then I am returned a list
If you can provide a concrete example of your issue you are facing, we can look to suggest a way forward if you want
d
Thanks for you help but perhaps I am not explaining the scenario correctly API response [ { "type": "parent", "children": [] #no children }, { "type": "parent" "children" :[{"name": "Buzz"}..... ] # children }, ..... ]
y
So if you had support for optional, during the pact verification you could get a result like
Copy code
[
{
   "type": "parent",
   "children": []  #no children
},
{
    "type": "parent"
     "children" :[] #no children
},
.....
]
and your test would pass, and you would be safe to deploy. however your provider, when it has children could actually return a different structure than you expected for the list with children, and you wouldn’t know until it was too late?
Copy code
[
{
   "type": "parent",
   "children": []  #no children
},
{
    "type": "parent"
     "children" :[{"firstName": "Buzz"}..... ] # children
},
.....
]
d
OK I see. What I was asking is can a matcher be implemented to verify an empty list or an object(s) like the one in the response but only that shape ? It appears not . I prefer to say either not optional , either no objects or the ones of a shape I define. Either one state or another, there's only two states. Like Enum isn't optional its one of a list of string s, does that make sense ?
I mean if developers cannot validate a response with this kind of structure Pact is very limited in its use , don't you think ? Unless there is a way to restructure the response without sacrificing payload size or simplicity.
OK I'll stop now just to say , this isn't an optional field , its there but in two different states. Thanks for your help, I really expected for a paid for service this stuff would be covered off. Thankfully we are on a 14 day trail.
y
I mean if developers cannot validate a response with this kind of structure Pact is very limited in its use , don’t you think ?
I don’t no, you can use provider states for this purpose.
I really expected for a paid for service this stuff would be covered off.
You’re asking for a fundamental change in a project, which has associated documentation to explain the rationale, so why would you assume that this would be covered?
I’ll take some time to digest this, when I am free, pretty chocka today 👍
What I was asking is can a matcher be implemented to verify an empty list or an object(s) like the one in the response but only that shape ? It appears not . I prefer to say either not optional , either no objects or the ones of a shape I define. Either one state or another, there’s only two states. Like Enum isn’t optional its one of a list of string s, does that make sense ?
If your provider documented in OpenAPI, you will be able to utilise cross-contract comparison, over the Pact Provider verification with matchers, which may ease the pain of building your matchers 🙂
d
Thanks is this part of the bidirectional testing approach here https://docs.pactflow.io/docs/bi-directional-contract-testing/ or can you send a link for me to find out more . Thanks for taking time out to assist me
y
yeah that is the one chap, you still utilise your existing pact contracts generated on the consumer side, and for the provider you upload an oas spec, plus verification results to show that the provider implements the specification. https://docs.pactflow.io/docs/bi-directional-contract-testing/contracts/oas#publishing-the-provider-contract--results-to-pactflow You can start with a plain text file or upload the OAS as the verification result file to get started quickly :)
And no worries, sorry to hear you are frustrated! We certainly want to help ease your pain, but sometimes even the strongest of wills must bend to the tool 🙌
👏 1
m
The idea of Pact tests is that you can control the data coming back from your provider. Another option to test what you’re after is to do something like this:
Copy code
[
    {
        "type": "parentwithnochildrcen",
        "children": []  #no children
    },
    {
        "type": "parentwithchildren",
        "children" :eachLike({"firstName": "Buzz"}) # children
    }
]
This would not match an arbitrary length array, but again, if you can control the data you now have tested both cases in a single test. If you can’t use provider states as Yousaf says, the
arrayContaining
matcher in JS might do what you need. It would allow you to specify multiple different object shapes, and would ensure the provider returns an array that at least has both of them. This is not yet available in Python though, which if I recall, is what you need
d
Thanks for all you help. if you have a moment and I know I have used alot of your time already , I see atLeastLike in the js version , this allows setting of the min length , would this allow an empty array (min=0) ?
🙏 1
m
Unfortunately we're still bound by the min=1 here also
👍 1