From 6aa1a441a9c635a79decffe9f99e43404f1aa53c Mon Sep 17 00:00:00 2001 From: Bryan Date: Fri, 22 May 2020 00:23:03 +0800 Subject: [PATCH] Add test cases for get bukcet, object exists, list dirs and list files. Refactor the scope of mock s3 --- testing/test_awswrangler/test_moto.py | 40 +++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/testing/test_awswrangler/test_moto.py b/testing/test_awswrangler/test_moto.py index ba8d605f4..9e754d47a 100644 --- a/testing/test_awswrangler/test_moto.py +++ b/testing/test_awswrangler/test_moto.py @@ -10,11 +10,12 @@ from ._utils import ensure_data_types, get_df_csv, get_df_list -@pytest.fixture(scope="module") +@pytest.fixture(scope="function") def s3(): with moto.mock_s3(): - boto3.resource("s3").create_bucket(Bucket="bucket") - yield True + s3 = boto3.resource("s3") + s3.create_bucket(Bucket="bucket") + yield s3 @pytest.fixture(scope="module") @@ -38,6 +39,39 @@ def subnet(): yield subnet.id +def test_get_bucket_region_succeed(s3): + region = wr.s3.get_bucket_region('bucket', boto3_session=boto3.Session()) + assert region == 'us-east-1' + + +def test_object_not_exist_succeed(s3): + result = wr.s3.does_object_exist('s3://bucket/test.csv') + assert result is False + + +def test_object_exist_succeed(s3): + path = "s3://bucket/test.csv" + wr.s3.to_csv(df=get_df_csv(), path=path, index=False) + result = wr.s3.does_object_exist(path) + assert result is True + + +def test_list_directories_succeed(s3): + path = "s3://bucket" + s3_object1 = s3.Object("bucket", "foo/foo.tmp") + s3_object2 = s3.Object("bucket", "bar/bar.tmp") + s3_object1.put(Body=b'foo') + s3_object2.put(Body=b'bar') + + dirs = wr.s3.list_directories(path) + files = wr.s3.list_objects(path) + + assert sorted(dirs) == sorted(["s3://bucket/foo/", + "s3://bucket/bar/"]) + assert sorted(files) == sorted(["s3://bucket/foo/foo.tmp", + "s3://bucket/bar/bar.tmp"]) + + def test_csv(s3): path = "s3://bucket/test.csv" wr.s3.to_csv(df=get_df_csv(), path=path, index=False)