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 4 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
14 changes: 11 additions & 3 deletions pytest_minio_mock/plugin.py
Original file line number Diff line number Diff line change
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 @@ -1453,5 +1457,9 @@ def minio_mock_init(
client.connect(minio_mock_servers)
return client

patched = mocker.patch.object(Minio, "__new__", new=minio_mock_init)
yield patched
patched = mocker.patch.object(Minio, "__new__", new=minio_mock_init, create=True)
satheler marked this conversation as resolved.
Show resolved Hide resolved
try:
yield patched
mocker.stop(patched)
finally:
Minio.__new__ = lambda cls, *args, **kw: object.__new__(cls)
35 changes: 34 additions & 1 deletion tests/test_minio_mock_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@

@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 == None
assert client._secret_key == None
assert client._session_token == None
assert client._secure is is_secure
assert client._region == None
assert client._http_client == None
assert client._credentials == 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
Expand Down Expand Up @@ -67,3 +86,17 @@ def test_mock_minio_client_init_error_handling(self):
TypeError, match="missing 1 required positional argument: 'endpoint'"
):
client = 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