Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermef committed May 1, 2022
1 parent 340f5ed commit 872cd12
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
35 changes: 24 additions & 11 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
from thumbor_aws.storage import Storage


@pytest.mark.usefixtures("test_images")
class LoaderTestCase(BaseS3TestCase):
class LoaderBaseTestCase(BaseS3TestCase):
def setUp(self):
super().setUp()
thumbor_aws.loader.S3_CLIENT = None


@pytest.mark.usefixtures("test_images")
class LoaderTestCase(LoaderBaseTestCase):
@property
def bucket_name(self):
"""Name of the bucket to put test files in"""
Expand Down Expand Up @@ -86,11 +88,11 @@ def bucket_name(self):


@pytest.mark.usefixtures("test_images")
class EmptyBucketConfigLoaderTestCase(BaseS3TestCase):
@property
def bucket_name(self):
"""Name of the bucket to put test files in"""
return self.context.config.AWS_LOADER_BUCKET_NAME
class EmptyBucketConfigLoaderTestCase(LoaderBaseTestCase):
def get_config(self) -> Config:
cfg = super().get_config()
cfg.AWS_LOADER_BUCKET_NAME = ""
return cfg

@gen_test
async def test_can_load_file_from_s3(self):
Expand All @@ -107,14 +109,25 @@ async def test_can_load_file_from_s3(self):
expect(exists).to_be_true()

filepath_with_bucket = (
f"/{self.context.config.AWS_LOADER_BUCKET_NAME}{filepath}"
f"/{self.context.config.AWS_STORAGE_BUCKET_NAME}{filepath}"
)

self.context.config.AWS_LOADER_BUCKET_NAME = ""

result = await load(self.context, filepath_with_bucket)
result = await thumbor_aws.loader.load(
self.context, filepath_with_bucket
)

expect(result.successful).to_be_true()
expect(result.buffer).to_equal(expected)
expect(result.metadata["size"]).to_equal(len(expected))
expect(result.metadata["updated_at"]).not_to_be_null()


@pytest.mark.usefixtures("test_images")
class LoaderNoPrefixTestCase(LoaderTestCase):
def get_config(self) -> Config:
cfg = super().get_config()
cfg.AWS_LOADER_BUCKET_NAME = "test-bucket-loader-no-prefix"
cfg.AWS_STORAGE_BUCKET_NAME = "test-bucket-loader-no-prefix"
cfg.AWS_LOADER_ROOT_PATH = ""
cfg.AWS_STORAGE_ROOT_PATH = ""
return cfg
7 changes: 5 additions & 2 deletions thumbor_aws/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ def get_s3_client(context):
async def load(context, path):
"""Loader to get source files from S3"""
s3_client = get_s3_client(context)
norm_path = normalize_url(s3_client.configuration["root_path"], path)
bucket, real_path = get_bucket_and_path(
s3_client.configuration["bucket_name"], path
)
norm_path = normalize_url(s3_client.configuration["root_path"], real_path)
result = LoaderResult()

status_code, body, last_modified = await s3_client.get_data(
norm_path, expiration=None
bucket, norm_path, expiration=None
)

if status_code != 200:
Expand Down

0 comments on commit 872cd12

Please sign in to comment.