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_prettification(src, lineno, case):
parts = case.split('\n=\n')
original = parts[0].strip()
parts = parts[1].split('\n:\n')
expected = parts[0].strip().replace('\\n\\\n', '\n')
if len(parts) == 2:
options = literal_eval(parts[1])
else:
options = {}
prettified = IndentedStream(**options)(original)
assert expected == prettified, "%s:%d:%r != %r" % (src, lineno, expected, prettified)
def test_prettification(sample):
parts = sample.split('\n=\n')
original = parts[0].strip()
parts = parts[1].split('\n:\n')
expected = parts[0].strip()
if len(parts) == 2:
options = literal_eval(parts[1])
else:
options = {}
prettified = IndentedStream(**options)(original)
assert expected == prettified, "%r != %r" % (expected, prettified)
def roundtrip(sql):
orig_ast = parse_sql(sql)
_remove_stmt_len_and_location(orig_ast)
serialized = RawStream()(Node(orig_ast))
try:
serialized_ast = parse_sql(serialized)
except: # noqa
raise RuntimeError("Could not reparse %r" % serialized)
_remove_stmt_len_and_location(serialized_ast)
assert orig_ast == serialized_ast, "%r != %r" % (sql, serialized)
indented = IndentedStream()(Node(orig_ast))
try:
indented_ast = parse_sql(indented)
except: # noqa
raise RuntimeError("Could not reparse %r" % indented)
_remove_stmt_len_and_location(indented_ast)
assert orig_ast == indented_ast, "%r != %r" % (sql, indented)
# Run ``pytest -s tests/`` to see the following output
print()
print(indented)
def test_indented_stream_with_sql():
raw_stmt_printer = printer.NODE_PRINTERS.pop('RawStmt', None)
try:
@printer.node_printer('RawStmt')
def raw_stmt(node, output):
output.write('Yeah')
output = printer.IndentedStream()
result = output('SELECT 1; SELECT 2')
assert result == 'Yeah;\n\nYeah'
output = printer.IndentedStream(separate_statements=False)
result = output('SELECT 1; SELECT 2')
assert result == 'Yeah;\nYeah'
finally:
if raw_stmt_printer is not None:
printer.NODE_PRINTERS['RawStmt'] = raw_stmt_printer
else:
printer.NODE_PRINTERS.pop('RawStmt', None)