Update test style
This commit is contained in:
parent
f188724d4b
commit
2db2253578
18 changed files with 778 additions and 686 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import boto3
|
||||
import pytest
|
||||
|
||||
PATH = Path(__file__).parent.resolve()
|
||||
|
||||
|
|
@ -19,104 +19,112 @@ credentials = {
|
|||
}
|
||||
|
||||
|
||||
class TestLargeFileS3(unittest.TestCase):
|
||||
def test_uninitialized(self) -> None:
|
||||
self.assertRaises(ValueError, LargeFileS3, "test-file")
|
||||
def test_uninitialized() -> None:
|
||||
with pytest.raises(ValueError):
|
||||
LargeFileS3("test-file")
|
||||
|
||||
def test_bad_file_modes(self) -> None:
|
||||
self.assertRaises(ValueError, LargeFileS3, "test-file", "w", version=3)
|
||||
self.assertRaises(ValueError, LargeFileS3, "test-file", "wb", version=3)
|
||||
self.assertRaises(ValueError, LargeFileS3, "test-file", "w+r")
|
||||
self.assertRaises(ValueError, LargeFileS3, "test-file", "test")
|
||||
|
||||
@patch.object(boto3, "client")
|
||||
def test_initialized_with_dict(self, client: Any) -> None:
|
||||
s3 = Mock()
|
||||
s3.list_objects_v2 = Mock(
|
||||
return_value={
|
||||
"Contents": [
|
||||
{
|
||||
"Key": "test-file/0",
|
||||
"Size": 30,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/1",
|
||||
"Size": 300,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/2",
|
||||
"Size": 187,
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
boto3.client = Mock(return_value=s3)
|
||||
def test_bad_file_modes() -> None:
|
||||
with pytest.raises(ValueError):
|
||||
LargeFileS3("test-file", "w", version=3)
|
||||
|
||||
LargeFileS3.configure_credentials(
|
||||
aws_region_name=credentials["aws_region_name"],
|
||||
aws_access_key_id=credentials["aws_access_key_id"],
|
||||
aws_secret_access_key=credentials["aws_secret_access_key"],
|
||||
large_files_bucket_name=credentials["large_files_bucket_name"],
|
||||
aws_endpoint_url=credentials["aws_endpoint_url"],
|
||||
)
|
||||
lf = LargeFileS3("test-file")
|
||||
with pytest.raises(ValueError):
|
||||
LargeFileS3("test-file", "wb", version=3)
|
||||
|
||||
boto3.client.assert_called_once_with(
|
||||
"s3",
|
||||
aws_access_key_id=credentials["aws_access_key_id"],
|
||||
aws_secret_access_key=credentials["aws_secret_access_key"],
|
||||
region_name=credentials["aws_region_name"],
|
||||
endpoint_url=credentials["aws_endpoint_url"],
|
||||
)
|
||||
with pytest.raises(ValueError):
|
||||
LargeFileS3("test-file", "w+r")
|
||||
|
||||
s3.list_objects_v2.assert_called_once_with(
|
||||
Bucket=credentials["large_files_bucket_name"], Prefix="test-file"
|
||||
)
|
||||
with pytest.raises(ValueError):
|
||||
LargeFileS3("test-file", "test")
|
||||
|
||||
assert lf._version == 2
|
||||
assert lf._local_name == "test-file-2"
|
||||
|
||||
@patch.object(boto3, "client")
|
||||
def test_initialized_with_file(self, client: Any) -> None:
|
||||
s3 = Mock()
|
||||
s3.list_objects_v2 = Mock(
|
||||
return_value={
|
||||
"Contents": [
|
||||
{
|
||||
"Key": "test-file/0",
|
||||
"Size": 30,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/1",
|
||||
"Size": 300,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/2",
|
||||
"Size": 187,
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
@patch.object(boto3, "client")
|
||||
def test_initialized_with_dict(client: Any) -> None:
|
||||
s3 = Mock()
|
||||
s3.list_objects_v2 = Mock(
|
||||
return_value={
|
||||
"Contents": [
|
||||
{
|
||||
"Key": "test-file/0",
|
||||
"Size": 30,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/1",
|
||||
"Size": 300,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/2",
|
||||
"Size": 187,
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
boto3.client = Mock(return_value=s3)
|
||||
|
||||
boto3.client = Mock(return_value=s3)
|
||||
LargeFileS3.configure_credentials(
|
||||
aws_region_name=credentials["aws_region_name"],
|
||||
aws_access_key_id=credentials["aws_access_key_id"],
|
||||
aws_secret_access_key=credentials["aws_secret_access_key"],
|
||||
large_files_bucket_name=credentials["large_files_bucket_name"],
|
||||
aws_endpoint_url=credentials["aws_endpoint_url"],
|
||||
)
|
||||
lf = LargeFileS3("test-file")
|
||||
|
||||
LargeFileS3.configure_credentials_from_file(
|
||||
PATH / "../../docs/example_secrets.ini"
|
||||
)
|
||||
lf = LargeFileS3("test-file")
|
||||
boto3.client.assert_called_once_with(
|
||||
"s3",
|
||||
aws_access_key_id=credentials["aws_access_key_id"],
|
||||
aws_secret_access_key=credentials["aws_secret_access_key"],
|
||||
region_name=credentials["aws_region_name"],
|
||||
endpoint_url=credentials["aws_endpoint_url"],
|
||||
)
|
||||
|
||||
boto3.client.assert_called_once_with(
|
||||
"s3",
|
||||
aws_access_key_id=credentials["aws_access_key_id"],
|
||||
aws_secret_access_key=credentials["aws_secret_access_key"],
|
||||
region_name=credentials["aws_region_name"],
|
||||
endpoint_url=credentials["aws_endpoint_url"],
|
||||
)
|
||||
s3.list_objects_v2.assert_called_once_with(
|
||||
Bucket=credentials["large_files_bucket_name"], Prefix="test-file"
|
||||
)
|
||||
|
||||
assert s3.list_objects_v2.called
|
||||
s3.list_objects_v2.assert_called_once_with(
|
||||
Bucket=credentials["large_files_bucket_name"], Prefix="test-file"
|
||||
)
|
||||
assert lf._version == 2
|
||||
assert lf._local_name == "test-file-2"
|
||||
|
||||
assert lf._version == 2
|
||||
assert lf._local_name == "test-file-2"
|
||||
|
||||
@patch.object(boto3, "client")
|
||||
def test_initialized_with_file(client: Any) -> None:
|
||||
s3 = Mock()
|
||||
s3.list_objects_v2 = Mock(
|
||||
return_value={
|
||||
"Contents": [
|
||||
{
|
||||
"Key": "test-file/0",
|
||||
"Size": 30,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/1",
|
||||
"Size": 300,
|
||||
},
|
||||
{
|
||||
"Key": "test-file/2",
|
||||
"Size": 187,
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
boto3.client = Mock(return_value=s3)
|
||||
|
||||
LargeFileS3.configure_credentials_from_file(PATH / "../../docs/example_secrets.ini")
|
||||
lf = LargeFileS3("test-file")
|
||||
|
||||
boto3.client.assert_called_once_with(
|
||||
"s3",
|
||||
aws_access_key_id=credentials["aws_access_key_id"],
|
||||
aws_secret_access_key=credentials["aws_secret_access_key"],
|
||||
region_name=credentials["aws_region_name"],
|
||||
endpoint_url=credentials["aws_endpoint_url"],
|
||||
)
|
||||
|
||||
assert s3.list_objects_v2.called
|
||||
s3.list_objects_v2.assert_called_once_with(
|
||||
Bucket=credentials["large_files_bucket_name"], Prefix="test-file"
|
||||
)
|
||||
|
||||
assert lf._version == 2
|
||||
assert lf._local_name == "test-file-2"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue