Skip to content

Commit

Permalink
Bucket unit test for v1 and v2 are now common
Browse files Browse the repository at this point in the history
  • Loading branch information
mpnowacki-reef committed May 10, 2021
1 parent 4cf12ce commit e877b15
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Refactored `sync.file.*File` and `sync.file.*FileVersion` to `sync.path.*SyncPath`
* Encryption settings, types and providers are now part of the public API
* Refactored `FileVersionInfo` to `FileVersion`
* `Bucket` unit tests for v1 and v2 are now common

### Removed
* Remove `Bucket.copy_file` and `Bucket.start_large_file`
Expand Down
9 changes: 9 additions & 0 deletions test/unit/bucket/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
######################################################################
#
# File: test/unit/bucket/__init__.py
#
# Copyright 2021 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
76 changes: 48 additions & 28 deletions test/unit/v1/test_bucket.py → test/unit/bucket/test_bucket.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
######################################################################
#
# File: test/unit/v1/test_bucket.py
# File: test/unit/bucket/test_bucket.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
# Copyright 2021 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
Expand All @@ -17,7 +17,7 @@

from ..test_base import TestBase

from .deps_exception import (
from apiver_deps_exception import (
AlreadyFailed,
B2Error,
B2RequestTimeoutDuringUpload,
Expand All @@ -30,20 +30,26 @@
FileSha1Mismatch,
SSECKeyError,
)
from .deps import B2Api
from .deps import LargeFileUploadState
from .deps import DownloadDestBytes, PreSeekedDownloadDest
from .deps import FileVersionInfo
from .deps import MetadataDirectiveMode
from .deps import Part
from .deps import AbstractProgressListener
from .deps import StubAccountInfo, RawSimulator, BucketSimulator, FakeResponse, FileSimulator
from .deps import ParallelDownloader
from .deps import SimpleDownloader
from .deps import UploadSourceBytes
from .deps import hex_sha1_of_bytes, TempDir
from .deps import EncryptionAlgorithm, EncryptionSetting, EncryptionMode, EncryptionKey, SSE_NONE, SSE_B2_AES
from .deps import CopySource, UploadSourceLocalFile, WriteIntent
from apiver_deps import B2Api
from apiver_deps import LargeFileUploadState
from apiver_deps import DownloadDestBytes, PreSeekedDownloadDest
from apiver_deps import MetadataDirectiveMode
from apiver_deps import Part
from apiver_deps import AbstractProgressListener
from apiver_deps import StubAccountInfo, RawSimulator, BucketSimulator, FakeResponse, FileSimulator
from apiver_deps import ParallelDownloader
from apiver_deps import SimpleDownloader
from apiver_deps import UploadSourceBytes
from apiver_deps import hex_sha1_of_bytes, TempDir
from apiver_deps import EncryptionAlgorithm, EncryptionSetting, EncryptionMode, EncryptionKey, SSE_NONE, SSE_B2_AES
from apiver_deps import CopySource, UploadSourceLocalFile, WriteIntent
import apiver_deps
if apiver_deps.V <= 1:
from apiver_deps import FileVersionInfo as VFileVersionInfo
else:
from apiver_deps import FileVersion as VFileVersionInfo

pytestmark = [pytest.mark.apiver(from_ver=1)]

SSE_C_AES = EncryptionSetting(
mode=EncryptionMode.SSE_C,
Expand Down Expand Up @@ -210,10 +216,12 @@ def __call__(self, *args, **kwargs):


class TestListParts(TestCaseWithBucket):
@pytest.mark.apiver(to_ver=1)
def testEmpty(self):
file1 = self.bucket.start_large_file('file1.txt', 'text/plain', {})
self.assertEqual([], list(self.bucket.list_parts(file1.file_id, batch_size=1)))

@pytest.mark.apiver(to_ver=1)
def testThree(self):
file1 = self.bucket.start_large_file('file1.txt', 'text/plain', {})
content = b'hello world'
Expand All @@ -238,6 +246,7 @@ def testThree(self):


class TestUploadPart(TestCaseWithBucket):
@pytest.mark.apiver(to_ver=1)
def test_error_in_state(self):
file1 = self.bucket.start_large_file('file1.txt', 'text/plain', {})
content = b'hello world'
Expand All @@ -258,10 +267,12 @@ class TestListUnfinished(TestCaseWithBucket):
def test_empty(self):
self.assertEqual([], list(self.bucket.list_unfinished_large_files()))

@pytest.mark.apiver(to_ver=1)
def test_one(self):
file1 = self.bucket.start_large_file('file1.txt', 'text/plain', {})
self.assertEqual([file1], list(self.bucket.list_unfinished_large_files()))

@pytest.mark.apiver(to_ver=1)
def test_three(self):
file1 = self.bucket.start_large_file('file1.txt', 'text/plain', {})
file2 = self.bucket.start_large_file('file2.txt', 'text/plain', {})
Expand All @@ -270,6 +281,7 @@ def test_three(self):
[file1, file2, file3], list(self.bucket.list_unfinished_large_files(batch_size=1))
)

@pytest.mark.apiver(to_ver=1)
def test_prefix(self):
self.bucket.start_large_file('fileA', 'text/plain', {})
file2 = self.bucket.start_large_file('fileAB', 'text/plain', {})
Expand All @@ -293,7 +305,7 @@ def test_version_by_name(self):

info = self.bucket.get_file_info_by_name('a')

self.assertIsInstance(info, FileVersionInfo)
self.assertIsInstance(info, VFileVersionInfo)
expected = (a_id, 'a', 11, None, 'b2/x-auto', 'none')
actual = (
info.id_, info.file_name, info.size, info.action, info.content_type,
Expand All @@ -307,7 +319,7 @@ def test_version_by_id(self):

info = self.bucket.get_file_info_by_id(b_id)

self.assertIsInstance(info, FileVersionInfo)
self.assertIsInstance(info, VFileVersionInfo)
expected = (b_id, 'b', 11, 'upload', 'b2/x-auto', 'none')
actual = (
info.id_, info.file_name, info.size, info.action, info.content_type,
Expand Down Expand Up @@ -372,6 +384,7 @@ def test_three_files_multiple_versions(self):
]
self.assertEqual(expected, actual)

@pytest.mark.apiver(to_ver=1)
def test_started_large_file(self):
self.bucket.start_large_file('hello.txt')
expected = [('hello.txt', 0, 'start', None)]
Expand Down Expand Up @@ -502,18 +515,21 @@ def test_encryption(self):


class TestCopyFile(TestCaseWithBucket):
@pytest.mark.apiver(to_ver=1)
def test_copy_without_optional_params(self):
file_id = self._make_file()
self.bucket.copy_file(file_id, 'hello_new.txt')
expected = [('hello.txt', 11, 'upload', None), ('hello_new.txt', 11, 'copy', None)]
self.assertBucketContents(expected, '', show_versions=True)

@pytest.mark.apiver(to_ver=1)
def test_copy_with_range(self):
file_id = self._make_file()
self.bucket.copy_file(file_id, 'hello_new.txt', bytes_range=(3, 9))
expected = [('hello.txt', 11, 'upload', None), ('hello_new.txt', 6, 'copy', None)]
self.assertBucketContents(expected, '', show_versions=True)

@pytest.mark.apiver(to_ver=1)
def test_copy_with_invalid_metadata(self):
file_id = self._make_file()
try:
Expand All @@ -532,6 +548,7 @@ def test_copy_with_invalid_metadata(self):
expected = [('hello.txt', 11, 'upload', None)]
self.assertBucketContents(expected, '', show_versions=True)

@pytest.mark.apiver(to_ver=1)
def test_copy_with_invalid_metadata_replace(self):
file_id = self._make_file()
try:
Expand All @@ -549,6 +566,7 @@ def test_copy_with_invalid_metadata_replace(self):
expected = [('hello.txt', 11, 'upload', None)]
self.assertBucketContents(expected, '', show_versions=True)

@pytest.mark.apiver(to_ver=1)
def test_copy_with_replace_metadata(self):
file_id = self._make_file()
self.bucket.copy_file(
Expand All @@ -567,6 +585,7 @@ def test_copy_with_replace_metadata(self):
]
self.assertEqual(expected, actual)

@pytest.mark.apiver(to_ver=1)
def test_copy_with_unsatisfied_range(self):
file_id = self._make_file()
try:
Expand All @@ -584,6 +603,7 @@ def test_copy_with_unsatisfied_range(self):
expected = [('hello.txt', 11, 'upload', None)]
self.assertBucketContents(expected, '', show_versions=True)

@pytest.mark.apiver(to_ver=1)
def test_copy_with_different_bucket(self):
source_bucket = self.api.create_bucket('source-bucket', 'allPublic')
file_id = self._make_file(source_bucket)
Expand Down Expand Up @@ -720,7 +740,7 @@ def test_copy_encryption(self):
]:
with self.subTest(kwargs=kwargs, length=length, data=data):
file_info = self.bucket.copy(**kwargs, new_file_name='new_file', length=length)
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self.assertEqual(file_info.server_side_encryption, expected_encryption)

def _make_file(self, bucket=None):
Expand All @@ -733,20 +753,20 @@ class TestUpload(TestCaseWithBucket):
def test_upload_bytes(self):
data = b'hello world'
file_info = self.bucket.upload_bytes(data, 'file1')
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self._check_file_contents('file1', data)
self.assertEqual(file_info.server_side_encryption, SSE_NONE)

def test_upload_bytes_sse_b2(self):
data = b'hello world'
file_info = self.bucket.upload_bytes(data, 'file1', encryption=SSE_B2_AES)
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self.assertEqual(file_info.server_side_encryption, SSE_B2_AES)

def test_upload_bytes_sse_c(self):
data = b'hello world'
file_info = self.bucket.upload_bytes(data, 'file1', encryption=SSE_C_AES)
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self.assertEqual(SSE_C_AES_NO_SECRET, file_info.server_side_encryption)

def test_upload_local_file_sse_b2(self):
Expand All @@ -755,7 +775,7 @@ def test_upload_local_file_sse_b2(self):
data = b'hello world'
write_file(path, data)
file_info = self.bucket.upload_local_file(path, 'file1', encryption=SSE_B2_AES)
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self.assertEqual(file_info.server_side_encryption, SSE_B2_AES)
self._check_file_contents('file1', data)

Expand All @@ -765,7 +785,7 @@ def test_upload_local_file_sse_c(self):
data = b'hello world'
write_file(path, data)
file_info = self.bucket.upload_local_file(path, 'file1', encryption=SSE_C_AES)
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self.assertEqual(SSE_C_AES_NO_SECRET, file_info.server_side_encryption)
self._check_file_contents('file1', data)

Expand All @@ -782,7 +802,7 @@ def test_upload_local_file(self):
write_file(path, data)
file_info = self.bucket.upload_local_file(path, 'file1')
self._check_file_contents('file1', data)
self.assertTrue(isinstance(file_info, FileVersionInfo))
self.assertTrue(isinstance(file_info, VFileVersionInfo))
self.assertEqual(file_info.server_side_encryption, SSE_NONE)
print(file_info.as_dict())
self.assertEqual(file_info.as_dict()['serverSideEncryption'], {'mode': 'none'})
Expand Down Expand Up @@ -991,7 +1011,7 @@ def test_create_remote(self):
],
file_name='created_file'
)
self.assertIsInstance(created_file, FileVersionInfo)
self.assertIsInstance(created_file, VFileVersionInfo)
actual = (
created_file.id_, created_file.file_name, created_file.size,
created_file.server_side_encryption
Expand All @@ -1016,7 +1036,7 @@ def test_create_remote_encryption(self):
file_name='created_file_%s' % (len(data),),
encryption=SSE_C_AES
)
self.assertIsInstance(created_file, FileVersionInfo)
self.assertIsInstance(created_file, VFileVersionInfo)
actual = (
created_file.id_, created_file.file_name, created_file.size,
created_file.server_side_encryption
Expand Down
16 changes: 16 additions & 0 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ def test_illegal_regex(self, exc_class, exc_msg):
with pytest.raises(exc_class, match=exc_msg):
error_raising_function()
If a test is merked with "apiver" multiple times, it will be skipped if at least one of the apiver conditions
cause it to be skipped. E.g. if a test module is marked with
.. code-block:: python
pytestmark = [pytest.mark.apiver(from_ver=1)]
and a test function is marked with
.. code-block:: python
@pytest.mark.apiver(to_ver=1)
def test_function(self):
...
the test function will be run only for apiver=v1
"""
for mark in item.iter_markers(name='apiver'):
if mark.args and mark.kwargs:
Expand Down

0 comments on commit e877b15

Please sign in to comment.