From 4f3a8d6d1ec47c5c20609b7e599a6f391d6a89b7 Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Fri, 12 Apr 2024 14:54:43 +0200 Subject: [PATCH] Replace mocking with monkeypatching for minio --- src/pytroll_watchers/testing.py | 22 ++++++++++++++-------- tests/test_bucket_notification_watcher.py | 12 ++++++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/pytroll_watchers/testing.py b/src/pytroll_watchers/testing.py index 2459ba3..f845852 100644 --- a/src/pytroll_watchers/testing.py +++ b/src/pytroll_watchers/testing.py @@ -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: @@ -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 diff --git a/tests/test_bucket_notification_watcher.py b/tests/test_bucket_notification_watcher.py index d2dba22..6a62d98 100644 --- a/tests/test_bucket_notification_watcher.py +++ b/tests/test_bucket_notification_watcher.py @@ -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", @@ -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", @@ -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", @@ -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", @@ -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",