Skip to content

Commit

Permalink
feat: expose unsatisfied_expectations()
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Schimmelschulze <[email protected]>
  • Loading branch information
becktob and andreas-sipgate committed May 21, 2024
1 parent ffa8302 commit e008a02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 7 additions & 3 deletions http_request_recorder/http_request_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,16 @@ async def __aenter__(self):
return self

async def __aexit__(self, *args, **kwargs):
still_expecting = [exp for exp in self._expectations if exp.is_still_expecting_requests()]
if len(still_expecting) > 0:
self._logger.warning(f"{self} is exiting but there are unsatisfied Expectations: {still_expecting}")
if len(self.unsatisfied_expectations()) > 0:
self._logger.warning(
f"{self} is exiting but there are unsatisfied Expectations: {self.unsatisfied_expectations()}")

await self.runner.cleanup()

def unsatisfied_expectations(self) -> list[ExpectedInteraction]:
"""Usage in unittest: `self.assertListEqual([], a_recorder.unsatisfied_expectations())`"""
return [exp for exp in self._expectations if exp.is_still_expecting_requests()]

async def handle_request(self, request: BaseRequest):
request_body = await request.read()
self._logger.info(f"{self} got {await self._request_string_for_log(request)}")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='http_request_recorder',
version='0.2.2',
version='0.3.0',
description='A package to record an respond to http requests, primarily for use in black box testing.',
long_description=readme,
author='',
Expand Down
12 changes: 12 additions & 0 deletions tests/test_http_request_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ async def test_log_warning_for_unrequested_expected_request(self):
for path in request_paths:
self.assertIn(path, record.msg)

async def test_provide_unrequested_expected_requests(self):
expected_paths = ["/called", "/never_gets_called", "/neither"]
async with (HttpRequestRecorder(name="disappointed recorder", port=self.port) as recorder,
ClientSession() as http_session):
for path in expected_paths:
recorder.expect_path(path=path, responses="unused response")

await http_session.get(f"http://localhost:{self.port}/called")

unsatisfied = {e.name for e in recorder.unsatisfied_expectations()}
self.assertSetEqual({"/never_gets_called", "/neither"}, unsatisfied)

async def test_should_handle_late_request(self):
async with HttpRequestRecorder(name="patient recorder", port=self.port) as recorder, ClientSession() as http_session:
expectation = recorder.expect_path(path='/called-late', responses="response")
Expand Down

0 comments on commit e008a02

Please sign in to comment.