From 22e7e7ecf7d5945291e3c171e7c9bdedc4d2d0ed Mon Sep 17 00:00:00 2001 From: Tobias Beckmann Date: Thu, 23 May 2024 15:23:22 +0200 Subject: [PATCH] refactor: standardize unsatisfied/unexpected Co-authored-by: Andreas Schimmelschulze --- http_request_recorder/http_request_recorder.py | 15 ++++++++------- tests/test_http_request_recorder.py | 18 +++++------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/http_request_recorder/http_request_recorder.py b/http_request_recorder/http_request_recorder.py index cf7ebe8..050e62e 100644 --- a/http_request_recorder/http_request_recorder.py +++ b/http_request_recorder/http_request_recorder.py @@ -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)}") @@ -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() diff --git a/tests/test_http_request_recorder.py b/tests/test_http_request_recorder.py index a6ea8c9..9ea6ae1 100644 --- a/tests/test_http_request_recorder.py +++ b/tests/test_http_request_recorder.py @@ -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)) @@ -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: