diff options
| author | Vik <vmuriart@users.noreply.github.com> | 2016-06-11 05:29:39 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-06-11 05:29:39 -0700 |
| commit | 751933d3abdce2234bd869ee65a1ebc7ccbf6b53 (patch) | |
| tree | 77fd5be087ae64c4416c0bd775f6c1843b45a007 /tests | |
| parent | 00304afc15a554f2ac8decca1d916ba66c143b45 (diff) | |
| parent | 1fd3da42bd55bfc1e916e3c3f301f0364b0ded21 (diff) | |
| download | sqlparse-751933d3abdce2234bd869ee65a1ebc7ccbf6b53.tar.gz | |
Merge pull request #254 from vmuriart/tests_str-format
Add various tests and change to new style str-format
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_format.py | 30 | ||||
| -rw-r--r-- | tests/test_grouping.py | 2 | ||||
| -rw-r--r-- | tests/test_parse.py | 64 | ||||
| -rw-r--r-- | tests/test_regressions.py | 27 | ||||
| -rw-r--r-- | tests/test_split.py | 5 | ||||
| -rw-r--r-- | tests/test_tokenize.py | 2 |
6 files changed, 107 insertions, 23 deletions
diff --git a/tests/test_format.py b/tests/test_format.py index 7b5af06..74fce71 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -524,6 +524,22 @@ class TestOutputFormat(TestCaseBase): self.ndiffAssertEqual(f(sql), ("sql = ('select * '\n" " 'from foo;')")) + def test_python_multiple_statements(self): + sql = 'select * from foo; select 1 from dual' + f = lambda sql: sqlparse.format(sql, output_format='python') + self.ndiffAssertEqual(f(sql), ("sql = 'select * from foo; '\n" + "sql2 = 'select 1 from dual'")) + + @pytest.mark.xfail(reason="Needs fixing") + def test_python_multiple_statements_with_formatting(self): + sql = 'select * from foo; select 1 from dual' + f = lambda sql: sqlparse.format(sql, output_format='python', + reindent=True) + self.ndiffAssertEqual(f(sql), ("sql = ('select * '\n" + " 'from foo;')\n" + "sql2 = ('select 1 '\n" + " 'from dual')")) + def test_php(self): sql = 'select * from foo;' f = lambda sql: sqlparse.format(sql, output_format='php') @@ -587,3 +603,17 @@ def test_having_produces_newline(): 'having sum(bar.value) > 100' ] assert formatted == '\n'.join(expected) + + +def test_format_right_margin_invalid_input(): + with pytest.raises(SQLParseError): + sqlparse.format('foo', right_margin=2) + + with pytest.raises(SQLParseError): + sqlparse.format('foo', right_margin="two") + + +@pytest.mark.xfail(reason="Needs fixing") +def test_format_right_margin(): + # TODO: Needs better test, only raises exception right now + sqlparse.format('foo', right_margin="79") diff --git a/tests/test_grouping.py b/tests/test_grouping.py index fdcd4a7..7ea1c75 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -373,7 +373,7 @@ def test_comparison_with_functions(): # issue230 @pytest.mark.parametrize('start', ['FOR', 'FOREACH']) def test_forloops(start): - p = sqlparse.parse('%s foo in bar LOOP foobar END LOOP' % start)[0] + p = sqlparse.parse('{0} foo in bar LOOP foobar END LOOP'.format(start))[0] assert (len(p.tokens)) == 1 assert isinstance(p.tokens[0], sql.For) diff --git a/tests/test_parse.py b/tests/test_parse.py index 2ea0f40..8654ec4 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -9,7 +9,7 @@ from tests.utils import TestCaseBase import sqlparse import sqlparse.sql from sqlparse import tokens as T -from sqlparse.compat import u +from sqlparse.compat import u, StringIO class SQLParseTest(TestCaseBase): @@ -178,9 +178,9 @@ def test_psql_quotation_marks(): # issue83 def test_double_precision_is_builtin(): sql = 'DOUBLE PRECISION' t = sqlparse.parse(sql)[0].tokens - assert (len(t) == 1 - and t[0].ttype == sqlparse.tokens.Name.Builtin - and t[0].value == 'DOUBLE PRECISION') + assert len(t) == 1 + assert t[0].ttype == sqlparse.tokens.Name.Builtin + assert t[0].value == 'DOUBLE PRECISION' @pytest.mark.parametrize('ph', ['?', ':1', ':foo', '%s', '%(foo)s']) @@ -218,10 +218,10 @@ def test_single_quotes_with_linebreaks(): # issue118 def test_sqlite_identifiers(): # Make sure we still parse sqlite style escapes p = sqlparse.parse('[col1],[col2]')[0].tokens - assert (len(p) == 1 - and isinstance(p[0], sqlparse.sql.IdentifierList) - and [id.get_name() for id in p[0].get_identifiers()] - == ['[col1]', '[col2]']) + id_names = [id.get_name() for id in p[0].get_identifiers()] + assert len(p) == 1 + assert isinstance(p[0], sqlparse.sql.IdentifierList) + assert id_names == ['[col1]', '[col2]'] p = sqlparse.parse('[col1]+[col2]')[0] types = [tok.ttype for tok in p.flatten()] @@ -233,9 +233,9 @@ def test_simple_1d_array_index(): assert len(p) == 1 assert p[0].get_name() == 'col' indices = list(p[0].get_array_indices()) - assert (len(indices) == 1 # 1-dimensional index - and len(indices[0]) == 1 # index is single token - and indices[0][0].value == '1') + assert len(indices) == 1 # 1-dimensional index + assert len(indices[0]) == 1 # index is single token + assert indices[0][0].value == '1' def test_2d_array_index(): @@ -303,3 +303,45 @@ def test_names_and_special_names(sql): p = sqlparse.parse(sql)[0] assert len(p.tokens) == 1 assert isinstance(p.tokens[0], sqlparse.sql.Identifier) + + +def test_get_token_at_offset(): + # 0123456789 + p = sqlparse.parse('select * from dual')[0] + assert p.get_token_at_offset(0) == p.tokens[0] + assert p.get_token_at_offset(1) == p.tokens[0] + assert p.get_token_at_offset(6) == p.tokens[1] + assert p.get_token_at_offset(7) == p.tokens[2] + assert p.get_token_at_offset(8) == p.tokens[3] + assert p.get_token_at_offset(9) == p.tokens[4] + assert p.get_token_at_offset(10) == p.tokens[4] + + +def test_pprint(): + p = sqlparse.parse('select * from dual')[0] + output = StringIO() + + p._pprint_tree(f=output) + pprint = u'\n'.join([ + " | 0 DML 'select'", + " | 1 Whitespace ' '", + " | 2 Wildcard '*'", + " | 3 Whitespace ' '", + " | 4 Keyword 'from'", + " | 5 Whitespace ' '", + " +-6 Identifier 'dual'", + " | 0 Name 'dual'", + "", + ]) + assert output.getvalue() == pprint + + +def test_wildcard_multiplication(): + p = sqlparse.parse('select * from dual')[0] + assert p.tokens[2].ttype == T.Wildcard + + p = sqlparse.parse('select a0.* from dual a0')[0] + assert p.tokens[2][2].ttype == T.Wildcard + + p = sqlparse.parse('select 1 * 2 from dual')[0] + assert p.tokens[2][2].ttype == T.Operator diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 13ca04b..9f3c9a9 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -171,10 +171,11 @@ def test_comment_encoding_when_reindent(): def test_parse_sql_with_binary(): # See https://github.com/andialbrecht/sqlparse/pull/88 + # digest = '|ËêplL4¡høN{' digest = '\x82|\xcb\x0e\xea\x8aplL4\xa1h\x91\xf8N{' - sql = 'select * from foo where bar = \'%s\'' % digest + sql = "select * from foo where bar = '{0}'".format(digest) formatted = sqlparse.format(sql, reindent=True) - tformatted = 'select *\nfrom foo\nwhere bar = \'%s\'' % digest + tformatted = "select *\nfrom foo\nwhere bar = '{0}'".format(digest) if sys.version_info < (3,): tformatted = tformatted.decode('unicode-escape') assert formatted == tformatted @@ -193,10 +194,8 @@ def test_dont_alias_keywords(): def test_format_accepts_encoding(): # issue20 sql = load_file('test_cp1251.sql', 'cp1251') formatted = sqlparse.format(sql, reindent=True, encoding='cp1251') - if sys.version_info < (3,): - tformatted = u'insert into foo\nvalues (1); -- Песня про надежду\n' - else: - tformatted = 'insert into foo\nvalues (1); -- Песня про надежду\n' + tformatted = u'insert into foo\nvalues (1); -- Песня про надежду\n' + assert formatted == tformatted @@ -278,10 +277,7 @@ def test_issue186_get_type(): def test_issue212_py2unicode(): - if sys.version_info < (3,): - t1 = sql.Token(T.String, u"schöner ") - else: - t1 = sql.Token(T.String, "schöner ") + t1 = sql.Token(T.String, u"schöner ") t2 = sql.Token(T.String, u"bug") l = sql.TokenList([t1, t2]) assert str(l) == 'schöner bug' @@ -304,3 +300,14 @@ def test_issue227_gettype_cte(): INSERT INTO elsewhere SELECT * FROM foo JOIN bar; ''') assert with2_stmt[0].get_type() == 'INSERT' + + +def test_issue207_runaway_format(): + sql = 'select 1 from (select 1 as one, 2 as two, 3 from dual) t0' + p = sqlparse.format(sql, reindent=True) + assert p == '\n'.join(["select 1", + "from", + " (select 1 as one,", + " 2 as two,", + " 3", + " from dual) t0"]) diff --git a/tests/test_split.py b/tests/test_split.py index f6d5f50..7c2645d 100644 --- a/tests/test_split.py +++ b/tests/test_split.py @@ -133,6 +133,11 @@ class SQLSplitTest(TestCaseBase): stmts = list(sqlparse.parsestream(stream)) self.assertEqual(type(stmts[0].tokens[0].value), text_type) + def test_unicode_parsestream(self): + stream = StringIO(u"SELECT ö") + stmts = list(sqlparse.parsestream(stream)) + self.assertEqual(str(stmts[0]), "SELECT ö") + def test_split_simple(): stmts = sqlparse.split('select * from foo; select * from bar;') diff --git a/tests/test_tokenize.py b/tests/test_tokenize.py index 2e931ba..adfd1ea 100644 --- a/tests/test_tokenize.py +++ b/tests/test_tokenize.py @@ -151,7 +151,7 @@ class TestStream(unittest.TestCase): 'CROSS JOIN', 'STRAIGHT JOIN', 'INNER JOIN', 'LEFT INNER JOIN']) def test_parse_join(expr): - p = sqlparse.parse('%s foo' % expr)[0] + p = sqlparse.parse('{0} foo'.format(expr))[0] assert len(p.tokens) == 3 assert p.tokens[0].ttype is T.Keyword |
