From 3a25b6f2201c75c8c0ac39c1006df4b253c4d85d Mon Sep 17 00:00:00 2001 From: Kirill Kouzoubov Date: Thu, 16 Apr 2020 14:15:03 +1000 Subject: [PATCH] Rework unit tests for utils.uri Also removes doc test that is already covered by unit tests --- datacube/utils/uris.py | 13 ------------- tests/test_utils_other.py | 34 +++++++++++++++++----------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/datacube/utils/uris.py b/datacube/utils/uris.py index d3911a79e2..78f85b8a92 100644 --- a/datacube/utils/uris.py +++ b/datacube/utils/uris.py @@ -14,19 +14,6 @@ def is_url(url_str: str) -> bool: """ Check if url_str tastes like a url (starts with blah://) - - >>> is_url('file:///etc/blah') - True - >>> is_url('http://greg.com/greg.txt') - True - >>> is_url('s3:///etc/blah') - True - >>> is_url('gs://data/etc/blah.yaml') - True - >>> is_url('/etc/blah') - False - >>> is_url('C:/etc/blah') - False """ try: return URL_RE.match(url_str) is not None diff --git a/tests/test_utils_other.py b/tests/test_utils_other.py index 63662851b6..99439187f4 100644 --- a/tests/test_utils_other.py +++ b/tests/test_utils_other.py @@ -35,7 +35,7 @@ ) from datacube.utils.py import sorted_items from datacube.utils.uris import (uri_to_local_path, mk_part_uri, get_part_from_uri, as_url, is_url, - pick_uri, uri_resolve, + pick_uri, uri_resolve, is_vsipath, normalise_path, default_base_dir) from datacube.utils.io import check_write_path from datacube.testutils import mk_sample_product, remove_crs @@ -104,26 +104,25 @@ def test_uri_to_local_path(): uri_to_local_path('ftp://example.com/tmp/something.txt') -def test_uri_resolve(): +@pytest.mark.parametrize("base", [ + "s3://foo", + "gs://foo", + "/vsizip//vsicurl/https://host.tld/some/path", +]) +def test_uri_resolve(base): abs_path = '/abs/path/to/something' some_uri = 'http://example.com/file.txt' - s3_base = 's3://foo' - gs_base = 'gs://foo' - vsi_base = '/vsizip//vsicurl/https://host.tld/some/path' - assert uri_resolve(s3_base, abs_path) == "file://" + abs_path - assert uri_resolve(s3_base, some_uri) is some_uri - assert uri_resolve(s3_base, None) is s3_base - assert uri_resolve(s3_base, '') is s3_base - assert uri_resolve(s3_base, 'relative/path') == s3_base + '/relative/path' - assert uri_resolve(gs_base, abs_path) == "file://" + abs_path - assert uri_resolve(gs_base, some_uri) is some_uri - assert uri_resolve(gs_base, None) is gs_base - assert uri_resolve(gs_base, '') is gs_base - assert uri_resolve(gs_base, 'relative/path') == gs_base + '/relative/path' + assert uri_resolve(base, abs_path) == "file://" + abs_path + assert uri_resolve(base, some_uri) is some_uri + assert uri_resolve(base, None) is base + assert uri_resolve(base, '') is base + assert uri_resolve(base, 'relative/path') == base + '/relative/path' + assert uri_resolve(base + '/', 'relative/path') == base + '/relative/path' + assert uri_resolve(base + '/some/dir/', 'relative/path') == base + '/some/dir/relative/path' - assert uri_resolve(vsi_base, 'relative/path') == vsi_base + '/relative/path' - assert uri_resolve(vsi_base + '/', 'relative/path') == vsi_base + '/relative/path' + if not is_vsipath(base): + assert uri_resolve(base + '/some/dir/file.txt', 'relative/path') == base + '/some/dir/relative/path' def test_pick_uri(): @@ -511,6 +510,7 @@ def test_testutils_geobox(): ("file:///foo/bar/file.txt", True), ("test.bar", False), ("s3://mybucket/objname.tiff", True), + ("gs://mybucket/objname.tiff", True), ("ftp://host.name/filename.txt", True), ("https://host.name.com/path/file.txt", True), ("http://host.name.com/path/file.txt", True),