def test_data_objects_can_contain_data_object(): config = { 'SimpleObject': { '_xpath': '/path/element', 'SimpleValue': '/SHORT-NAME', 'OtherObject': { '_xpath': '/path/other_element', 'Value': '@T>date:/date_time' } } } builder = QueryBuilder() data_objects = builder.build(config) other_object = data_objects[0].values[1] assert isinstance(other_object, DataObject) assert isinstance(other_object.path, DataQuery.XPath) assert other_object.path.xpath == '/path/other_element' data_value = other_object.values[0] assert isinstance(data_value, DataValue) assert data_value.name == 'Value' assert isinstance(data_value.query.path, DataQuery.XPath) assert data_value.query.path.xpath == '/date_time' assert data_value.query.value == '@T' assert data_value.query.format == DataQuery.Format.Date
def test_build_simple_data_object_with_xpath(): config = { 'SimpleObject': { '_xpath': '/path/element', 'SimpleValue': '/SHORT-NAME' } } builder = QueryBuilder() data_objects = builder.build(config) assert isinstance(data_objects, list) data_object = data_objects[0] assert isinstance(data_object, DataObject) assert data_object.name == 'SimpleObject' assert isinstance(data_object.path, DataQuery.XPath) assert data_object.path.xpath == '/path/element' data_value = data_object.values[0] assert isinstance(data_value, DataValue) assert data_value.name == 'SimpleValue' assert isinstance(data_value.query, DataQuery) assert isinstance(data_value.query.path, DataQuery.XPath) assert data_value.query.path.xpath == '/SHORT-NAME' assert data_value.query.path.is_reference is False assert data_value.query.value == 'text' assert data_value.query.format == DataQuery.Format.String
def test_object_has_too_many_separators(): config = { 'SimpleObject': { '_xpath': '/path/element', 'SimpleValue': '/ar:AR-PACKAGE/ar:SHORT-NAME' } } builder = QueryBuilder() with pytest.raises(ValueError): builder.build(config)
def test_invalid_inline_reference(): config = { 'SimpleObject': { '_ref': '/path/element', 'RefValue': '&path-to-elementvalue' } } builder = QueryBuilder() with pytest.raises(ValueError): builder.build(config)
def test_invalid_attribute_characters(): config = { 'SimpleObject': { '_xpath': '/path/element', 'SimpleValue': '@:/SHORT-NAME' } } builder = QueryBuilder() with pytest.raises(ValueError): builder.build(config)
def test_object_with_xpath_and_ref(): config = { 'SimpleObject': { '_xpath': '/path/element', '_ref': '/ref/element', 'SimpleValue': '/SHORT-NAME' } } builder = QueryBuilder() with pytest.raises(ValueError): builder.build(config)
def build_queries(config): logger = logging.getLogger() logger.info('START PROCESS - building queries from configuration') try: query_builder = QueryBuilder() queries = query_builder.build(config) except Exception as e: handle_exception('building queries', e) sys.exit(-1) logger.info('END PROCESS - successfully finished building queries from configuration') return queries
def test_text_value_with_invalid_format(): config = { 'SimpleObject': { '_xpath': '/path/element', 'SimpleValue': 'text>bool:/SHORT-NAME' } } builder = QueryBuilder() data_objects = builder.build(config) data_value = data_objects[0].values[0] assert isinstance(data_value, DataValue) assert data_value.name == 'SimpleValue' assert data_value.query.path == DataQuery.XPath('/SHORT-NAME') assert data_value.query.value == 'text' assert data_value.query.format == DataQuery.Format.String
def test_attribute_value(): config = { 'SimpleObject': { '_xpath': '/path/element', 'SimpleValue': '@T:/SHORT-NAME' } } builder = QueryBuilder() data_objects = builder.build(config) data_value = data_objects[0].values[0] assert isinstance(data_value, DataValue) assert data_value.name == 'SimpleValue' assert data_value.query.path == DataQuery.XPath('/SHORT-NAME') assert data_value.query.value == '@T' assert data_value.query.format == DataQuery.Format.String
def test_extracting_nested_objects(): yaml = """ 'PDUs': '_xpath': './/I-SIGNAL-I-PDU' 'Name': 'text:SHORT-NAME' 'Length': 'text>int:LENGTH' 'Cyclic Timing': 'text>float:.//TRANSMISSION-MODE-TRUE-TIMING/CYCLIC-TIMING/TIME-PERIOD/VALUE' 'Signal Mappings': '_xpath': './/I-SIGNAL-TO-I-PDU-MAPPING' 'Signal': 'SHORT-NAME' 'Start Position': 'text>int:START-POSITION' 'ISignal': '_xref': 'I-SIGNAL-REF' 'Init Value': 'text>int:.//VALUE' 'Length': 'text>int:LENGTH' """ config_provider = ConfigProvider() config = config_provider.parse(yaml) query_builder = QueryBuilder() queries = query_builder.build(config) query_handler = QueryHandler() data = query_handler.handle_queries(arxml, queries) assert isinstance(data, dict) assert len(data['PDUs']) == 2 assert data['PDUs'][0]['Name'] == 'TxMessage' assert data['PDUs'][0]['Length'] == 5 assert data['PDUs'][0]['Cyclic Timing'] == 0.1 assert data['PDUs'][0]['Signal Mappings']['Signal'] == 'Signal1' assert data['PDUs'][0]['Signal Mappings']['Start Position'] == 0 assert data['PDUs'][0]['Signal Mappings']['ISignal']['Init Value'] == 128 assert data['PDUs'][0]['Signal Mappings']['ISignal']['Length'] == 5 assert data['PDUs'][1]['Name'] == 'RxMessage' assert data['PDUs'][1]['Length'] == 2 assert data['PDUs'][1]['Cyclic Timing'] == 0.1 assert data['PDUs'][1]['Signal Mappings'][0]['Signal'] == 'Signal2' assert data['PDUs'][1]['Signal Mappings'][0]['Start Position'] == 0 assert data['PDUs'][1]['Signal Mappings'][0]['ISignal']['Init Value'] == 0 assert data['PDUs'][1]['Signal Mappings'][0]['ISignal']['Length'] == 1 assert data['PDUs'][1]['Signal Mappings'][1]['Signal'] == 'Signal3' assert data['PDUs'][1]['Signal Mappings'][1]['Start Position'] == 1 assert data['PDUs'][1]['Signal Mappings'][1]['ISignal']['Init Value'] == 0 assert data['PDUs'][1]['Signal Mappings'][1]['ISignal']['Length'] == 1
def test_inline_reference(): config = { 'SimpleObject': { '_ref': '/path/element', 'RefValue': '&(path-to-element)value' } } builder = QueryBuilder() data_objects = builder.build(config) simple_object = data_objects[0] ref_value = simple_object.values[0] assert isinstance(simple_object, DataObject) assert isinstance(ref_value, DataValue) assert isinstance(ref_value.query.path, DataQuery.XPath) assert ref_value.query.path.xpath == '&(path-to-element)value' assert ref_value.query.path.is_reference is True
def test_object_contains_xref(): config = { 'BaseObject': { '_xpath': '/path/element', 'SimpleValue': '/SHORT-NAME', 'ReferredObject': { '_xref': '/ref/object', 'Value': '/value' } } } builder = QueryBuilder() data_objects = builder.build(config) referred_object = data_objects[0].values[1] assert isinstance(referred_object, DataObject) assert isinstance(referred_object.path, DataQuery.XPath) assert referred_object.path.xpath == '/ref/object' assert referred_object.path.is_reference is True
def test_extracting_simple_object(): yaml = """ 'CAN Cluster': '_ref': '/Cluster/CAN' 'Name': 'SHORT-NAME' 'Baudrate': 'text>int:CAN-CLUSTER-VARIANTS/CAN-CLUSTER-CONDITIONAL/BAUDRATE' 'Long Name': 'text:LONG-NAME/L-4' 'Language': '@L:LONG-NAME/L-4' """ config_provider = ConfigProvider() config = config_provider.parse(yaml) query_builder = QueryBuilder() queries = query_builder.build(config) query_handler = QueryHandler() data = query_handler.handle_queries(arxml, queries) assert isinstance(data, dict) assert data['CAN Cluster']['Name'] == 'CAN' assert data['CAN Cluster']['Baudrate'] == 500000 assert data['CAN Cluster']['Long Name'] == 'CAN Channel 1' assert data['CAN Cluster']['Language'] == 'FOR-ALL'
def test_object_is_missing_xpath_or_ref(): config = {'SimpleObject': {'SimpleValue': '/SHORT-NAME'}} builder = QueryBuilder() with pytest.raises(ValueError): builder.build(config)