-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import requests | ||
import argparse | ||
|
||
def fetch_data(url): | ||
""" | ||
Fetches data from the given URL and returns it as JSON. | ||
""" | ||
try: | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.exceptions.RequestException as e: | ||
print(f"An error occurred: {e}") | ||
return None | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Fetch JSON data from a specified URL.") | ||
parser.add_argument('-u', '--url', type=str, required=True, help="The URL to fetch data from") | ||
|
||
args = parser.parse_args() | ||
url = args.url | ||
|
||
data = fetch_data(url) | ||
print(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
import requests | ||
from fetch_data import fetch_data | ||
|
||
class TestFetchData(unittest.TestCase): | ||
|
||
@patch('requests.get') | ||
def test_fetch_data_success(self, mock_get): | ||
mock_response = unittest.mock.Mock() | ||
mock_response.raise_for_status.return_value = None # Simulate no HTTP error | ||
mock_response.json.return_value = {"userId": 1, "id": 1, "title": "Test Post", "body": "This is a test."} | ||
mock_get.return_value = mock_response | ||
|
||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
result = fetch_data(url) | ||
|
||
self.assertEqual(result, {"userId": 1, "id": 1, "title": "Test Post", "body": "This is a test."}) | ||
|
||
@patch('requests.get') | ||
def test_fetch_data_http_error(self, mock_get): | ||
mock_response = unittest.mock.Mock() | ||
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Not Found") | ||
mock_get.return_value = mock_response | ||
|
||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
result = fetch_data(url) | ||
|
||
self.assertIsNone(result) | ||
|
||
@patch('requests.get') | ||
def test_fetch_data_request_exception(self, mock_get): | ||
mock_get.side_effect = requests.exceptions.RequestException("Network error") | ||
|
||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
result = fetch_data(url) | ||
|
||
self.assertIsNone(result) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |