Пример #1
0
    def test_geopoints_for(self):
        ref_to_original = CityIDRegistry._get_lines
        CityIDRegistry._get_lines = self._mock_get_lines_with_homonymies

        # No matches
        result = self._instance.geopoints_for('aaaaaaaaaa')
        self.assertEqual(0, len(result))

        # One match
        expected = Location('Bologna', -83.250488, 30.57184, 2829449,
                            'IT').to_geopoint()
        result = self._instance.geopoints_for("Bologna")
        self.assertEqual(1, len(result))
        self._assertGeopointsEqual(expected, result[0])

        # Multiple matches
        expected1 = Location('Abbans-Dessus', 5.88188, 47.120548, 3038800,
                             'FR').to_geopoint()
        expected2 = Location('Abbans-Dessus', 5.88333, 47.116669, 6452202,
                             'FR').to_geopoint()
        result = self._instance.geopoints_for("Abbans-Dessus")
        self.assertEqual(2, len(result))
        self._assertGeopointsEqual(expected1, result[0])
        self._assertGeopointsEqual(expected2, result[1])

        CityIDRegistry._get_lines = ref_to_original
Пример #2
0
    def test_locations_for_restricted_to_country(self):
        ref_to_original = CityIDRegistry._get_lines
        CityIDRegistry._get_lines = self._mock_get_lines_with_homonymies

        result = self._instance.locations_for("Abbeville", country='JP')
        self.assertEquals(0, len(result))

        result = self._instance.locations_for("Abbeville", country='US')
        self.assertEquals(4, len(result))
        self._assertLocationsEqual(
            Location('Abbeville', -83.306824, 31.992121, 4178992, 'US'),
            result[0])
        self._assertLocationsEqual(
            Location('Abbeville', -92.134293, 29.974649, 4314295, 'US'),
            result[1])
        self._assertLocationsEqual(
            Location('Abbeville', -82.379013, 34.178169, 4568985, 'US'),
            result[2])
        self._assertLocationsEqual(
            Location('Abbeville', -85.250488, 31.57184, 4829449, 'US'),
            result[3])

        result = self._instance.locations_for("Abbeville", country='FR')
        self.assertEquals(1, len(result))
        self._assertLocationsEqual(
            Location("Abbeville", 1.83333, 50.099998, 3038789, 'FR'),
            result[0])

        CityIDRegistry._get_lines = ref_to_original
Пример #3
0
 def locations_for(self, city_name, country=None, matching='nocase'):
     """
     Returns a list of Location objects corresponding to
     the int IDs and relative toponyms and 2-chars country of the cities
     matching the provided city name.
     The rule for identifying matchings is according to the provided
     `matching` parameter value.
     If `country` is provided, the search is restricted to the cities of
     the specified country.
     :param country: two character str representing the country where to
     search for the city. Defaults to `None`, which means: search in all
     countries.
     :param matching: str among `exact` (literal, case-sensitive matching),
     `nocase` (literal, case-insensitive matching) and `like` (matches cities
     whose name contains as a substring the string fed to the function, no
     matter the case). Defaults to `nocase`.
     :raises ValueError if the value for `matching` is unknown
     :return: list of `webapi25.location.Location` objects
     """
     if not city_name:
         return []
     if matching not in self.MATCHINGS:
         raise ValueError("Unknown type of matching: "
                          "allowed values are %s" %
                          ", ".join(self.MATCHINGS))
     if country is not None and len(country) != 2:
         raise ValueError("Country must be a 2-char string")
     splits = self._filter_matching_lines(city_name, country, matching)
     return [
         Location(item[0], float(item[3]), float(item[2]), int(item[1]),
                  item[4]) for item in splits
     ]
Пример #4
0
    def test_locations_for_matching_criteria(self):
        original_get_lines = CityIDRegistry._get_lines
        original_get_all_lines = CityIDRegistry._get_all_lines
        CityIDRegistry._get_lines = self._mock_get_lines_with_homonymies
        CityIDRegistry._get_all_lines = self._mock_get_all_lines

        # look for an empty name
        result = self._instance.locations_for("")
        self.assertEquals(0, len(result))

        expected = Location('Bologna', -83.250488, 30.57184, 2829449, 'IT')

        # case sensitive
        result = self._instance.locations_for("bologna", matching='exact')
        self.assertEquals(0, len(result))

        result = self._instance.locations_for("Bologna", matching='exact')
        self.assertEquals(1, len(result))
        self._assertLocationsEqual(expected, result[0])

        # case insensitive
        result = self._instance.locations_for("bologna", matching='nocase')
        self.assertEquals(1, len(result))
        self._assertLocationsEqual(expected, result[0])

        result = self._instance.locations_for("Bologna", matching='nocase')
        self.assertEquals(1, len(result))
        self._assertLocationsEqual(expected, result[0])

        # like
        expected1 = Location('Abbans-Dessus', 5.88188, 47.120548, 3038800,
                             'FR')
        expected2 = Location('Abbans-Dessus', 5.88333, 47.116669, 6452202,
                             'FR')

        result = self._instance.locations_for("abbans", matching='like')
        self.assertEquals(2, len(result))
        self._assertLocationsEqual(expected1, result[0])
        self._assertLocationsEqual(expected2, result[1])

        result = self._instance.locations_for("Dessus", matching='like')
        self.assertEquals(2, len(result))
        self._assertLocationsEqual(expected1, result[0])
        self._assertLocationsEqual(expected2, result[1])

        CityIDRegistry._get_lines = original_get_lines
        CityIDRegistry._get_all_lines = original_get_all_lines
class TestObservation(unittest.TestCase):

    __test_reception_time = 1234567
    __test_iso_reception_time = "1970-01-15 06:56:07+00"
    __test_date_reception_time = datetime.strptime(
        __test_iso_reception_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())
    __test_location = Location('test', 12.3, 43.7, 987, 'UK')
    __test_weather = Weather(1378459200, 1378496400, 1378449600, 67,
                             {"all": 20}, {"all": 0}, {
                                 "deg": 252.002,
                                 "speed": 1.100
                             }, 57, {
                                 "press": 1030.119,
                                 "sea_level": 1038.589
                             }, {
                                 "temp": 294.199,
                                 "temp_kf": -1.899,
                                 "temp_max": 296.098,
                                 "temp_min": 294.199
                             }, "Clouds", "Overcast clouds", 804, "04d", 1000,
                             300.0, 298.0, 296.0)
    __test_instance = Observation(__test_reception_time, __test_location,
                                  __test_weather)

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, Observation, -1234567, \
                          self.__test_location, self.__test_weather)

    def test_getters_return_expected_data(self):
        self.assertEqual(self.__test_instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_location(),
                         self.__test_location)
        self.assertEqual(self.__test_instance.get_weather(),
                         self.__test_weather)

    def test_returning_different_formats_for_reception_time(self):
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \
                         self.__test_iso_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='date'), \
                         self.__test_date_reception_time)

    # Test JSON and XML comparisons by ordering strings (this overcomes
    # interpeter-dependant serialization of XML/JSON objects)

    def test_to_JSON(self):
        ordered_base_json = ''.join(sorted(OBSERVATION_JSON_DUMP))
        ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON()))
        self.assertEqual(ordered_base_json, ordered_actual_json)

    def test_to_XML(self):
        ordered_base_xml = ''.join(sorted(OBSERVATION_XML_DUMP))
        ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML()))
        self.assertEqual(ordered_base_xml, ordered_actual_xml)
Пример #6
0
 def test_getters_return_expected_data(self):
     instance = Location(self.__test_name, self.__test_lon, self.__test_lat,
                         self.__test_ID, self.__test_country)
     self.assertEqual(instance.get_name(), self.__test_name)
     self.assertEqual(instance.get_lon(), self.__test_lon)
     self.assertEqual(instance.get_lat(), self.__test_lat)
     self.assertEqual(instance.get_ID(), self.__test_ID)
     self.assertEqual(instance.get_country(), self.__test_country)
Пример #7
0
 def to_geopoint(self):
     loc_1 = Location(self.__test_name, None, self.__test_lat,
                      self.__test_ID, self.__test_country)
     loc_2 = Location(self.__test_name, self.__test_lon, None,
                      self.__test_ID, self.__test_country)
     loc_3 = Location(self.__test_name, self.__test_lon, self.__test_lat,
                      self.__test_ID, self.__test_country)
     self.assertIsNone(loc_1.to_geopoint())
     self.assertIsNone(loc_2.to_geopoint())
     result = loc_3.to_geopoint()
     self.assertTrue(isinstance(result, Point))
     expected_geojson = json.dumps({
         "coordinates": [12.3, 43.7],
         "type": "Point"
     })
     self.assertEqual(sorted(expected_geojson), sorted(result.geojson()))
Пример #8
0
 def test_location_for(self):
     expected = Location('dongen', 4.938890, 51.626671, 2756723, 'NL')
     result = self._instance.location_for('dongen')
     self.assertEqual(result.get_name(), expected.get_name())
     self.assertEqual(result.get_country(), expected.get_country())
     self.assertEqual(result.get_ID(), expected.get_ID())
     self.assertEqual(result.get_lat(), expected.get_lat())
     self.assertEqual(result.get_lon(), expected.get_lon())
     self.assertTrue(self._instance.location_for('aaaaaaaaaa') is None)
Пример #9
0
    def test_locations_for_with_commas_in_city_names(self):
        ref_to_original = CityIDRegistry._get_lines
        CityIDRegistry._get_lines = self._mock_test_file_contents_with_commas_in_names

        result = self._instance.locations_for("Thale, Stadt")
        self.assertEquals(1, len(result))
        self._assertLocationsEqual(
            Location('Thale, Stadt', 11.058, 51.7528, 6550950, 'DE'),
            result[0])

        CityIDRegistry._get_lines = ref_to_original
Пример #10
0
 def test_getters_return_expected_data(self):
     instance = Location(self.__test_name, self.__test_lon, self.__test_lat,
                         self.__test_ID, self.__test_country)
     self.assertEqual(instance.get_name(), self.__test_name)
     self.assertEqual(instance.get_lon(), self.__test_lon)
     self.assertEqual(instance.get_lat(), self.__test_lat)
     self.assertEqual(instance.get_ID(), self.__test_ID)
     self.assertEqual(instance.get_country(), self.__test_country)
Пример #11
0
 def test_location_for(self):
     expected = Location('dongen', 4.938890, 51.626671, 2756723L, 'NL')
     result = self._instance.location_for('dongen')
     self.assertEqual(result.get_name(), expected.get_name())
     self.assertEqual(result.get_country(), expected.get_country())
     self.assertEqual(result.get_ID(), expected.get_ID())
     self.assertEqual(result.get_lat(), expected.get_lat())
     self.assertEqual(result.get_lon(), expected.get_lon())
     self.assertTrue(self._instance.location_for('aaaaaaaaaa') is None)
Пример #12
0
 def test_location_for(self):
     ref_to_original = CityIDRegistry._get_lines
     CityIDRegistry._get_lines = self._mock_get_lines
     expected = Location('dongdu', 117.699997, 35.849998, 1812597, 'CN')
     result_1 = self._instance.location_for('dongdu')
     result_2 = self._instance.location_for('aaaaaaaaaa')
     CityIDRegistry._get_lines = ref_to_original
     self.assertEqual(result_1.get_name(), expected.get_name())
     self.assertEqual(result_1.get_country(), expected.get_country())
     self.assertEqual(result_1.get_ID(), expected.get_ID())
     self.assertEqual(result_1.get_lat(), expected.get_lat())
     self.assertEqual(result_1.get_lon(), expected.get_lon())
     self.assertTrue(result_2 is None)
Пример #13
0
 def test_location_for(self):
     ref_to_original = CityIDRegistry._get_lines
     CityIDRegistry._get_lines = self._mock_get_lines
     expected = Location('dongen', 4.938890, 51.626671, 2756723, 'NL')
     result_1 = self._instance.location_for('dongen')
     result_2 = self._instance.location_for('aaaaaaaaaa')
     CityIDRegistry._get_lines = ref_to_original
     self.assertEqual(result_1.get_name(), expected.get_name())
     self.assertEqual(result_1.get_country(), expected.get_country())
     self.assertEqual(result_1.get_ID(), expected.get_ID())
     self.assertEqual(result_1.get_lat(), expected.get_lat())
     self.assertEqual(result_1.get_lon(), expected.get_lon())
     self.assertTrue(result_2 is None)
Пример #14
0
    def location_for(self, city_name):
        """
        Returns the *Location* object corresponding to the provided city name.

        :param city_name: the city name you want a *Location* for
        :type city_name: str
        :returns: a *Location* instance or ``None`` if the lookup fails

        """
        line = self._lookup_line_by_city_name(city_name)
        if line is None:
            return None
        tokens = line.split(",")
        return Location(tokens[0], float(tokens[3]), float(tokens[2]),
                        int(tokens[1]), 'NL')
Пример #15
0
 def test_locations_for(self):
     expected1 = Location('Abbans-Dessus', 5.88188, 47.120548, 3038800,
                          'FR')
     expected2 = Location('Abbans-Dessus', 5.88333, 47.116669, 6452202,
                          'FR')
     result = self._instance.locations_for("Abbans-Dessus")
     self.assertEquals(2, len(result))
     for l in result:
         self.assertTrue(isinstance(l, Location))
         self.assertTrue(
             l.get_ID() in [expected1.get_ID(),
                            expected2.get_ID()])
Пример #16
0
 def test_location_for(self):
     ref_to_original = CityIDRegistry._get_lines
     CityIDRegistry._get_lines = self._mock_get_lines
     expected = Location('dongen', 4.938890, 51.626671, 2756723, 'NL')
     result_1 = self._instance.location_for('dongen')
     result_2 = self._instance.location_for('aaaaaaaaaa')
     CityIDRegistry._get_lines = ref_to_original        
     self.assertEqual(result_1.get_name(), expected.get_name())
     self.assertEqual(result_1.get_country(), expected.get_country())
     self.assertEqual(result_1.get_ID(), expected.get_ID())
     self.assertEqual(result_1.get_lat(), expected.get_lat())
     self.assertEqual(result_1.get_lon(), expected.get_lon())
     self.assertTrue(result_2 is None)
Пример #17
0
    def location_for(self, city_name):
        """
        Returns the *Location* object corresponding to the first city found
        that matches the provided city name. The lookup is case insensitive.

        :param city_name: the city name you want a *Location* for
        :type city_name: str
        :returns: a *Location* instance or ``None`` if the lookup fails

        .. deprecated:: 3.0.0
           Use :func:`locations_for` instead.


        """
        line = self._lookup_line_by_city_name(city_name)
        if line is None:
            return None
        tokens = line.split(",")
        return Location(tokens[0], float(tokens[3]), float(tokens[2]),
                        int(tokens[1]), tokens[4])
Пример #18
0
class TestObservation(unittest.TestCase):

    __test_reception_time = 1234567
    __test_iso_reception_time = "1970-01-15 06:56:07+00"
    __test_location = Location('test', 12.3, 43.7, 987, 'UK')
    __test_weather = Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20},
            {"all": 0}, {"deg": 252.002, "speed": 1.100}, 57,
            {"press": 1030.119, "sea_level": 1038.589},
            {"temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098,
                "temp_min": 294.199
            },
            "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0)
    __test_instance = Observation(__test_reception_time, __test_location,
                                  __test_weather)

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, Observation, -1234567, \
                          self.__test_location, self.__test_weather)

    def test_getters_return_expected_data(self):
        self.assertEqual(self.__test_instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_location(),
                         self.__test_location)
        self.assertEqual(self.__test_instance.get_weather(),
                         self.__test_weather)

    def test_returning_different_formats_for_reception_time(self):
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \
                         self.__test_iso_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \
                         self.__test_reception_time)

    # Only test to_JSON and to_XML functions when running Python 2.7
    from sys import version_info
    if version_info[0] < 3:
        def test_to_JSON(self):
            self.assertEqual(OBSERVATION_JSON_DUMP, self.__test_instance.to_JSON())

        def test_to_XML(self):
            self.assertEqual(OBSERVATION_XML_DUMP, self.__test_instance.to_XML())
Пример #19
0
class TestUVIndex(unittest.TestCase):

    __test_reception_time = 1475283600
    __test_iso_reception_time = "2016-10-01 01:00:00+00"
    __test_date_reception_time = datetime.strptime(
        __test_iso_reception_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    __test_reference_time = 1234567
    __test_iso_reference_time = "1970-01-15 06:56:07+00"
    __test_date_reference_time = datetime.strptime(
        __test_iso_reference_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    __test_location = Location('test', 12.3, 43.7, 987, 'UK')
    __test_uv_intensity = 6.8
    __test_exposure_risk = 'high'
    __test_instance = UVIndex(__test_reference_time, __test_location,
                              __test_uv_intensity, __test_reception_time)

    def test_init_fails_when_reference_time_is_negative(self):
        self.assertRaises(ValueError, UVIndex, -1234567, self.__test_location,
                          self.__test_uv_intensity, self.__test_reception_time)

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, UVIndex, self.__test_reference_time,
                          self.__test_location, self.__test_uv_intensity,
                          -1234567)

    def test_init_fails_when_uv_intensity_is_negative(self):
        self.assertRaises(ValueError, UVIndex, self.__test_reference_time,
                          self.__test_location, -8.9,
                          self.__test_reception_time)

    def test_getters_return_expected_data(self):
        self.assertEqual(self.__test_instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_reference_time(),
                         self.__test_reference_time)
        self.assertEqual(self.__test_instance.get_location(),
                         self.__test_location)
        self.assertEqual(self.__test_instance.get_value(),
                         self.__test_uv_intensity)
        self.assertEqual(self.__test_instance.get_exposure_risk(),
                         self.__test_exposure_risk)

    def test_returning_different_formats_for_reception_time(self):
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \
                         self.__test_iso_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='date'), \
                         self.__test_date_reception_time)

    def test_returning_different_formats_for_reference_time(self):
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='iso'), \
                         self.__test_iso_reference_time)
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='unix'), \
                         self.__test_reference_time)
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='date'), \
                         self.__test_date_reference_time)

    def test_uv_intensity_to_exposure_risk(self):
        self.assertEqual(uv_intensity_to_exposure_risk(0.5), 'low')
        self.assertEqual(uv_intensity_to_exposure_risk(3.5), 'moderate')
        self.assertEqual(uv_intensity_to_exposure_risk(6.5), 'high')
        self.assertEqual(uv_intensity_to_exposure_risk(8.5), 'very high')
        self.assertEqual(uv_intensity_to_exposure_risk(30.5), 'extreme')

    # Test JSON and XML comparisons by ordering strings (this overcomes
    # interpeter-dependant serialization of XML/JSON objects)

    def test_to_JSON(self):
        ordered_base_json = ''.join(sorted(UVINDEX_JSON_DUMP))
        ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON()))
        self.assertEqual(ordered_base_json, ordered_actual_json)

    def test_to_XML(self):
        ordered_base_xml = ''.join(sorted(UVINDEX_XML_DUMP))
        ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML()))
        self.assertEqual(ordered_base_xml, ordered_actual_xml)
Пример #20
0
class TestForecast(unittest.TestCase):

    __test_reception_time = 1234567
    __test_iso_reception_time = "1970-01-15 06:56:07+00"
    __test_date_reception_time = datetime.strptime(
        __test_iso_reception_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())
    __test_location = Location('test', 12.3, 43.7, 987, 'IT')
    __test_weathers = [
        Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20},
                {"all": 0}, {
                    "deg": 252.002,
                    "speed": 1.100
                }, 57, {
                    "press": 1030.119,
                    "sea_level": 1038.589
                }, {
                    "temp": 294.199,
                    "temp_kf": -1.899,
                    "temp_max": 296.098,
                    "temp_min": 294.199
                }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0,
                296.0),
        Weather(1378459690, 1378496480, 1378449510, 23, {"all": 10},
                {"all": 0}, {
                    "deg": 103.4,
                    "speed": 4.2
                }, 12, {
                    "press": 1070.119,
                    "sea_level": 1078.589
                }, {
                    "temp": 297.199,
                    "temp_kf": -1.899,
                    "temp_max": 299.0,
                    "temp_min": 295.6
                }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0,
                296.0)
    ]
    __test_n_weathers = len(__test_weathers)
    __test_instance = Forecast("daily", __test_reception_time, __test_location,
                               __test_weathers)

    def test_actualize(self):
        weathers = [
            Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20},
                    {"all": 0}, {
                        "deg": 252.002,
                        "speed": 1.100
                    }, 57, {
                        "press": 1030.119,
                        "sea_level": 1038.589
                    }, {
                        "temp": 294.199,
                        "temp_kf": -1.899,
                        "temp_max": 296.098,
                        "temp_min": 294.199
                    }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0,
                    298.0, 296.0),
            # will this time ever be reached?
            Weather(9999999999, 1378496480, 1378449510, 23, {"all": 10},
                    {"all": 0}, {
                        "deg": 103.4,
                        "speed": 4.2
                    }, 12, {
                        "press": 1070.119,
                        "sea_level": 1078.589
                    }, {
                        "temp": 297.199,
                        "temp_kf": -1.899,
                        "temp_max": 299.0,
                        "temp_min": 295.6
                    }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0,
                    296.0)
        ]
        f = Forecast("daily", self.__test_reception_time, self.__test_location,
                     weathers)
        self.assertEqual(2, len(f))
        f.actualize()
        self.assertEqual(1, len(f))

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, Forecast, "3h", -1234567,
                          self.__test_location, self.__test_weathers)

    def test_get(self):
        index = 1
        self.assertEqual(self.__test_weathers[index],
                         self.__test_instance.get(index))

    def test_accessors_interval_property(self):
        former_interval = self.__test_instance.get_interval()
        self.__test_instance.set_interval("3h")
        result = self.__test_instance.get_interval()
        self.__test_instance.set_interval(former_interval)
        self.assertEqual("3h", result)

    def test_getters_return_expected_3h_data(self):
        """
        Test either for "3h" forecast and "daily" ones
        """
        instance = Forecast("3h", self.__test_reception_time,
                            self.__test_location, self.__test_weathers)
        self.assertEqual(instance.get_interval(), "3h")
        self.assertEqual(instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(instance.get_location(), self.__test_location)
        self.assertEqual(instance.get_weathers(), self.__test_weathers)

    def test_getters_return_expected_daily_data(self):
        instance = Forecast("daily", self.__test_reception_time,
                            self.__test_location, self.__test_weathers)
        self.assertEqual(instance.get_interval(), "daily")
        self.assertEqual(instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(instance.get_location(), self.__test_location)
        self.assertEqual(instance.get_weathers(), self.__test_weathers)

    def test_returning_different_formats_for_reception_time(self):
        instance = self.__test_instance
        self.assertEqual(instance.get_reception_time(timeformat='iso'),
                         self.__test_iso_reception_time)
        self.assertEqual(instance.get_reception_time(timeformat='unix'),
                         self.__test_reception_time)
        self.assertEqual(instance.get_reception_time(timeformat='date'),
                         self.__test_date_reception_time)

    def test_count_weathers(self):
        instance = self.__test_instance
        self.assertEqual(instance.count_weathers(), self.__test_n_weathers)

    def test_forecast_iterator(self):
        instance = self.__test_instance
        counter = 0
        for weather in instance:
            self.assertTrue(isinstance(weather, Weather))
            counter += 1
        self.assertEqual(instance.count_weathers(), counter)

    def test__len__(self):
        self.assertEqual(len(self.__test_instance), len(self.__test_weathers))

    # Test JSON and XML comparisons by ordering strings (this overcomes
    # interpeter-dependant serialization of XML/JSON objects)

    def test_to_JSON(self):
        ordered_base_json = ''.join(sorted(FORECAST_JSON_DUMP))
        ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON()))
        self.assertEqual(ordered_base_json, ordered_actual_json)

    def test_to_XML(self):
        ordered_base_xml = ''.join(sorted(FORECAST_XML_DUMP))
        ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML()))
        self.assertEqual(ordered_base_xml, ordered_actual_xml)
Пример #21
0
class TestSO2Index(unittest.TestCase):

    __test_reception_time = 1475283600
    __test_iso_reception_time = "2016-10-01 01:00:00+00"
    __test_date_reception_time = datetime.strptime(
        __test_iso_reception_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    __test_reference_time = 1234567
    __test_iso_reference_time = "1970-01-15 06:56:07+00"
    __test_date_reference_time = datetime.strptime(
        __test_iso_reference_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())
    __test_location = Location('test', 12.3, 43.7, 987, 'UK')
    __test_so2_samples = [{
        "precision": -4.999999987376214e-7,
        "pressure": 1000,
        "value": 8.168363052618588e-8
    }, {
        "precision": -4.999999987376214e-7,
        "pressure": 681.2920532226562,
        "value": 8.686949115599418e-8
    }, {
        "precision": -4.999999987376214e-7,
        "pressure": 464.15887451171875,
        "value": 8.871462853221601e-8
    }]
    __test_interval = 'day'
    __test_instance = SO2Index(__test_reference_time, __test_location,
                               __test_interval, __test_so2_samples,
                               __test_reception_time)

    def test_init_fails_when_reference_time_is_negative(self):
        self.assertRaises(ValueError, SO2Index, -1234567, self.__test_location,
                          self.__test_interval, self.__test_so2_samples,
                          self.__test_reception_time)

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, SO2Index, self.__test_reference_time,
                          self.__test_location, self.__test_interval,
                          self.__test_so2_samples, -1234567)

    def test_init_fails_when_so2_samples_is_not_a_list(self):
        self.assertRaises(ValueError, SO2Index, self.__test_reference_time,
                          self.__test_location, self.__test_interval, 'test',
                          self.__test_reception_time)

    def test_getters_return_expected_data(self):
        self.assertEqual(self.__test_instance.get_reference_time(),
                         self.__test_reference_time)
        self.assertEqual(self.__test_instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_location(),
                         self.__test_location)
        result = self.__test_instance.get_so2_samples()
        self.assertEqual(self.__test_so2_samples, result)
        self.assertEqual(self.__test_instance.get_interval(),
                         self.__test_interval)

    def test_returning_different_formats_for_reference_time(self):
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='iso'), \
                         self.__test_iso_reference_time)
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='unix'), \
                         self.__test_reference_time)
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='date'), \
                         self.__test_date_reference_time)

    def test_returning_different_formats_for_reception_time(self):
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \
                         self.__test_iso_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='date'), \
                         self.__test_date_reception_time)

    def test_is_forecast(self):
        self.assertFalse(self.__test_instance.is_forecast())
        in_a_year = _datetime_to_UNIXtime(datetime.utcnow()) + 31536000
        uvindex = SO2Index(in_a_year, self.__test_location,
                           self.__test_interval, [],
                           self.__test_reception_time)
        self.assertTrue(uvindex.is_forecast())

    # Test JSON and XML comparisons by ordering strings (this overcomes
    # interpeter-dependant serialization of XML/JSON objects)

    def test_to_JSON(self):
        ordered_base_json = ''.join(sorted(SO2INDEX_JSON_DUMP))
        ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON()))
        self.assertEqual(ordered_base_json, ordered_actual_json)

    def test_to_XML(self):
        ordered_base_xml = ''.join(sorted(SO2INDEX_XML_DUMP))
        ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML()))
        self.assertEqual(ordered_base_xml, ordered_actual_xml)
Пример #22
0
class TestForecast(unittest.TestCase):

    __test_reception_time = 1234567
    __test_iso_reception_time = "1970-01-15 06:56:07+00"
    __test_location = Location('test', 12.3, 43.7, 987, 'IT')
    __test_weathers = [
        Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20},
                {"all": 0}, {
                    "deg": 252.002,
                    "speed": 1.100
                }, 57, {
                    "press": 1030.119,
                    "sea_level": 1038.589
                }, {
                    "temp": 294.199,
                    "temp_kf": -1.899,
                    "temp_max": 296.098,
                    "temp_min": 294.199
                }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0,
                296.0),
        Weather(1378459690, 1378496480, 1378449510, 23, {"all": 10},
                {"all": 0}, {
                    "deg": 103.4,
                    "speed": 4.2
                }, 12, {
                    "press": 1070.119,
                    "sea_level": 1078.589
                }, {
                    "temp": 297.199,
                    "temp_kf": -1.899,
                    "temp_max": 299.0,
                    "temp_min": 295.6
                }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0,
                296.0)
    ]
    __test_n_weathers = len(__test_weathers)
    __test_instance = Forecast("daily", __test_reception_time, __test_location,
                               __test_weathers)

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, Forecast, "3h", -1234567,
                          self.__test_location, self.__test_weathers)

    def test_get(self):
        index = 1
        self.assertEqual(self.__test_weathers[index],
                         self.__test_instance.get(index))

    def test_accessors_interval_property(self):
        former_interval = self.__test_instance.get_interval()
        self.__test_instance.set_interval("3h")
        result = self.__test_instance.get_interval()
        self.__test_instance.set_interval(former_interval)
        self.assertEqual("3h", result)

    def test_getters_return_expected_3h_data(self):
        """
        Test either for "3h" forecast and "daily" ones
        """
        instance = Forecast("3h", self.__test_reception_time,
                            self.__test_location, self.__test_weathers)
        self.assertEqual(instance.get_interval(), "3h")
        self.assertEqual(instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(instance.get_location(), self.__test_location)
        self.assertEqual(instance.get_weathers(), self.__test_weathers)

    def test_getters_return_expected_daily_data(self):
        instance = Forecast("daily", self.__test_reception_time,
                            self.__test_location, self.__test_weathers)
        self.assertEqual(instance.get_interval(), "daily")
        self.assertEqual(instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(instance.get_location(), self.__test_location)
        self.assertEqual(instance.get_weathers(), self.__test_weathers)

    def test_returning_different_formats_for_reception_time(self):
        instance = self.__test_instance
        self.assertEqual(instance.get_reception_time(timeformat='iso'),
                         self.__test_iso_reception_time)
        self.assertEqual(instance.get_reception_time(timeformat='unix'),
                         self.__test_reception_time)

    def test_count_weathers(self):
        instance = self.__test_instance
        self.assertEqual(instance.count_weathers(), self.__test_n_weathers)

    def test_forecast_iterator(self):
        instance = self.__test_instance
        counter = 0
        for weather in instance:
            self.assertTrue(isinstance(weather, Weather))
            counter += 1
        self.assertEqual(instance.count_weathers(), counter)

    def test__len__(self):
        self.assertEqual(len(self.__test_instance), len(self.__test_weathers))

    # Only test to_JSON and to_XML functions when running Python 2.7
    from sys import version_info
    if version_info[0] < 3:

        def test_to_JSON(self):
            self.assertEqual(FORECAST_JSON_DUMP,
                             self.__test_instance.to_JSON())

        def test_to_XML(self):
            self.assertEqual(FORECAST_XML_DUMP, self.__test_instance.to_XML())
Пример #23
0
class TestLocation(unittest.TestCase):

    __test_name = 'London'
    __test_lon = 12.3
    __test_lat = 43.7
    __test_ID = 1234
    __test_country = 'UK'
    __test_instance = Location(__test_name, __test_lon, __test_lat, __test_ID,
                               __test_country)

    def test_init_fails_when_lat_or_lon_are_none(self):
        self.assertRaises(ValueError, Location, 'London', None, 43.7, 1234)
        self.assertRaises(ValueError, Location, 'London', 200.0, None, 1234)

    def test_init_fails_when_coordinates_are_out_of_bounds(self):
        """
        Test failure when providing: lon < -180, lon > 180, lat < -90, lat > 90
        """
        self.assertRaises(ValueError, Location, 'London', -200.0, 43.7, 1234)
        self.assertRaises(ValueError, Location, 'London', 200.0, 43.7, 1234)
        self.assertRaises(ValueError, Location, 'London', 12.3, -100.0, 1234)
        self.assertRaises(ValueError, Location, 'London', 12.3, 100.0, 1234)

    def test_from_dictionary(self):
        dict1 = {
            "coord": {
                "lon": -0.12574,
                "lat": 51.50853
            },
            "id": 2643743,
            "name": "London",
            "cnt": 9
        }
        dict2 = {
            "city": {
                "coord": {
                    "lat": 51.50853,
                    "lon": -0.125739
                },
                "country": "GB",
                "id": 2643743,
                "name": "London",
                "population": 1000000
            }
        }
        dict3 = {"station": {"coord": {"lon": -90.47, "lat": 39.38}}}
        result1 = location_from_dictionary(dict1)
        result2 = location_from_dictionary(dict2)
        result3 = location_from_dictionary(dict3)
        self.assertTrue(isinstance(result1, Location))
        self.assertTrue(isinstance(result2, Location))
        self.assertFalse(result1.get_country() is not None)
        self.assertTrue(result1.get_ID() is not None)
        self.assertTrue(result1.get_lat() is not None)
        self.assertTrue(result1.get_lon() is not None)
        self.assertTrue(result1.get_name() is not None)
        self.assertTrue(result2.get_country() is not None)
        self.assertTrue(result2.get_ID() is not None)
        self.assertTrue(result2.get_lat() is not None)
        self.assertTrue(result2.get_lon() is not None)
        self.assertTrue(result2.get_name() is not None)
        self.assertTrue(result3.get_lat() is not None)
        self.assertTrue(result3.get_lon() is not None)
        self.assertTrue(result3.get_country() is None)
        self.assertTrue(result3.get_name() is None)
        self.assertTrue(result3.get_ID() is None)

    def test_from_dictionary_holds_the_lack_of_geocoords(self):
        dict1 = {"station": {"coord": {}}}
        dict2 = {"coord": {}}
        result1 = location_from_dictionary(dict1)
        self.assertTrue(isinstance(result1, Location))
        self.assertEqual(result1.get_lat(), 0.0)
        self.assertEqual(result1.get_lon(), 0.0)
        self.assertTrue(result1.get_country() is None)
        self.assertTrue(result1.get_name() is None)
        self.assertTrue(result1.get_ID() is None)
        result2 = location_from_dictionary(dict2)
        self.assertTrue(isinstance(result2, Location))
        self.assertEqual(result2.get_lat(), 0.0)
        self.assertEqual(result2.get_lon(), 0.0)
        self.assertTrue(result2.get_country() is None)
        self.assertTrue(result2.get_name() is None)
        self.assertTrue(result2.get_ID() is None)

    def test_getters_return_expected_data(self):
        instance = Location(self.__test_name, self.__test_lon, self.__test_lat,
                            self.__test_ID, self.__test_country)
        self.assertEqual(instance.get_name(), self.__test_name)
        self.assertEqual(instance.get_lon(), self.__test_lon)
        self.assertEqual(instance.get_lat(), self.__test_lat)
        self.assertEqual(instance.get_ID(), self.__test_ID)
        self.assertEqual(instance.get_country(), self.__test_country)

    def to_geopoint(self):
        loc_1 = Location(self.__test_name, None, self.__test_lat,
                         self.__test_ID, self.__test_country)
        loc_2 = Location(self.__test_name, self.__test_lon, None,
                         self.__test_ID, self.__test_country)
        loc_3 = Location(self.__test_name, self.__test_lon, self.__test_lat,
                         self.__test_ID, self.__test_country)
        self.assertIsNone(loc_1.to_geopoint())
        self.assertIsNone(loc_2.to_geopoint())
        result = loc_3.to_geopoint()
        self.assertTrue(isinstance(result, Point))
        expected_geojson = json.dumps({
            "coordinates": [12.3, 43.7],
            "type": "Point"
        })
        self.assertEqual(sorted(expected_geojson), sorted(result.geojson()))

    # Test JSON and XML comparisons by ordering strings (this overcomes
    # interpeter-dependant serialization of XML/JSON objects

    def test_to_JSON(self):
        ordered_base_json = ''.join(sorted(LOCATION_JSON_DUMP))
        ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON()))
        self.assertEqual(ordered_base_json, ordered_actual_json)

    def test_to_XML(self):
        ordered_base_xml = ''.join(sorted(LOCATION_XML_DUMP))
        ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML()))
        self.assertEqual(ordered_base_xml, ordered_actual_xml)
Пример #24
0
class TestOzone(unittest.TestCase):

    __test_reception_time = 1475283600
    __test_iso_reception_time = "2016-10-01 01:00:00+00"
    __test_date_reception_time = datetime.strptime(
        __test_iso_reception_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    __test_reference_time = 1234567
    __test_iso_reference_time = "1970-01-15 06:56:07+00"
    __test_date_reference_time = datetime.strptime(
        __test_iso_reference_time,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    __test_location = Location('test', 12.3, 43.7, 987, 'UK')
    __test_du_value = 6.8
    __test_interval = 'day'
    __test_exposure_risk = 'high'
    __test_instance = Ozone(__test_reference_time, __test_location,
                            __test_interval, __test_du_value,
                            __test_reception_time)

    def test_init_fails_when_reference_time_is_negative(self):
        self.assertRaises(ValueError, Ozone, -1234567, self.__test_location,
                          self.__test_interval, self.__test_du_value,
                          self.__test_reception_time)

    def test_init_fails_when_reception_time_is_negative(self):
        self.assertRaises(ValueError, Ozone, self.__test_reference_time,
                          self.__test_location, self.__test_interval,
                          self.__test_du_value, -1234567)

    def test_init_fails_when_uv_intensity_is_negative(self):
        self.assertRaises(ValueError, Ozone, self.__test_reference_time,
                          self.__test_location, self.__test_interval, -8.9,
                          self.__test_reception_time)

    def test_getters_return_expected_data(self):
        self.assertEqual(self.__test_instance.get_reception_time(),
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_reference_time(),
                         self.__test_reference_time)
        self.assertEqual(self.__test_instance.get_location(),
                         self.__test_location)
        self.assertEqual(self.__test_instance.get_du_value(),
                         self.__test_du_value)
        self.assertEqual(self.__test_instance.get_interval(),
                         self.__test_interval)

    def test_returning_different_formats_for_reception_time(self):
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \
                         self.__test_iso_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \
                         self.__test_reception_time)
        self.assertEqual(self.__test_instance.get_reception_time(timeformat='date'), \
                         self.__test_date_reception_time)

    def test_returning_different_formats_for_reference_time(self):
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='iso'), \
                         self.__test_iso_reference_time)
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='unix'), \
                         self.__test_reference_time)
        self.assertEqual(self.__test_instance.get_reference_time(timeformat='date'), \
                         self.__test_date_reference_time)

    def test_is_forecast(self):
        self.assertFalse(self.__test_instance.is_forecast())
        in_a_year = _datetime_to_UNIXtime(datetime.utcnow()) + 31536000
        uvindex = Ozone(in_a_year, self.__test_location, self.__test_interval,
                        self.__test_du_value, self.__test_reception_time)
        self.assertTrue(uvindex.is_forecast())

    # Test JSON and XML comparisons by ordering strings (this overcomes
    # interpeter-dependant serialization of XML/JSON objects)

    def test_to_JSON(self):
        ordered_base_json = ''.join(sorted(OZONE_JSON_DUMP))
        ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON()))
        self.assertEqual(ordered_base_json, ordered_actual_json)

    def test_to_XML(self):
        ordered_base_xml = ''.join(sorted(OZONE_XML_DUMP))
        ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML()))
        self.assertEqual(ordered_base_xml, ordered_actual_xml)
Пример #25
0
class TestForecaster(unittest.TestCase):

    __test_time_1 = 1379090800
    __test_start_coverage_iso = "2013-09-13 16:46:40+00"
    __test_date_start_coverage = datetime.strptime(
        __test_start_coverage_iso,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())
    __test_middle_1_coverage = 1379226100
    __test_middle_1_coverage_iso = "2013-09-15 06:21:40+00"
    __test_date_middle_1_coverage = datetime.strptime(
        __test_middle_1_coverage_iso,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())
    __test_time_2 = 1379361400
    __test_middle_2_coverage_iso = "2013-09-16 19:56:40+00"
    __test_date_middle_2_coverage = datetime.strptime(
        __test_middle_2_coverage_iso,
        '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())
    __test_end_coverage = 1379902600
    __test_end_coverage_iso = "2013-09-23 02:16:40+00"
    __test_date_end_coverage = datetime.strptime(
        __test_end_coverage_iso, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    __test_location = Location('test', 12.3, 43.7, 987, 'IT')

    __test_weather_rainsnow = Weather(__test_time_1, 1378496400, 1378449600,
                                      67, {"all": 30}, {"all": 1}, {
                                          "deg": 252.002,
                                          "speed": 4.100
                                      }, 57, {
                                          "press": 1030.119,
                                          "sea_level": 1038.589
                                      }, {
                                          "temp": 294.199,
                                          "temp_kf": -1.899,
                                          "temp_max": 296.098,
                                          "temp_min": 294.199
                                      }, "Rain", "Light rain", 500, "10d",
                                      1000, 300.0, 298.0, 296.0)
    __test_weather_clouds = Weather(__test_middle_1_coverage, 1378496480,
                                    1378449510, 23, {"all": 0}, {}, {
                                        "deg": 103.4,
                                        "speed": 1.2
                                    }, 12, {
                                        "press": 1070.119,
                                        "sea_level": 1078.589
                                    }, {
                                        "temp": 297.199,
                                        "temp_kf": -1.899,
                                        "temp_max": 299.0,
                                        "temp_min": 295.6
                                    }, "Clouds", "Overcast clouds", 804, "02d",
                                    1000, 300.0, 298.0, 296.0)
    __test_weather_sun_1 = Weather(__test_time_2, 1378496480, 1378449510, 5,
                                   {"all": 0}, {}, {
                                       "deg": 103.4,
                                       "speed": 1.2
                                   }, 12, {
                                       "press": 1090.119,
                                       "sea_level": 1078.589
                                   }, {
                                       "temp": 299.199,
                                       "temp_kf": -1.899,
                                       "temp_max": 301.0,
                                       "temp_min": 297.6
                                   }, "Clear", "Sky is clear", 800, "01d",
                                   1000, 300.0, 298.0, 296.0)
    __test_weather_sun_2 = Weather(__test_end_coverage, 1378496480, 1378449510,
                                   5, {"all": 0}, {}, {
                                       "deg": 99.4,
                                       "speed": 0.8
                                   }, 7, {
                                       "press": 1091.119,
                                       "sea_level": 1079.589
                                   }, {
                                       "temp": 299.599,
                                       "temp_kf": -1.899,
                                       "temp_max": 301.9,
                                       "temp_min": 298.0
                                   }, "Clear", "Sky is clear", 800, "01d",
                                   1000, 300.0, 298.0, 296.0)

    __test_weather_storm = Weather(__test_end_coverage, 1378496480, 1378449510,
                                   5, {"all": 0}, {}, {
                                       "deg": 99.4,
                                       "speed": 0.8
                                   }, 7, {
                                       "press": 1071.119,
                                       "sea_level": 1059.589
                                   }, {
                                       "temp": 299.599,
                                       "temp_kf": -1.899,
                                       "temp_max": 301.9,
                                       "temp_min": 298.0
                                   }, "Storm", "Violent storm", 961, "01d",
                                   1000, 300.0, 298.0, 296.0)

    __test_weather_hurricane = Weather(__test_end_coverage, 1378496480,
                                       1378449510, 5, {"all": 0}, {}, {
                                           "deg": 99.4,
                                           "speed": 0.8
                                       }, 7, {
                                           "press": 1071.119,
                                           "sea_level": 1059.589
                                       }, {
                                           "temp": 299.599,
                                           "temp_kf": -1.899,
                                           "temp_max": 301.9,
                                           "temp_min": 298.0
                                       }, "Hurricane", "Hurricane", 962, "01d",
                                       1000, 300.0, 298.0, 296.0)

    __test_none_values = Weather(__test_end_coverage, 1378496480, 1378449510,
                                 5, {}, {}, {}, 7, {
                                     "press": 1091.119,
                                     "sea_level": 1079.589
                                 }, {
                                     "temp": 299.599,
                                     "temp_kf": -1.899
                                 }, "Clear", "Sky is clear", 800, "01d", 1000,
                                 300.0, 298.0, 296.0)
    __test_weathers = [
        __test_weather_rainsnow, __test_weather_clouds, __test_weather_sun_1,
        __test_weather_sun_2, __test_weather_storm, __test_weather_hurricane
    ]

    __test_forecast = Forecast("daily", 1379089800, __test_location,
                               __test_weathers)

    __test_instance = Forecaster(__test_forecast)

    def test_getter_returns_expected_data(self):
        self.assertEqual(self.__test_instance.get_forecast(),
                         self.__test_forecast)

    def test_when_starts_returning_different_timeformats(self):
        self.assertEqual(self.__test_instance.when_starts(timeformat='iso'),
                         self.__test_start_coverage_iso)
        self.assertEqual(self.__test_instance.when_starts(timeformat='unix'),
                         self.__test_time_1)
        self.assertEqual(self.__test_instance.when_starts(timeformat='date'),
                         self.__test_date_start_coverage)

    def test_when_ends_returning_different_timeformats(self):
        self.assertEqual(self.__test_instance.when_ends(timeformat='iso'),
                         self.__test_end_coverage_iso)
        self.assertEqual(self.__test_instance.when_ends(timeformat='unix'),
                         self.__test_end_coverage)
        self.assertEqual(self.__test_instance.when_ends(timeformat='date'),
                         self.__test_date_end_coverage)

    def test_will_have_rain(self):
        self.assertTrue(self.__test_instance.will_have_rain())

    def test_will_have_sun(self):
        self.assertTrue(self.__test_instance.will_have_sun())

    def test_will_have_clouds(self):
        self.assertTrue(self.__test_instance.will_have_clouds())

    def test_will_have_fog(self):
        self.assertFalse(self.__test_instance.will_have_fog())

    def test_will_have_snow(self):
        self.assertFalse(self.__test_instance.will_have_snow())

    def test_will_have_storm(self):
        self.assertTrue(self.__test_instance.will_have_storm())

    def test_will_have_tornado(self):
        self.assertFalse(self.__test_instance.will_have_tornado())

    def test_will_have_hurricane(self):
        self.assertTrue(self.__test_instance.will_have_hurricane())

    def test_when_rain(self):
        self.assertEqual([self.__test_weather_rainsnow],
                         self.__test_instance.when_rain())

    def test_when_sun(self):
        self.assertEqual(
            [self.__test_weather_sun_1, self.__test_weather_sun_2],
            self.__test_instance.when_sun())

    def test_when_clouds(self):
        self.assertEqual([self.__test_weather_clouds],
                         self.__test_instance.when_clouds())

    def test_when_fog(self):
        self.assertFalse(self.__test_instance.when_fog())

    def test_when_snow(self):
        self.assertFalse(self.__test_instance.when_snow())

    def test_when_storm(self):
        self.assertTrue(self.__test_instance.when_storm())

    def test_when_tornado(self):
        self.assertFalse(self.__test_instance.when_tornado())

    def test_when_hurricane(self):
        self.assertTrue(self.__test_instance.when_hurricane())

    def test_will_be_rainy_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertTrue(self.__test_instance.will_be_rainy_at(time_1))
        self.assertFalse(self.__test_instance.will_be_rainy_at(time_2))
        self.assertFalse(self.__test_instance.will_be_rainy_at(time_3))

    def test_will_be_rainy_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_rainy_at,
                          self.__test_instance, 45.7)

    def test_will_be_sunny_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_sunny_at(time_1))
        self.assertFalse(self.__test_instance.will_be_sunny_at(time_2))
        self.assertTrue(self.__test_instance.will_be_sunny_at(time_3))

    def test_will_be_sunny_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_sunny_at,
                          self.__test_instance, 45.7)

    def test_will_be_snowy_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_snowy_at(time_1))
        self.assertFalse(self.__test_instance.will_be_snowy_at(time_2))
        self.assertFalse(self.__test_instance.will_be_snowy_at(time_3))

    def test_will_be_snowy_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_snowy_at,
                          self.__test_instance, 45.7)

    def test_will_be_cloudy_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_cloudy_at(time_1))
        self.assertTrue(self.__test_instance.will_be_cloudy_at(time_2))
        self.assertFalse(self.__test_instance.will_be_cloudy_at(time_3))

    def test_will_be_cloudy_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_cloudy_at,
                          self.__test_instance, 45.7)

    def test_will_be_foggy_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_foggy_at(time_1))
        self.assertFalse(self.__test_instance.will_be_foggy_at(time_2))
        self.assertFalse(self.__test_instance.will_be_foggy_at(time_3))

    def test_will_be_foggy_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_foggy_at,
                          self.__test_instance, 45.7)

    def test_will_be_stormy_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_stormy_at(time_1))
        self.assertFalse(self.__test_instance.will_be_stormy_at(time_2))
        self.assertFalse(self.__test_instance.will_be_stormy_at(time_3))

    def test_will_be_stormy_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_stormy_at,
                          self.__test_instance, 45.7)

    def test_will_be_tornado_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_tornado_at(time_1))
        self.assertFalse(self.__test_instance.will_be_tornado_at(time_2))
        self.assertFalse(self.__test_instance.will_be_tornado_at(time_3))

    def test_will_be_tornado_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_tornado_at,
                          self.__test_instance, 45.7)

    def test_will_be_hurricane_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertFalse(self.__test_instance.will_be_hurricane_at(time_1))
        self.assertFalse(self.__test_instance.will_be_hurricane_at(time_2))
        self.assertFalse(self.__test_instance.will_be_hurricane_at(time_3))

    def test_will_be_hurricane_at_fails_with_bad_parameters(self):
        self.assertRaises(TypeError, Forecaster.will_be_hurricane_at,
                          self.__test_instance, 45.7)

    def test_get_weather_at(self):
        time_1 = datetime(2013, 9, 13, 16, 47, 0)
        time_2 = 1379226110
        time_3 = "2013-09-16 19:56:50+00"
        self.assertEqual(self.__test_instance.get_weather_at(time_1),
                         self.__test_weather_rainsnow)
        self.assertEqual(self.__test_instance.get_weather_at(time_2),
                         self.__test_weather_clouds)
        self.assertEqual(self.__test_instance.get_weather_at(time_3),
                         self.__test_weather_sun_1)

    def test_get_weather_at_fails_with_bad_parameter(self):
        self.assertRaises(TypeError, Forecaster.get_weather_at,
                          self.__test_instance, 45.7)

    def test_most_hot(self):
        self.assertEqual(self.__test_weather_sun_2,
                         self.__test_instance.most_hot())

    def test_most_hot_returning_None(self):
        fcstr = Forecaster(
            Forecast("daily", 1379089800, self.__test_location,
                     [self.__test_none_values]))
        self.assertFalse(fcstr.most_hot())

    def test_most_cold(self):
        self.assertEqual(self.__test_weather_rainsnow,
                         self.__test_instance.most_cold())

    def test_most_cold_returning_None(self):
        fcstr = Forecaster(
            Forecast("daily", 1379089800, self.__test_location,
                     [self.__test_none_values]))
        self.assertFalse(fcstr.most_hot())

    def test_most_humid(self):
        self.assertEqual(self.__test_weather_rainsnow,
                         self.__test_instance.most_humid())

    def test_most_rainy(self):
        self.assertEqual(self.__test_weather_rainsnow,
                         self.__test_instance.most_rainy())

    def test_most_snowy(self):
        self.assertEqual(self.__test_weather_rainsnow,
                         self.__test_instance.most_snowy())

    def test_most_rainy_returning_None(self):
        fcstr = Forecaster(
            Forecast("daily", 1379089800, self.__test_location,
                     [self.__test_none_values]))
        self.assertFalse(fcstr.most_rainy())

    def test_most_windy(self):
        self.assertEqual(self.__test_weather_rainsnow,
                         self.__test_instance.most_windy())

    def test_most_windy_returning_None(self):
        fcstr = Forecaster(
            Forecast("daily", 1379089800, self.__test_location,
                     [self.__test_none_values]))
        self.assertFalse(fcstr.most_windy())
Пример #26
0
class TestLocation(unittest.TestCase):

    __test_name = 'London'
    __test_lon = 12.3
    __test_lat = 43.7
    __test_ID = 1234
    __test_country = 'UK'
    __test_instance = Location(__test_name, __test_lon, __test_lat, __test_ID,
                               __test_country)

    def test_init_fails_when_lat_or_lon_are_none(self):
        self.assertRaises(ValueError, Location, 'London', None, 43.7, 1234)
        self.assertRaises(ValueError, Location, 'London', 200.0, None, 1234)

    def test_init_fails_when_coordinates_are_out_of_bounds(self):
        """
        Test failure when providing: lon < -180, lon > 180, lat < -90, lat > 90
        """
        self.assertRaises(ValueError, Location, 'London', -200.0, 43.7, 1234)
        self.assertRaises(ValueError, Location, 'London', 200.0, 43.7, 1234)
        self.assertRaises(ValueError, Location, 'London', 12.3, -100.0, 1234)
        self.assertRaises(ValueError, Location, 'London', 12.3, 100.0, 1234)

    def test_from_dictionary(self):
        dict1 = {
            "coord": {
                "lon": -0.12574,
                "lat": 51.50853
            },
            "id": 2643743,
            "name": "London",
            "cnt": 9
        }
        dict2 = {
            "city": {
                "coord": {
                    "lat": 51.50853,
                    "lon": -0.125739
                },
                "country": "GB",
                "id": 2643743,
                "name": "London",
                "population": 1000000
            }
        }
        dict3 = {"station": {"coord": {"lon": -90.47, "lat": 39.38}}}
        result1 = location_from_dictionary(dict1)
        result2 = location_from_dictionary(dict2)
        result3 = location_from_dictionary(dict3)
        self.assertTrue(isinstance(result1, Location))
        self.assertTrue(isinstance(result2, Location))
        self.assertFalse(result1.get_country() is not None)
        self.assertTrue(result1.get_ID() is not None)
        self.assertTrue(result1.get_lat() is not None)
        self.assertTrue(result1.get_lon() is not None)
        self.assertTrue(result1.get_name() is not None)
        self.assertTrue(result2.get_country() is not None)
        self.assertTrue(result2.get_ID() is not None)
        self.assertTrue(result2.get_lat() is not None)
        self.assertTrue(result2.get_lon() is not None)
        self.assertTrue(result2.get_name() is not None)
        self.assertTrue(result3.get_lat() is not None)
        self.assertTrue(result3.get_lon() is not None)
        self.assertTrue(result3.get_country() is None)
        self.assertTrue(result3.get_name() is None)
        self.assertTrue(result3.get_ID() is None)

    def test_getters_return_expected_data(self):
        instance = Location(self.__test_name, self.__test_lon, self.__test_lat,
                            self.__test_ID, self.__test_country)
        self.assertEqual(instance.get_name(), self.__test_name)
        self.assertEqual(instance.get_lon(), self.__test_lon)
        self.assertEqual(instance.get_lat(), self.__test_lat)
        self.assertEqual(instance.get_ID(), self.__test_ID)
        self.assertEqual(instance.get_country(), self.__test_country)

    # Only test to_JSON and to_XML functions when running Python 2.7
    from sys import version_info
    if version_info[0] < 3:

        def test_to_JSON(self):
            self.assertEqual(LOCATION_JSON_DUMP,
                             self.__test_instance.to_JSON())

        def test_to_XML(self):
            self.assertEqual(LOCATION_XML_DUMP, self.__test_instance.to_XML())