Tatiana
10/04/2022, 5:25 PMfrom fastapi import APIRouter
from pydantic import BaseModel
from eventhub.core.events.types import EventLifecycle, EventStatus
from tests.e2e.conftest import team_factory, event_factory
pact_router = APIRouter()
STARTED_EVENT_ID = "ba5ec002-6fac-4285-bbbb-46c8801fb283"
TEAM_ID = "dummy_tid"
class ProviderState(BaseModel):
state: str # noqa: E999
@pact_router.post("/_pact/provider_states")
async def provider_states(provider_state: ProviderState):
mapping = {
"I can get a specific event": setup_started_event
}
mapping[provider_state.state]()
return {"result": mapping[provider_state.state]}
async def setup_started_event(db_session):
await team_factory(db_session, team_id=TEAM_ID)
await event_factory(
db_session, STARTED_EVENT_ID, team_id=TEAM_ID, life_cycle=EventLifecycle.STARTED, status=EventStatus.PUBLISHED
)
The application is running and I am trying to verify my pact by running:
pact-verifier --provider-base-url=<http://localhost:8000> --pact-url=./pacts/mousedeer-events-consumer-eventhub-provider.json --provider-states-setup-url=<http://localhost:8000/_pact/provider_states>
But I am getting the errors:
Failures:
1) Verifying a pact between mousedeer-events-consumer and eventhub-provider Given I can get a specific event a request to get a specific event with GET /events/ba5ec002-6fac-4285-bbbb-46c8801fb283 returns a response which has status code 200
Failure/Error: set_up_provider_states interaction.provider_states, options[:consumer]
Pact::ProviderVerifier::SetUpProviderStateError:
Error setting up provider state 'I can get a specific event' for consumer 'mousedeer-events-consumer' at <http://localhost:8000/_pact/provider_states>. response status=404 response body={"detail":"Not Found"}
...
...
...
6) Verifying a pact between mousedeer-events-consumer and eventhub-provider Given I can get all events a request to get all events with GET /events?limit=10&name=&offset=0 returns a response which includes headers "Content-Type" which equals "application/json"
Failure/Error: set_up_provider_states interaction.provider_states, options[:consumer]
Pact::ProviderVerifier::SetUpProviderStateError:
Error setting up provider state 'I can get all events' for consumer 'mousedeer-events-consumer' at <http://localhost:8000/_pact/provider_states>. response status=404 response body={"detail":"Not Found"}
First, I am wondering why it tries to setup a state for my all two endpoints if I provided state just for one?
Second, does this setup looks fine or something is wrong? Unfortunately, I don’t fully understand how post request should be implemented for pact states (as I can see it should be done according to this), so I just tried to use your example.
Third, do I understand correctly that in my case I should seed my local DB (the app and DB is running against localhost in my case)? or I can use fakedb as in your example?Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Third, do I understand correctly that in my case I should seed my local DB (the app and DB is running against localhost in my case)? or I can use fakedb as in your example? (edited)Pact is agnostic to how you setup the state - stubs, mocks, database seeding etc. The provider should get to choose the method that is most convenient for it, rather than have the consumer specify how the provider does that (like in e2e tests).
Matt (pactflow.io / pact-js / pact-go)
Jayesh Guru
10/17/2022, 10:08 PM@pact_router.post("/_pact/provider_states")
in our provider side of tests?
The states are defined in the pact file. It will always try to setup the states, even if you haven’t defined them in your test.
Matt (pactflow.io / pact-js / pact-go)
Jayesh Guru
10/18/2022, 1:29 AMMatt (pactflow.io / pact-js / pact-go)
200
ok is all you need. The point of the provider states endpoint is to setup mocks/data to be able to respond to a scenario-Matt (pactflow.io / pact-js / pact-go)
User A exists
when the state handler is executed, you should ensure that User A exists. This could be injecting data into a database, setting up a mock etc.Jayesh Guru
10/18/2022, 1:33 AM