Skip to content

Commit

Permalink
Add BucketTrackingMixin class for tracking buckets created by `B2Ap…
Browse files Browse the repository at this point in the history
…i` instances
  • Loading branch information
mlesniewski-reef committed Nov 26, 2022
1 parent 2339a81 commit 998e46b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Additional tests for listing files/versions
* Ensured that changelog validation only happens on pull requests
* Upgraded GitHub actions checkout to v3, python-setup to v4
* Add `BucketTrackingMixin` class for tracking buckets created by `B2Api` instances

## [1.18.0] - 2022-09-20

Expand Down
2 changes: 1 addition & 1 deletion b2sdk/_v3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# core

from b2sdk.api import B2Api
from b2sdk.api import B2Api, BucketTrackingMixin
from b2sdk.api import Services
from b2sdk.bucket import Bucket
from b2sdk.bucket import BucketFactory
Expand Down
23 changes: 23 additions & 0 deletions b2sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,26 @@ def _populate_bucket_cache_from_key(self):
raise RestrictedBucketMissing()

self.cache.save_bucket(self.BUCKET_CLASS(self, allowed_bucket_id, name=allowed_bucket_name))


class BucketTrackingMixin:
"""
Mixin class for B2Api, which enables bucket tracking.
This mixin will add a `buckets` member to the B2Api instance and will use it track created and
deleted buckets. The main purpose of this are tests -- the `buckets` member can be used in test
teardown to ensure proper bucket cleanup.
"""

def __init__(self, *args, **kwargs):
self.buckets = []
super().__init__(*args, **kwargs)

def create_bucket(self, name, *args, **kwargs):
bucket = super().create_bucket(name, *args, **kwargs)
self.buckets.append(bucket)
return bucket

def delete_bucket(self, bucket):
super().delete_bucket(bucket)
self.buckets = [b for b in self.buckets if b.id_ != bucket.id_]
31 changes: 28 additions & 3 deletions test/unit/v_all/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest

from apiver_deps import B2Api
from apiver_deps import B2Api, BucketTrackingMixin
from apiver_deps import B2HttpApiConfig
from apiver_deps import Bucket
from apiver_deps import InMemoryCache
Expand Down Expand Up @@ -102,11 +102,13 @@ def test_api_initialization(self, kwargs, _raw_api_class):
assert download_manager.strategies[0].max_streams == kwargs['max_download_streams_per_file']


class TestApi(TestBase):
class TestApiBase(TestBase):
B2_API_CLASS = B2Api

def setUp(self):
self.account_info = InMemoryAccountInfo()
self.cache = InMemoryCache()
self.api = B2Api(
self.api = self.B2_API_CLASS(
self.account_info, self.cache, api_config=B2HttpApiConfig(_raw_api_class=RawSimulator)
)
self.raw_api = self.api.session.raw_api
Expand All @@ -115,6 +117,8 @@ def setUp(self):
def _authorize_account(self):
self.api.authorize_account('production', self.application_key_id, self.master_key)


class TestApi(TestApiBase):
@pytest.mark.apiver(to_ver=1)
def test_get_bucket_by_id_up_to_v1(self):
bucket = self.api.get_bucket_by_id("this id doesn't even exist")
Expand Down Expand Up @@ -158,3 +162,24 @@ def test_get_download_url_for_fileid(self):
download_url = self.api.get_download_url_for_fileid('file-id')

assert download_url == 'http://download.example.com/b2api/v2/b2_download_file_by_id?fileId=file-id'


class TestBucketTrackingMixin(TestApiBase):
class BucketTrackingApi(BucketTrackingMixin, B2Api):
pass

B2_API_CLASS = BucketTrackingApi

def test_bucket_tracking(self):
self._authorize_account()

bucket_1, bucket_2, bucket_3 = [
self.api.create_bucket(f'bucket-{i + 1}', 'allPrivate') for i in range(3)
]

self.api.delete_bucket(bucket_2)
self.api.delete_bucket(bucket_3)

bucket_4 = self.api.create_bucket('bucket-4', 'allPrivate')

assert {bucket.id_ for bucket in self.api.buckets} == {bucket_1.id_, bucket_4.id_}

0 comments on commit 998e46b

Please sign in to comment.