Skip to content

Commit

Permalink
tests: Do not try to import mock and patch from mock
Browse files Browse the repository at this point in the history
We no longer support older versions of Python so we can import
everything only from the unittest module.
  • Loading branch information
vojtechtrefny committed Feb 1, 2024
1 parent 8a2e395 commit dd0bc77
Show file tree
Hide file tree
Showing 35 changed files with 60 additions and 196 deletions.
5 changes: 1 addition & 4 deletions tests/storage_tests/devices_test/misc_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import os
import unittest

try:
from unittest.mock import patch
except ImportError:
from mock import patch
from unittest.mock import patch

from ..storagetestcase import StorageTestCase

Expand Down
5 changes: 1 addition & 4 deletions tests/storage_tests/devices_test/partition_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from uuid import UUID
import parted

try:
from unittest.mock import patch
except ImportError:
from mock import patch
from unittest.mock import patch

from blivet.devices import DiskFile
from blivet.devices import PartitionDevice
Expand Down
6 changes: 1 addition & 5 deletions tests/storage_tests/partitioning_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from unittest.mock import patch, Mock
except ImportError:
from mock import patch, Mock

import unittest
from unittest.mock import patch, Mock

import parted

Expand Down
6 changes: 1 addition & 5 deletions tests/storage_tests/unsupported_disklabel_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from unittest.mock import patch, sentinel, DEFAULT
except ImportError:
from mock import patch, sentinel, DEFAULT

import unittest
from unittest.mock import patch, sentinel, DEFAULT

from blivet.actionlist import ActionList
from blivet.deviceaction import ActionDestroyFormat
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/action_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import unittest

try:
from unittest.mock import Mock, patch
except ImportError:
from mock import Mock, patch
from unittest.mock import Mock, patch

from .blivettestcase import BlivetTestCase
import blivet
Expand Down
5 changes: 1 addition & 4 deletions tests/unit_tests/blivettestcase.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

import unittest
try:
from unittest.mock import Mock, patch
except ImportError:
from mock import Mock, patch
from unittest.mock import Mock, patch

import parted

Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/dbus_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import random

try:
from unittest.mock import patch, Mock, call
except ImportError:
from mock import patch, Mock, call

from unittest.mock import patch, Mock, call
from unittest import TestCase

import dbus
Expand Down
10 changes: 3 additions & 7 deletions tests/unit_tests/devicefactory_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@

import unittest
from decimal import Decimal
import os
import unittest
from unittest.mock import patch

try:
from unittest.mock import patch
except ImportError:
from mock import patch
from decimal import Decimal

import blivet

Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devicelibs_test/disk_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# pylint: skip-file
try:
from unittest.mock import Mock, patch, sentinel
except ImportError:
from mock import Mock, patch, sentinel

import unittest
from unittest.mock import Mock, patch, sentinel

from blivet.devicelibs import disk as disklib
from blivet.size import Size
Expand Down
13 changes: 5 additions & 8 deletions tests/unit_tests/devicelibs_test/edd_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
try:
from unittest import mock
except ImportError:
import mock

import unittest
import os
import logging
import copy
import logging
import os
import unittest

from unittest import mock

from blivet import arch
from blivet.devicelibs import edd
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/btrfs_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import unittest

try:
from unittest.mock import patch
except ImportError:
from mock import patch
from unittest.mock import patch

import blivet

Expand Down
9 changes: 3 additions & 6 deletions tests/unit_tests/devices_test/device_dependencies_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
try:
from unittest.mock import patch, PropertyMock
except ImportError:
from mock import patch, PropertyMock

import unittest
import os
import unittest
from unittest.mock import patch, PropertyMock

import blivet
import gi
gi.require_version("BlockDev", "3.0")
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/device_methods_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import unittest

try:
from unittest.mock import patch, Mock, PropertyMock
except ImportError:
from mock import patch, PropertyMock
from unittest.mock import patch, Mock, PropertyMock

from blivet.devices import StorageDevice
from blivet.devices import DiskDevice, PartitionDevice
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/device_names_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# vim:set fileencoding=utf-8
try:
from unittest.mock import patch
except ImportError:
from mock import patch

import unittest
from unittest.mock import patch

from blivet.devices import LVMVolumeGroupDevice
from blivet.devices import LVMLogicalVolumeDevice
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/device_properties_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import unittest
from unittest.mock import patch, Mock

import gi
gi.require_version("BlockDev", "3.0")

from gi.repository import BlockDev as blockdev

try:
from unittest.mock import patch, Mock
except ImportError:
from mock import patch, Mock

import blivet

from blivet.errors import BTRFSValueError
Expand Down
5 changes: 1 addition & 4 deletions tests/unit_tests/devices_test/disk_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# pylint: skip-file
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import unittest
from unittest.mock import patch

from blivet.devices import DiskDevice
from blivet.devicelibs import disk as disklib
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/lvm_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from unittest.mock import patch, PropertyMock
except ImportError:
from mock import patch, PropertyMock

import unittest
from unittest.mock import patch, PropertyMock

import blivet

Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/md_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import unittest

try:
from unittest.mock import patch
except ImportError:
from mock import patch
from unittest.mock import patch

import blivet

Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/partition_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from collections import namedtuple
import unittest

try:
from unittest.mock import patch, Mock
except ImportError:
from mock import patch, Mock
from unittest.mock import patch, Mock

from blivet.devices import PartitionDevice
from blivet.devices import StorageDevice
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devices_test/stratis_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import unittest

try:
from unittest.mock import patch
except ImportError:
from mock import patch
from unittest.mock import patch

import blivet

Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/devicetree_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from unittest.mock import patch, Mock, PropertyMock, sentinel
except ImportError:
from mock import patch, Mock, PropertyMock, sentinel

import unittest
from unittest.mock import patch, Mock, PropertyMock, sentinel

from blivet.actionlist import ActionList
from blivet.errors import DeviceTreeError, DuplicateUUIDError, InvalidMultideviceSelection
Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/events_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
try:
from unittest.mock import patch, Mock
except ImportError:
from mock import patch, Mock

import time
from unittest import TestCase
from unittest.mock import patch, Mock

from blivet.events.manager import Event, EventManager

Expand Down
36 changes: 15 additions & 21 deletions tests/unit_tests/formats_tests/disklabel_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
try:
from unittest import mock
except ImportError:
import mock

import parted
import unittest
from unittest.mock import Mock, PropertyMock, patch

import blivet
from blivet.size import Size

patch = mock.patch


class DiskLabelTestCase(unittest.TestCase):

@patch("blivet.formats.disklabel.DiskLabel.fresh_parted_disk", None)
def test_get_alignment(self):
dl = blivet.formats.disklabel.DiskLabel()
dl._parted_disk = mock.Mock()
dl._parted_device = mock.Mock()
dl._parted_disk = Mock()
dl._parted_device = Mock()
dl._parted_device.sectorSize = 512

# 512 byte grain sze
Expand Down Expand Up @@ -111,11 +105,11 @@ def test_platform_label_types(self, arch):

def test_label_type_size_check(self):
dl = blivet.formats.disklabel.DiskLabel()
dl._parted_disk = mock.Mock()
dl._parted_device = mock.Mock()
dl._parted_disk = Mock()
dl._parted_device = Mock()

with patch("blivet.formats.disklabel.parted") as patched_parted:
patched_parted.freshDisk.return_value = mock.Mock(name="parted.Disk", maxPartitionStartSector=10)
patched_parted.freshDisk.return_value = Mock(name="parted.Disk", maxPartitionStartSector=10)
dl._parted_device.length = 100
self.assertEqual(dl._label_type_size_check("foo"), False)

Expand All @@ -125,7 +119,7 @@ def test_label_type_size_check(self):
dl._parted_device.length = 9
self.assertEqual(dl._label_type_size_check("foo"), True)

with patch.object(blivet.formats.disklabel.DiskLabel, "parted_device", new=mock.PropertyMock(return_value=None)):
with patch.object(blivet.formats.disklabel.DiskLabel, "parted_device", new=PropertyMock(return_value=None)):
# no parted device -> no passing size check
self.assertEqual(dl._label_type_size_check("msdos"), False)

Expand All @@ -138,8 +132,8 @@ def test_best_label_type(self, arch):
4. is dasd for non-fba dasd on S390
"""
dl = blivet.formats.disklabel.DiskLabel()
dl._parted_disk = mock.Mock()
dl._parted_device = mock.Mock()
dl._parted_disk = Mock()
dl._parted_device = Mock()
dl._device = "labeltypefakedev"

arch.is_s390.return_value = False
Expand All @@ -149,7 +143,7 @@ def test_best_label_type(self, arch):
arch.is_pmac.return_value = False
arch.is_x86.return_value = False

with mock.patch.object(dl, '_label_type_size_check') as size_check:
with patch.object(dl, '_label_type_size_check') as size_check:
# size check passes for first type ("msdos")
size_check.return_value = True
self.assertEqual(dl._get_best_label_type(), "msdos")
Expand All @@ -163,23 +157,23 @@ def test_best_label_type(self, arch):
self.assertEqual(dl._get_best_label_type(), "gpt")

arch.is_pmac.return_value = True
with mock.patch.object(dl, '_label_type_size_check') as size_check:
with patch.object(dl, '_label_type_size_check') as size_check:
size_check.return_value = True
self.assertEqual(dl._get_best_label_type(), "mac")
arch.is_pmac.return_value = False

arch.is_efi.return_value = True
with mock.patch.object(dl, '_label_type_size_check') as size_check:
with patch.object(dl, '_label_type_size_check') as size_check:
size_check.return_value = True
self.assertEqual(dl._get_best_label_type(), "gpt")
arch.is_efi.return_value = False

arch.is_s390.return_value = True
with mock.patch('blivet.util.detect_virt') as virt:
with patch('blivet.util.detect_virt') as virt:
virt.return_value = False
with mock.patch.object(dl, '_label_type_size_check') as size_check:
with patch.object(dl, '_label_type_size_check') as size_check:
size_check.return_value = True
with mock.patch("blivet.formats.disklabel.blockdev.s390") as _s390:
with patch("blivet.formats.disklabel.blockdev.s390") as _s390:
_s390.dasd_is_fba.return_value = False
self.assertEqual(dl._get_best_label_type(), "msdos")

Expand Down
6 changes: 1 addition & 5 deletions tests/unit_tests/formats_tests/luks_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from unittest.mock import patch
except ImportError:
from mock import patch

import unittest
from unittest.mock import patch

from blivet.formats.luks import LUKS
from blivet.size import Size
Expand Down
8 changes: 2 additions & 6 deletions tests/unit_tests/formats_tests/lvmpv_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import unittest
from unittest.mock import patch

from contextlib import contextmanager

import unittest

from blivet.formats.lvmpv import LVMPhysicalVolume
from blivet.flags import flags

Expand Down
Loading

0 comments on commit dd0bc77

Please sign in to comment.