Skip to content

Commit

Permalink
chore(deps-dev): apply ruff format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ehossack committed Jan 6, 2025
1 parent ba350b9 commit 6d1f5cd
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 89 deletions.
143 changes: 89 additions & 54 deletions tests/test_b2_storage_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,57 @@ def test_requires_configuration_for_auth(settings):


def test_explicit_opts_take_precedence_over_django_config(settings):
with mock.patch.object(
settings, "BACKBLAZE_CONFIG", _settings_dict({"bucket": "uncool-bucket"})
), mock.patch.object(B2Api, "authorize_account"), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({"bucket": "uncool-bucket"})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
BackblazeB2Storage(opts={"bucket": "cool-bucket"})

B2Api.get_bucket_by_name.assert_called_once_with("cool-bucket")


def test_complains_with_unrecognized_options(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
with pytest.raises(ImproperlyConfigured) as error:
BackblazeB2Storage(opts={"unrecognized": "option"})

assert str(error.value) == "Unrecognized options: ['unrecognized']"


def test_kwargs_take_precedence_over_django_config(settings):
with mock.patch.object(
settings, "BACKBLAZE_CONFIG", _settings_dict({"bucket": "uncool-bucket"})
), mock.patch.object(B2Api, "authorize_account"), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({"bucket": "uncool-bucket"})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
BackblazeB2Storage(bucket="cool-bucket")

B2Api.get_bucket_by_name.assert_called_once_with("cool-bucket")


def test_complains_with_opts_and_kwargs(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
with pytest.raises(ImproperlyConfigured) as error:
BackblazeB2Storage(bucket="cool-bucket", opts={"allow_file_overwrites": True})

assert str(error.value) == "Can only specify opts or keyword args, not both!"


def test_defaults_to_authorize_on_init(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
BackblazeB2Storage(opts={})

B2Api.authorize_account.assert_called_once_with(
Expand All @@ -84,29 +94,34 @@ def test_defaults_to_authorize_on_init(settings):


def test_defaults_to_validate_init(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
BackblazeB2Storage(opts={})

B2Api.get_bucket_by_name.assert_called_once_with("django")


def test_defaults_to_not_creating_bucket(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name", side_effect=NonExistentBucket):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name", side_effect=NonExistentBucket),
):
with pytest.raises(NonExistentBucket):
BackblazeB2Storage(opts={})

B2Api.get_bucket_by_name.assert_called_once_with("django")


def test_can_create_bucket(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name", side_effect=NonExistentBucket), mock.patch.object(
B2Api, "create_bucket"
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name", side_effect=NonExistentBucket),
mock.patch.object(B2Api, "create_bucket"),
):
BackblazeB2Storage(opts={"non_existent_bucket_details": {}})

Expand All @@ -115,9 +130,11 @@ def test_can_create_bucket(settings):


def test_lazy_authorization(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
storage = BackblazeB2Storage(opts={"authorize_on_init": False})
B2Api.authorize_account.assert_not_called()
B2Api.get_bucket_by_name.assert_not_called()
Expand Down Expand Up @@ -154,18 +171,22 @@ def test_cached_account_info(settings):
)
cache_account_info.save_bucket(bucket)

with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "list_buckets"):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "list_buckets"),
):
BackblazeB2Storage(opts={"account_info": {"type": "django-cache", "cache": cache_name}})

B2Api.list_buckets.assert_not_called()


def test_lazy_bucket_non_existent(settings):
with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name", side_effect=NonExistentBucket):
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name", side_effect=NonExistentBucket),
):
storage = BackblazeB2Storage(opts={"validate_on_init": False})
B2Api.get_bucket_by_name.assert_not_called()

Expand Down Expand Up @@ -198,9 +219,11 @@ def test_get_available_name_with_overwrites(settings):
)
mocked_bucket.name = "bucket"

with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name") as api:
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name") as api,
):
api.return_value = mocked_bucket
storage = BackblazeB2Storage(opts={"allow_file_overwrites": True})

Expand All @@ -217,9 +240,11 @@ def test_get_created_time(settings):
)
mocked_bucket.name = "bucket"

with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name") as api:
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name") as api,
):
api.return_value = mocked_bucket
storage = BackblazeB2Storage()

Expand All @@ -236,9 +261,11 @@ def test_get_modified_time(settings):
)
mocked_bucket.name = "bucket"

with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name") as api:
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name") as api,
):
api.return_value = mocked_bucket
storage = BackblazeB2Storage()

Expand All @@ -256,9 +283,11 @@ def test_get_size_without_caching(settings):
)
mocked_bucket.name = "bucket"

with mock.patch.object(
settings, "BACKBLAZE_CONFIG", _settings_dict({"forbid_file_property_caching": True})
), mock.patch.object(B2Api, "authorize_account"), mock.patch.object(B2Api, "get_bucket_by_name") as api:
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({"forbid_file_property_caching": True})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name") as api,
):
api.return_value = mocked_bucket
storage = BackblazeB2Storage()

Expand Down Expand Up @@ -286,9 +315,11 @@ def test_exists_file_does_not_exist(settings):
mocked_bucket.name = "bucketname"
mocked_bucket.get_file_info_by_name.side_effect = FileNotPresent()

with mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})), mock.patch.object(
B2Api, "authorize_account"
), mock.patch.object(B2Api, "get_bucket_by_name") as api:
with (
mock.patch.object(settings, "BACKBLAZE_CONFIG", _settings_dict({})),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name") as api,
):
api.return_value = mocked_bucket
storage = BackblazeB2Storage(opts={})

Expand All @@ -302,11 +333,15 @@ def test_can_use_sqlite_account_info(settings, tmpdir, caplog):
caplog.set_level(logging.DEBUG, logger="django-backblaze-b2")
tempfile = tmpdir.mkdir("sub").join("database.sqlite3")
tempfile.write("some-invalid-context")
with mock.patch.object(
settings,
"BACKBLAZE_CONFIG",
_settings_dict({"account_info": {"type": "sqlite", "database_path": str(tempfile)}}),
), mock.patch.object(B2Api, "authorize_account"), mock.patch.object(B2Api, "get_bucket_by_name"):
with (
mock.patch.object(
settings,
"BACKBLAZE_CONFIG",
_settings_dict({"account_info": {"type": "sqlite", "database_path": str(tempfile)}}),
),
mock.patch.object(B2Api, "authorize_account"),
mock.patch.object(B2Api, "get_bucket_by_name"),
):
with pytest.raises(CorruptAccountInfo) as error:
BackblazeB2Storage(opts={})

Expand Down
61 changes: 40 additions & 21 deletions tests/test_django_backblaze_b2.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ def test_raises_no_exception_when_loading_model():
@pytest.mark.django_db
def test_uploads_bytes_to_bucket(tempfile):
_mock_filedoesnotexist(tempfile)
with _mocked_bucket(), mock.patch.object(
B2Api, "get_download_url_for_file_name", return_value="http://randonneurs.bc.ca"
) as get_download_url:
with (
_mocked_bucket(),
mock.patch.object(
B2Api, "get_download_url_for_file_name", return_value="http://randonneurs.bc.ca"
) as get_download_url,
):
from tests.test_project.files.models import Files

files_object = Files.objects.create(b2_storagefile=tempfile)
Expand Down Expand Up @@ -152,11 +155,15 @@ def test_generates_public_file_url(tempfile):

@pytest.mark.django_db
def test_generates_public_file_url_as_raw_b2_url():
with _mocked_bucket(), mock.patch.object(bucket, "as_dict", return_value=sdk_public_bucket_dict), mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
) as get_download_url:
with (
_mocked_bucket(),
mock.patch.object(bucket, "as_dict", return_value=sdk_public_bucket_dict),
mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
) as get_download_url,
):
from django_backblaze_b2 import PublicStorage

storage = PublicStorage()
Expand All @@ -167,10 +174,14 @@ def test_generates_public_file_url_as_raw_b2_url():

@pytest.mark.django_db
def test_generates_public_file_url_as_cdn_url():
with _mocked_bucket(), mock.patch.object(bucket, "as_dict", return_value=sdk_public_bucket_dict), mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
with (
_mocked_bucket(),
mock.patch.object(bucket, "as_dict", return_value=sdk_public_bucket_dict),
mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
),
):
from django_backblaze_b2 import PublicStorage

Expand All @@ -183,10 +194,14 @@ def test_generates_public_file_url_as_cdn_url():

@pytest.mark.django_db
def test_generates_public_file_url_as_cdn_url_without_path():
with _mocked_bucket(), mock.patch.object(bucket, "as_dict", return_value=sdk_public_bucket_dict), mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
with (
_mocked_bucket(),
mock.patch.object(bucket, "as_dict", return_value=sdk_public_bucket_dict),
mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
),
):
from django_backblaze_b2 import PublicStorage

Expand All @@ -210,11 +225,15 @@ def get_dict() -> Union[_SdkBucketDict, Dict[str, None]]:
return {}
return sdk_public_bucket_dict

with _mocked_bucket(), mock.patch.object(bucket, "as_dict", side_effect=get_dict), mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
) as get_download_url:
with (
_mocked_bucket(),
mock.patch.object(bucket, "as_dict", side_effect=get_dict),
mock.patch.object(
B2Api,
"get_download_url_for_file_name",
side_effect=lambda bucket_name, file_name: f"https://f000.backblazeb2.com/file/{bucket_name}/{file_name}",
) as get_download_url,
):
from django_backblaze_b2 import PublicStorage

storage = PublicStorage()
Expand Down
Loading

0 comments on commit 6d1f5cd

Please sign in to comment.