From c2567174efff6ab13996592036d303f7fab2f65b Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Mon, 18 Nov 2024 16:53:16 +0000 Subject: [PATCH] test: add ruby/rust impl based DELETE reqs with body --- src/consumer.py | 10 ++++++++++ tests/consumer/test_products_consumer.py | 15 +++++++++++++++ tests/consumer/test_products_consumer_v3.py | 19 ++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/consumer.py b/src/consumer.py index 4fba594..e72f7c8 100644 --- a/src/consumer.py +++ b/src/consumer.py @@ -14,6 +14,16 @@ def get_product(self, id): json = response.json() return Product(json['id'], json['type'], json['name']) + def delete_product(self, id): + """Delete product by ID""" + uri = self.base_uri + '/product/' + id + response = requests.delete(uri, json={'id': id}) + if response.status_code == 404: + return None + + status_code = response.status_code + return status_code + class Product(object): def __init__(self, id, type, name ): diff --git a/tests/consumer/test_products_consumer.py b/tests/consumer/test_products_consumer.py index 2b50ae3..d95497b 100644 --- a/tests/consumer/test_products_consumer.py +++ b/tests/consumer/test_products_consumer.py @@ -54,3 +54,18 @@ def test_get_product(pact, consumer): with pact: user = consumer.get_product('10') assert user.name == 'Margharita' + +def test_delete_product_with_body(pact, consumer): + expected = { + 'id': "27" + } + + (pact + .given('a product with ID 10 exists') + .upon_receiving('a request to delete a product') + .with_request('DELETE', '/product/10', headers={'Content-Type': 'application/json'}, body=Like(expected)) + .will_respond_with(204)) + + with pact: + response = consumer.delete_product('10') + assert response == 204 diff --git a/tests/consumer/test_products_consumer_v3.py b/tests/consumer/test_products_consumer_v3.py index 64d3d91..1fc628f 100644 --- a/tests/consumer/test_products_consumer_v3.py +++ b/tests/consumer/test_products_consumer_v3.py @@ -11,7 +11,7 @@ log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) -@pytest.fixture(scope='session') +@pytest.fixture def pact() -> Generator[Pact, None, None]: pact = Pact("pactflow-example-consumer-python-v3", "pactflow-example-provider-python-v3") yield pact.with_specification("V4") @@ -35,3 +35,20 @@ def test_get_product(pact) -> None: consumer = ProductConsumer(str(srv.url)) user = consumer.get_product('10') assert user.name == 'Margharita' + +def test_delete_product_with_body(pact) -> None: + expected = { + 'id': "27" + } + + (pact + .upon_receiving('a request to delete a product') + .given('a product with ID 10 exists') + .with_request(method='DELETE', path='/product/10') + .with_body(like(expected)) + .will_respond_with(204)) + + with pact.serve() as srv: + consumer = ProductConsumer(str(srv.url)) + response = consumer.delete_product('10') + assert response == 204