Skip to content

Commit

Permalink
refactor: standardize unsatisfied/unexpected
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 23, 2024
1 parent 6825e03 commit 22e7e7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
15 changes: 8 additions & 7 deletions http_request_recorder/http_request_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ async def __aexit__(self, *args, **kwargs):

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()]

def unexpected_requests(self) -> list[RecordedRequest]:
return self._unexpected_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 Expand Up @@ -183,6 +176,14 @@ def matcher(request):
name=f"XmlRpc: {in_body.decode('UTF-8')}",
timeout=timeout)

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()]

def unexpected_requests(self) -> list[RecordedRequest]:
"""Usage in unittest: `self.assertListEqual([], a_recorder._unexpected_requests())`"""
return self._unexpected_requests

@staticmethod
async def _request_string_for_log(request):
request_body = await request.read()
Expand Down
18 changes: 5 additions & 13 deletions tests/test_http_request_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ async def test_successful_response_logs_debug_message(self):
self.assertIn(request_path, logs[0])
self.assertIn("PUT", logs[0])

async def test_log_warning_for_unrequested_expected_request(self):
async def test_handle_unsatisfied_expectations(self):
with self.assertLogs("recorder", level=logging.INFO) as log_recorder:
logging.getLogger("recorder").addHandler(logging.StreamHandler()) # also output logging

request_paths = ["/never_gets_called", "/neither"]
async with HttpRequestRecorder(name="disappointed recorder", port=self.port) as recorder:

for path in request_paths:
recorder.expect_path(path=path, responses="unused response")
# no request is sent.

with self.subTest("logs warning"):
logs = log_recorder.records
self.assertEqual(1, len(logs))

Expand All @@ -136,17 +136,9 @@ async def test_log_warning_for_unrequested_expected_request(self):
for path in request_paths:
self.assertIn(path, record.msg)

async def test_provide_unsatisfied_expectations(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)
with self.subTest("provides unsatisfied expectations"):
unsatisfied = {e.name for e in recorder.unsatisfied_expectations()}
self.assertSetEqual({"/never_gets_called", "/neither"}, unsatisfied)

async def test_handle_unexpected_requests(self):
with self.assertLogs("recorder", level=logging.INFO) as log_recorder:
Expand Down

0 comments on commit 22e7e7e

Please sign in to comment.