Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle custom __new__ arguments in Minio mock fixture and few adjustments #40

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions pytest_minio_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def get_object(self, version_id, versioning: VersioningConfig):
object_name=self.object_name,
)

# If version == None, give version_id a value
# If version is None, give version_id a value
if not version_id:
if not self._versions:
raise S3Error(
Expand Down Expand Up @@ -547,7 +547,7 @@ def stat_object(

try:
the_object = self.objects[object_name]
except KeyError as e:
except KeyError:
raise S3Error(
message="Object does not exist",
resource=f"/{self.bucket_name}/{object_name}",
Expand Down Expand Up @@ -793,6 +793,10 @@ def __init__(
credentials (optional): The credentials object. Defaults to None.
cert_check (optional): Whether to check certificates. Defaults to True.
"""
if "://" not in endpoint:
scheme = "https" if secure else "http"
endpoint = f"{scheme}://{endpoint}"

self._base_url = endpoint
self._access_key = access_key
self._secret_key = secret_key
Expand All @@ -816,7 +820,7 @@ def _health_check(self):
if not self._base_url:
raise ValueError("base_url is empty")
if not validators.hostname(self._base_url) and not validators.url(
self._base_url
self._base_url, simple_host=True
):
raise ValueError(f"base_url {self._base_url} is not valid")

Expand Down Expand Up @@ -1432,26 +1436,24 @@ def minio_mock_servers():


@pytest.fixture
def minio_mock(mocker, minio_mock_servers):
def minio_mock(minio_mock_servers):
"""
Pytest fixture to patch the Minio client with a mock.

Args:
mocker: The pytest-mock fixture.
minio_mock_servers: The fixture providing a Servers instance.

Yields:
MockMinioClient: The patched Minio client.
"""

def minio_mock_init(
cls,
*args,
**kwargs,
):
def minio_mock_init(cls, *args, **kwargs):
client = MockMinioClient(*args, **kwargs)
client.connect(minio_mock_servers)
return client

patched = mocker.patch.object(Minio, "__new__", new=minio_mock_init)
yield patched
Minio.__new__ = minio_mock_init
try:
yield Minio
finally:
Minio.__new__ = lambda cls, *args, **kw: object.__new__(cls)
49 changes: 41 additions & 8 deletions tests/test_minio_mock_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,37 @@

@pytest.mark.UNIT
class TestsMockMinioClient:
@pytest.mark.parametrize("is_secure", [True, False])
@pytest.mark.UNIT
def test_mock_minio_client_init_without_endpoint_schema_and_with_minimal_parameters(
self, is_secure: bool
):
endpoint = "localhost:9000"
client = MockMinioClient(endpoint, secure=is_secure)

base_url_schema = "https" if is_secure else "http"
assert client._base_url == f"{base_url_schema}://{endpoint}"
assert client._access_key is None
assert client._secret_key is None
assert client._session_token is None
assert client._secure is is_secure
assert client._region is None
assert client._http_client is None
assert client._credentials is None
assert client._cert_check is True

@pytest.mark.UNIT
def test_mock_minio_client_init_with_minimal_parameters(self):
endpoint = "http://localhost:9000"
endpoint = "https://localhost:9000"
client = MockMinioClient(endpoint)
assert client._base_url == endpoint
assert client._access_key == None
assert client._secret_key == None
assert client._session_token == None
assert client._access_key is None
assert client._secret_key is None
assert client._session_token is None
assert client._secure is True
assert client._region == None
assert client._http_client == None
assert client._credentials == None
assert client._region is None
assert client._http_client is None
assert client._credentials is None
assert client._cert_check is True

@pytest.mark.UNIT
Expand Down Expand Up @@ -66,4 +85,18 @@ def test_mock_minio_client_init_error_handling(self):
with pytest.raises(
TypeError, match="missing 1 required positional argument: 'endpoint'"
):
client = MockMinioClient() # not passing endpoint should raise an error
MockMinioClient() # not passing endpoint should raise an error

@pytest.mark.parametrize(
"endpoint",
[
"http://localhost:9000",
"https://localhost:9000",
"localhost:9000",
"any-endpoint.local",
],
)
@pytest.mark.UNIT
def test_mock_minio_client_health_check(self, endpoint: str):
client = MockMinioClient(endpoint)
assert client._health_check() is None
Loading