Hey, something came up recently and I'm not sure w...
# general
j
Hey, something came up recently and I'm not sure what the correct "pacty" way to handle it is: I have a POST rest endpoint I'd like to test which takes an object and creates it in the database. One of the properties on this object must be the ID for an inner object that must exist already in the database, so in my state setup function inside my provider I'm creating the inner object. The problem is that the object is always created by the database with a random UUID, and a database constraint prevents me from creating the object with a known ID. So how do I write my contract such that the object being posted is using the ID of the inner object in its own properties if it's randomly generated? I see that I can use matchers in the POST body in the contract, but if I don't actually know the real ID of the inner object that was created during the setup, I'm going to get a 400 error when I try and post my new object because it's not matching on a valid row of the inner object in the provider's database, so the matcher doesn't actually help me in this instance. Thanks!
t
You can do this with provider state, where the provider state setup returns the value you need
☝️ 1
m
What language are you using @json?
j
For this interaction the consumer is TypeScript and the provider is Python
The database the provider is using under-the-hood is postgres
and like I said, the data I'm trying to insert into the fake database as part of my provider state setup has a constraint on that ID field so I can't set it, it gets set automatically when the row is created
t
The provider state provided variables are perfect for this. Once the row is created in your state hook, you read the ID field and return it in the response from the state hook. This needs spec version 3, which means you’ll need to upgrade to pact-js v10
The method you need on the consumer side is called something like “fromProviderState”
j
Oh I had no idea you could return state from the state setup hook. What does that look like? Can you point me to an example somewhere?
You're right though, that does sound like exactly what I want
t
It's a new feature for pact-js, only just out of beta thanks to the hard work of Matt and team
j
Oh wait, so if my provider is in Python then I won't be able to do this?
t
See the “provider state injected variables” section here: https://github.com/pact-foundation/pact-js/blob/master/docs/matching.md
Oh, sorry, I thought you said typescript
j
The consumer is typescript
t
Yes, you start this on the consumer
You put it in the contract as “this thing is set by the provider”
*by the provider state
I don't know if pact-python supports this yet (it will if it supports v3 of the pact spec), but you don't need to do provider verification the same language as the provider
j
How would I create the state setup router in my FastAPI service without using Python?
t
you can use a state change endpoint (in python), but drive the test with some other pact-supported language
you would need to have something scripted that starts the provider with this state change endpoint available