def test_relationship_detail(variable, types, length, properties, query, parameters): ast = RelationshipDetail( variable=Variable(variable) if variable is not None else None, types=NonEmptySequence[RelTypeName]( RelTypeName(types[0]), *( RelTypeName(type_) for type_ in types[1:] ), ) if types else None, length=RangeLiteral( start=length[0], end=length[1], ) if length else None, properties=MapLiteral([ (key, Parameter(key, key, value)) for key, value in properties.items() ]) if properties else None, ) assert_that( str(ast), is_(equal_to(query)), ) assert_that( dict(ast), is_(equal_to(parameters)), )
def test_match(expression, variable, query, parameters): ast = Unwind( expression=Expression(expression), variable=Variable(variable), ) assert_that( str(ast), is_(equal_to(query)), ) assert_that( dict(ast), is_(equal_to(parameters)), )
def test_pattern(): ast = Pattern(items=NonEmptySequence[PatternPart](PatternPart( pattern_element=PatternElement(items=[], ), variable=Variable("foo"), ), ), ) assert_that( str(ast), is_(equal_to("foo = ()")), ) assert_that( dict(ast), is_(equal_to(dict())), )
def test_expression_alias(): ast = ExpressionAlias( Expression("foo"), Variable("bar"), ) assert_that( str(ast), is_(equal_to("foo AS bar")), ) assert_that( dict(ast), is_(equal_to(dict())), )
def test_set_variable_node_labels_item(): ast = SetVariableNodeLabelsItem( target=Variable("foo"), value=NodeLabels(NodeLabel("Bar"), ), ) assert_that( str(ast), is_(equal_to("foo:Bar")), ) assert_that( dict(ast), is_(equal_to(dict())), )
def test_set_variable_item(variable, expression, mutate, query, parameters): ast = SetVariableItem( target=Variable(variable), value=Expression(expression), mutate=mutate, ) assert_that( str(ast), is_(equal_to(query)), ) assert_that( dict(ast), is_(equal_to(parameters)), )
def test_relationship_detail(variable, labels, properties, query, parameters): ast = NodePattern( variable=Variable(variable) if variable is not None else None, labels=NonEmptySequence[NodeLabel]( NodeLabel(labels[0]), *(NodeLabel(label) for label in labels[1:]), ) if labels else None, properties=MapLiteral([(key, Parameter(key, key, value)) for key, value in properties.items()]) if properties else None, ) assert_that( str(ast), is_(equal_to(query)), ) assert_that( dict(ast), is_(equal_to(parameters)), )
def test_set(): ast = Set(items=SetItems( SetVariableNodeLabelsItem( target=Variable("foo"), value=NodeLabels(NodeLabel("Bar"), ), ), *(SetPropertyItem( target=PropertyExpression( value="foo", properties=NonEmptySequence[PropertyLookup]( PropertyLookup("bar"), ), ), value=Expression("baz"), ), ), ), ) assert_that( str(ast), is_(equal_to("SET foo:Bar, foo.bar = baz")), ) assert_that( dict(ast), is_(equal_to(dict())), )
def remove_item(cls, head: str, tail: str) -> RemoveItem: if ":" in tail: node_labels = [ NodeLabel(item.lstrip(":")) for item in tail.split() ] return RemoveItem(value=( Variable(head), NodeLabels( node_labels[0], *node_labels[1:], ), ), ) else: property_lookups = [ PropertyLookup(item) for item in tail.split(".") ] return RemoveItem(value=PropertyExpression( value=head, properties=NonEmptySequence[PropertyLookup]( property_lookups[0], *property_lookups[1:], ), ), )
def rel( self, variable: Optional[str] = None, types: Optional[Union[str, Sequence[str]]] = None, properties: Optional[Union[MapLiteral, Mapping[str, str]]] = None, length: Optional[Union[Tuple, Tuple[int], Tuple[int, int]]] = None, pattern_type=RelationshipPatternType.NONE ) -> RelationshipDetailBuilder: if isinstance(types, str): types = (types, ) if isinstance(properties, Mapping): properties = build_properties( build_parameters( name_prefix=variable, **properties, ), ) return RelationshipDetailBuilder( builder=self, relationship_pattern=RelationshipPattern( detail=RelationshipDetail( variable=Variable(variable) if variable else None, types=NonEmptySequence( RelTypeName(types[0]), *(RelTypeName(type_) for type_ in types[1:]), ) if types else None, length=RangeLiteral( start=length[0] if len(length) > 0 else None, end=length[1] if len(length) > 1 else None, ) if length is not None else None, properties=properties, ), pattern_type=pattern_type, ), )
def _node( variable: Optional[str] = None, labels: Optional[Union[str, Sequence[str]]] = None, properties: Optional[Union[MapLiteral, Mapping[str, str]]] = None ) -> NodePattern: if isinstance(labels, str): labels = (labels, ) if isinstance(properties, Mapping): properties = build_properties( build_parameters( name_prefix=variable, **properties, ), ) return NodePattern( variable=Variable(variable) if variable else None, labels=NonEmptySequence( NodeLabel(labels[0]), *(NodeLabel(label) for label in labels[1:]), ) if labels else None, properties=properties, )
def set_item(cls, parameter: Parameter) -> SetItem: return SetVariableItem( target=Variable(parameter.key), value=expr(parameter), )
NodeLabels, NonEmptySequence, Remove, RemoveItem, RemoveItems, Parameter, PropertyExpression, PropertyLookup, Variable, ) @parameterized([ ( ( Variable("foo"), NodeLabels( NodeLabel("Bar"), NodeLabel("Baz"), ), ), "REMOVE foo:Bar:Baz", dict(), ), ( PropertyExpression( "foo", NonEmptySequence[PropertyLookup](PropertyLookup("bar"), ), ), "REMOVE foo.bar", dict(),