Skip to content

Commit e877b15

Browse files
Bucket unit test for v1 and v2 are now common
1 parent 4cf12ce commit e877b15

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
* Refactored `sync.file.*File` and `sync.file.*FileVersion` to `sync.path.*SyncPath`
2323
* Encryption settings, types and providers are now part of the public API
2424
* Refactored `FileVersionInfo` to `FileVersion`
25+
* `Bucket` unit tests for v1 and v2 are now common
2526

2627
### Removed
2728
* Remove `Bucket.copy_file` and `Bucket.start_large_file`

test/unit/bucket/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
######################################################################
2+
#
3+
# File: test/unit/bucket/__init__.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################

test/unit/v1/test_bucket.py renamed to test/unit/bucket/test_bucket.py

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
######################################################################
22
#
3-
# File: test/unit/v1/test_bucket.py
3+
# File: test/unit/bucket/test_bucket.py
44
#
5-
# Copyright 2019 Backblaze Inc. All Rights Reserved.
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
66
#
77
# License https://www.backblaze.com/using_b2_code.html
88
#
@@ -17,7 +17,7 @@
1717

1818
from ..test_base import TestBase
1919

20-
from .deps_exception import (
20+
from apiver_deps_exception import (
2121
AlreadyFailed,
2222
B2Error,
2323
B2RequestTimeoutDuringUpload,
@@ -30,20 +30,26 @@
3030
FileSha1Mismatch,
3131
SSECKeyError,
3232
)
33-
from .deps import B2Api
34-
from .deps import LargeFileUploadState
35-
from .deps import DownloadDestBytes, PreSeekedDownloadDest
36-
from .deps import FileVersionInfo
37-
from .deps import MetadataDirectiveMode
38-
from .deps import Part
39-
from .deps import AbstractProgressListener
40-
from .deps import StubAccountInfo, RawSimulator, BucketSimulator, FakeResponse, FileSimulator
41-
from .deps import ParallelDownloader
42-
from .deps import SimpleDownloader
43-
from .deps import UploadSourceBytes
44-
from .deps import hex_sha1_of_bytes, TempDir
45-
from .deps import EncryptionAlgorithm, EncryptionSetting, EncryptionMode, EncryptionKey, SSE_NONE, SSE_B2_AES
46-
from .deps import CopySource, UploadSourceLocalFile, WriteIntent
33+
from apiver_deps import B2Api
34+
from apiver_deps import LargeFileUploadState
35+
from apiver_deps import DownloadDestBytes, PreSeekedDownloadDest
36+
from apiver_deps import MetadataDirectiveMode
37+
from apiver_deps import Part
38+
from apiver_deps import AbstractProgressListener
39+
from apiver_deps import StubAccountInfo, RawSimulator, BucketSimulator, FakeResponse, FileSimulator
40+
from apiver_deps import ParallelDownloader
41+
from apiver_deps import SimpleDownloader
42+
from apiver_deps import UploadSourceBytes
43+
from apiver_deps import hex_sha1_of_bytes, TempDir
44+
from apiver_deps import EncryptionAlgorithm, EncryptionSetting, EncryptionMode, EncryptionKey, SSE_NONE, SSE_B2_AES
45+
from apiver_deps import CopySource, UploadSourceLocalFile, WriteIntent
46+
import apiver_deps
47+
if apiver_deps.V <= 1:
48+
from apiver_deps import FileVersionInfo as VFileVersionInfo
49+
else:
50+
from apiver_deps import FileVersion as VFileVersionInfo
51+
52+
pytestmark = [pytest.mark.apiver(from_ver=1)]
4753

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

211217

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

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

239247

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

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

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

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

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

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

308320
info = self.bucket.get_file_info_by_id(b_id)
309321

310-
self.assertIsInstance(info, FileVersionInfo)
322+
self.assertIsInstance(info, VFileVersionInfo)
311323
expected = (b_id, 'b', 11, 'upload', 'b2/x-auto', 'none')
312324
actual = (
313325
info.id_, info.file_name, info.size, info.action, info.content_type,
@@ -372,6 +384,7 @@ def test_three_files_multiple_versions(self):
372384
]
373385
self.assertEqual(expected, actual)
374386

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

503516

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

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

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

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

569+
@pytest.mark.apiver(to_ver=1)
552570
def test_copy_with_replace_metadata(self):
553571
file_id = self._make_file()
554572
self.bucket.copy_file(
@@ -567,6 +585,7 @@ def test_copy_with_replace_metadata(self):
567585
]
568586
self.assertEqual(expected, actual)
569587

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

606+
@pytest.mark.apiver(to_ver=1)
587607
def test_copy_with_different_bucket(self):
588608
source_bucket = self.api.create_bucket('source-bucket', 'allPublic')
589609
file_id = self._make_file(source_bucket)
@@ -720,7 +740,7 @@ def test_copy_encryption(self):
720740
]:
721741
with self.subTest(kwargs=kwargs, length=length, data=data):
722742
file_info = self.bucket.copy(**kwargs, new_file_name='new_file', length=length)
723-
self.assertTrue(isinstance(file_info, FileVersionInfo))
743+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
724744
self.assertEqual(file_info.server_side_encryption, expected_encryption)
725745

726746
def _make_file(self, bucket=None):
@@ -733,20 +753,20 @@ class TestUpload(TestCaseWithBucket):
733753
def test_upload_bytes(self):
734754
data = b'hello world'
735755
file_info = self.bucket.upload_bytes(data, 'file1')
736-
self.assertTrue(isinstance(file_info, FileVersionInfo))
756+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
737757
self._check_file_contents('file1', data)
738758
self.assertEqual(file_info.server_side_encryption, SSE_NONE)
739759

740760
def test_upload_bytes_sse_b2(self):
741761
data = b'hello world'
742762
file_info = self.bucket.upload_bytes(data, 'file1', encryption=SSE_B2_AES)
743-
self.assertTrue(isinstance(file_info, FileVersionInfo))
763+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
744764
self.assertEqual(file_info.server_side_encryption, SSE_B2_AES)
745765

746766
def test_upload_bytes_sse_c(self):
747767
data = b'hello world'
748768
file_info = self.bucket.upload_bytes(data, 'file1', encryption=SSE_C_AES)
749-
self.assertTrue(isinstance(file_info, FileVersionInfo))
769+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
750770
self.assertEqual(SSE_C_AES_NO_SECRET, file_info.server_side_encryption)
751771

752772
def test_upload_local_file_sse_b2(self):
@@ -755,7 +775,7 @@ def test_upload_local_file_sse_b2(self):
755775
data = b'hello world'
756776
write_file(path, data)
757777
file_info = self.bucket.upload_local_file(path, 'file1', encryption=SSE_B2_AES)
758-
self.assertTrue(isinstance(file_info, FileVersionInfo))
778+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
759779
self.assertEqual(file_info.server_side_encryption, SSE_B2_AES)
760780
self._check_file_contents('file1', data)
761781

@@ -765,7 +785,7 @@ def test_upload_local_file_sse_c(self):
765785
data = b'hello world'
766786
write_file(path, data)
767787
file_info = self.bucket.upload_local_file(path, 'file1', encryption=SSE_C_AES)
768-
self.assertTrue(isinstance(file_info, FileVersionInfo))
788+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
769789
self.assertEqual(SSE_C_AES_NO_SECRET, file_info.server_side_encryption)
770790
self._check_file_contents('file1', data)
771791

@@ -782,7 +802,7 @@ def test_upload_local_file(self):
782802
write_file(path, data)
783803
file_info = self.bucket.upload_local_file(path, 'file1')
784804
self._check_file_contents('file1', data)
785-
self.assertTrue(isinstance(file_info, FileVersionInfo))
805+
self.assertTrue(isinstance(file_info, VFileVersionInfo))
786806
self.assertEqual(file_info.server_side_encryption, SSE_NONE)
787807
print(file_info.as_dict())
788808
self.assertEqual(file_info.as_dict()['serverSideEncryption'], {'mode': 'none'})
@@ -991,7 +1011,7 @@ def test_create_remote(self):
9911011
],
9921012
file_name='created_file'
9931013
)
994-
self.assertIsInstance(created_file, FileVersionInfo)
1014+
self.assertIsInstance(created_file, VFileVersionInfo)
9951015
actual = (
9961016
created_file.id_, created_file.file_name, created_file.size,
9971017
created_file.server_side_encryption
@@ -1016,7 +1036,7 @@ def test_create_remote_encryption(self):
10161036
file_name='created_file_%s' % (len(data),),
10171037
encryption=SSE_C_AES
10181038
)
1019-
self.assertIsInstance(created_file, FileVersionInfo)
1039+
self.assertIsInstance(created_file, VFileVersionInfo)
10201040
actual = (
10211041
created_file.id_, created_file.file_name, created_file.size,
10221042
created_file.server_side_encryption

test/unit/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ def test_illegal_regex(self, exc_class, exc_msg):
108108
with pytest.raises(exc_class, match=exc_msg):
109109
error_raising_function()
110110
111+
If a test is merked with "apiver" multiple times, it will be skipped if at least one of the apiver conditions
112+
cause it to be skipped. E.g. if a test module is marked with
113+
114+
.. code-block:: python
115+
116+
pytestmark = [pytest.mark.apiver(from_ver=1)]
117+
118+
and a test function is marked with
119+
120+
.. code-block:: python
121+
122+
@pytest.mark.apiver(to_ver=1)
123+
def test_function(self):
124+
...
125+
126+
the test function will be run only for apiver=v1
111127
"""
112128
for mark in item.iter_markers(name='apiver'):
113129
if mark.args and mark.kwargs:

0 commit comments

Comments
 (0)