Skip to content

Commit

Permalink
Avoid mocking private functions
Browse files Browse the repository at this point in the history
mocking is unavoidable here, but can atleast
mock as far away as possible (the manager itself)
  • Loading branch information
stveit committed Nov 15, 2023
1 parent fd2d9d4 commit 8d2abfb
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions tests/unittests/portadmin/portadmin_poe_cisco_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mock import Mock
from mock import Mock, patch

import pytest

Expand All @@ -7,6 +7,7 @@
POEIndexNotFoundError,
POEStateNotSupportedError,
)
from nav.models import manage


class TestGetPoeStateOptions:
Expand All @@ -18,38 +19,40 @@ def test_returns_correct_options(self, handler_cisco):
assert Cisco.POE_DISABLE in state_options


@pytest.mark.usefixtures('get_poeport_mock')
class TestGetPoeState:
def test_should_raise_exception_if_unknown_poe_state_cisco(self, handler_cisco):
def test_should_raise_exception_if_unknown_poe_state_cisco(
self, handler_cisco, poeport_get_mock
):
handler_cisco._query_netbox = Mock(return_value=76)
interface = Mock(interface="interface")
with pytest.raises(POEStateNotSupportedError):
handler_cisco.get_poe_states([interface])

def test_should_raise_exception_if_no_poe_indexes_cisco(self, handler_cisco):
handler_cisco._get_poe_indexes_for_interface = Mock(
side_effect=POEIndexNotFoundError("Fail")
)
def test_should_raise_exception_if_no_poe_indexes_cisco(
self, handler_cisco, poeport_get_mock_error
):
interface = Mock(interface="interface")
with pytest.raises(POEIndexNotFoundError):
handler_cisco.get_poe_states([interface])

def test_dict_should_give_none_if_interface_does_not_support_poe(
self, handler_cisco
self, handler_cisco, poeport_get_mock
):
handler_cisco._query_netbox = Mock(return_value=None)
interface = Mock(interface="interface")
states = handler_cisco.get_poe_states([interface])
assert states[interface.ifname] is None

def test_returns_correct_poe_state_cisco(self, handler_cisco):
def test_returns_correct_poe_state_cisco(self, handler_cisco, poeport_get_mock):
expected_state = Cisco.POE_AUTO
handler_cisco._query_netbox = Mock(return_value=expected_state.state)
interface = Mock(ifname="interface")
state = handler_cisco.get_poe_states([interface])
assert state[interface.ifname] == expected_state

def test_use_interfaces_from_db_if_empty_interfaces_arg(self, handler_cisco):
def test_use_interfaces_from_db_if_empty_interfaces_arg(
self, handler_cisco, poeport_get_mock
):
expected_state = Cisco.POE_AUTO
handler_cisco._query_netbox = Mock(return_value=expected_state.state)
interface = Mock(ifname="interface")
Expand All @@ -59,7 +62,16 @@ def test_use_interfaces_from_db_if_empty_interfaces_arg(self, handler_cisco):


@pytest.fixture()
def get_poeport_mock(handler_cisco):
poegroup_mock = Mock(index=1)
poeport_mock = Mock(poegroup=poegroup_mock, index=1)
handler_cisco._get_poeport = Mock(return_value=poeport_mock)
def poeport_get_mock():
with patch("nav.portadmin.snmp.cisco.manage.POEPort.objects.get") as get_mock:
poegroup_mock = Mock(index=1)
poeport_mock = Mock(poegroup=poegroup_mock, index=1)
get_mock.return_value = poeport_mock
yield get_mock


@pytest.fixture()
def poeport_get_mock_error():
with patch("nav.portadmin.snmp.cisco.manage.POEPort.objects.get") as get_mock:
get_mock.side_effect = manage.POEPort.DoesNotExist
yield get_mock

0 comments on commit 8d2abfb

Please sign in to comment.