forked from wasmerio/Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_handler.py
More file actions
37 lines (31 loc) · 1.12 KB
/
Copy pathtest_api_handler.py
File metadata and controls
37 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))
import unittest
from unittest.mock import patch
from src.api_handler import get_exchange_data
class TestAPIHandler(unittest.TestCase):
"""
Unit tests for the get_exchange_data function in api_handler.py.
"""
@patch("api_handler.requests.get")
def test_get_exchange_data_success(self, mock_get):
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {
"base": "EUR",
"date": "2025-06-02",
"rates": {"USD": 1.14, "GBP": 0.84}
}
data = get_exchange_data()
self.assertEqual(data["base"], "EUR")
self.assertIn("USD", data["rates"])
@patch("api_handler.requests.get")
def test_get_exchange_data_failure(self, mock_get):
"""
Test that get_exchange_data raises an exception when API response fails.
"""
mock_get.return_value.status_code = 404
with self.assertRaises(Exception):
get_exchange_data()
if __name__ == "__main__":
unittest.main()