# Path: format_sql/main.py
# def _get_args(call_args):
#     parser = ArgumentParser('format-sql')
# 
#     parser.add_argument('--types', dest='types', type=str,
#                         action='append',
#                         help='Only process these given file types.')
#     parser.add_argument('paths', type=str, nargs='+')
#     parser.add_argument('-r', '--recursive', dest='recursive',
#                         action='store_true', default=False,
#                         help='Process files found in subdirectories.')
#     parser.add_argument('--no-semicolon', action='store_true', default=False,
#                         help='Try to detect SQL queries with no trailing semicolon.')
#     parser.add_argument('--version', action='version',
#                         version='format-sql 0.12.0')
#     parser.add_argument('--debug', dest='debug', action='store_true', default=False,
#                         help='Print available debug information.')
#     parser.add_argument('--dry-run', dest='dry_run', action='store_true', default=False,
#                         help='Print the altered output and do not change the file.')
# 
#     args, _unused_unknown_args = parser.parse_known_args(call_args)
#     if not args.types:
#         args.types = []
# 
#     return args
# 
# def get_statements(lines):
#     regex = r'(\s*)[_\w\d\.]*\s*=*\s*[_\w\d\.]*\s*\(*("""|\'\'\')(\s*.*?;*\s*)\2'
#     queries = re.findall(regex, lines, re.DOTALL)
#     for indent, query_quote_start, query in queries:
#         indent = indent.strip('\n')
#         indent += ' ' * 4
#         old_query = query
#         query = ' '.join(query.split())
# 
#         first = query.split()
#         if not first:
#             continue
# 
#         first = first[0].lower()
#         if not first in ['select', 'insert']:
#             continue
# 
#         yield old_query, query, indent
# 
# def handle_py_file(filename, debug=False):
#     with open(filename) as f:
#         lines = f.read()
# 
#     for old_query, query, indent in get_statements(lines):
#         if debug:
#             print_non_data('Found query: %s' % query)
# 
#         try:
#             fmt = format_sql(query, debug)
#         except InvalidSQL as e:
#             print_non_data(e)
#             continue
# 
#         fs = []
#         for line in fmt:
#             s = '%s%s' % (indent, line)
#             fs.append(s.rstrip())
# 
#         lines = lines.replace(old_query, '\n%s ' % '\n'.join(fs))
#     return lines
# 
# def handle_sql_file(filename, debug=False):
#     with open(filename) as f:
#         lines = f.read()
# 
#     try:
#         sql = format_sql(lines, debug)
#     except InvalidSQL as e:
#         print_non_data(e)
#         return
# 
#     lines = '\n'.join(sql)
#     return lines
# 
# def main(args=sys.argv[1:]):
#     args = _get_args(args)
#     filenames = _get_filenames(args.paths, args.recursive)
# 
#     if args.types:
#         filenames = filter(lambda fn: fn.endswith(tuple(args.types)), filenames)
# 
#     for filename in filenames:
#         print_non_data(filename)
# 
#         if filename.lower().endswith('.py'):
#             lines = handle_py_file(filename, args.debug)
#         else:
#             lines = handle_sql_file(filename, args.debug)
# 
#         _write_back(filename, lines, args.dry_run)


import pytest
from format_sql.main import (_get_args, get_statements, handle_py_file,
                             handle_sql_file, main)
from mock import call, patch
    from itertools import zip_longest
    from itertools import izip_longest as zip_longest
def test_sql_file_formatting(test_data, filename, expected_sql):
    test_filename = test_data.get_path(filename)
    result = handle_sql_file(test_filename)
    assert result == expected_sql


@pytest.mark.parametrize(('filename', 'expected_filename'), [
    ('test_02/test_00.py', 'test_02/test_00_expected.py'),
    ('test_02/test_01.py', 'test_02/test_01_expected.py'),
    ('test_02/test_02.py', 'test_02/test_02_expected.py'),
    ('test_02/test_03.py', 'test_02/test_03_expected.py'),
    ('test_02/test_04.py', 'test_02/test_04_expected.py'),
])
def test_py_file_formatting(test_data, filename, expected_filename):
    test_filename = test_data.get_path(filename)
    expected_filename = test_data.get_path(expected_filename)

    result = handle_py_file(test_filename)

    with open(expected_filename) as f:
        assert result == f.read()


@pytest.mark.parametrize(('filename', 'expected_filename'), [
    ('test_03/before.sql', 'test_03/after.sql'),
])
def test_multiple_statements_per_sql_file(test_data, filename, expected_filename):
    test_filename = test_data.get_path(filename)
    expected_filename = test_data.get_path(expected_filename)


# Path: format_sql/main.py
# def _get_args(call_args):
#     parser = ArgumentParser('format-sql')
# 
#     parser.add_argument('--types', dest='types', type=str,
#                         action='append',
#                         help='Only process these given file types.')
#     parser.add_argument('paths', type=str, nargs='+')
#     parser.add_argument('-r', '--recursive', dest='recursive',
#                         action='store_true', default=False,
#                         help='Process files found in subdirectories.')
#     parser.add_argument('--no-semicolon', action='store_true', default=False,
#                         help='Try to detect SQL queries with no trailing semicolon.')
#     parser.add_argument('--version', action='version',
#                         version='format-sql 0.12.0')
#     parser.add_argument('--debug', dest='debug', action='store_true', default=False,
#                         help='Print available debug information.')
#     parser.add_argument('--dry-run', dest='dry_run', action='store_true', default=False,
#                         help='Print the altered output and do not change the file.')
# 
#     args, _unused_unknown_args = parser.parse_known_args(call_args)
#     if not args.types:
#         args.types = []
# 
#     return args
# 
# def get_statements(lines):
#     regex = r'(\s*)[_\w\d\.]*\s*=*\s*[_\w\d\.]*\s*\(*("""|\'\'\')(\s*.*?;*\s*)\2'
#     queries = re.findall(regex, lines, re.DOTALL)
#     for indent, query_quote_start, query in queries:
#         indent = indent.strip('\n')
#         indent += ' ' * 4
#         old_query = query
#         query = ' '.join(query.split())
# 
#         first = query.split()
#         if not first:
#             continue
# 
#         first = first[0].lower()
#         if not first in ['select', 'insert']:
#             continue
# 
#         yield old_query, query, indent
# 
# def handle_py_file(filename, debug=False):
#     with open(filename) as f:
#         lines = f.read()
# 
#     for old_query, query, indent in get_statements(lines):
#         if debug:
#             print_non_data('Found query: %s' % query)
# 
#         try:
#             fmt = format_sql(query, debug)
#         except InvalidSQL as e:
#             print_non_data(e)
#             continue
# 
#         fs = []
#         for line in fmt:
#             s = '%s%s' % (indent, line)
#             fs.append(s.rstrip())
# 
#         lines = lines.replace(old_query, '\n%s ' % '\n'.join(fs))
#     return lines
# 
# def handle_sql_file(filename, debug=False):
#     with open(filename) as f:
#         lines = f.read()
# 
#     try:
#         sql = format_sql(lines, debug)
#     except InvalidSQL as e:
#         print_non_data(e)
#         return
# 
#     lines = '\n'.join(sql)
#     return lines
# 
# def main(args=sys.argv[1:]):
#     args = _get_args(args)
#     filenames = _get_filenames(args.paths, args.recursive)
# 
#     if args.types:
#         filenames = filter(lambda fn: fn.endswith(tuple(args.types)), filenames)
# 
#     for filename in filenames:
#         print_non_data(filename)
# 
#         if filename.lower().endswith('.py'):
#             lines = handle_py_file(filename, args.debug)
#         else:
#             lines = handle_sql_file(filename, args.debug)
# 
#         _write_back(filename, lines, args.dry_run)


# Path: tests/test_main.py
import pytest
from format_sql.main import (_get_args, get_statements, handle_py_file,
                             handle_sql_file, main)
from mock import call, patch
    from itertools import zip_longest
    from itertools import izip_longest as zip_longest

def test_sql_file_formatting(test_data, filename, expected_sql):
    test_filename = test_data.get_path(filename)
    result = handle_sql_file(test_filename)
    assert result == expected_sql


@pytest.mark.parametrize(('filename', 'expected_filename'), [
    ('test_02/test_00.py', 'test_02/test_00_expected.py'),
    ('test_02/test_01.py', 'test_02/test_01_expected.py'),
    ('test_02/test_02.py', 'test_02/test_02_expected.py'),
    ('test_02/test_03.py', 'test_02/test_03_expected.py'),
    ('test_02/test_04.py', 'test_02/test_04_expected.py'),
])
def test_py_file_formatting(test_data, filename, expected_filename):
    test_filename = test_data.get_path(filename)
    expected_filename = test_data.get_path(expected_filename)

    result = handle_py_file(test_filename)

    with open(expected_filename) as f:
        assert result == f.read()


@pytest.mark.parametrize(('filename', 'expected_filename'), [
    ('test_03/before.sql', 'test_03/after.sql'),
])
def test_multiple_statements_per_sql_file(test_data, filename, expected_filename):
    test_filename = test_data.get_path(filename)
    expected_filename = test_data.get_path(expected_filename)


    result = handle_sql_file(test_filename)