diff --git a/CHANGELOG.md b/CHANGELOG.md index 49b445b98..d3d4e3d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/test/unit/bucket/__init__.py b/test/unit/bucket/__init__.py new file mode 100644 index 000000000..9d7c06249 --- /dev/null +++ b/test/unit/bucket/__init__.py @@ -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 +# +###################################################################### diff --git a/test/unit/v1/test_bucket.py b/test/unit/bucket/test_bucket.py similarity index 95% rename from test/unit/v1/test_bucket.py rename to test/unit/bucket/test_bucket.py index a0163088e..8606c5e6f 100644 --- a/test/unit/v1/test_bucket.py +++ b/test/unit/bucket/test_bucket.py @@ -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 # @@ -17,7 +17,7 @@ from ..test_base import TestBase -from .deps_exception import ( +from apiver_deps_exception import ( AlreadyFailed, B2Error, B2RequestTimeoutDuringUpload, @@ -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, @@ -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' @@ -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' @@ -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', {}) @@ -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', {}) @@ -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, @@ -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, @@ -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)] @@ -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: @@ -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: @@ -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( @@ -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: @@ -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) @@ -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): @@ -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): @@ -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) @@ -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) @@ -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'}) @@ -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 @@ -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 diff --git a/test/unit/conftest.py b/test/unit/conftest.py index 2510007c8..fe94258ba 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -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: