diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-10-24 18:08:05 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-10-24 23:11:13 -0400 |
commit | 654ca5463d2045d8dc74d7d790081f58554d796d (patch) | |
tree | f3202afc7f7345e9fbee321c80660a112d262cfb /test/sql/test_operators.py | |
parent | f34b180ca9059a74c3bf1db1b79e187c3f4b81c9 (diff) | |
download | sqlalchemy-654ca5463d2045d8dc74d7d790081f58554d796d.tar.gz |
Rework autoescape to be a simple boolean; escape the escape character
Reworked the new "autoescape" feature introduced in
:ref:`change_2694` in 1.2.0b2 to be fully automatic; the escape
character now defaults to a forwards slash ``"/"`` and
is applied to percent, underscore, as well as the escape
character itself, for fully automatic escaping. The
character can also be changed using the "escape" parameter.
Change-Id: I74894a2576983c0f6eb89480c9e5727f49fa9c25
Fixes: #2694
Diffstat (limited to 'test/sql/test_operators.py')
-rw-r--r-- | test/sql/test_operators.py | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index 2a1168844..f5446a856 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -1,6 +1,7 @@ from sqlalchemy.testing import fixtures, eq_, is_, is_not_ from sqlalchemy import testing from sqlalchemy.testing import assert_raises_message +from sqlalchemy.testing import expect_warnings from sqlalchemy.sql import column, desc, asc, literal, collate, null, \ true, false, any_, all_ from sqlalchemy.sql import sqltypes @@ -2299,9 +2300,9 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_contains_autoescape(self): self.assert_compile( - column('x').contains('a%b_c', autoescape='\\'), - "x LIKE '%' || :x_1 || '%' ESCAPE '\\'", - checkparams={'x_1': 'a\\%b\\_c'} + column('x').contains('a%b_c/d', autoescape=True), + "x LIKE '%' || :x_1 || '%' ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} ) def test_contains_literal(self): @@ -2334,9 +2335,9 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_not_contains_autoescape(self): self.assert_compile( - ~column('x').contains('a%b_c', autoescape='\\'), - "x NOT LIKE '%' || :x_1 || '%' ESCAPE '\\'", - checkparams={'x_1': 'a\\%b\\_c'} + ~column('x').contains('a%b_c/d', autoescape=True), + "x NOT LIKE '%' || :x_1 || '%' ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} ) def test_contains_concat(self): @@ -2443,9 +2444,16 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_startswith_autoescape(self): self.assert_compile( - column('x').startswith('a%b_c', autoescape='\\'), - "x LIKE :x_1 || '%' ESCAPE '\\'", - checkparams={'x_1': 'a\\%b\\_c'} + column('x').startswith('a%b_c/d', autoescape=True), + "x LIKE :x_1 || '%' ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} + ) + + def test_startswith_autoescape_custom_escape(self): + self.assert_compile( + column('x').startswith('a%b_c/d^e', autoescape=True, escape='^'), + "x LIKE :x_1 || '%' ESCAPE '^'", + checkparams={'x_1': 'a^%b^_c/d^^e'} ) def test_not_startswith(self): @@ -2464,9 +2472,9 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_not_startswith_autoescape(self): self.assert_compile( - ~column('x').startswith('a%b_c', autoescape='\\'), - "x NOT LIKE :x_1 || '%' ESCAPE '\\'", - checkparams={'x_1': 'a\\%b\\_c'} + ~column('x').startswith('a%b_c/d', autoescape=True), + "x NOT LIKE :x_1 || '%' ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} ) def test_startswith_literal(self): @@ -2547,9 +2555,32 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_endswith_autoescape(self): self.assert_compile( - column('x').endswith('a%b_c', autoescape='\\'), - "x LIKE '%' || :x_1 ESCAPE '\\'", - checkparams={'x_1': 'a\\%b\\_c'} + column('x').endswith('a%b_c/d', autoescape=True), + "x LIKE '%' || :x_1 ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} + ) + + def test_endswith_autoescape_custom_escape(self): + self.assert_compile( + column('x').endswith('a%b_c/d^e', autoescape=True, escape="^"), + "x LIKE '%' || :x_1 ESCAPE '^'", + checkparams={'x_1': 'a^%b^_c/d^^e'} + ) + + def test_endswith_autoescape_warning(self): + with expect_warnings("The autoescape parameter is now a simple"): + self.assert_compile( + column('x').endswith('a%b_c/d', autoescape='P'), + "x LIKE '%' || :x_1 ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} + ) + + def test_endswith_autoescape_nosqlexpr(self): + assert_raises_message( + TypeError, + "String value expected when autoescape=True", + column('x').endswith, + literal_column("'a%b_c/d'"), autoescape=True ) def test_not_endswith(self): @@ -2568,9 +2599,9 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_not_endswith_autoescape(self): self.assert_compile( - ~column('x').endswith('a%b_c', autoescape='\\'), - "x NOT LIKE '%' || :x_1 ESCAPE '\\'", - checkparams={'x_1': 'a\\%b\\_c'} + ~column('x').endswith('a%b_c/d', autoescape=True), + "x NOT LIKE '%' || :x_1 ESCAPE '/'", + checkparams={'x_1': 'a/%b/_c//d'} ) def test_endswith_literal(self): |