From 6c02e8225bc3df92bb09ea6b2467fff56bfe8302 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Wed, 16 Oct 2024 22:46:04 +0500 Subject: [PATCH] Replace mocker with unittest.mock --- .../unit/plugins/catalog/test_get_app_details.py | 10 +++++++--- .../catalog/test_min_max_scale_version_update.py | 7 +++++-- .../catalog/test_retrieve_cached_versions_data.py | 8 +++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py index a0074e4a3be5..d3ef14cdfb07 100644 --- a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py @@ -1,3 +1,5 @@ +import unittest + import pytest from middlewared.plugins.catalog.apps_util import get_app_details @@ -143,9 +145,11 @@ } ), ]) -def test_get_app_details(mocker, app_data, versions, expected): - mocker.patch('middlewared.plugins.catalog.apps_util.normalize_questions') - mocker.patch('middlewared.plugins.catalog.apps_util.retrieve_cached_versions_data', return_value=versions) +@unittest.mock.patch('middlewared.plugins.catalog.apps_util.normalize_questions') +@unittest.mock.patch('middlewared.plugins.catalog.apps_util.retrieve_cached_versions_data') +def test_get_app_details(mock_retrieve_cached_versions_data, mock_normalize_questions, app_data, versions, expected): + mock_retrieve_cached_versions_data.return_value = versions + if isinstance(expected, dict): result = get_app_details('/path/to/app', app_data, {}) assert expected == result diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py index c553cf728c22..ea632cbb84a1 100644 --- a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py @@ -1,3 +1,5 @@ +import unittest + import pytest from middlewared.plugins.catalog.apps_util import minimum_scale_version_check_update @@ -149,7 +151,8 @@ } ), ]) -def test_min_max_scale_version_update(mocker, version_data, expected): - mocker.patch('middlewared.plugins.catalog.apps_util.sw_info', return_value={'version': '25.04.0'}) +@unittest.mock.patch('middlewared.plugins.catalog.apps_util.sw_info') +def test_min_max_scale_version_update(sw_info, version_data, expected): + sw_info.return_value = {'version': '25.04.0'} result = minimum_scale_version_check_update(version_data) assert result == expected diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py index 002aa282fb67..19ed424f8959 100644 --- a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py @@ -1,4 +1,5 @@ import textwrap +import unittest import pytest @@ -69,9 +70,10 @@ True ), ]) -def test_retrieve_caches_versions_data(mocker, file, should_work): - mock_file = mocker.mock_open(read_data=file) - mocker.patch('builtins.open', mock_file) +@unittest.mock.patch('builtins.open', new_callable=unittest.mock.mock_open) +def test_retrieve_cached_versions_data(mock_file, file, should_work): + mock_file.return_value.read.return_value = file + if should_work: result = retrieve_cached_versions_data('/path/to/app', 'actual-budget') assert isinstance(result, dict)