<s> import pytest <EOL> from tests . utils import TestCaseBase <EOL> import sqlparse <EOL> from sqlparse . exceptions import SQLParseError <EOL> class TestFormat ( TestCaseBase ) : <EOL> def test_keywordcase ( self ) : <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , keyword_case = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> res = sqlparse . format ( sql , keyword_case = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> res = sqlparse . format ( sql . upper ( ) , keyword_case = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , sql , <EOL> keyword_case = '<STR_LIT:foo>' ) <EOL> def test_identifiercase ( self ) : <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , identifier_case = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> res = sqlparse . format ( sql , identifier_case = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> res = sqlparse . format ( sql . upper ( ) , identifier_case = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , sql , <EOL> identifier_case = '<STR_LIT:foo>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , identifier_case = "<STR_LIT>" ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> def test_strip_comments_single ( self ) : <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , sql , <EOL> strip_comments = None ) <EOL> def test_strip_comments_multi ( self ) : <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> res = sqlparse . format ( sql , strip_comments = True ) <EOL> self . ndiffAssertEqual ( res , '<STR_LIT>' ) <EOL> def test_strip_ws ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , strip_whitespace = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT>' ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT>' ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , s , <EOL> strip_whitespace = None ) <EOL> def test_preserve_ws ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , strip_whitespace = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT>' ) <EOL> def test_notransform_of_quoted_crlf ( self ) : <EOL> s1 = "<STR_LIT>" <EOL> s2 = "<STR_LIT>" <EOL> s3 = "<STR_LIT>" <EOL> s4 = "<STR_LIT>" <EOL> f = lambda x : sqlparse . format ( x ) <EOL> self . ndiffAssertEqual ( f ( s1 ) , "<STR_LIT>" ) <EOL> self . ndiffAssertEqual ( <EOL> f ( s2 ) , "<STR_LIT>" ) <EOL> self . ndiffAssertEqual ( <EOL> f ( s3 ) , "<STR_LIT>" ) <EOL> self . ndiffAssertEqual ( <EOL> f ( s4 ) , "<STR_LIT>" ) <EOL> def test_outputformat ( self ) : <EOL> sql = '<STR_LIT>' <EOL> self . assertRaises ( SQLParseError , sqlparse . format , sql , <EOL> output_format = '<STR_LIT:foo>' ) <EOL> class TestFormatReindent ( TestCaseBase ) : <EOL> def test_option ( self ) : <EOL> self . assertRaises ( SQLParseError , sqlparse . format , '<STR_LIT:foo>' , <EOL> reindent = <NUM_LIT:2> ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , '<STR_LIT:foo>' , <EOL> indent_tabs = <NUM_LIT:2> ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , '<STR_LIT:foo>' , <EOL> reindent = True , indent_width = '<STR_LIT:foo>' ) <EOL> self . assertRaises ( SQLParseError , sqlparse . format , '<STR_LIT:foo>' , <EOL> reindent = True , indent_width = - <NUM_LIT:12> ) <EOL> def test_stmts ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT>' ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT>' ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT>' ) <EOL> def test_keywords ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_keywords_between ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_parenthesis ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , <EOL> '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> ) <EOL> def test_where ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def test_join ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) ) <EOL> def test_identifier_list ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_identifier_list_with_functions ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( <EOL> [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) ) <EOL> def test_case ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:end>' ] ) ) <EOL> def test_case2 ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:end>' ] ) ) <EOL> def test_nested_identifier_list ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_duplicate_linebreaks ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> s = '<STR_LIT>' <EOL> r = sqlparse . format ( s , reindent = True , strip_comments = True ) <EOL> self . ndiffAssertEqual ( r , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_keywordfunctions ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_identifier_and_functions ( self ) : <EOL> f = lambda sql : sqlparse . format ( sql , reindent = True ) <EOL> s = '<STR_LIT>' <EOL> self . ndiffAssertEqual ( f ( s ) , '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> class TestOutputFormat ( TestCaseBase ) : <EOL> def test_python ( self ) : <EOL> sql = '<STR_LIT>' <EOL> f = lambda sql : sqlparse . format ( sql , output_format = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( f ( sql ) , "<STR_LIT>" ) <EOL> f = lambda sql : sqlparse . format ( sql , output_format = '<STR_LIT>' , <EOL> reindent = True ) <EOL> self . ndiffAssertEqual ( f ( sql ) , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def test_php ( self ) : <EOL> sql = '<STR_LIT>' <EOL> f = lambda sql : sqlparse . format ( sql , output_format = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( f ( sql ) , '<STR_LIT>' ) <EOL> f = lambda sql : sqlparse . format ( sql , output_format = '<STR_LIT>' , <EOL> reindent = True ) <EOL> self . ndiffAssertEqual ( f ( sql ) , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def test_sql ( self ) : <EOL> sql = '<STR_LIT>' <EOL> f = lambda sql : sqlparse . format ( sql , output_format = '<STR_LIT>' ) <EOL> self . ndiffAssertEqual ( f ( sql ) , '<STR_LIT>' ) <EOL> def test_format_column_ordering ( ) : <EOL> sql = '<STR_LIT>' <EOL> formatted = sqlparse . format ( sql , reindent = True ) <EOL> expected = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert formatted == expected <EOL> def test_truncate_strings ( ) : <EOL> sql = '<STR_LIT>' + '<STR_LIT:x>' * <NUM_LIT:1000> + '<STR_LIT>' <EOL> formatted = sqlparse . format ( sql , truncate_strings = <NUM_LIT:10> ) <EOL> assert formatted == '<STR_LIT>' <EOL> formatted = sqlparse . format ( sql , truncate_strings = <NUM_LIT:3> , truncate_char = '<STR_LIT>' ) <EOL> assert formatted == '<STR_LIT>' <EOL> def test_truncate_strings_invalid_option ( ) : <EOL> pytest . raises ( SQLParseError , sqlparse . format , <EOL> '<STR_LIT:foo>' , truncate_strings = '<STR_LIT:bar>' ) <EOL> pytest . raises ( SQLParseError , sqlparse . format , <EOL> '<STR_LIT:foo>' , truncate_strings = - <NUM_LIT:1> ) <EOL> pytest . raises ( SQLParseError , sqlparse . format , <EOL> '<STR_LIT:foo>' , truncate_strings = <NUM_LIT:0> ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_truncate_strings_doesnt_truncate_identifiers ( sql ) : <EOL> formatted = sqlparse . format ( sql , truncate_strings = <NUM_LIT:2> ) <EOL> assert formatted == sql <EOL> def test_having_produces_newline ( ) : <EOL> sql = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> formatted = sqlparse . format ( sql , reindent = True ) <EOL> expected = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> assert formatted == '<STR_LIT:\n>' . join ( expected ) </s>
