|
|
|
|
|
import unittest |
|
|
import os |
|
|
from s3pathlib.utils import ( |
|
|
split_s3_uri, |
|
|
join_s3_uri, |
|
|
split_parts, |
|
|
smart_join_s3_key, |
|
|
make_s3_console_url, |
|
|
ensure_s3_object, |
|
|
ensure_s3_dir, |
|
|
repr_data_size, |
|
|
parse_data_size, |
|
|
) |
|
|
from s3pathlib.tests import run_cov_test |
|
|
|
|
|
dir_here = os.path.dirname(os.path.abspath(__file__)) |
|
|
dir_project_root = os.path.dirname(dir_here) |
|
|
|
|
|
|
|
|
class TestS3Utils(unittest.TestCase): |
|
|
|
|
|
def test_split_s3_uri(self): |
|
|
s3_uri = "s3://my-bucket/my-prefix/my-file.zip" |
|
|
bucket, key = split_s3_uri(s3_uri) |
|
|
self.assertEqual(bucket, "my-bucket") |
|
|
self.assertEqual(key, "my-prefix/my-file.zip") |
|
|
|
|
|
def test_join_s3_uri(self): |
|
|
bucket = "my-bucket" |
|
|
key = "my-prefix/my-file.zip" |
|
|
s3_uri = join_s3_uri(bucket, key) |
|
|
self.assertEqual(s3_uri, "s3://my-bucket/my-prefix/my-file.zip") |
|
|
|
|
|
def test_split_parts(self): |
|
|
self.assertEqual(split_parts("a/b/c"), ["a", "b", "c"]) |
|
|
self.assertEqual(split_parts("//a//b//c//"), ["a", "b", "c"]) |
|
|
self.assertEqual(split_parts(""), []) |
|
|
self.assertEqual(split_parts("////"), []) |
|
|
|
|
|
def test_s3_key_smart_join(self): |
|
|
self.assertEqual(smart_join_s3_key(parts=["/a/", "b/", "/c"], is_dir=True), "a/b/c/") |
|
|
self.assertEqual(smart_join_s3_key(parts=["/a/", "b/", "/c"], is_dir=False), "a/b/c") |
|
|
self.assertEqual(smart_join_s3_key(parts=["//a//b//c//"], is_dir=True), "a/b/c/") |
|
|
self.assertEqual(smart_join_s3_key(parts=["//a//b//c//"], is_dir=False), "a/b/c") |
|
|
|
|
|
def test_make_s3_console_url(self): |
|
|
|
|
|
url = make_s3_console_url("my-bucket", "my-file.zip") |
|
|
self.assertIn("object", url) |
|
|
|
|
|
|
|
|
url = make_s3_console_url("my-bucket", "my-folder/") |
|
|
self.assertIn("bucket", url) |
|
|
|
|
|
|
|
|
url = make_s3_console_url(s3_uri="s3://my-bucket/my-folder/data.json") |
|
|
self.assertEqual(url, "https://console.aws.amazon.com/s3/object/my-bucket?prefix=my-folder/data.json") |
|
|
|
|
|
|
|
|
url = make_s3_console_url(s3_uri="s3://my-bucket/") |
|
|
self.assertEqual(url, "https://console.aws.amazon.com/s3/buckets/my-bucket?tab=objects") |
|
|
|
|
|
|
|
|
url = make_s3_console_url(s3_uri="s3://my-bucket/my-folder/my-file.zip", version_id="v123") |
|
|
self.assertEqual(url, "https://console.aws.amazon.com/s3/object/my-bucket?prefix=my-folder/my-file.zip&versionId=v123") |
|
|
|
|
|
|
|
|
url = make_s3_console_url(s3_uri="s3://my-bucket/my-folder/data.json", is_us_gov_cloud=True) |
|
|
self.assertEqual(url, "https://console.amazonaws-us-gov.com/s3/object/my-bucket?prefix=my-folder/data.json") |
|
|
|
|
|
with self.assertRaises(ValueError): |
|
|
make_s3_console_url(bucket="") |
|
|
|
|
|
with self.assertRaises(ValueError): |
|
|
make_s3_console_url(prefix="", s3_uri="") |
|
|
|
|
|
def test_ensure_s3_object(self): |
|
|
ensure_s3_object("path/to/key") |
|
|
with self.assertRaises(Exception): |
|
|
ensure_s3_object("path/to/dir/") |
|
|
|
|
|
def test_ensure_s3_dir(self): |
|
|
ensure_s3_dir("path/to/dir/") |
|
|
with self.assertRaises(Exception): |
|
|
ensure_s3_dir("path/to/key") |
|
|
|
|
|
def test_repr_data_size(self): |
|
|
self.assertEqual(repr_data_size(3600000), "3.43 MB") |
|
|
|
|
|
def test_parse_data_size(self): |
|
|
self.assertEqual(parse_data_size("3.43 MB"), 3596615) |
|
|
self.assertEqual(parse_data_size("2_512.4 MB"), 2634442342) |
|
|
self.assertEqual(parse_data_size("2,512.4 MB"), 2634442342) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
run_cov_test(__file__, "s3pathlib.utils", preview=False) |