<reponame>lajanki/alarmpi
import requests
from unittest.mock import patch, mock_open

from src.handlers import get_weather, get_next_trains



def test_failed_train_api_request():
    """Does get_next_trains.run return None if API call fails"""
    parser = get_next_trains.TrainParser({"station_code": "XYZ"})

    # Error raised during request
    with patch("requests.get") as mock_get:
        mock_get.side_effect = requests.exceptions.RequestException()
        res = parser.run()
        assert res is None

    # Invalid response
    with patch("requests.get") as mock_get:
        mock_get.return_value.status_code = 500
        res = parser.run()
        assert res is None
    
def test_failed_weather_api_request():
    """Does fetch_and_format_weather return None if API call fails"""
    # Mock opening non existing credentials file
    section_data = {"credentials": None, "city_id": None, "units": None}
    with patch("builtins.open", mock_open(read_data="data")) as mock_file:
        parser = get_weather.OpenWeatherMapClient(section_data)

    # Error raised during request
    with patch("requests.get") as mock_get:
        mock_get.side_effect = requests.exceptions.RequestException()
        res = parser.fetch_and_format_weather()
        assert res is None

    # Invalid response
    with patch("requests.get") as mock_get:
        mock_get.return_value.status_code = 500
        res = parser.fetch_and_format_weather()
        assert res is None
import requests
from unittest.mock import patch, mock_open

from src.handlers import get_weather, get_next_trains



def test_failed_train_api_request():
    """Does get_next_trains.run return None if API call fails"""
    parser = get_next_trains.TrainParser({"station_code": "XYZ"})

    # Error raised during request
    with patch("requests.get") as mock_get:
        mock_get.side_effect = requests.exceptions.RequestException()
        res = parser.run()
        assert res is None

    # Invalid response
    with patch("requests.get") as mock_get:
        mock_get.return_value.status_code = 500
        res = parser.run()
        assert res is None
    
def test_failed_weather_api_request():
    """Does fetch_and_format_weather return None if API call fails"""
    # Mock opening non existing credentials file
    section_data = {"credentials": None, "city_id": None, "units": None}
    with patch("builtins.open", mock_open(read_data="data")) as mock_file:
        parser = get_weather.OpenWeatherMapClient(section_data)

    # Error raised during request
    with patch("requests.get") as mock_get:
        mock_get.side_effect = requests.exceptions.RequestException()
        res = parser.fetch_and_format_weather()
        assert res is None

    # Invalid response
    with patch("requests.get") as mock_get:
        mock_get.return_value.status_code = 500
        res = parser.fetch_and_format_weather()
        assert res is None
Does get_next_trains.run return None if API call fails # Error raised during request # Invalid response Does fetch_and_format_weather return None if API call fails # Mock opening non existing credentials file # Error raised during request # Invalid response