def test_int_field(): d = { 'bar': 42 } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert struct['bar'] == 42
def test_null_field(): d = { 'bar': None } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert struct['bar'] is None
def test_float_field(): d = { 'bar': 0.1 } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert struct['bar'] == 0.1
def test_empty_list(): d = { 'bar': [] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert list(struct['bar']) == []
def Process(self, request, context=None): event_id = request.event_id event_service_instance_id = request.event_service_instance_id logger.debug( 'processor_id: %s received process request on event_id: %s with event_service_instance_id: %s', self._runner.processor_id, event_id, event_service_instance_id) params = {} _structs.copy_struct_to_dict(request.params, params) try: response = processing_pb2.ProcessResponse() result, times, added_indices = self._runner.call_process( event_id, event_service_instance_id, params) if result is not None: _structs.copy_dict_to_struct(result, response.result, []) _timing.add_times(self._times_map, times) for k, l in times.items(): response.timing_info[k].FromTimedelta(l) for document_name, l in added_indices.items(): for index_name in l: created_index = response.created_indices.add() created_index.document_name = document_name created_index.index_name = index_name return response except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) return empty_pb2.Empty()
def test_bool_field(): d = { 'bar': True } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert struct['bar'] is True
def test_str_field(): d = { 'bar': 'baz' } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert struct['bar'] == 'baz'
def test_string_list(): d = { 'bar': ['a', 'b', 'c', 'd'] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert isinstance(struct['bar'], struct_pb2.ListValue) assert list(struct['bar']) == ['a', 'b', 'c', 'd']
def test_float_list(): d = { 'bar': [0.1, 1.2, 2.2, 3.3] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert isinstance(struct['bar'], struct_pb2.ListValue) assert list(struct['bar']) == [0.1, 1.2, 2.2, 3.3]
def test_int_list(): d = { 'bar': [0, 1, 2, 3] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert isinstance(struct['bar'], struct_pb2.ListValue) assert list(struct['bar']) == [0, 1, 2, 3]
def test_bool_list(): d = { 'bar': [True, False, False, True] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert isinstance(struct['bar'], struct_pb2.ListValue) assert list(struct['bar']) == [True, False, False, True]
def test_list_of_lists(): d = { 'bar': [[0, 1, 2, 3], [4, 5, 6]] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert isinstance(struct['bar'], struct_pb2.ListValue) assert list(struct['bar'][0]) == [0, 1, 2, 3] assert list(struct['bar'][1]) == [4, 5, 6]
def test_copies_object(): class TestClass: def __init__(self): self.bar = 'baz' b = TestClass() a = { 'b': b } with pytest.raises(TypeError): copy_dict_to_struct(a, struct_pb2.Struct())
def test_dict_in_dict(): a = { 'b': { 'bar': 'baz' } } struct = struct_pb2.Struct() copy_dict_to_struct(a, struct) assert isinstance(struct['b'], struct_pb2.Struct) assert struct['b']['bar'] == 'baz'
def add_to_message(self, labels: List['mtap.GenericLabel'], request): generic_labels = request.generic_labels for label in labels: label_message = generic_labels.labels.add() label_message.identifier = label.identifier label_message.start_index = label.start_index label_message.end_index = label.end_index _structs.copy_dict_to_struct(label.fields, label_message.fields, [label]) _structs.copy_dict_to_struct(label.reference_field_ids, label_message.reference_ids, [label])
def test_list_of_objects(): class TestClass: def __init__(self, val): self.bar = val d = { 'bar': [ TestClass('a'), TestClass(1), TestClass(False) ] } with pytest.raises(TypeError): copy_dict_to_struct(d, struct_pb2.Struct())
def test_list_of_dicts(): d = { 'a': [ { 'bar': 'baz' }, { 'foo': 'bar' } ] } struct = struct_pb2.Struct() copy_dict_to_struct(d, struct) assert isinstance(struct['a'], struct_pb2.ListValue) assert isinstance(struct['a'][0], struct_pb2.Struct) assert struct['a'][0]['bar'] == 'baz' assert isinstance(struct['a'][1], struct_pb2.Struct) assert struct['a'][1]['foo'] == 'bar'
def call_process(self, event_id, event_instance_id, params): logger.debug( 'processor_id: %s calling process on event_id: %s with event_instance_id: %s', self.processor_id, event_id, event_instance_id) p = dict(self.params or {}) if params is not None: p.update(params) with _base.Processor.enter_context() as context: request = processing_pb2.ProcessRequest( processor_id=self.processor_id, event_id=event_id, event_service_instance_id=event_instance_id) _structs.copy_dict_to_struct(p, request.params, [p]) with _base.Processor.started_stopwatch('remote_call'): try: response = self._stub.Process(request) except Exception as e: logger.error( "Error while processing event_id %s through remote pipeline " "component '%s'", event_id, self.component_id) raise e r = {} _structs.copy_struct_to_dict(response.result, r) timing_info = response.timing_info for k, v in timing_info.items(): context.add_time(k, v.ToTimedelta()) created_indices = {} for created_index in response.created_indices: try: doc_created_indices = created_indices[ created_index.document_name] except KeyError: doc_created_indices = [] created_indices[ created_index.document_name] = doc_created_indices doc_created_indices.append(created_index.index_name) return r, context.times, created_indices
def call_process(self, event_id, params): self.processed += 1 p = dict(self.params or {}) if params is not None: p.update(params) with EventProcessor.enter_context() as context: try: request = processing_pb2.ProcessRequest( processor_id=self._processor_id, event_id=event_id) _structs.copy_dict_to_struct(p, request.params, [p]) with Processor.started_stopwatch('remote_call'): response = self._stub.Process(request) r = {} _structs.copy_struct_to_dict(response.result, r) timing_info = response.timing_info for k, v in timing_info.items(): context.add_time(k, v.ToTimedelta()) created_indices = {} for created_index in response.created_indices: try: doc_created_indices = created_indices[ created_index.document_name] except KeyError: doc_created_indices = [] created_indices[ created_index.document_name] = doc_created_indices doc_created_indices.append(created_index.index_name) return r, context.times, created_indices except Exception as e: self.failure_count += 1 logger.error( 'Processor "%s" failed while processing event with id: %s', self.component_id, event_id) logger.error(e) raise e
def test_recursive_looping(): a = {} a['a'] = {'a': a} with pytest.raises(ValueError): copy_dict_to_struct(a, struct_pb2.Struct())
def GetInfo(self, request, context): response = processing_pb2.GetInfoResponse() _structs.copy_dict_to_struct(self._runner.metadata, response.metadata) return response
def test_empty(): d = {} copy_dict_to_struct(d, struct_pb2.Struct())
def add_to_message(self, labels, request): json_labels = request.json_labels for label in labels: _structs.copy_dict_to_struct(label.fields, json_labels.labels.add(), [label])
def test_recursive_looping_in_list(): a = {} a['bar'] = [a] with pytest.raises(ValueError): copy_dict_to_struct(a, struct_pb2.Struct())