https://pact.io logo
Join Slack
Powered by
# pact-python
  • e

    Elliott Murray

    09/11/2022, 10:54 AM
    I just did the admin. The credit goes to @Yousaf Nabi (pactflow.io) for testing and clean up and @Shuying Lin for actually doing the work!
    👍 1
  • e

    Elliott Murray

    09/11/2022, 10:55 AM
    Publish pact and verify on branch now supported for everyone else.
    🙌 2
  • m

    Maurice Mretens

    09/21/2022, 6:49 PM
    Hi, I am trying pact out for the first time and we are mainly message based. That is where my problem lies. I want to use the matchers in my consumer tests but using them is kinda weird. The way that I would expect them to work according to docs is:
    Copy code
    event = {
            "event": "delivery:created",
            "delivery_type": matchers.Term("express|standard", "standard"),
        }
    
        (
            pact_no_publish.given("Delivery has standard shipping")
            .expects_to_receive("delivery:created")
            .with_content(event)
            .with_metadata({"Content-Type": "application/json"})
        )
    What this does is to pass the Term object into the message. What I have to do in this case is this:
    Copy code
    event = {
            "event": "delivery:created",
            "delivery_type": matchers.Term("express|standard", "standard").generate()["data"]["generate"],
        }
    Is this the intended way or should it be done im some other way?
  • t

    Tatiana

    10/04/2022, 5:25 PM
    Hi All! I am trying to understand how pact states work and how it can be implemented. Input data: • My provider uses Python 3.10 and FastAPI • I have two consumer tests, one to get one specific event, another one to get all events. And pact is generated. I have to ensure that at least one event exists in my local database, so I need to use pact states. I would like to insert just one event to my DB. So, I am doing the following: I’m trying to implement post endpoint as it’s shown here. I am mapping just one state for my getting a specific endpoint (exactly for this endpoint I need a specific event existed in the DB). My code looks like:
    Copy code
    from 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:
    Copy code
    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:
    Copy code
    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?
    m
    j
    • 3
    • 10
  • j

    Jayesh Guru

    10/14/2022, 7:56 PM
    Hello Pact Python Team, I am trying to write Provider Test as below, it is able to communicate with pactbroker but not able to get the request body from pact so getting 400 error, Am I missing anything?
    Copy code
    from pact import Verifier
    
    PACT_BROKER_URL = "<http://pactbroker.service.xxx.xxx.xxx.com/>"
    # I am spinning up our provider service(my-service) in docker-compose on 5002 port
    PROVIDER_HOST = "my-service"
    PROVIDER_PORT = 5002
    PROVIDER_URL = f"http://{PROVIDER_HOST}:{PROVIDER_PORT}"
    
    @pytest.fixture
    def broker_opts():
        return {
            "broker_url": PACT_BROKER_URL,
            "publish_version": "3",
            "publish_verification_results": False,
        }
    
    
    def test_service_provider_against_broker(broker_opts):
        headers=["Authorization: Bearer my_jwt_token"]
        headers.append("Content-Type:application/json")
        verifier = Verifier(provider="my_provider_python_service", provider_base_url=PROVIDER_URL)
        success, logs = verifier.verify_with_broker(
            **broker_opts,
            verbose=True,
            provider_states_setup_url=f"{PROVIDER_URL}/my_path",
            enable_pending=False,
            headers=headers
        )
        assert success == 0
    m
    • 2
    • 31
  • o

    Orhun Vatansever

    10/18/2022, 7:51 PM
    Hello everyone, I am currently trying to create contract tests between 2 applications that communicate through message queue using pact broker. The producer is written by python and the consumer is kotlin. I have created consumer contracts on kotlin using
    pact-jvm-consumer-java8_2.12
    , and the python one using pact-foundation package. I can see the pacts published on my broker, and when I want to verify my producer contract in python, I get an error called "no matched handler". I saw some github discussions about message queue contracts being v3 and python having ruby implementation which supports v2, could this error be about that? I'm also sharing my logs, can provide more info if needed. Many thanks
    Copy code
    I, [2022-10-18T22:10:20.628161 #97499]  INFO -- : Running example 'Verifying a pact between sms-consumer and sms-provider Given a user was created a create event has matching content'
    I, [2022-10-18T22:10:20.629846 #97499]  INFO -- : Sending POST request to path: "/" with headers: {"CONTENT_TYPE"=>"application/json", "HTTP_X_PACT_ORIGINAL_HEADER_NAMES"=>"Content-Type", "X_PACT_PROVIDER_STATES"=>[{"name"=>"a user was created"}]}, see debug logs for body
    D, [2022-10-18T22:10:20.629899 #97499] DEBUG -- : body :{"description":"a create event","providerStates":[{"name":"a user was created","params":{}}],"metadata":{"Content-Type":"application/json; charset=UTF-8"}}
    I, [2022-10-18T22:10:20.646374 #97499]  INFO -- : Received response with status: 500, headers: {"Date"=>"Tue, 18 Oct 2022 19:10:20 GMT", "Server"=>"uvicorn", "Content-Length"=>"32", "Content-Type"=>"application/json"}, see debug logs for body
    D, [2022-10-18T22:10:20.646463 #97499] DEBUG -- : body: {"detail":"No matched handler."}
    m
    • 2
    • 19
  • n

    Nuno Frias

    11/07/2022, 10:15 AM
    Hi all, I would like to use matchers with messages, but all the examples I see have exact values https://github.com/pact-foundation/pact-python/tree/master/examples/message . Do matchers work with messages ? If so where can I find an example / documentation ?
    m
    • 2
    • 7
  • n

    Nuno Frias

    11/07/2022, 10:15 AM
    Thank you
  • n

    Nuno Frias

    11/08/2022, 11:17 AM
    Hi again In Pact-go for asynchronous interactions there are 2 concepts: state handlers (which set the provider state), and message handlers (which I use to serialize the message into a JSON format). In Pact-python I can only see the equivalent to state handlers. Is there a way for me to have a message handler function? In my case my provider tests is a component wide test (not a unit test) and I need to be able to grab the published message to feed it into pact.
    m
    • 2
    • 2
  • g

    guru raghavendra.r

    11/09/2022, 2:29 PM
    Hi Team, Can someone help me how to mock AWS lambda function using python? I didn't find any example for Lambda contract testing between 2 services. Any information will be really helpful to start with for me. Thank you
    m
    • 2
    • 2
  • j

    Jayesh Guru

    11/17/2022, 12:31 AM
    Hello Team, I am using Pact Matchers to match either integer or None but could not find any matcher to match 2 data types (int or Nonetype)
    Copy code
    .will_respond_with(
            status=200,
            body={
                "id": Like(1|None)
            }
    )
    m
    s
    • 3
    • 6
  • a

    Alan Zhu

    11/23/2022, 2:34 AM
    Hey, team I raise an issue for pact-python because the pact binaries(version 1.88.83) combined in pact-python is incompatible with dev env and CI pipeline. I always got the same error response when I try to verify my pact. But when I use the latest binaries version(1.91.0). The issue won't exist. So are we going to upgrade the version combined in pact-python or is there a suitable way I can config the version that pact-python will call? That's will be much helpful for my implementation. Thanks!
    b
    m
    • 3
    • 17
  • m

    Mohit Soni

    11/24/2022, 10:27 AM
    Hello, I am trying to add a simple test case using pact python, but I am getting error "ImportError while importing test module" (Python version - 3.7.13). Can anyone please help me.
  • m

    Mohit Soni

    11/24/2022, 10:28 AM
    Error Log:
  • m

    Mohit Soni

    11/24/2022, 10:28 AM
    Traceback:
    ../../../.pyenv/versions/3.7.13/lib/python3.7/importlib/__init__.py127 in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    tests/pactflow_contract_testing/pact_consumer_api_token.py4 in <module>
    from pact import Consumer, Provider
    .venv/lib/python3.7/site-packages/pact/__init__.py3 in <module>
    from .consumer import Consumer
    .venv/lib/python3.7/site-packages/pact/consumer.py2 in <module>
    from .pact import Pact
    .venv/lib/python3.7/site-packages/pact/pact.py8 in <module>
    import psutil
    .venv/lib/python3.7/site-packages/psutil/__init__.py123 in <module>
    from . import _psosx as _psplatform
    .venv/lib/python3.7/site-packages/psutil/_psosx.py13 in <module>
    from . import _psposix
    .venv/lib/python3.7/site-packages/psutil/_psposix.py28 in <module>
    from . import _psutil_osx
    E ImportError: dlopen(/<PATH>.venv/lib/python3.7/site-packages/psutil/_psutil_osx.cpython-37m-darwin.so, 0x0002): tried: '/.venv/lib/python3.7/site-packages/psutil/_psutil_osx.cpython-37m-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
  • m

    Mohit Soni

    11/24/2022, 10:28 AM
    Follow this link for sample test - https://docs.pact.io/implementation_guides/python/readme
  • m

    Matt (pactflow.io / pact-js / pact-go)

    11/24/2022, 12:55 PM
    Looks like an M1 issue. Have you tried running in Rosetta mode?
  • m

    Mohit Soni

    11/24/2022, 12:58 PM
    Let me try Rosetta mode. Thanks
  • m

    Mohit Soni

    11/24/2022, 1:08 PM
    Hi Matt, Due to IT policy I can't check in rosetta mode. Do you have any other ways to resolve this issue.
  • m

    Matt (pactflow.io / pact-js / pact-go)

    11/24/2022, 2:04 PM
    If you remove Pact from the project do you still have an issue? The error seems unrelated but I'm pretty sure it will need to run under Rosetta with Pact
  • m

    Matt (pactflow.io / pact-js / pact-go)

    11/24/2022, 2:05 PM
    It might be that Pact needs an os specific package
  • y

    Yousaf Nabi (pactflow.io)

    11/25/2022, 4:14 PM
    Hello hello, Calling all Pact Pythonista's. Have you been itching for • Proper messaging support in Pact-Python • Support for up to V4 of the Pact specification • Pact Plugin support You can check out some of the updates in this tracking issue https://github.com/pact-foundation/pact-python/issues/88#issuecomment-1327645996 We would love your help, guidance and assistance in unlocking these capabilities to the masses but also being able to take the time to address some of the things about Pact-Python that you may have wanted to improve during your time using it!
    ❤️ 1
  • t

    Tony Nguyen

    12/05/2022, 7:58 PM
    Hi team, similar to adding verbose and disable ssl for the standalone in Python, how do we add proxy setting to the standalone Ruby? Example of the os env for verbose and ssl:
    Copy code
    os.environ['PACT_DISABLE_SSL_VERIFICATION'] = 'true'
    os.environ['VERBOSE'] = 'true'
  • m

    Matt (pactflow.io / pact-js / pact-go)

    12/05/2022, 10:07 PM
    The Ruby app should respect the standard proxy environment variables (
    http_proxy
    ,
    https_proxy
    and
    no_proxy
    )
  • j

    Jayesh Guru

    12/07/2022, 6:07 PM
    I have a use case to write contract test for file upload endpoint where we use MultiPartEncoder to upload a file but it looks pact does not accept Multipart Encoder request body, Do we have alternative solution in pact-python
    m
    • 2
    • 4
  • g

    GitHub

    12/07/2022, 8:21 PM
    The GitHub user bethesque has signed out. Use the following command/s to recreate subscriptions /github subscribe pact-foundation/pact-python
  • b

    Beth (pactflow.io/Pact Broker/pact-ruby)

    12/08/2022, 12:34 AM
    /github subscribe pact-foundation/pact-python
  • n

    Nuno Frias

    12/08/2022, 4:43 PM
    Hi all, Any chance some higher power could review and if happy, merge https://github.com/pact-foundation/pact-python/pull/322
    👍 1
    m
    • 2
    • 8
  • t

    Tomasz Rdzak

    12/09/2022, 12:02 PM
    👋 Hello, team! I’ve been trying hard to find an example of how to set up a Mock Service Provider to run pacts verification process without running a server in Python. I’m currently using Pactman in Consumer side, but open to change. Please point me to a good resource on how to verify pacts in unit tests. Thanks!
    👋 1
    m
    • 2
    • 10
  • a

    Aaron Swerlein

    01/06/2023, 4:18 PM
    Hey everyone. I am trying to implement provider side checks in a separate repo from the consumer side, and I am having trouble finding the pact on pactflow.
    • 1
    • 1
1234567Latest