From 6d03c853beffc9b77dd46e53f809c46039dded57 Mon Sep 17 00:00:00 2001 From: rbadzhanov Date: Tue, 10 Sep 2024 14:27:14 +0200 Subject: [PATCH 1/2] Add change to create PR --- echo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/echo.py b/echo.py index 1c58b3a..9d0483f 100644 --- a/echo.py +++ b/echo.py @@ -11,6 +11,7 @@ def main(): args = sys.argv[-1] resp = requests.get(args) console.print(Markdown(resp.text)) + print("very important change") if __name__ == "__main__": From 5320a423ada7e70773c1f2473c5ec736bf95eaf8 Mon Sep 17 00:00:00 2001 From: rbadzhanov Date: Tue, 10 Sep 2024 14:44:51 +0200 Subject: [PATCH 2/2] Add async request with tests --- echo.py | 34 ++++++++++++++++++++++++++++------ test_echo.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 test_echo.py diff --git a/echo.py b/echo.py index 9d0483f..14990ab 100644 --- a/echo.py +++ b/echo.py @@ -1,18 +1,40 @@ import sys import requests +import asyncio from rich.console import Console from rich.markdown import Markdown console = Console() -def main(): - args = sys.argv[-1] - resp = requests.get(args) - console.print(Markdown(resp.text)) - print("very important change") + +async def fetch_content_async(url): + loop = asyncio.get_event_loop() + response = await loop.run_in_executor(None, requests.get, url) + return response + + +def fetch_content_sync(url): + response = requests.get(url) + return response + + +async def main(): + if len(sys.argv) < 2: + console.print("Error: URL is missing!") + return + + url = sys.argv[-1] + + # sync request + response_sync = fetch_content_sync(url) + console.print(Markdown(response_sync.text)) + + # async + response_async = await fetch_content_async(url) + console.print(Markdown(response_async.text)) if __name__ == "__main__": - main() + asyncio.run(main()) \ No newline at end of file diff --git a/test_echo.py b/test_echo.py new file mode 100644 index 0000000..4ac4a7f --- /dev/null +++ b/test_echo.py @@ -0,0 +1,36 @@ +import unittest +from unittest.mock import patch, MagicMock +import asyncio +from echo import fetch_content_sync, fetch_content_async + + +class TestMain(unittest.TestCase): + + @patch('requests.get') + def test_fetch_content_sync(self, mock_get): + mock_response = MagicMock() + mock_response.text = "# Mocked Markdown Response" + mock_get.return_value = mock_response + + url = "https://example.com" + response = fetch_content_sync(url) + + mock_get.assert_called_with(url) + self.assertEqual(response.text, "# Mocked Markdown Response") + + @patch('requests.get') + def test_fetch_content_async(self, mock_get): + mock_response = MagicMock() + mock_response.text = "# Mocked Async Markdown Response" + mock_get.return_value = mock_response + + url = "https://example.com" + + response = asyncio.run(fetch_content_async(url)) + + mock_get.assert_called_with(url) + self.assertEqual(response.text, "# Mocked Async Markdown Response") + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file