def get(self, cls, **attr_filter): attr_filter = dict_to_db_values_dict(attr_filter) # Workaround that allows nodes and relationships with no # attrs to be saved. `save` will cause this method to be called # with an empty attr_filter, and when it receives None, will add # a new object. if not attr_filter: return None type_registry = self.type_registry unique_attrs = [key for _, key in type_registry.get_unique_attrs(cls)] query_params = { key: value for key, value in attr_filter.items() if key in unique_attrs and value is not None } if not query_params: raise ValueError( 'No relevant indexes found when calling get for class: {}' ' with filter {}'.format(cls, attr_filter) ) labels = type_registry.get_labels_for_type(cls) # since we found an index, we have at least one label node_declaration = 'n:' + ':'.join(labels) params = parameter_map(attr_filter, 'params') return self.query_single( "MATCH (%s %s) RETURN n" % (node_declaration, params), params=attr_filter)
def test_parameter_map(): assert parameter_map({'foo': 'bar', 'baz': 'ಠ_ಠ'}, "params") == ( '{foo: {params}.foo, baz: {params}.baz}' )