Skip to content

Commit 566b671

Browse files
Move test helpers and fixtures to the project's testing module
1 parent fd4274f commit 566b671

File tree

16 files changed

+182
-121
lines changed

16 files changed

+182
-121
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ venv
1313
.venv
1414
.vscode
1515
.pdm-build/
16-
.pdm-python
16+
.pdm-python
17+
.env

b2sdk/_internal/testing/__init__.py

Whitespace-only changes.

test/integration/cleanup_buckets.py renamed to b2sdk/_internal/testing/cleanup_buckets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
from test.integration.helpers import authorize
1313

14-
from . import get_b2_auth_data
15-
from .bucket_cleaner import BucketCleaner
16-
from .test_raw_api import cleanup_old_buckets
14+
from b2sdk._internal.testing.helpers.api import get_b2_auth_data
15+
from b2sdk._internal.testing.helpers.bucket_cleaner import BucketCleaner
16+
# from .test_raw_api import cleanup_old_buckets
1717

1818
if __name__ == '__main__':
19-
cleanup_old_buckets()
19+
# cleanup_old_buckets()
2020
BucketCleaner(
2121
dont_cleanup_old_buckets=False, b2_api=authorize(get_b2_auth_data())[0]
2222
).cleanup_buckets()

b2sdk/_internal/testing/fixtures/__init__.py

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
######################################################################
3+
#
4+
# File: test/integration/conftest.py
5+
#
6+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
7+
#
8+
# License https://www.backblaze.com/using_b2_code.html
9+
#
10+
######################################################################
11+
from __future__ import annotations
12+
13+
import http
14+
import http.client
15+
import os
16+
17+
import pytest
18+
19+
from b2sdk._internal.testing.helpers.api import get_b2_auth_data, authorize
20+
21+
22+
@pytest.fixture(autouse=True, scope='session')
23+
def set_http_debug():
24+
if os.environ.get('B2_DEBUG_HTTP'):
25+
http.client.HTTPConnection.debuglevel = 1
26+
27+
28+
@pytest.fixture(scope='session')
29+
def b2_auth_data():
30+
try:
31+
return get_b2_auth_data()
32+
except ValueError as ex:
33+
pytest.fail(ex.args[0])
34+
35+
36+
@pytest.fixture(scope='session')
37+
def _b2_api(b2_auth_data):
38+
b2_api, _ = authorize(b2_auth_data)
39+
return b2_api
40+
41+
42+
@pytest.fixture(scope='session')
43+
def b2_api(_b2_api, bucket_cleaner):
44+
return _b2_api
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
######################################################################
2+
#
3+
# File: test/integration/conftest.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from __future__ import annotations
11+
import secrets
12+
13+
import pytest
14+
15+
from b2sdk._internal.utils import current_time_millis
16+
from b2sdk._internal.testing.helpers.bucket_cleaner import BucketCleaner
17+
from b2sdk._internal.testing.helpers.buckets import (
18+
BUCKET_CREATED_AT_MILLIS,
19+
get_bucket_name_prefix,
20+
random_bucket_name,
21+
)
22+
23+
24+
def pytest_addoption(parser):
25+
"""Add a flag for not cleaning up old buckets"""
26+
parser.addoption(
27+
'--dont-cleanup-old-buckets',
28+
action='store_true',
29+
default=False,
30+
)
31+
32+
33+
@pytest.fixture(scope='session')
34+
def dont_cleanup_old_buckets(request):
35+
return request.config.getoption('--dont-cleanup-old-buckets')
36+
37+
38+
@pytest.fixture(scope='session')
39+
def bucket_name_prefix():
40+
return get_bucket_name_prefix(8)
41+
42+
43+
@pytest.fixture(scope='session')
44+
def bucket_cleaner(bucket_name_prefix, dont_cleanup_old_buckets, _b2_api):
45+
cleaner = BucketCleaner(
46+
dont_cleanup_old_buckets,
47+
_b2_api,
48+
current_run_prefix=bucket_name_prefix,
49+
)
50+
yield cleaner
51+
cleaner.cleanup_buckets()
52+
53+
54+
@pytest.fixture
55+
def bucket(b2_api, bucket_name_prefix, bucket_cleaner):
56+
bucket = b2_api.create_bucket(
57+
random_bucket_name(bucket_name_prefix),
58+
'allPrivate',
59+
bucket_info={
60+
'created_by': 'b2-sdk integration test',
61+
BUCKET_CREATED_AT_MILLIS: str(current_time_millis()),
62+
},
63+
)
64+
yield bucket
65+
bucket_cleaner.cleanup_bucket(bucket)
66+
67+
68+
@pytest.fixture
69+
def b2_subfolder(bucket, request):
70+
subfolder_name = f'{request.node.name}_{secrets.token_urlsafe(4)}'
71+
return f'b2://{bucket.name}/{subfolder_name}'

b2sdk/_internal/testing/helpers/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
######################################################################
2+
#
3+
# File: test/integration/helpers.py
4+
#
5+
# Copyright 2022 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from __future__ import annotations
11+
12+
import os
13+
14+
from b2sdk.v3 import (
15+
DEFAULT_HTTP_API_CONFIG,
16+
B2Api,
17+
InMemoryAccountInfo,
18+
)
19+
20+
21+
def get_b2_auth_data():
22+
application_key_id = os.environ.get('B2_TEST_APPLICATION_KEY_ID')
23+
if application_key_id is None:
24+
raise ValueError('B2_TEST_APPLICATION_KEY_ID is not set.')
25+
26+
application_key = os.environ.get('B2_TEST_APPLICATION_KEY')
27+
if application_key is None:
28+
raise ValueError('B2_TEST_APPLICATION_KEY is not set.')
29+
return application_key_id, application_key
30+
31+
32+
def authorize(b2_auth_data, api_config=DEFAULT_HTTP_API_CONFIG):
33+
info = InMemoryAccountInfo()
34+
b2_api = B2Api(info, api_config=api_config)
35+
realm = os.environ.get('B2_TEST_ENVIRONMENT', 'production')
36+
b2_api.authorize_account(*b2_auth_data, realm=realm)
37+
return b2_api, info
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
from b2sdk.v2 import B2Api, current_time_millis
1515
from b2sdk.v2.exception import DuplicateBucketName
16-
from test.integration.bucket_cleaner import BucketCleaner
17-
from test.integration.helpers import (
16+
from b2sdk._internal.testing.helpers.bucket_cleaner import BucketCleaner
17+
from b2sdk._internal.testing.helpers.buckets import (
1818
BUCKET_CREATED_AT_MILLIS,
1919
random_bucket_name,
2020
)
@@ -58,7 +58,7 @@ def create_bucket(self):
5858
try:
5959
bucket = self.b2_api.create_bucket(
6060
bucket_name,
61-
'allPublic',
61+
'allPrivate',
6262
bucket_info={BUCKET_CREATED_AT_MILLIS: str(current_time_millis())},
6363
)
6464
except DuplicateBucketName:

test/integration/bucket_cleaner.py renamed to b2sdk/_internal/testing/helpers/bucket_cleaner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
current_time_millis,
2121
)
2222
from b2sdk.v3.exception import BadRequest
23+
from b2sdk._internal.testing.helpers.buckets import BUCKET_CREATED_AT_MILLIS, GENERAL_BUCKET_NAME_PREFIX
2324

24-
from .helpers import BUCKET_CREATED_AT_MILLIS, GENERAL_BUCKET_NAME_PREFIX
2525

2626
ONE_HOUR_MILLIS = 60 * 60 * 1000
2727

0 commit comments

Comments
 (0)