Skip to content

Commit

Permalink
Import patch and mock_open directly
Browse files Browse the repository at this point in the history
  • Loading branch information
pieqq committed Sep 25, 2023
1 parent 10776b9 commit 0fddec7
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions providers/base/tests/test_snap_update_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
import logging
import unittest
from unittest import mock
from unittest.mock import patch, mock_open

import snap_update_test

Expand All @@ -16,29 +16,29 @@


class SnapUpdateTests(unittest.TestCase):
@mock.patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.list")
def test_guess_snaps(self, mock_snapd_list):
mock_snapd_list.return_value = snapd_list_sample
snaps = snap_update_test.guess_snaps()
expected_snaps = {"kernel": "pi-kernel", "snapd": "snapd", "gadget": "pi"}
self.assertEqual(snaps, expected_snaps)

@mock.patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.list")
def test_guess_snaps_nothing(self, mock_snapd_list):
mock_snapd_list.return_value = snapd_list_no_kernel_snapd_gadget_snap
snaps = snap_update_test.guess_snaps()
self.assertEqual(snaps, {})

@mock.patch("snap_update_test.glob")
@patch("snap_update_test.glob")
def test_get_snap_base_rev(self, mock_glob):
mock_glob.return_value = snapd_seed_glob_data
snap_rev = snap_update_test.get_snap_base_rev()
self.assertEqual(len(snap_rev), 4)
self.assertEqual(snap_rev["pc-kernel"], "1289")

@mock.patch("snap_update_test.get_snap_base_rev")
@mock.patch("snap_update_test.Snapd.list")
@mock.patch("snap_update_test.Snapd.find")
@patch("snap_update_test.get_snap_base_rev")
@patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.find")
def test_get_snap_info(self, mock_snapd_find, mock_snapd_list, mock_base_revs):
mock_base_revs.return_value = {"firefox": "2605"}
mock_snapd_list.return_value = snapd_list_firefox_snap
Expand All @@ -63,8 +63,8 @@ def test_get_snap_info(self, mock_snapd_find, mock_snapd_list, mock_base_revs):
snap_info = snap_update_test.get_snap_info("firefox")
self.assertEqual(snap_info, expected_snap_info)

@mock.patch("snap_update_test.get_snap_info")
@mock.patch("sys.stdout", new_callable=io.StringIO)
@patch("snap_update_test.get_snap_info")
@patch("sys.stdout", new_callable=io.StringIO)
def test_print_resource_info(self, mock_stdout, mock_snap_info):
mock_snap_info.return_value = snap_info_pi_kernel
expected_output = (
Expand All @@ -78,8 +78,8 @@ def test_print_resource_info(self, mock_stdout, mock_snap_info):


class SnapRefreshRevertTests(unittest.TestCase):
@mock.patch("snap_update_test.Snapd")
@mock.patch("snap_update_test.get_snap_info")
@patch("snap_update_test.Snapd")
@patch("snap_update_test.get_snap_info")
def test_snap_refresh_same_revision(self, mock_snap_info, mock_snapd):
mock_snap_info.return_value = {"installed_revision": "132"}
srr = snap_update_test.SnapRefreshRevert(
Expand All @@ -88,9 +88,9 @@ def test_snap_refresh_same_revision(self, mock_snap_info, mock_snapd):
logging.disable(logging.CRITICAL)
self.assertEqual(srr.snap_refresh(), 1)

@mock.patch("builtins.open", new_callable=mock.mock_open())
@mock.patch("snap_update_test.Snapd.refresh")
@mock.patch("snap_update_test.get_snap_info")
@patch("builtins.open", new_callable=mock_open)
@patch("snap_update_test.Snapd.refresh")
@patch("snap_update_test.get_snap_info")
def test_snap_refresh_different_revision(
self, mock_snap_info, mock_snapd_refresh, mock_file
):
Expand All @@ -104,11 +104,11 @@ def test_snap_refresh_different_revision(
)
self.assertEqual(srr.snap_refresh(), 0)

@mock.patch("builtins.open", new_callable=mock.mock_open())
@mock.patch("snap_update_test.Snapd.list")
@mock.patch("snap_update_test.Snapd.change")
@mock.patch("snap_update_test.json.load")
@mock.patch("snap_update_test.get_snap_info")
@patch("builtins.open", new_callable=mock_open)
@patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.change")
@patch("snap_update_test.json.load")
@patch("snap_update_test.get_snap_info")
def test_verify_refresh_ok(
self,
mock_snap_info,
Expand All @@ -134,11 +134,11 @@ def test_verify_refresh_ok(
)
self.assertEqual(srr.verify_refresh(), 0)

@mock.patch("builtins.open", new_callable=mock.mock_open())
@mock.patch("snap_update_test.Snapd.list")
@mock.patch("snap_update_test.Snapd.change")
@mock.patch("snap_update_test.json.load")
@mock.patch("snap_update_test.get_snap_info")
@patch("builtins.open", new_callable=mock_open)
@patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.change")
@patch("snap_update_test.json.load")
@patch("snap_update_test.get_snap_info")
def test_verify_refresh_nok(
self,
mock_snap_info,
Expand Down Expand Up @@ -166,11 +166,11 @@ def test_verify_refresh_nok(
logging.disable(logging.CRITICAL)
self.assertEqual(srr.verify_refresh(), 1)

@mock.patch("builtins.open", new_callable=mock.mock_open())
@mock.patch("snap_update_test.Snapd.list")
@mock.patch("snap_update_test.Snapd.change")
@mock.patch("snap_update_test.json.load")
@mock.patch("snap_update_test.get_snap_info")
@patch("builtins.open", new_callable=mock_open)
@patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.change")
@patch("snap_update_test.json.load")
@patch("snap_update_test.get_snap_info")
def test_verify_revert_ok(
self,
mock_snap_info,
Expand All @@ -196,11 +196,11 @@ def test_verify_revert_ok(
)
self.assertEqual(srr.verify_revert(), 0)

@mock.patch("builtins.open", new_callable=mock.mock_open)
@mock.patch("snap_update_test.Snapd.list")
@mock.patch("snap_update_test.Snapd.change")
@mock.patch("snap_update_test.json.load")
@mock.patch("snap_update_test.get_snap_info")
@patch("builtins.open", new_callable=mock_open)
@patch("snap_update_test.Snapd.list")
@patch("snap_update_test.Snapd.change")
@patch("snap_update_test.json.load")
@patch("snap_update_test.get_snap_info")
def test_verify_revert_nok(
self,
mock_snap_info,
Expand All @@ -227,8 +227,8 @@ def test_verify_revert_nok(
logging.disable(logging.CRITICAL)
self.assertEqual(srr.verify_revert(), 1)

@mock.patch("snap_update_test.Snapd.revert")
@mock.patch("snap_update_test.get_snap_info")
@patch("snap_update_test.Snapd.revert")
@patch("snap_update_test.get_snap_info")
def test_snap_revert(self, mock_snap_info, mock_snapd_revert):
mock_file_data = (
'{"name": "test-snap", "original_revision": "10", '
Expand All @@ -243,7 +243,7 @@ def test_snap_revert(self, mock_snap_info, mock_snapd_revert):
"installed_revision": "132",
"tracking_channel": "22/beta",
}
with mock.patch("builtins.open", mock.mock_open(read_data=mock_file_data)) as m:
with patch("builtins.open", mock_open(read_data=mock_file_data)) as m:
srr.snap_revert()
mock_snapd_revert.assert_called()
m.assert_called_with("/test/info", "w")

0 comments on commit 0fddec7

Please sign in to comment.