<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

from pyowm.weatherapi25.one_call import OneCall
from pyowm.weatherapi25.weather import Weather


class TestWeather(unittest.TestCase):

    def test_one_call_from_dic(self):
        result = OneCall.from_dict(self.__test_data_bozen)
        self.assertTrue(isinstance(result, OneCall))
        self.assertEqual(46.49, result.lat)
        self.assertEqual(11.33, result.lon)
        self.assertEqual("Europe/Rome", result.timezone)
        self.assertTrue(isinstance(result.current, Weather))
        self.assertEqual(1587744158, result.current.reference_time())
        self.assertEqual(48, len(result.forecast_hourly))
        dt_hourly = 1587744000
        for i, weather in enumerate(result.forecast_hourly):
            self.assertTrue(isinstance(weather, Weather), f"entry {i} of forecast_hourly is invalid")
            self.assertEqual(dt_hourly, weather.reference_time())
            dt_hourly += 3600
        self.assertEqual(8, len(result.forecast_daily))
        dt_daily = 1587726000
        for i, weather in enumerate(result.forecast_daily):
            self.assertTrue(isinstance(weather, Weather), f"entry {i} of forecast_hourly is invalid")
            self.assertEqual(dt_daily, weather.reference_time())
            dt_daily += 86400

    def test_one_call_historical_from_dic(self):
        result = OneCall.from_dict(self.__test_data_hostorical_bozen)
        self.assertTrue(isinstance(result, OneCall))
        self.assertEqual(46.49, result.lat)
        self.assertEqual(11.33, result.lon)
        self.assertEqual("Europe/Rome", result.timezone)
        self.assertTrue(isinstance(result.current, Weather))
        self.assertEqual(1587686400, result.current.reference_time())
        self.assertEqual(11, len(result.forecast_hourly))
        dt_hourly = 1587686400
        for i, weather in enumerate(result.forecast_hourly):
            self.assertTrue(isinstance(weather, Weather), f"entry {i} of forecast_hourly is invalid")
            self.assertEqual(dt_hourly, weather.reference_time())
            dt_hourly += 3600
        self.assertIsNone(result.forecast_daily)

    __test_data_bozen = {
        "lat": 46.49,
        "lon": 11.33,
        "timezone": "Europe/Rome",
        "current": {
            "dt": 1587744158,
            "sunrise": 1587701469,
            "sunset": 1587752042,
            "temp": 295.34,
            "feels_like": 291.46,<commit_msg>add one call error tests<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

from pyowm.commons.exceptions import ParseAPIResponseError, APIResponseError
from pyowm.weatherapi25.one_call import OneCall
from pyowm.weatherapi25.weather import Weather


class TestWeather(unittest.TestCase):

    def test_one_call_from_dic(self):
        result = OneCall.from_dict(self.__test_data_bozen)
        self.assertTrue(isinstance(result, OneCall))
        self.assertEqual(46.49, result.lat)
        self.assertEqual(11.33, result.lon)
        self.assertEqual("Europe/Rome", result.timezone)
        self.assertTrue(isinstance(result.current, Weather))
        self.assertEqual(1587744158, result.current.reference_time())
        self.assertEqual(48, len(result.forecast_hourly))
        dt_hourly = 1587744000
        for i, weather in enumerate(result.forecast_hourly):
            self.assertTrue(isinstance(weather, Weather), f"entry {i} of forecast_hourly is invalid")
            self.assertEqual(dt_hourly, weather.reference_time())
            dt_hourly += 3600
        self.assertEqual(8, len(result.forecast_daily))
        dt_daily = 1587726000
        for i, weather in enumerate(result.forecast_daily):
            self.assertTrue(isinstance(weather, Weather), f"entry {i} of forecast_hourly is invalid")
            self.assertEqual(dt_daily, weather.reference_time())
            dt_daily += 86400

    def test_one_call_historical_from_dic(self):
        result = OneCall.from_dict(self.__test_data_hostorical_bozen)
        self.assertTrue(isinstance(result, OneCall))
        self.assertEqual(46.49, result.lat)
        self.assertEqual(11.33, result.lon)
        self.assertEqual("Europe/Rome", result.timezone)
        self.assertTrue(isinstance(result.current, Weather))
        self.assertEqual(1587686400, result.current.reference_time())
        self.assertEqual(11, len(result.forecast_hourly))
        dt_hourly = 1587686400
        for i, weather in enumerate(result.forecast_hourly):
            self.assertTrue(isinstance(weather, Weather), f"entry {i} of forecast_hourly is invalid")
            self.assertEqual(dt_hourly, weather.reference_time())
            dt_hourly += 3600
        self.assertIsNone(result.forecast_daily)

    def test_one_call_current_none(self):
        self.assertRaises(ValueError, lambda: OneCall(46.49, 11.33, None, None, None, None))

    def test_one_call_from_dic_none(self):
        self.assertRaises(ParseAPIResponseError, lambda: OneCall.from_dict(None))

    def test_one_call_from_dict_error_400(self):
        data = {
            "cod": "400",
            "message": "Nothing to geocode"
        }
        self.assertRaises(APIResponseError, lambda: OneCall.from_dict(data))

    def test_one_call_from_dict_error_404(self):
        data = {
            "cod": "404",
            "message": "Not found"
        }
        self.assertIsNone(OneCall.from_dict(data))

    def test_one_call_from_dict_error_429(self):
        data = {
            "cod": 429,
            "message": "Your account is temporary blocked due to exceeding of requests limitation "
                       "of your subscription type. Please choose the proper subscription "
                       "http://openweathermap.org/price"
        }
        self.assertRaises(APIResponseError, lambda: OneCall.from_dict(data))

    __test_data_bozen = {
        "lat": 46.49,
        "lon": 11.33,
        "timezone": "Europe/Rome",
        "current": {
            "dt": 1587744158,
            "sunrise": 1587701469,
            "sunset": 1587752042,
            "temp": 295.34,
            "feels_like": 291.46,
{'fraction_non_alphanumeric': 0.10646264040371155, 'fraction_numerical': 0.04883607357968419, 'mean_word_length': 2.8690176322418135, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2259878', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1857, 'n_words': 334}