From eb06dca9cbf542bdfa824ffa7cc646a576d07c14 Mon Sep 17 00:00:00 2001 From: Wanda Carlson <87515018+wanda-carlson@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:49:36 -0800 Subject: [PATCH] Create mock.py --- mock.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 mock.py diff --git a/mock.py b/mock.py new file mode 100644 index 0000000..f89b7e7 --- /dev/null +++ b/mock.py @@ -0,0 +1,31 @@ +# mock_script.py + +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() + else: + return {"error": f"Failed to fetch data. Status code: {response.status_code}"} + +if __name__ == "__main__": + 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 + + result = fetch_data_from_api("https://mockapi.test/data") + + assert result == mock_response + print("Test passed: API data mocked successfully")