From e10d6f27f8c50201c9a1420326cfc5c84522a751 Mon Sep 17 00:00:00 2001 From: Stephen Thompson Date: Thu, 17 Aug 2023 15:28:09 +0100 Subject: [PATCH] Issue #31 added mocks and tests to cover most of get_frame --- tests/polaris_mocks.py | 74 +++++++++++++++++++ tests/pylintrc | 2 +- ...st_sksurgerynditracker_mockndi_getframe.py | 62 ++++++++++++++++ ...est_sksurgerynditracker_mockndi_polaris.py | 59 +-------------- 4 files changed, 141 insertions(+), 56 deletions(-) create mode 100644 tests/polaris_mocks.py create mode 100644 tests/test_sksurgerynditracker_mockndi_getframe.py diff --git a/tests/polaris_mocks.py b/tests/polaris_mocks.py new file mode 100644 index 0000000..4c007f6 --- /dev/null +++ b/tests/polaris_mocks.py @@ -0,0 +1,74 @@ +# coding=utf-8 + +"""scikit-surgerynditracker mocks for polaris""" + +import ndicapy + +SETTINGS_POLARIS = { + "tracker type": "polaris", + "ports to probe": 20, + "romfiles" : [ + "data/something_else.rom", + "data/8700339.rom"] + } + +class MockPort: + """A fake serial port for ndi""" + device = 'bad port' + +def mockndiProbe(port_name): #pylint:disable=invalid-name + """Mock of ndiProbe""" + if port_name == 'good port': + return ndicapy.NDI_OKAY + return ndicapy.NDI_PROBE_FAIL + +def mockndiOpen(port_name): #pylint:disable=invalid-name + """Mock of ndiOpen""" + if port_name == 'good port': + return True + return False + +def mockndiOpen_fail(port_name): #pylint:disable=invalid-name + """Mock of ndiOpen that always fail""" + if port_name == 'good port': + return False + return False + +def mockndiGetError(_device): #pylint:disable=invalid-name + """Mock of ndiGetError""" + return ndicapy.NDI_OKAY + +def mockComports(): #pylint:disable=invalid-name + """Returns a list of mock comports""" + mock_ports = [MockPort]*20 + mock_ports[5].device = 'good port' + return mock_ports + +def mockndiGetPHSRNumberOfHandles(_device): #pylint:disable=invalid-name + """Mock of ndiGetPHSRNumberOfHandles""" + return 4 + +def mockndiGetPHRQHandle(_device): #pylint:disable=invalid-name + """Mock of ndiGetPHRQHandle""" + return int(0) + +def mockndiGetPHSRHandle(_device, index): #pylint:disable=invalid-name + """Mock of ndiGetPHSRHandle""" + return int(index) + +def mockndiVER(_device, _other_arg): #pylint:disable=invalid-name + """Mock of ndiVER""" + return 'Mock for Testing' + +def mockndiGetBXFrame(_device, _port_handle): #pylint:disable=invalid-name + """Mock of ndiGetBXFrame""" + bx_frame_count = 0 + return bx_frame_count + +def mockndiGetBXTransform(_device, _port_handle): #pylint:disable=invalid-name + """Mock of ndiGetBXTransform""" + return [0,0,0,0,0,0,0,0] + +def mockndiGetBXTransformMissing(_device, _port_handle): #pylint:disable=invalid-name + """Mock of ndiGetBXTransform""" + return "MISSING" diff --git a/tests/pylintrc b/tests/pylintrc index 6380c31..cc5da78 100644 --- a/tests/pylintrc +++ b/tests/pylintrc @@ -341,4 +341,4 @@ analyse-fallback-blocks=no # Exceptions that will emit a warning when being caught. Defaults to # "Exception" -overgeneral-exceptions=Exception +overgeneral-exceptions=builtins.Exception diff --git a/tests/test_sksurgerynditracker_mockndi_getframe.py b/tests/test_sksurgerynditracker_mockndi_getframe.py new file mode 100644 index 0000000..950c22f --- /dev/null +++ b/tests/test_sksurgerynditracker_mockndi_getframe.py @@ -0,0 +1,62 @@ +# coding=utf-8 + +"""scikit-surgerynditracker tests using a mocked ndicapy""" +from sksurgerynditracker.nditracker import NDITracker + +from tests.polaris_mocks import SETTINGS_POLARIS, mockndiProbe, \ + mockndiOpen, mockndiGetError, mockComports, \ + mockndiGetPHSRNumberOfHandles, mockndiGetPHRQHandle, \ + mockndiGetPHSRHandle, mockndiVER, mockndiGetBXFrame, \ + mockndiGetBXTransform, mockndiGetBXTransformMissing + +def test_getframe_polaris_mock(mocker): + """ + connects and configures, mocks ndicapy.ndiProbe to pass + reqs: 03, 04 + """ + tracker = None + mocker.patch('serial.tools.list_ports.comports', mockComports) + mocker.patch('ndicapy.ndiProbe', mockndiProbe) + mocker.patch('ndicapy.ndiOpen', mockndiOpen) + mocker.patch('ndicapy.ndiCommand') + mocker.patch('ndicapy.ndiGetError', mockndiGetError) + mocker.patch('ndicapy.ndiClose') + mocker.patch('ndicapy.ndiGetPHSRNumberOfHandles', + mockndiGetPHSRNumberOfHandles) + mocker.patch('ndicapy.ndiGetPHRQHandle', mockndiGetPHRQHandle) + mocker.patch('ndicapy.ndiPVWRFromFile') + mocker.patch('ndicapy.ndiGetPHSRHandle', mockndiGetPHSRHandle) + mocker.patch('ndicapy.ndiVER', mockndiVER) + mocker.patch('ndicapy.ndiGetBXFrame', mockndiGetBXFrame) + mocker.patch('ndicapy.ndiGetBXTransform', mockndiGetBXTransform) + + tracker = NDITracker(SETTINGS_POLARIS) + tracker.get_frame() + + del tracker + +def test_getframe_missing(mocker): + """ + connects and configures, mocks ndicapy.ndiProbe to pass + reqs: 03, 04 + """ + tracker = None + mocker.patch('serial.tools.list_ports.comports', mockComports) + mocker.patch('ndicapy.ndiProbe', mockndiProbe) + mocker.patch('ndicapy.ndiOpen', mockndiOpen) + mocker.patch('ndicapy.ndiCommand') + mocker.patch('ndicapy.ndiGetError', mockndiGetError) + mocker.patch('ndicapy.ndiClose') + mocker.patch('ndicapy.ndiGetPHSRNumberOfHandles', + mockndiGetPHSRNumberOfHandles) + mocker.patch('ndicapy.ndiGetPHRQHandle', mockndiGetPHRQHandle) + mocker.patch('ndicapy.ndiPVWRFromFile') + mocker.patch('ndicapy.ndiGetPHSRHandle', mockndiGetPHSRHandle) + mocker.patch('ndicapy.ndiVER', mockndiVER) + mocker.patch('ndicapy.ndiGetBXFrame', mockndiGetBXFrame) + mocker.patch('ndicapy.ndiGetBXTransform', mockndiGetBXTransformMissing) + + tracker = NDITracker(SETTINGS_POLARIS) + tracker.get_frame() + + del tracker diff --git a/tests/test_sksurgerynditracker_mockndi_polaris.py b/tests/test_sksurgerynditracker_mockndi_polaris.py index 6ba1606..df8ab34 100644 --- a/tests/test_sksurgerynditracker_mockndi_polaris.py +++ b/tests/test_sksurgerynditracker_mockndi_polaris.py @@ -6,61 +6,10 @@ from mock import call from sksurgerynditracker.nditracker import NDITracker -SETTINGS_POLARIS = { - "tracker type": "polaris", - "ports to probe": 20, - "romfiles" : [ - "data/something_else.rom", - "data/8700339.rom"] - } - -class MockPort: - """A fake serial port for ndi""" - device = 'bad port' - -def mockndiProbe(port_name): #pylint:disable=invalid-name - """Mock of ndiProbe""" - if port_name == 'good port': - return ndicapy.NDI_OKAY - return ndicapy.NDI_PROBE_FAIL - -def mockndiOpen(port_name): #pylint:disable=invalid-name - """Mock of ndiOpen""" - if port_name == 'good port': - return True - return False - -def mockndiOpen_fail(port_name): #pylint:disable=invalid-name - """Mock of ndiOpen that always fail""" - if port_name == 'good port': - return False - return False - -def mockndiGetError(_device): #pylint:disable=invalid-name - """Mock of ndiGetError""" - return ndicapy.NDI_OKAY - -def mockComports(): #pylint:disable=invalid-name - """Returns a list of mock comports""" - mock_ports = [MockPort]*20 - mock_ports[5].device = 'good port' - return mock_ports - -def mockndiGetPHSRNumberOfHandles(_device): #pylint:disable=invalid-name - """Mock of ndiGetPHSRNumberOfHandles""" - return 4 - -def mockndiGetPHRQHandle(_device): #pylint:disable=invalid-name - """Mock of ndiGetPHRQHandle""" - return int(0) - -def mockndiGetPHSRHandle(_device, index): #pylint:disable=invalid-name - """Mock of ndiGetPHSRHandle""" - return int(index) - -def mockndiVER(_device, _other_arg): #pylint:disable=invalid-name - """Mock of ndiVER""" - return 'Mock for Testing' +from tests.polaris_mocks import SETTINGS_POLARIS, mockndiProbe, \ + mockndiOpen, mockndiOpen_fail, mockndiGetError, mockComports, \ + mockndiGetPHSRNumberOfHandles, mockndiGetPHRQHandle, \ + mockndiGetPHSRHandle, mockndiVER def test_connect_polaris_mock(mocker): """