def test_no_json_data(self): """ Request weather for Essen City on 11.02.2017 will raise into 500 - Server error: https://www.metaweather.com/de/648820/2017/2/11/ The API Request will return []: https://www.metaweather.com/api/location/648820/2017/2/11/ MetaWeatherCom().location_day() will raise NoWeatherData """ lat, lon = (51.4109, 6.7828) # Duisburg -> WOEID: 648820 (Essen, city) date = datetime.datetime(year=2017, month=2, day=10, hour=12, minute=00) with requests_mock.mock() as m: m.get( 'https://www.metaweather.com/api/location/search/?lattlong=51.41,6.78', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5141_678.json')) m.get('https://www.metaweather.com/api/location/648820/2017/2/10/', headers={'Content-Type': 'application/json'}, content=b'[]') with self.assertRaises(NoWeatherData): temperature, weather_state = meta_weather_com.coordinates2weather( lat, lon, date=date)
def test_add_from_files(self): with locmem_stats_override_storage( ) as storage_stats, requests_mock.mock() as m: m.get( 'https://www.metaweather.com/api/location/search/?lattlong=51.44,6.62', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5144_662.json')) m.get('https://www.metaweather.com/api/location/648820/2018/2/21/', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_location_648820_2018_2_21.json')) m.get( 'https://www.metaweather.com/api/location/search/?lattlong=52.52,13.38', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_5252_1338.json') # 4.5°C 'Light Cloud' ) m.get( 'https://www.metaweather.com/api/location/638242/2011/1/13/', headers={'Content-Type': 'application/json'}, content=b'[]', # No weather data for start. ) m.get( 'https://www.metaweather.com/api/location/search/?lattlong=46.95,7.44', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_4695_744.json')) m.get( 'https://www.metaweather.com/api/location/784794/2011/1/15/', headers={'Content-Type': 'application/json'}, content=b'[]', # No weather data for start. ) instances = [ str(instance) for instance in add_from_files( gpx_files_file_path=FIXTURES_PATH, user=self.user, skip_errors=True, ) ] assert_pformat_equal(instances, ["2018-02-21", "2011-01-13"]) assert storage_stats.fields_saved == [ ('for_runners', 'gpxmodel', 'track_svg'), ('for_runners', 'gpxmodel', 'gpx_file') ] assert storage_stats.fields_read == []
def test_add_gpx(self): with locmem_stats_override_storage( ) as storage_stats, requests_mock.mock() as m: m.get( 'https://www.metaweather.com/api/location/search/?lattlong=51.44,6.62', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5144_662.json')) m.get('https://www.metaweather.com/api/location/648820/2018/2/21/', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_location_648820_2018_2_21.json')) gpx_content = fixture_content('garmin_connect_1.gpx', mode='r') instance = add_gpx(gpx_content=gpx_content, user=self.user) self.assert_garmin_connect_1_gpx(instance) assert storage_stats.fields_saved == [ ('for_runners', 'gpxmodel', 'track_svg'), ('for_runners', 'gpxmodel', 'gpx_file') ] assert storage_stats.fields_read == []
def test_reverse_geo(self): # first point from: for_runners/tests/fixture_files/garmin_connect_1.gpx lat = 51.43788929097354412078857421875 lon = 6.617012657225131988525390625 with requests_mock.mock() as m: m.get(( 'https://nominatim.openstreetmap.org/reverse' '?lat=51.43789&lon=6.61701&format=json&addressdetails=1&zoom=17' ), headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5143789_661701.json')) address = reverse_geo(lat, lon) assert address.short == 'Moers' assert address.full == ( '148, Filder Straße, Vinn, Moers, Kreis Wesel, Nordrhein-Westfalen, 47447, Deutschland' ) # Check if cache works in tests: assert settings.CACHES['default']['BACKEND'] == ( 'django.core.cache.backends.locmem.LocMemCache') cache.set('foo', 'bar', timeout=None) assert cache.get('foo') == 'bar' # Cache filled? address = cache.get('reverse_geo_51.43789_6.61701') assert address is not None assert address == ( '148, Filder Straße, Vinn, Moers, Kreis Wesel, Nordrhein-Westfalen, 47447, Deutschland', { 'city': 'Moers', 'country': 'Deutschland', 'country_code': 'de', 'county': 'Kreis Wesel', 'hamlet': 'Vinn', 'house_number': '148', 'postcode': '47447', 'road': 'Filder Straße', 'state': 'Nordrhein-Westfalen', 'suburb': 'Moers' }) # Second request is cached -> no request with requests_mock.mock(): address = reverse_geo(lat, lon) assert address.short == 'Moers' assert address.full == ( '148, Filder Straße, Vinn, Moers, Kreis Wesel, Nordrhein-Westfalen, 47447, Deutschland' )
def test_small_max_seconds(self): lat, lon = (51.4109, 6.7828) # Duisburg -> WOEID: 648820 (Essen, city) date = datetime.datetime(year=2018, month=6, day=20, hour=20, minute=30) with requests_mock.mock() as m: m.get( 'https://www.metaweather.com/api/location/search/?lattlong=51.41,6.78', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5141_678.json')) m.get('https://www.metaweather.com/api/location/648820/2018/6/20/', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_location_648820_2018_6_20.json')) temperature, weather_state = meta_weather_com.coordinates2weather( lat, lon, date=date, max_seconds=0.1) self.assert_equal_rounded(temperature, 27.94, decimal_places=2) self.assertEqual(weather_state, "Light Cloud")
def test_staff_upload(self): self.client.force_login(self.superuser) gpx_file_path1 = Path(FIXTURES_PATH, "garmin_connect_1.gpx") gpx_file_path2 = Path(FIXTURES_PATH, "no_track_points.gpx") with gpx_file_path1.open("rb") as file1, gpx_file_path2.open("rb") as file2, \ locmem_stats_override_storage() as storage_stats, requests_mock.mock() as m: m.get( 'https://www.metaweather.com/api/location/search/?lattlong=51.44,6.62', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5144_662.json'), ) m.get( 'https://www.metaweather.com/api/location/648820/2018/2/21/', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_location_648820_2018_2_21.json'), ) m.get( ('https://nominatim.openstreetmap.org/reverse?lat=51.43789&lon=6.61701' '&format=json&addressdetails=1&zoom=17'), headers={'Content-Type': 'application/json'}, content=fixture_content('osm_5143789_661701.json'), ) m.get( # Start point lat=51.437889290973544 - lat=6.617012657225132 ('https://nominatim.openstreetmap.org/reverse?lat=51.43785&lon=6.61701' '&format=json&addressdetails=1&zoom=17'), headers={'Content-Type': 'application/json'}, content=fixture_content('osm_5143789_661701.json'), ) m.get( # End point lat=51.437847297638655 - lat=6.6170057002455 ('https://nominatim.openstreetmap.org/reverse?lat=51.43789&lon=6.61701' '&format=json&addressdetails=1&zoom=17'), headers={'Content-Type': 'application/json'}, content=fixture_content('osm_5143785_661701.json'), ) response = self.client.post( "/en/admin/for_runners/gpxmodel/upload/", data={"gpx_files": [file1, file2]}, HTTP_ACCEPT_LANGUAGE="en", ) # debug_response(response) assert storage_stats.fields_saved == [ ('for_runners', 'gpxmodel', 'track_svg'), ('for_runners', 'gpxmodel', 'gpx_file'), ] assert storage_stats.fields_read == [] tracks = GpxModel.objects.all() self.assertEqual(tracks.count(), 1) new_track = tracks[0] self.assertRedirects( response, expected_url= f"/en/admin/for_runners/gpxmodel/{new_track.pk:d}/change/", status_code=302, target_status_code=200, fetch_redirect_response=False, ) response = self.client.get("/en/admin/for_runners/gpxmodel/", HTTP_ACCEPT_LANGUAGE="en") # print(repr(self.get_messages(response))) self.assert_html_parts( response, parts=( f"<title>Select GPX Track to change | Django-ForRunners v{__version__}</title>", # "Process garmin_connect_1.gpx...", # "Created: 2018-02-21 14:30:50 Moers", # # "Process no_track_points.gpx...", # "Error process GPX data: Can't get first track", '<td class="field-human_pace">7:03 min/km</td>', '<p class="paginator">1 GPX Track</p>', ), ) self.assertTemplateUsed( response, template_name="admin/for_runners/gpxmodel/change_list.html") self.assert_messages( response, expected_messages=[ "Process garmin_connect_1.gpx...", "Created: 2018-02-21 Moers", "Process no_track_points.gpx...", "Error process GPX data: Can't get first track", ], ) assert storage_stats.fields_read == []
def test_import(self): test_username = "******" assert GpxModel.objects.filter( tracked_by__username=test_username).count() == 0 base_path = Path(for_runners.__file__).parent fixture_files_path = Path(base_path, "tests/fixture_files") assert_is_dir(fixture_files_path) with StdoutStderrBuffer() as buff, \ locmem_stats_override_storage() as storage_stats, \ requests_mock.mock() as m: m.get( 'https://www.metaweather.com/api/location/search/?lattlong=51.44,6.62', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_5144_662.json')) m.get('https://www.metaweather.com/api/location/648820/2018/2/21/', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_location_648820_2018_2_21.json')) m.get( 'https://www.metaweather.com/api/location/search/?lattlong=52.52,13.38', headers={'Content-Type': 'application/json'}, content=fixture_content( 'metaweather_5252_1338.json') # 4.5°C 'Light Cloud' ) m.get( 'https://www.metaweather.com/api/location/638242/2011/1/13/', headers={'Content-Type': 'application/json'}, content=b'[]', # No weather data for start. ) m.get( 'https://www.metaweather.com/api/location/search/?lattlong=46.95,7.44', headers={'Content-Type': 'application/json'}, content=fixture_content('metaweather_4695_744.json')) m.get( 'https://www.metaweather.com/api/location/784794/2011/1/15/', headers={'Content-Type': 'application/json'}, content=b'[]', # No weather data for start. ) m.get(('https://nominatim.openstreetmap.org/reverse' '?lat=0.0&lon=0.0&format=json&addressdetails=1&zoom=17'), headers={'Content-Type': 'application/json'}, content=fixture_content('nominatim_osm_reverse_0_0.json')) m.get(('https://nominatim.openstreetmap.org/reverse' '?lat=0.0&lon=180.0&format=json&addressdetails=1&zoom=17'), headers={'Content-Type': 'application/json'}, content=b'{"error":"Unable to geocode"}') m.get(( 'https://nominatim.openstreetmap.org/reverse' '?lat=51.437889290973544&lon=6.617012657225132&format=json&addressdetails=1&zoom=17' ), headers={'Content-Type': 'application/json'}, content=fixture_content('nominatim_osm_reverse_0_0.json')) call_command(import_gpx.Command(), "--username", test_username, str(fixture_files_path)) assert storage_stats.fields_saved == [ ('for_runners', 'gpxmodel', 'track_svg'), ('for_runners', 'gpxmodel', 'gpx_file') ] assert storage_stats.fields_read == [] output = buff.get_output() print(output) self.assertIn("Add new gpx tracks for user: normal_test_user", output) self.assertIn("1 - Add new track: 2018-02-21", output) self.assertIn("2 - Add new track: 2011-01-13", output) self.assertIn("Added 2 new gpx tracks.", output) self.assertIn("User normal_test_user has now 2 tracks.", output) qs = GpxModel.objects.filter(tracked_by__username=test_username) existing_tracks = [str(track) for track in qs] assert_pformat_equal(existing_tracks, ['2018-02-21', '2011-01-13']) assert qs.count() == 2 assert storage_stats.fields_read == []