def test_get_attribute_vi_string_for_loop_multiple_devices(self): self.patched_library.niModInst_GetInstalledDeviceAttributeViString.side_effect = self.niModInst_GetInstalledDeviceAttributeViString_looping self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = self.num_string_devices_looping with nimodinst.Session('') as session: for d in session: attr_int = d.device_name assert(attr_int == self.string_vals_device_looping[self.iteration_device_looping - 1]) # Have to subtract once since it was already incremented in the callback function
def test_iterating_for(self): self.side_effects_helper['OpenInstalledDevicesSession'][ 'deviceCount'] = 2 with nimodinst.Session('') as session: assert len(session) == 2 for d in session: pass
def test_cannot_add_properties_to_device_set(self): with nimodinst.Session('') as session: try: session.devices[0].non_existent_property = 5 assert False except AttributeError as e: assert str(e) == "__setattr__ not supported."
def test_cannot_add_properties_to_device_get(self): with nimodinst.Session('') as session: try: session.devices[0].non_existent_property assert False except AttributeError as e: assert str(e) == "'_Device' object has no attribute 'non_existent_property'"
def test_device_name_attribute(): with nimodinst.Session('') as session: assert len( session) > 0, 'Must have hardware for ModInst tests to be valid.' assert isinstance(session.devices[0].device_name, str) assert len(session.devices[0].device_name ) > 0 # device name must be at least 1 character
def test_device_model_attribute(): with nimodinst.Session('') as session: assert len(session) > 0, 'Must have hardware for ModInst tests to be valid.' assert len(session.devices[0].device_model) > 0 assert isinstance(session.devices[0].device_model, six.text_type) pattern = r'(NI )?[A-Z]+e?-\d\d\d\d' assert re.search(pattern, session.devices[0].device_model) is not None # NI Model numbers are generally "NI PXIe-2532", but might also be "USB-2532"
def test_device_name_attribute(): with nimodinst.Session('') as session: assert len( session ) > 0, 'This test expects an instrument in the system (real or simulated).' assert isinstance(session.devices[0].device_name, str) assert len(session.devices[0].device_name ) > 0 # device name must be at least 1 character
def test_get_attribute_session(self): val = 123 self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = 1 self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViInt32 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['attributeValue'] = val with nimodinst.Session('') as session: attr_int = session.devices[0].chassis_number assert(attr_int == val)
def test_vi_int32_attribute_read_only(self): self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = 1 with nimodinst.Session('') as session: try: session.devices[0].chassis_number = 5 assert False except AttributeError as e: assert str(e) == "__setattr__ not supported."
def test_get_attribute_vi_int32_for_loop_index(self): self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.niModInst_GetInstalledDeviceAttributeViInt32_looping self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = self.num_int_devices_looping index = 0 with nimodinst.Session('') as session: attr_int = session.devices[index].chassis_number index += 1 assert(attr_int == self.int_vals_device_looping[self.iteration_device_looping - 1]) # Have to subtract once since it was already incremented in the callback function
def test_open_and_close(self): session = nimodinst.Session('') self.patched_library.niModInst_OpenInstalledDevicesSession.assert_called_once_with( _matchers.ViStringMatcher(''), _matchers.ViSessionPointerMatcher(), _matchers.ViInt32PointerMatcher()) session.close() self.patched_library.niModInst_CloseInstalledDevicesSession.assert_called_once_with( _matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST))
def test_vi_string_attribute_read_only(self): self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = 1 with nimodinst.Session('') as session: try: session.devices[0].device_name = "Not Possible" assert False except AttributeError as e: assert str(e) == "__setattr__ not supported."
def test_iterating_for_empty(self): self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = 0 with nimodinst.Session('') as session: assert len(session) == 0 count = 0 for d in session: count += 1 assert count == len(session)
def test_string_attribute_error_on_non_existant_device(): with nimodinst.Session('') as session: device = len(session) + 1 try: session.devices[device].slot_number assert False except IndexError: pass
def test_iterating_next(self): self.side_effects_helper['OpenInstalledDevicesSession'][ 'deviceCount'] = 2 with nimodinst.Session('') as session: assert len(session) == 2 d1 = session.next() d2 = session.next() assert d1 != d2
def test_context_manager(self): with nimodinst.Session('') as session: assert type(session) == nimodinst.Session self.patched_library.niModInst_OpenInstalledDevicesSession.assert_called_once_with( _matchers.ViStringMatcher(''), _matchers.ViSessionPointerMatcher(), _matchers.ViInt32PointerMatcher()) self.patched_library.niModInst_CloseInstalledDevicesSession.assert_called_once_with( _matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST))
def example(): with nimodinst.Session('') as session: if len(session) > 0: print("%d items" % len(session)) print("{: >20} {: >15} {: >10}".format('Name', 'Model', 'S/N')) for d in session: print("{: >20} {: >15} {: >10}".format(d.device_name, d.device_model, d.serial_number))
def test_serial_number_attribute(): with nimodinst.Session('') as session: assert len( session) > 0, 'Must have hardware for ModInst tests to be valid.' pattern = r'^[0-9A-F]+$' assert isinstance(session.devices[0].serial_number, str) assert (len(session.devices[0].serial_number) == 0) | ( re.search(pattern, session.devices[0].serial_number) is not None ) # NI Serial numbers hex unless it is simulated than it is 0
def test_get_extended_error_info(self): error_string = 'Error' self.patched_library.niModInst_GetExtendedErrorInfo.side_effect = self.side_effects_helper.niModInst_GetExtendedErrorInfo self.side_effects_helper['GetExtendedErrorInfo']['errorInfo'] = error_string with nimodinst.Session('') as session: # Calling the private function directly, as _get_extended_error_info() functions differently than other IVI Dance functions. # As a result, it cannot be used directly during error handling. result = session._get_extended_error_info() assert result == error_string
def test_serial_number_attribute(): with nimodinst.Session('') as session: assert len( session ) > 0, 'This test expects an instrument in the system (real or simulated).' pattern = r'^[0-9A-F]+$' assert isinstance(session.devices[0].serial_number, str) assert (len(session.devices[0].serial_number) == 0) | ( re.search(pattern, session.devices[0].serial_number) is not None ) # NI Serial numbers hex unless it is simulated than it is 0
def test_string_attribute_error_on_non_existant_device(): with nimodinst.Session('') as session: device = len(session) + 1 try: session[device].slot_number assert False except nimodinst.Error as e: assert e.code == -250202 # NIMODINST_ERROR_INVALID_DEVICE_INDEX assert e.description.lower().find( 'the device index is out of the range of valid device indices for this session.' ) != -1
def test_get_error_description_fails(self): self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViInt32 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['return'] = -1 self.patched_library.niModInst_GetExtendedErrorInfo.side_effect = self.side_effects_helper.niModInst_GetExtendedErrorInfo self.side_effects_helper['GetExtendedErrorInfo']['return'] = -2 with nimodinst.Session('') as session: try: session.devices[0].chassis_number except nimodinst.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_device_model_attribute(): with nimodinst.Session('') as session: assert len( session ) > 0, 'This test expects an instrument in the system (real or simulated).' assert len(session.devices[0].device_model) > 0 assert isinstance(session.devices[0].device_model, str) pattern = r'(NI )?[A-Z]+e?-\d\d\d\d' assert re.search( pattern, session.devices[0].device_model ) is not None # NI Model numbers are generally "NI PXIe-2532", but might also be "USB-2532"
def test_get_attribute_session_no_index(self): val = 123 self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = 1 self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViInt32 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['attributeValue'] = val with nimodinst.Session('') as session: try: session.chassis_number assert False except AttributeError as e: assert str(e) == "'Session' object has no attribute 'chassis_number'"
def test_repr_and_str(self): self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViInt32 self.patched_library.niModInst_GetInstalledDeviceAttributeViString.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViString self.side_effects_helper['OpenInstalledDevicesSession']['deviceCount'] = 2 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['attributeValue'] = 42 self.side_effects_helper['GetInstalledDeviceAttributeViString']['attributeValue'] = 'fourty two' with nimodinst.Session('') as session: session print(session) for d in session: d print(d)
def test_int_attribute_warning(self): warning_code = 1234 error_string = 'Error' self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViInt32 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['attributeValue'] = -1 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['return'] = warning_code self.patched_library.niModInst_GetExtendedErrorInfo.side_effect = self.side_effects_helper.niModInst_GetExtendedErrorInfo self.side_effects_helper['GetExtendedErrorInfo']['errorInfo'] = error_string with nimodinst.Session('') as session: with warnings.catch_warnings(record=True) as w: session.devices[0].chassis_number assert len(w) == 1 assert issubclass(w[0].category, nimodinst.DriverWarning) assert error_string in str(w[0].message)
def test_cannot_add_properties_to_session(self): with nimodinst.Session('') as session: try: session.nonexistent_property = 5 assert False except TypeError as e: print(e) pass try: value = session.nonexistent_property # noqa: F841 assert False except AttributeError as e: print(e) pass
def test_int_attribute_error(self): error_code = -1234 error_string = 'Error' self.patched_library.niModInst_GetInstalledDeviceAttributeViInt32.side_effect = self.side_effects_helper.niModInst_GetInstalledDeviceAttributeViInt32 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['attributeValue'] = -1 self.side_effects_helper['GetInstalledDeviceAttributeViInt32']['return'] = error_code self.patched_library.niModInst_GetExtendedErrorInfo.side_effect = self.side_effects_helper.niModInst_GetExtendedErrorInfo self.side_effects_helper['GetExtendedErrorInfo']['errorInfo'] = error_string with nimodinst.Session('') as session: try: session.devices[0].chassis_number assert False except nimodinst.Error as e: assert e.code == error_code assert e.description == error_string
def get_modinst_devices(driver): """ Creates a list of instruments matching the current driver. Set self._driver before calling this method :return: list of nimodinst device objects :throws: RuntimeError if called on non-Windows systems. """ if platform.system() != 'Windows': raise RuntimeError('Cannot connect to ModInst ' 'devices on non-Windows systems.') # make sure NI ModInst package is only loaded when required # pylint: disable=import-outside-toplevel import nimodinst session = nimodinst.Session(driver) return session.devices
def device_info(request): device_info = {} device_name = "unknown" device_sn = "unknown" try: with nimodinst.Session('nidmm') as session: if len(session) > 0: device_name = session.device_name[0] device_sn = session.serial_number[0] except nimodinst.Error as e: sys.stderr.write(str(e)) sys.exit(e.code) device_info['name'] = device_name device_info['sn'] = device_sn return device_info