Skip to content

Commit

Permalink
add typings to tests, add non byte error test
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ11teen committed May 16, 2021
1 parent 1e0acd6 commit 9cb91f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
13 changes: 7 additions & 6 deletions tests/tests/1_storageproviders.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import pytest

from cloudmappings.storageproviders.storageprovider import KeySyncError
from cloudmappings.errors import KeySyncError
from cloudmappings.storageproviders.storageprovider import StorageProvider


class StorageProviderTests:
def test_create_if_not_exists(self, storage_provider):
def test_create_if_not_exists(self, storage_provider: StorageProvider):
# The pytest arg "test_container_id",
# combined with the this being the first test run for each provider,
# ensures that initally storage does not exist.
# We therefore expect False for the first call and True for the second:
assert storage_provider.create_if_not_exists() == False
assert storage_provider.create_if_not_exists() == True

def test_data_is_stored(self, storage_provider, test_id):
def test_data_is_stored(self, storage_provider: StorageProvider, test_id: str):
key = test_id + "-data-store-test"

etag = storage_provider.upload_data(key, None, b"data")
data = storage_provider.download_data(key, etag)

assert data == b"data"

def test_keys_and_etags_are_listed(self, storage_provider, test_id):
def test_keys_and_etags_are_listed(self, storage_provider: StorageProvider, test_id: str):
key_1 = test_id + "-keys-and-etags-list-test-1"
key_2 = test_id + "-keys-and-etags-list-test-2"

Expand All @@ -36,7 +37,7 @@ def test_keys_and_etags_are_listed(self, storage_provider, test_id):
for key, etag in keys_and_etags.items():
assert etag is not None, (key, etag)

def test_keys_are_deleted(self, storage_provider, test_id):
def test_keys_are_deleted(self, storage_provider: StorageProvider, test_id: str):
key = test_id + "-keys-deleted-test"

etag = storage_provider.upload_data(key, None, b"data")
Expand All @@ -45,7 +46,7 @@ def test_keys_are_deleted(self, storage_provider, test_id):
cloud_key_list = storage_provider.list_keys_and_etags(key)
assert key not in cloud_key_list

def test_etags_are_enforced(self, storage_provider, test_id):
def test_etags_are_enforced(self, storage_provider: StorageProvider, test_id: str):
key = test_id + "etags-enforced-test"

with pytest.raises(KeySyncError):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import pytest

from cloudmappings.cloudstoragemapping import CloudMapping
from cloudmappings.errors import KeySyncError
from cloudmappings.storageproviders.storageprovider import StorageProvider


class SingletonCloudMappingTests:
def test_initialising_mapping(self, storage_provider):
def test_initialising_mapping(self, storage_provider: StorageProvider):
cm = CloudMapping(storageprovider=storage_provider)

def test_repr(self, storage_provider):
def test_repr(self, storage_provider: StorageProvider):
cm = CloudMapping(storageprovider=storage_provider)

_repr = str(cm)
Expand All @@ -23,7 +25,22 @@ def test_repr(self, storage_provider):
elif "AWS" in _repr:
assert "BucketName=" in _repr

def test_basic_setting_and_getting(self, storage_provider, test_id):
def test_non_byte_values_error(self, storage_provider: StorageProvider, test_id: str):
cm = CloudMapping(storageprovider=storage_provider)
key = test_id["non-bytes-error"]

with pytest.raises(ValueError, match="must be bytes like"):
cm[key] = True
with pytest.raises(ValueError, match="must be bytes like"):
cm[key] = 10
with pytest.raises(ValueError, match="must be bytes like"):
cm[key] = "string-data"
with pytest.raises(ValueError, match="must be bytes like"):
cm[key] = [0, 1, 0, 1]
with pytest.raises(ValueError, match="must be bytes like"):
cm[key] = {"or": "something more", "elaborate": True}

def test_basic_setting_and_getting(self, storage_provider: StorageProvider, test_id: str):
cm = CloudMapping(storageprovider=storage_provider)

cm[test_id + "-key-A"] = b"100"
Expand All @@ -34,7 +51,7 @@ def test_basic_setting_and_getting(self, storage_provider, test_id):
assert cm[test_id + "-key-a"] == b"uncapitalised"
assert cm[test_id + "-key-3"] == b"three"

def test_complex_keys(self, storage_provider, test_id):
def test_complex_keys(self, storage_provider: StorageProvider, test_id: str):
cm = CloudMapping(storageprovider=storage_provider)

cm[test_id + "/here/are/some/sub/dirs"] = b"0"
Expand All @@ -43,7 +60,7 @@ def test_complex_keys(self, storage_provider, test_id):
assert cm[test_id + "/here/are/some/sub/dirs"] == b"0"
assert cm[test_id + "/howaboutsome ˆøœ¨åß∆∫ı˜ unusual !@#$%^* characters"] == b"1"

def test_deleting_keys(self, storage_provider, test_id):
def test_deleting_keys(self, storage_provider: StorageProvider, test_id: str):
cm = CloudMapping(storageprovider=storage_provider)
key = test_id + "/delete-test"

Expand All @@ -52,7 +69,7 @@ def test_deleting_keys(self, storage_provider, test_id):
with pytest.raises(KeyError):
cm[key]

def test_contains(self, storage_provider, test_id):
def test_contains(self, storage_provider: StorageProvider, test_id: str):
cm = CloudMapping(storageprovider=storage_provider)
key = test_id + "/contains-test"

Expand All @@ -61,7 +78,7 @@ def test_contains(self, storage_provider, test_id):
cm[key] = b"0"
assert key in cm

def test_length(self, storage_provider, test_id):
def test_length(self, storage_provider: StorageProvider, test_id: str):
cm = CloudMapping(storageprovider=storage_provider)
key_1 = test_id + "/length-test/1"
key_2 = test_id + "/length-test/2"
Expand Down

0 comments on commit 9cb91f0

Please sign in to comment.