def test_config_constants(): # the default Config values assert Config.PyVISA_LIBRARY == '@ni' assert not Config.DEMO_MODE assert len(Config.PATH) == 0 path = os.path.join(os.path.dirname(__file__), 'config1.xml') c = Config(path) # the new Config values assert path == c.path assert c.root.tag == 'msl' assert Config.PyVISA_LIBRARY == '@py' assert Config.DEMO_MODE assert len(Config.PATH) > 0 assert 'docs' in Config.PATH assert os.path.join('docs', '_api') in Config.PATH assert os.path.join('docs', '_static') in os.environ['PATH'] assert os.path.join('docs', '_templates') in os.environ['PATH'] assert c.value('some_value') == '1.2345' # database default values assert path == c.database().path assert c.database().equipment == {} assert c.database().records() == [] assert c.database().connections() == []
def test_connection_properties(): dbase = Config(os.path.join(os.path.dirname(__file__), 'db.xml')).database() props = dbase.records(serial='37871232')[0].connection.properties assert props['a'] == 1 assert props['b'] == 1.1 assert isinstance(props['c'], bool) and props['c'] assert isinstance(props['d'], bool) and props['d'] assert isinstance(props['e'], bool) and not props['e'] assert isinstance(props['f'], bool) and not props['f'] assert props['g'] is None assert props['h'] == '' assert props['i_termination'] == constants.LF assert props['j_termination'] == constants.CR assert props['k_termination'] == constants.CR + constants.LF assert props['l'] == 'some text' assert props['m'] == 'D:\\Data\\'
def test_config_constants_reloaded(): # make sure that loading a new config file does not alter the class-level attributes path = os.path.join(os.path.dirname(__file__), 'config2.xml') c = Config(path) assert path == c.path assert Config.PyVISA_LIBRARY == '@py' assert Config.DEMO_MODE assert 'docs' in Config.PATH assert len(c.database().records()) == 7 # reset the default Config values so that other tests can assume the default values Config.PyVISA_LIBRARY = '@ni' Config.DEMO_MODE = False assert Config.PyVISA_LIBRARY == '@ni' assert not Config.DEMO_MODE
def test_encoding(): IS_PYTHON2 = sys.version_info[0] == 2 if IS_PYTHON2: reload(sys) # required for the sys.setdefaultencoding() calls below print('') for cfg in ['utf8_txt.xml', 'cp1252_txt.xml', 'xlsx.xml']: db = Config(os.path.join(os.path.dirname(__file__), 'db_encoding_' + cfg)).database() if IS_PYTHON2: if cfg.startswith('cp1252'): sys.setdefaultencoding('cp1252') # a legacy encoding used by Microsoft Windows elif cfg.startswith('utf8'): sys.setdefaultencoding('utf-8') print(db.path) # test printing the database records for r in db.records(): print(r) r.to_dict() r.to_xml() for r in db.connections(): print(r) r.to_dict() r.to_xml() assert db.records(manufacturer='Kepco')[0].manufacturer == u'Kepco and \u201cTMK\u201d shunt' assert db.records(model='MFF101/M')[0].description == u'Motorized Filter Flip Mount for \xd825mm Optics'
def test_asrl(): c = Config(os.path.join(os.path.dirname(__file__), 'db_asrl.xml')) dbase = c.database() pyvisa = dbase.equipment['pyvisa'].connection msl = dbase.equipment['msl'].connection assert pyvisa.address == 'COM1' assert msl.address == 'COM1' assert pyvisa.properties['baud_rate'] == 119200 assert msl.properties['baud_rate'] == 119200 assert pyvisa.properties['data_bits'] == DataBits.SEVEN assert msl.properties['data_bits'] == DataBits.SEVEN assert pyvisa.properties['parity'] == Parity.ODD assert msl.properties['parity'] == Parity.ODD assert pyvisa.properties['stop_bits'] == StopBits.ONE_POINT_FIVE assert msl.properties['stop_bits'] == StopBits.ONE_POINT_FIVE
def test_database_io_errors(): # no <path></path> tag with pytest.raises(IOError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err0.xml')).database() assert '<path>' in str(err.value) # database file does not exist with pytest.raises(IOError): Config(os.path.join(os.path.dirname(__file__), 'db_err1.xml')).database() # unsupported database file with pytest.raises(IOError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err2.xml')).database() assert 'database' in str(err.value) # more than 1 Sheet in the Excel database with pytest.raises(IOError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err3.xml')).database() assert 'Sheet' in str(err.value) # the 'equipment' item in the xml file is not a valid Equipment Record with pytest.raises(AttributeError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err4.xml')).database() assert 'attributes' in str(err.value) # the 'equipment' item in the xml file is not a unique Equipment Record with pytest.raises(AttributeError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err5.xml')).database() assert 'unique' in str(err.value) # the 'equipment' item in the xml file is has multiple aliases with pytest.raises(ValueError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err6.xml')).database() assert 'aliases' in str(err.value) # invalid Sheet name in Excel database with pytest.raises(IOError) as err: Config(os.path.join(os.path.dirname(__file__), 'db_err7.xml')).database() assert 'Sheet' in str(err.value)
def test_database_user_defined(): path = os.path.join(os.path.dirname(__file__), 'db_user_defined.xml') cfg = Config(path) db = cfg.database() for record in db.records(): if record.team == 'Any': assert len(record.user_defined) == 2 assert record.user_defined['nothing_relevant'] == 'XXXXXXXXXX' assert record.user_defined['policies'] == 'MSLE.X.YYY' else: assert len(record.user_defined) == 0 path = os.path.join(os.path.dirname(__file__), 'db_user_defined_bad.xml') cfg = Config(path) db = cfg.database() for record in db.records(): if record.team == 'Any': assert len(record.user_defined) == 1 assert record.user_defined['policies'] == 'MSLE.X.YYY' else: assert len(record.user_defined) == 0
def test_dbase(): path = os.path.join(os.path.dirname(__file__), 'db.xml') c = Config(path) dbase = c.database() eq1 = dbase.equipment['712ae'] assert eq1.manufacturer == 'F D080' assert eq1.model == '712ae' assert eq1.serial == '49e39f1' assert eq1.date_calibrated.year == 2010 assert eq1.date_calibrated.month == 11 assert eq1.date_calibrated.day == 1 assert eq1.category == 'DMM' assert eq1.location == 'General' assert eq1.description == 'Digital Multimeter' assert eq1.connection is None eq2 = dbase.equipment['dvm'] assert eq2.alias == 'dvm' assert eq2.calibration_cycle == 5 assert eq2.category == 'DVM' assert eq2.date_calibrated.year == 2009 assert eq2.date_calibrated.month == 11 assert eq2.date_calibrated.day == 12 assert eq2.description == 'Digital nanovoltmeter' assert eq2.location == 'Watt Lab' assert eq2.manufacturer == 'Agilent' assert eq2.model == '34420A' assert eq2.team == 'Any' assert eq2.serial == 'A00024' c = eq2.connection assert c.manufacturer == 'Agilent' assert c.model == '34420A' assert c.serial == 'A00024' assert c.address == 'ASRL1::INSTR' assert c.backend == Backend.MSL assert c.interface == MSLInterface.SERIAL assert c.properties['baud_rate'] == 9600 assert c.properties['read_termination'] == CR + LF assert c.properties['write_termination'] == LF if PY2: string = (str, unicode) else: string = str for r in dbase.records(): for key, value in r.to_dict().items(): if key == 'calibration_cycle': assert isinstance(value, float) elif key == 'date_calibrated': if value is not None: assert isinstance(value, datetime.date) elif key == 'user_defined': assert isinstance(value, dict) elif key == 'connection': if isinstance(value, dict): for k, v in value.items(): if k == 'backend': assert isinstance(v, Backend) elif k == 'interface': assert isinstance(v, MSLInterface) elif k == 'properties': assert isinstance(v, dict) else: assert isinstance(v, string) else: assert value is None else: assert isinstance(value, string)
def test_exceptions(): Config.DEMO_MODE = False with pytest.raises(TypeError) as err: connect(None) # not an EquipmentRecord assert 'EquipmentRecord' in str(err.value) with pytest.raises(TypeError) as err: connect(ConnectionRecord()) # not an EquipmentRecord assert 'EquipmentRecord' in str(err.value) with pytest.raises(TypeError) as err: connect([None]) # not an EquipmentRecord assert 'EquipmentRecord' in str(err.value) with pytest.raises(TypeError) as err: connect({None}) # not an EquipmentRecord assert 'EquipmentRecord' in str(err.value) with pytest.raises(TypeError) as err: connect([ EquipmentRecord(connection=ConnectionRecord(address='COM1', backend=Backend.MSL)), EquipmentRecord(connection=ConnectionRecord(address='COM2', backend=Backend.MSL)) ]) # only 1 EquipmentRecord allowed assert 'list' in str(err.value) with pytest.raises(TypeError) as err: connect({ 'a': EquipmentRecord(connection=ConnectionRecord(address='COM1', backend=Backend.MSL)), 'b': EquipmentRecord(connection=ConnectionRecord(address='COM2', backend=Backend.MSL)) }) # only 1 EquipmentRecord allowed assert 'dict' in str(err.value) c = Config(os.path.join(os.path.dirname(__file__), 'db.xml')) with pytest.raises(TypeError) as err: connect(c.database().equipment) # only 1 EquipmentRecord allowed assert 'dict' in str(err.value) with pytest.raises(ValueError) as err: connect(EquipmentRecord() ) # no ConnectionRecord defined for the EquipmentRecord assert 'connection object' in str(err.value) with pytest.raises(ValueError) as err: connect(EquipmentRecord( connection=ConnectionRecord())) # no address has been set assert 'connection address' in str(err.value) with pytest.raises(ValueError) as err: connect( EquipmentRecord(connection=ConnectionRecord( address='COM1', backend=Backend.UNKNOWN))) # no backend assert 'connection backend' in str(err.value) # the SDK library does not exist with pytest.raises(IOError) as err: connect( EquipmentRecord(manufacturer='Thorlabs', model='FW212C', connection=ConnectionRecord( address='SDK::invalid.dll', backend=Backend.MSL))) assert 'loadlib' in str(err)