| 1 |
import os |
| 2 |
import unittest |
| 3 |
from urllib.error import HTTPError |
| 4 |
from urllib.request import Request |
| 5 |
from meteoclimatic.exceptions import StationNotFound, MeteoclimaticError |
| 6 |
from meteoclimatic import MeteoclimaticClient |
| 7 |
from meteoclimatic import __version__ |
| 8 |
from unittest.mock import patch, MagicMock |
| 9 |
|
| 10 |
|
| 11 |
class TestMeteoclimaticClient(unittest.TestCase): |
| 12 |
|
| 13 |
def setUp(self): |
| 14 |
self.client = MeteoclimaticClient() |
| 15 |
|
| 16 |
@patch('meteoclimatic.client.urlopen', autospec=True) |
| 17 |
def test_get_station_info_ok(self, mock_urlopen): |
| 18 |
f = open(os.path.join(os.path.dirname( |
| 19 |
__file__), "feeds", "full_station.xml")) |
| 20 |
mock_urlopen.return_value.read.return_value = f |
| 21 |
|
| 22 |
res = self.client.weather_at_station("ESCAT4300000043206B") |
| 23 |
|
| 24 |
|
| 25 |
self.assertEqual(mock_urlopen.call_count, 1) |
| 26 |
called_request = mock_urlopen.call_args[0][0] |
| 27 |
self.assertIsInstance(called_request, Request) |
| 28 |
self.assertEqual(called_request.full_url, |
| 29 |
"https://www.meteoclimatic.net/feed/rss/ESCAT4300000043206B") |
| 30 |
self.assertEqual(called_request.get_header("User-agent"), |
| 31 |
f"pymeteoclimatic/{__version__}") |
| 32 |
self.assertEqual(res.station.code, "ESCAT4300000043206B") |
| 33 |
|
| 34 |
@patch('meteoclimatic.client.urlopen', autospec=True) |
| 35 |
def test_get_station_info_no_xml(self, mock_urlopen): |
| 36 |
mock_urlopen.return_value.read.return_value = "" |
| 37 |
|
| 38 |
with self.assertRaises(StationNotFound) as error: |
| 39 |
self.client.weather_at_station("ESCAT4300000043206B") |
| 40 |
self.assertEqual(error.exception.station_code, "ESCAT4300000043206B") |
| 41 |
self.assertEqual(str( |
| 42 |
error.exception), "Station code ESCAT4300000043206B did not return any item") |
| 43 |
|
| 44 |
@patch('meteoclimatic.client.urlopen', autospec=True) |
| 45 |
def test_get_station_info_404(self, mock_urlopen): |
| 46 |
mock_urlopen.side_effect = HTTPError("", 404, "Not Found", [], None) |
| 47 |
|
| 48 |
with self.assertRaises(MeteoclimaticError) as error: |
| 49 |
self.client.weather_at_station("ESCAT4300000043206B") |
| 50 |
self.assertEqual(str( |
| 51 |
error.exception), "Error fetching station data [status_code=404]") |
| 52 |
|
| 53 |
@patch('meteoclimatic.client.urlopen', autospec=True) |
| 54 |
def test_user_agent_header_is_set(self, mock_urlopen): |
| 55 |
"""Test that the User-Agent header is correctly set with version""" |
| 56 |
mock_response = MagicMock() |
| 57 |
mock_response.read.return_value = open(os.path.join( |
| 58 |
os.path.dirname(__file__), "feeds", "full_station.xml")).read() |
| 59 |
mock_urlopen.return_value = mock_response |
| 60 |
|
| 61 |
self.client.weather_at_station("ESCAT4300000043206B") |
| 62 |
|
| 63 |
|
| 64 |
called_request = mock_urlopen.call_args[0][0] |
| 65 |
self.assertIsInstance(called_request, Request) |
| 66 |
user_agent = called_request.get_header("User-agent") |
| 67 |
self.assertEqual(user_agent, f"pymeteoclimatic/{__version__}") |
| 68 |
|
| 69 |
self.assertTrue(user_agent.startswith("pymeteoclimatic/")) |