Skip to content

Commit

Permalink
Replace mocking with monkeypatching for minio
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Apr 12, 2024
1 parent 929fcf3 commit 4f3a8d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/pytroll_watchers/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def fake_iterator(_):
return _patched_local_events


@contextmanager
def patched_bucket_listener(records):
@pytest.fixture(autouse=True)
def patched_bucket_listener(monkeypatch):
"""Patch the records produced by the underlying bucket listener.
Example:
Expand All @@ -38,10 +38,16 @@ def patched_bucket_listener(records):
... # do something with the record
"""
from unittest import mock

import minio
fake_minio = mock.Mock(wraps=minio.Minio)
fake_minio.return_value.listen_bucket_notification.return_value = nullcontext(enter_result=records)
with mock.patch("minio.Minio", fake_minio):
@contextmanager
def _patched_bucket_listener(records):
#from unittest import mock
def fake_listen(*args, **kwargs):
return nullcontext(enter_result=records)
import minio
monkeypatch.setattr(minio.Minio, "listen_bucket_notification", fake_listen)
yield
#fake_minio = mock.Mock(wraps=minio.Minio)
#fake_minio.return_value.listen_bucket_notification.return_value = nullcontext(enter_result=records)
#with mock.patch("minio.Minio", fake_minio):
# yield
return _patched_bucket_listener
12 changes: 6 additions & 6 deletions tests/test_bucket_notification_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from posttroll.message import Message
from posttroll.testing import patched_publisher
from pytroll_watchers import minio_notification_watcher
from pytroll_watchers.testing import patched_bucket_listener
from pytroll_watchers.testing import patched_bucket_listener # noqa
from upath import UPath

sdr_file_pattern = ("sdr/SV{channel_name:3s}_{platform_name}_d{start_date:%Y%m%d}_t{start_time:%H%M%S%f}_"
"e{end_time:%H%M%S%f}_b{orbit_number:d}_c{processing_datetime:%Y%m%d%H%M%S%f}_cspp_dev.h5")


def test_generate_paths():
def test_generate_paths(patched_bucket_listener):
"""Test generating paths."""
profile="someprofile"
s3_config = dict(endpoint_url="someendpoint",
Expand All @@ -26,7 +26,7 @@ def test_generate_paths():
profile=profile)


def test_generate_paths_with_pattern():
def test_generate_paths_with_pattern(patched_bucket_listener): # noqa
"""Test generating paths."""
profile = "someprofile"
s3_config = dict(endpoint_url="someendpoint",
Expand All @@ -42,7 +42,7 @@ def test_generate_paths_with_pattern():
assert metadata["platform_name"] == "npp"


def test_generate_paths_with_pattern_fixes_endtime():
def test_generate_paths_with_pattern_fixes_endtime(patched_bucket_listener): # noqa
"""Test generating paths."""
s3_config = dict(endpoint_url="someendpoint",
bucket_name="viirs-data",
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_fix_times():
assert metadata["start_time"] < metadata["end_time"]


def test_publish_paths():
def test_publish_paths(patched_bucket_listener): # noqa
"""Test publishing paths."""
s3_config = dict(endpoint_url="someendpoint",
bucket_name="viirs-data",
Expand All @@ -91,7 +91,7 @@ def test_publish_paths():
'"profile": "someprofile"}')


def test_publish_paths_with_pattern():
def test_publish_paths_with_pattern(patched_bucket_listener): # noqa
"""Test publishing paths."""
s3_config = dict(endpoint_url="someendpoint",
bucket_name="viirs-data",
Expand Down

0 comments on commit 4f3a8d6

Please sign in to comment.