Esempio n. 1
0
def add_from_files(*, gpx_files_file_path, user, skip_errors=True):
    """
    Add all *.gpx files from <gpx_files_file_path> to <user>
    """
    assert_is_dir(gpx_files_file_path)

    gpx_files = gpx_files_file_path.glob("**/*.gpx")
    for gpx_file_file_path in sorted(gpx_files):
        try:
            with transaction.atomic():
                instance = add_from_file(gpx_file_file_path=gpx_file_file_path,
                                         user=user)
        except (IntegrityError, GpxDataError) as err:
            log.error("Skip .gpx file: %s", err)
            if not skip_errors:
                raise
        else:
            if instance:
                yield instance
Esempio n. 2
0
    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 == []
Esempio n. 3
0
    def test_is_dir_failed(self):
        with self.assertRaises(AssertionError) as cm:
            assert_is_dir("/does/not/exists/")

        assert_pformat_equal(cm.exception.args[0],
                             "Directory not exists: /does/not/exists")
Esempio n. 4
0
 def test_is_existing_dir_with_string(self):
     assert_is_dir(str(self.existing_dir_path))
Esempio n. 5
0
 def test_is_existing_dir_with_path_instance(self):
     assert_is_dir(self.existing_dir_path)
Esempio n. 6
0
 def assert_is_dir(self, path):
     warnings.warn("Use django_tools.unittest_utils.assertments.assert_is_dir!", DeprecationWarning)
     assertments.assert_is_dir(path)
Esempio n. 7
0
    def test_is_dir_failed(self):
        with self.assertRaises(AssertionError) as cm:
            assert_is_dir("/does/not/exists/")

        assert_pformat_equal(cm.exception.args[0], "Directory not exists: /does/not/exists")
Esempio n. 8
0
 def test_is_existing_dir_with_string(self):
     assert_is_dir(str(self.existing_dir_path))
Esempio n. 9
0
 def test_is_existing_dir_with_path_instance(self):
     assert_is_dir(self.existing_dir_path)