Skip to content

Commit

Permalink
Replace mocker with unittest.mock
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Oct 16, 2024
1 parent b92db82 commit 6c02e82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

import pytest

from middlewared.plugins.catalog.apps_util import get_app_details
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

import pytest

from middlewared.plugins.catalog.apps_util import minimum_scale_version_check_update
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import textwrap
import unittest

import pytest

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6c02e82

Please sign in to comment.