Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# mock_script.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import requests
from unittest.mock import patch

def fetch_data_from_api(url):
response = requests.get(url)
if response.status_code == 200:
return response.json()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else:
return {"error": f"Failed to fetch data. Status code: {response.status_code}"}

if __name__ == "__main__":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url = "https://api.example.com/data"
print("Fetching data from API...")
data = fetch_data_from_api(url)
print(data)

# Unit test section (mock example)

def test_fetch_data_from_api():
mock_response = {"id": 1, "name": "Mock Item", "value": 100}

with patch('requests.get') as mocked_get:
mocked_get.return_value.status_code = 200
mocked_get.return_value.json.return_value = mock_response

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


result = fetch_data_from_api("https://mockapi.test/data")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert result == mock_response
print("Test passed: API data mocked successfully")