Skip to content

Commit

Permalink
test: add ruby/rust impl based DELETE reqs with body
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Nov 18, 2024
1 parent 4a898b6 commit c256717
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ):
Expand Down
15 changes: 15 additions & 0 deletions tests/consumer/test_products_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 18 additions & 1 deletion tests/consumer/test_products_consumer_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

0 comments on commit c256717

Please sign in to comment.