Skip to content

Commit

Permalink
Issue #31 added mocks and tests to cover most of get_frame
Browse files Browse the repository at this point in the history
  • Loading branch information
thompson318 committed Aug 17, 2023
1 parent 370bdfc commit e10d6f2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 56 deletions.
74 changes: 74 additions & 0 deletions tests/polaris_mocks.py
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion tests/pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
62 changes: 62 additions & 0 deletions tests/test_sksurgerynditracker_mockndi_getframe.py
Original file line number Diff line number Diff line change
@@ -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
59 changes: 4 additions & 55 deletions tests/test_sksurgerynditracker_mockndi_polaris.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit e10d6f2

Please sign in to comment.