Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_basic():
ptree = [{'Foo': {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}},
{'Foo': {'bar': {'Bar': {'a': 0, 'f': False, 't': True, 'c': [
{'C': {'x': 0, 'y': 0}},
{'C': {'x': 0, 'y': 0}}
]}}}}]
root = Node(ptree)
assert root.parent_node is None
assert root.parent_attribute is None
assert isinstance(root, List)
assert len(root) == 2
assert repr(root) == '[2*{Foo}]'
assert str(root) == 'None=[2*{Foo}]'
with pytest.raises(AttributeError):
root.not_there
foo1 = root[0]
assert foo1 != root
assert foo1.node_tag == 'Foo'
assert foo1.parse_tree == {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}
assert foo1.parent_node is None
assert foo1.parent_attribute == (None, 0)
assert repr(foo1) == '{Foo}'
assert str(foo1) == 'None[0]={Foo}'
assert foo1.attribute_names == {'bar'}
assert foo1.not_there is Missing
foo2 = root[1]
assert foo2 != foo1
bar2 = foo2['bar']
assert bar2 != bar1
assert bar2.parent_attribute == 'bar'
assert bar2.attribute_names == {'a', 'c', 'f', 't'}
assert bar2.a == DummyIntEnum.ZERO
assert bar2.a != DummyIntEnum.ONE
assert not bar2.f
assert bar2.t
assert repr(bar2.f) == ''
assert repr(bar2.t) == ''
c = bar2.c
assert isinstance(c, List)
assert len(c) == 2
assert repr(c) == '[2*{C}]'
c1 = c[0]
c2 = c[1]
assert c1.parent_attribute == ('c', 0)
assert c2.parent_attribute == ('c', 1)
assert c1 != c2
assert c1.parent_node[c1.parent_attribute] == c1
assert str(c1) == 'c[0]={C}'
assert str(c2) == 'c[1]={C}'
x1 = c1['x']
x2 = c2['x']
assert isinstance(x1, Scalar)
assert x1 != x2
def __new__(cls, details, parent=None, name=None):
if not isinstance(parent, (Node, NoneType)):
raise ValueError("Unexpected value for 'parent', must be either None"
" or a Node instance, got %r" % type(parent))
if not isinstance(name, (NoneType, str, tuple)):
raise ValueError("Unexpected value for 'name', must be either None,"
" a string or a tuple, got %r" % type(name))
if isinstance(details, list):
self = super().__new__(List)
elif isinstance(details, dict):
self = super().__new__(Node)
else:
self = super().__new__(Scalar)
self._init(details, parent, name)
return self
def a_expr(node, output):
aek = enums.A_Expr_Kind
if node.kind == aek.AEXPR_OP:
with output.expression():
# lexpr is optional because these are valid: -(1+1), +(1+1), ~(1+1)
if node.lexpr is not Missing:
if node.lexpr.node_tag == 'A_Expr':
with output.expression():
output.print_node(node.lexpr)
else:
output.print_node(node.lexpr)
output.write(' ')
if isinstance(node.name, List) and len(node.name) > 1:
output.write('OPERATOR(')
output.print_symbol(node.name)
output.write(') ')
else:
output.print_symbol(node.name)
output.write(' ')
if node.rexpr.node_tag == 'A_Expr':
with output.expression():
output.print_node(node.rexpr)
else:
output.print_node(node.rexpr)
elif node.kind == aek.AEXPR_OP_ANY:
output.print_node(node.lexpr)
output.write(' ')
output.write(node.name.string_value)
output.write(' ANY(')
def __call__(self, sql, plpgsql=False):
"""Main entry point: execute :meth:`print_node` on each statement in `sql`.
:param sql: either the source SQL in textual form, or a :class:`~.node.Node` instance
:param bool plpgsql: whether `sql` is really a ``plpgsql`` statement
:returns: a string with the equivalent SQL obtained by serializing the syntax tree
"""
if isinstance(sql, str):
sql = Node(parse_plpgsql(sql) if plpgsql else parse_sql(sql))
elif isinstance(sql, Node):
sql = [sql]
elif not isinstance(sql, List):
raise ValueError("Unexpected value for 'sql', must be either a string,"
" a Node instance or a List instance, got %r" % type(sql))
first = True
for statement in sql:
if first:
first = False
else:
self.write(';')
self.newline()
for _ in range(self.separate_statements):
self.newline()
self.print_node(statement)
if self.semicolon_after_last_statement:
self.write(';')
return self.getvalue()
def def_elem(node, output):
output.print_node(node.defname)
if node.arg is not Missing:
output.write(' = ')
if isinstance(node.arg, List):
output.write(node.arg.string_value)
else:
output.print_node(node.arg)
if node.defaction != enums.DefElemAction.DEFELEM_UNSPEC: # pragma: nocover
raise NotImplementedError
nodes = list(node.object)
output.print_name(nodes.pop())
output.write(' ON ')
output.print_name(nodes)
elif node.objtype == otypes.OBJECT_DOMCONSTRAINT:
nodes = list(node.object)
output.print_name(nodes.pop())
output.write(' ON DOMAIN ')
output.print_name(nodes)
elif node.objtype == otypes.OBJECT_TRANSFORM:
nodes = list(node.object)
output.write('FOR ')
output.print_name(nodes.pop(0))
output.write(' LANGUAGE ')
output.print_name(nodes)
elif isinstance(node.object, List):
if node.object[0].node_tag != 'String':
output.write(' (')
output.print_list(node.object, ' AS ', standalone_items=False)
output.write(')')
else:
output.print_name(node.object)
else:
output.print_name(node.object)
output.newline()
output.space(2)
output.write('IS ')
with output.push_indent():
output._write_quoted_string(node.comment.value)
def print_name(self, nodes, sep='.'):
"Helper method, execute :meth:`print_node` or :meth:`print_list` as needed."
if isinstance(nodes, (List, list)):
self.print_list(nodes, sep, standalone_items=False, are_names=True)
else:
self.print_node(nodes, is_name=True)