diff --git a/Makefile b/Makefile index 945c2c7..f5533f3 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,10 @@ test: .env test_native: .env docker compose up pact_verifier_native --exit-code-from pact_verifier_native docker compose logs pact_verifier_native +run_api: + docker compose up api +run_test_local: + python3 app/test/app_spec.py ## ===================== ## Deploy tasks diff --git a/README.md b/README.md index f2c573b..75ba03e 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,7 @@ make test To run the "fake ci" task: ``` -export PACT_BROKER_BASE_URL=https://test.pactflow.io -export PACT_BROKER_USERNAME=dXfltyFMgNOFZAxr8io9wJ37iUpY42M -export PACT_BROKER_PASSWORD=O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1 +export PACT_BROKER_BASE_URL="https://test.pactflow.io" +export PACT_BROKER_TOKEN="129cCdfCWhMzcC9pFwb4bw" make fake_ci ``` \ No newline at end of file diff --git a/app/test/app_spec.py b/app/test/app_spec.py new file mode 100644 index 0000000..051c871 --- /dev/null +++ b/app/test/app_spec.py @@ -0,0 +1,47 @@ +import os +from pact import Verifier +import pact_ffi + +from typing import Literal, Any + +def provider_state_handler( + state: str, + action: Literal["setup", "teardown"], + parameters: dict[str, Any] | None, +) -> None: + """Handle all provider state changes.""" + parameters = parameters or {} + if state == "a product with ID 10 exists": + if action == "setup": + return + if action == "teardown": + return + msg = f"Unknown state/action: {state}/{action}" + raise ValueError(msg) + + +def test_provider(): + """Test the provider against the consumer contract.""" + pact_ffi.log_to_stderr("DEBUG") + verifier = ( + Verifier("pactflow-example-provider-python") # Provider name + .add_transport(url="http://localhost:8000") + .state_handler(provider_state_handler, teardown=False) + .broker_source( + url=os.environ.get("PACT_BROKER_BASE_URL", "https://broker.pactflow.io/"), + token=os.environ.get("PACT_BROKER_TOKEN", "broker-token"), + selector=True + ) + .consumer_versions('{"mainBranch": true}', '{"deployedOrReleased": true}') + .include_pending() + .include_wip_since("2025-11-10") + .build() + ) + # verifier.set_publish_options( + # version="123", + # branch="foo", + # ) + verifier.verify() + +if __name__ == "__main__": + test_provider() \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 56598b0..850199e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,8 +4,8 @@ services: context: . volumes: - ./app:/app - expose: - - "8000" + ports: + - 8000:8000 environment: - PORT=8000 diff --git a/requirements.txt b/requirements.txt index 0907dc7..cc5fe56 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ fastapi==0.121.2 uvicorn==0.38.0 +pact-python==3.1.0