Skip to content

Commit

Permalink
fix switch views tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julien6387 committed Apr 27, 2024
1 parent 173f429 commit 99f8c54
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
31 changes: 24 additions & 7 deletions supvisors/tests/test_viewhostinstance.py
Expand Up @@ -17,14 +17,13 @@
from unittest.mock import call, Mock

import pytest
from supervisor.web import StatusView

from supvisors.web.viewcontext import CPU, INTF, ViewContext
from supvisors.web.viewhandler import ViewHandler
from supvisors.web.viewhostinstance import HostInstanceView
from supvisors.web.viewimage import host_cpu_img, host_io_img, host_mem_img
from supvisors.web.webutils import HOST_INSTANCE_PAGE
from supvisors.web.webutils import HOST_INSTANCE_PAGE, PROC_INSTANCE_PAGE
from .base import DummyHttpContext
from .conftest import create_element


@pytest.fixture
Expand All @@ -39,8 +38,6 @@ def http_context(supvisors):
@pytest.fixture
def view(http_context):
""" Fixture for the instance to test. """
# apply the forced inheritance done in supvisors.plugin
StatusView.__bases__ = (ViewHandler,)
# create the instance to be tested
return HostInstanceView(http_context)

Expand All @@ -52,11 +49,31 @@ def test_init(view):

def test_write_options(mocker, view):
""" Test the ApplicationView.write_options method. """
mocked_period = mocker.patch('supvisors.web.viewhandler.ViewHandler.write_periods')
mocked_period = mocker.patch.object(view, 'write_periods')
mocked_switch = mocker.patch.object(view, 'write_view_switch')
mocked_header = Mock()
view.write_options(mocked_header)
assert mocked_period.call_args_list == [call(mocked_header)]
mocked_period.reset_mock()
assert mocked_switch.call_args_list == [call(mocked_header)]


def test_write_view_switch(supvisors, view):
""" Test the SupvisorsInstanceView.write_view_switch method. """
# set context (meant to be set through constructor and render)
view.view_ctx = Mock(**{'format_url.return_value': 'an url'})
supvisors.mapper.local_identifier = '10.0.0.1:25000'
# build root structure
mocked_process_view_mid = create_element()
mocked_host_view_mid = create_element()
mocked_header = create_element({'process_view_a_mid': mocked_process_view_mid,
'host_view_a_mid': mocked_host_view_mid})
# test call when SupvisorsInstanceView is a host page
view.page_name = HOST_INSTANCE_PAGE
view.write_view_switch(mocked_header)
assert mocked_header.findmeld.call_args_list == [call('process_view_a_mid'), call('host_view_a_mid'), ]
assert view.view_ctx.format_url.call_args_list == [call('', PROC_INSTANCE_PAGE)]
assert mocked_process_view_mid.attributes.call_args_list == [call(href='an url')]
assert mocked_host_view_mid.content.call_args_list == [call('10.0.0.1')]


def test_write_contents_no_plot(mocker, view):
Expand Down
6 changes: 0 additions & 6 deletions supvisors/tests/test_viewinstance.py
Expand Up @@ -38,18 +38,12 @@ def http_context(supvisors):
@pytest.fixture
def view(http_context):
""" Return the instance to test. """
# apply the forced inheritance done in supvisors.plugin
StatusView.__bases__ = (ViewHandler,)
# create the instance to be tested
return SupvisorsInstanceView(http_context, HOST_INSTANCE_PAGE)


@pytest.fixture
def view_no_stats(http_context):
""" Return the instance to test, in which statistics have been disabled. """
# apply the forced inheritance done in supvisors.plugin
StatusView.__bases__ = (ViewHandler,)
# create the instance to be tested
http_context.supervisord.supvisors.stats_collector = None
return SupvisorsInstanceView(http_context, HOST_INSTANCE_PAGE)

Expand Down
14 changes: 1 addition & 13 deletions supvisors/tests/test_viewprocinstance.py
Expand Up @@ -40,8 +40,6 @@ def http_context(supvisors):
@pytest.fixture
def view(http_context):
""" Return the instance to test. """
# apply the forced inheritance done in supvisors.plugin
StatusView.__bases__ = (ViewHandler,)
# create the instance to be tested
return ProcInstanceView(http_context)

Expand Down Expand Up @@ -127,17 +125,7 @@ def test_write_view_switch(supvisors, view):
mocked_host_view_mid = create_element()
mocked_header = create_element({'process_view_a_mid': mocked_process_view_mid,
'host_view_a_mid': mocked_host_view_mid})
# test call when SupvisorsInstanceView is a host page
view.page_name = HOST_INSTANCE_PAGE
view.write_view_switch(mocked_header)
assert mocked_header.findmeld.call_args_list == [call('process_view_a_mid'), call('host_view_a_mid'), ]
assert view.view_ctx.format_url.call_args_list == [call('', PROC_INSTANCE_PAGE)]
assert mocked_process_view_mid.attributes.call_args_list == [call(href='an url')]
assert mocked_host_view_mid.content.call_args_list == [call('10.0.0.1')]
mocked_header.reset_all()
view.view_ctx.format_url.reset_mock()
# test call when SupvisorsInstanceView is a process page
view.page_name = PROC_INSTANCE_PAGE
# test call
view.write_view_switch(mocked_header)
assert mocked_header.findmeld.call_args_list == [call('host_view_a_mid')]
assert view.view_ctx.format_url.call_args_list == [call('', HOST_INSTANCE_PAGE)]
Expand Down
4 changes: 2 additions & 2 deletions supvisors/utils.py
Expand Up @@ -185,9 +185,9 @@ def parse_docstring(comment: str) -> List:
result = SUPERVISOR_RETURN_FORMAT.match(stripped_line)
if result:
match = True
# Supervisor does not always provides a return name (e.g. signalProcess)
# Supervisor does not always provide a return name (e.g. signalProcess)
name = result.group('name') or ''
# Supervisor does not always provides a return description (e.g. getPID)
# Supervisor does not always provide a return description (e.g. getPID)
desc = result.group('desc')
returns = [idx, 'return', result.group('type'), name, [desc] if desc else []]
current_struct = returns
Expand Down

0 comments on commit 99f8c54

Please sign in to comment.