Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_set_vi_session_with_session_reference(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
self.patched_library.niTClk_SetAttributeViSession.side_effect = self.side_effects_helper.niTClk_SetAttributeViSession
attribute_id = 3
other_session_number = 43
other_session_reference = nitclk.SessionReference(other_session_number)
session.start_trigger_master_session = other_session_reference
self.patched_library.niTClk_SetAttributeViSession.assert_called_once_with(_matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST), _matchers.ViStringMatcher(''), _matchers.ViAttrMatcher(attribute_id), _matchers.ViSessionMatcher(other_session_number))
import _matchers
import _mock_helper
import hightime
import nitclk
from mock import patch
SESSION_NUM_FOR_TEST = 42
single_session = [SESSION_NUM_FOR_TEST]
single_session_reference = [nitclk.SessionReference(x) for x in single_session]
multiple_sessions = [SESSION_NUM_FOR_TEST, SESSION_NUM_FOR_TEST * 10, SESSION_NUM_FOR_TEST * 100, SESSION_NUM_FOR_TEST + 1]
multiple_session_references = [nitclk.SessionReference(x) for x in multiple_sessions]
class NitclkSupportingDriverSession(object):
'''Session objects for drivers that support NI-TClk are expected to have a property of type nitclk.SessionReference called tclk
This is why we're creating this fake driver class and adding the tclk property.
'''
def __init__(self, session_number):
self.tclk = nitclk.SessionReference(session_number)
class TestNitclkApi(object):
def setup_method(self, method):
self.patched_library_patcher = patch('nitclk._library.Library', autospec=True)
def __init__(self, session_number):
self.tclk = nitclk.SessionReference(session_number)
def test_set_timedelta_as_vi_real64(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
self.patched_library.niTClk_SetAttributeViReal64.side_effect = self.side_effects_helper.niTClk_SetAttributeViReal64
attribute_id = 11
test_number = 4.2
session.sample_clock_delay = test_number
self.patched_library.niTClk_SetAttributeViReal64.assert_called_once_with(_matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST), _matchers.ViStringMatcher(''), _matchers.ViAttrMatcher(attribute_id), _matchers.ViReal64Matcher(test_number))
def test_session_reference_error(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
error_string = 'Error'
self.patched_library.niTClk_GetAttributeViReal64.side_effect = self.side_effects_helper.niTClk_GetAttributeViReal64
self.side_effects_helper['GetAttributeViReal64']['return'] = -1
self.patched_library.niTClk_GetExtendedErrorInfo.side_effect = self.side_effects_helper.niTClk_GetExtendedErrorInfo
self.side_effects_helper['GetExtendedErrorInfo']['errorString'] = error_string
try:
test = session.sample_clock_delay
print(test) # Get rid of flake8 F841
assert False
except nitclk.Error as e:
assert e.code == -1
assert e.description == error_string
def test_get_vi_real64(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
self.patched_library.niTClk_GetAttributeViReal64.side_effect = self.side_effects_helper.niTClk_GetAttributeViReal64
attribute_id = 8
test_number = 4.2
self.side_effects_helper['GetAttributeViReal64']['value'] = test_number
attr_val = session.tclk_actual_period
assert(attr_val == test_number)
self.patched_library.niTClk_GetAttributeViReal64.assert_called_once_with(_matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST), _matchers.ViStringMatcher(''), _matchers.ViAttrMatcher(attribute_id), _matchers.ViReal64PointerMatcher())
def test_session_reference_get_error_description_fails(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
self.patched_library.niTClk_GetAttributeViReal64.side_effect = self.side_effects_helper.niTClk_GetAttributeViReal64
self.side_effects_helper['GetAttributeViReal64']['return'] = -1
self.patched_library.niTClk_GetExtendedErrorInfo.side_effect = self.side_effects_helper.niTClk_GetExtendedErrorInfo
self.side_effects_helper['GetExtendedErrorInfo']['return'] = -2
try:
test = session.sample_clock_delay
print(test) # Get rid of flake8 F841
assert False
except nitclk.Error as e:
assert e.code == -1 # we want the original error code from getting the attribute.
assert e.description == "Failed to retrieve error description."
def test_get_timedelta(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
self.patched_library.niTClk_GetAttributeViReal64.side_effect = self.side_effects_helper.niTClk_GetAttributeViReal64
attribute_id = 11
test_number = 4.2
self.side_effects_helper['GetAttributeViReal64']['value'] = test_number
attr_timedelta = session.sample_clock_delay
assert(attr_timedelta.total_seconds() == test_number)
self.patched_library.niTClk_GetAttributeViReal64.assert_called_once_with(_matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST), _matchers.ViStringMatcher(''), _matchers.ViAttrMatcher(attribute_id), _matchers.ViReal64PointerMatcher())
def test_set_vi_session_with_session(self):
session = nitclk.SessionReference(SESSION_NUM_FOR_TEST)
self.patched_library.niTClk_SetAttributeViSession.side_effect = self.side_effects_helper.niTClk_SetAttributeViSession
attribute_id = 3
other_session_number = 43
other_session = NitclkSupportingDriverSession(other_session_number)
session.start_trigger_master_session = other_session
self.patched_library.niTClk_SetAttributeViSession.assert_called_once_with(_matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST), _matchers.ViStringMatcher(''), _matchers.ViAttrMatcher(attribute_id), _matchers.ViSessionMatcher(other_session_number))
def test_nitclk_error_handling():
test_session_reference = nitclk.SessionReference(42) # Invalid session
try:
test_session_reference.exported_tclk_output_terminal = 'test'
assert False
except nitclk.errors.DriverError as e:
assert e.code == -250032