def multi_value_complex_object(): return DataObject('PDUs', DataQuery.XPath('.//I-SIGNAL-I-PDU'), [ DataValue('Name', DataQuery(DataQuery.XPath('SHORT-NAME'))), DataValue('Length', DataQuery(DataQuery.XPath('LENGTH'), format=DataQuery.Format.Integer)), DataValue( 'Unused Bit Pattern', (DataQuery(DataQuery.XPath('UNUSED-BIT-PATTERN'), format=DataQuery.Format.Integer))), DataObject('Timing Specification', DataQuery.XPath('./*/I-PDU-TIMING'), [ DataValue('Minimum Delay', DataQuery(DataQuery.XPath('MINIMUM-DELAY'), format=DataQuery.Format.Integer)), DataValue( 'Cyclic Timing', DataQuery( DataQuery.XPath( 'TRANSMISSION-MODE-DECLARATION/TRANSMISSION-MODE-TRUE-TIMING/CYCLIC-TIMING/TIME-PERIOD/VALUE' ), format=DataQuery.Format.Float)) ]), DataObject('Signal Mappings', DataQuery.XPath('.//I-SIGNAL-TO-I-PDU-MAPPING'), [ DataValue('Signal Name', DataQuery(DataQuery.XPath('SHORT-NAME'))), DataValue('Start Position', DataQuery(DataQuery.XPath('START-POSITION'), format=DataQuery.Format.Integer)), DataValue('Transfer Property', DataQuery(DataQuery.XPath('TRANSFER-PROPERTY'))), DataObject('Signal', DataQuery.XPath('I-SIGNAL-REF', is_reference=True), [ DataValue('Init Value', DataQuery(DataQuery.XPath('.//VALUE'), format=DataQuery.Format.Integer)), DataValue('Length', DataQuery(DataQuery.XPath('LENGTH'), format=DataQuery.Format.Integer)) ]) ]) ])
def simple_object_by_xpath(): return DataObject('CAN Cluster', DataQuery.XPath('.//AR-PACKAGE/ELEMENTS/CAN-CLUSTER'), [ DataValue('Name', DataQuery(DataQuery.XPath('./SHORT-NAME'))), DataValue( 'Baudrate', DataQuery( DataQuery.XPath('CAN-CLUSTER-VARIANTS/CAN-CLUSTER-CONDITIONAL/BAUDRATE'), format=DataQuery.Format.Integer)), DataValue('Language', DataQuery(DataQuery.XPath('LONG-NAME/L-4'), value='@L')), DataValue('Long Name', DataQuery(DataQuery.XPath('LONG-NAME/L-4'))) ])
def complex_object_by_ref(): return DataObject('CAN Cluster', DataQuery.Reference('/Cluster/CAN'), [ DataValue('Name', DataQuery(DataQuery.XPath('./SHORT-NAME'))), DataValue( 'Baudrate', DataQuery( DataQuery.XPath('CAN-CLUSTER-VARIANTS/CAN-CLUSTER-CONDITIONAL/BAUDRATE'), format=DataQuery.Format.Integer)), DataObject('Long Name', DataQuery.XPath('LONG-NAME/L-4'), [ DataValue('Language', DataQuery(DataQuery.XPath('.'), value='@L')), DataValue('Name', DataQuery(DataQuery.XPath('.'))) ]) ])
def test_create_data_query_with_relative_xpath(): path = DataQuery.XPath('/SHORT-NAME') query = DataQuery(path) assert query.path == path assert query.value == 'text' assert query.format == DataQuery.Format.String
def test_create_data_query_with_absolute_xpath(): path = DataQuery.XPath('/AR-PACKAGE/SHORT-NAME', False) query = DataQuery(path) assert query.path == path assert query.value == 'text' assert query.format == DataQuery.Format.String
def test_returns_empty_list_if_reference_not_found(only_value): data_object = DataObject('CAN Cluster', DataQuery.Reference('/Cluster/CAN1'), [DataValue('Name', DataQuery(DataQuery.XPath('./SHORT-NAME')))]) query_handler = QueryHandler() result = query_handler.handle_queries(arxml, [data_object]) assert result['CAN Cluster'] == []
def test_create_data_query_with_default_value(): path = DataQuery.XPath('/SHORT-NAME') format = DataQuery.Format.Date query = DataQuery(path, format=format) assert query.path == path assert query.value == 'text' assert query.format == format
def test_returns_none_value_if_no_attribute_found_with_specified_name(): data_object = DataObject('CAN Cluster', DataQuery.Reference('/Cluster/CAN'), [DataValue('UUID', DataQuery(DataQuery.XPath('.'), value='@S'))]) query_handler = QueryHandler() result = query_handler.handle_queries(arxml, [data_object]) assert isinstance(result['CAN Cluster'], dict) assert result['CAN Cluster']['UUID'] is None
def test_returns_none_value_if_no_element_found_with_xpath(): data_object = DataObject('CAN Cluster', DataQuery.Reference('/Cluster/CAN'), [DataValue('Name', DataQuery(DataQuery.XPath('/SHORT-NAME')))]) query_handler = QueryHandler() result = query_handler.handle_queries(arxml, [data_object]) assert isinstance(result['CAN Cluster'], dict) assert result['CAN Cluster']['Name'] is None
def __parse_query(self, text: str) -> DataQuery: if self.__path_separator not in text: path = self.__get_path(text) return DataQuery(path) raw_value_format, raw_path = text.split(self.__path_separator, 2) path = self.__get_path(raw_path) if self.__format_separator in raw_value_format: raw_value, raw_format = raw_value_format.split( self.__format_separator) value = self.__get_value(raw_value) format = self.__get_format(raw_format) else: value = self.__get_value(raw_value_format) format = DataQuery.Format.String return DataQuery(path, value, format)
def test_handle_inline_reference(): data_object = DataObject('SignalMapping', DataQuery.Reference('/PDU/TxMessage'), [ DataValue( 'Name', DataQuery( DataQuery.XPath( '&(I-SIGNAL-TO-PDU-MAPPINGS/I-SIGNAL-TO-I-PDU-MAPPING/I-SIGNAL-REF)SHORT-NAME', True))) ]) query_handler = QueryHandler() results = query_handler.handle_queries(arxml, [data_object]) assert isinstance(results, dict) assert isinstance(results['SignalMapping'], dict) assert results['SignalMapping']['Name'] == 'Signal1'
def test_create_data_query_with_default_format(path, value): query = DataQuery(path, value) assert query.path == path assert query.value == value assert query.format == DataQuery.Format.String
def test_create_data_query(path, value, format): query = DataQuery(path, value, format) assert query.path == path assert query.value == value assert query.format == format
def test_xpath_containing_namespaces_raise_value_error(): with pytest.raises(ValueError): DataQuery(DataQuery.XPath('/ar:SHORT-NAME'))
def value(): query = DataQuery(DataQuery.XPath('/SHORT-NAME')) return DataValue('Name', query)
def test_invalid_value_raise_value_error(): with pytest.raises(ValueError): DataQuery(DataQuery.XPath('/SHORT-NAME'), 'something')
def test_invalid_path_raises_exception(): with pytest.raises(TypeError): DataQuery('/SHORT-NAME')
import pytest from arxml_data_extractor.query.data_query import DataQuery from arxml_data_extractor.query.data_value import DataValue @pytest.mark.parametrize("name, query", [ ('Signal', DataQuery(DataQuery.XPath('/SHORT-NAME'))), ('Signal123', DataQuery(DataQuery.XPath('/SHORT-NAME'))), ('Signal_123', DataQuery(DataQuery.XPath('/SHORT-NAME'))), ('Signal 123', DataQuery(DataQuery.XPath('/SHORT-NAME'))), ]) def test_create_data_value(name, query): value = DataValue(name, query) assert value.name == name assert value.query == query def test_none_query_raises_exception(): with pytest.raises(Exception): DataValue('name', None)
def test_reference_containing_namespaces_raise_value_error(): with pytest.raises(ValueError): DataQuery(DataQuery.Reference('/ar:Signals'))
def only_value(): return DataValue( 'CAN Cluster', DataQuery(DataQuery.Reference('/Cluster/CAN1'), 'text', DataQuery.Format.String))