diff options
author | Federico Caselli <cfederico87@gmail.com> | 2020-07-16 21:32:52 +0200 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-08-27 17:30:18 -0400 |
commit | b1b97ed1fcac777c4f42fdf84e05f8d59f63b679 (patch) | |
tree | 93b67e4ae3eff79d3e49bca71cddac40cf40b9bd /test/dialect/mysql/test_compiler.py | |
parent | fe772672b4fc00df0b66aca92e2092779a844a2d (diff) | |
download | sqlalchemy-b1b97ed1fcac777c4f42fdf84e05f8d59f63b679.tar.gz |
Add support for regular expression on supported backend.
Two operations have been defined:
* :meth:`~.ColumnOperators.regexp_match` implementing a regular
expression match like function.
* :meth:`~.ColumnOperators.regexp_replace` implementing a regular
expression string replace function.
Fixes: #1390
Change-Id: I44556846e4668ccf329023613bd26861d5c674e6
Diffstat (limited to 'test/dialect/mysql/test_compiler.py')
-rw-r--r-- | test/dialect/mysql/test_compiler.py | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index aca1db33c..cca6a27de 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -960,3 +960,134 @@ class InsertOnDuplicateTest(fixtures.TestBase, AssertsCompiledSQL): "baz_1": "some literal", }, ) + + +class RegexpCommon(testing.AssertsCompiledSQL): + def setUp(self): + self.table = table( + "mytable", column("myid", Integer), column("name", String) + ) + + def test_regexp_match(self): + self.assert_compile( + self.table.c.myid.regexp_match("pattern"), + "mytable.myid REGEXP %s", + checkpositional=("pattern",), + ) + + def test_regexp_match_column(self): + self.assert_compile( + self.table.c.myid.regexp_match(self.table.c.name), + "mytable.myid REGEXP mytable.name", + checkpositional=(), + ) + + def test_regexp_match_str(self): + self.assert_compile( + literal("string").regexp_match(self.table.c.name), + "%s REGEXP mytable.name", + checkpositional=("string",), + ) + + def test_not_regexp_match(self): + self.assert_compile( + ~self.table.c.myid.regexp_match("pattern"), + "mytable.myid NOT REGEXP %s", + checkpositional=("pattern",), + ) + + def test_not_regexp_match_column(self): + self.assert_compile( + ~self.table.c.myid.regexp_match(self.table.c.name), + "mytable.myid NOT REGEXP mytable.name", + checkpositional=(), + ) + + def test_not_regexp_match_str(self): + self.assert_compile( + ~literal("string").regexp_match(self.table.c.name), + "%s NOT REGEXP mytable.name", + checkpositional=("string",), + ) + + def test_regexp_replace(self): + self.assert_compile( + self.table.c.myid.regexp_replace("pattern", "replacement"), + "REGEXP_REPLACE(mytable.myid, %s, %s)", + checkpositional=("pattern", "replacement"), + ) + + def test_regexp_replace_column(self): + self.assert_compile( + self.table.c.myid.regexp_replace("pattern", self.table.c.name), + "REGEXP_REPLACE(mytable.myid, %s, mytable.name)", + checkpositional=("pattern",), + ) + + def test_regexp_replace_column2(self): + self.assert_compile( + self.table.c.myid.regexp_replace(self.table.c.name, "replacement"), + "REGEXP_REPLACE(mytable.myid, mytable.name, %s)", + checkpositional=("replacement",), + ) + + def test_regexp_replace_string(self): + self.assert_compile( + literal("string").regexp_replace("pattern", self.table.c.name), + "REGEXP_REPLACE(%s, %s, mytable.name)", + checkpositional=("string", "pattern"), + ) + + +class RegexpTestMySql(fixtures.TestBase, RegexpCommon): + __dialect__ = "mysql" + + def test_regexp_match_flags(self): + self.assert_compile( + self.table.c.myid.regexp_match("pattern", flags="ig"), + "REGEXP_LIKE(mytable.myid, %s, %s)", + checkpositional=("pattern", "ig"), + ) + + def test_not_regexp_match_flags(self): + self.assert_compile( + ~self.table.c.myid.regexp_match("pattern", flags="ig"), + "NOT REGEXP_LIKE(mytable.myid, %s, %s)", + checkpositional=("pattern", "ig"), + ) + + def test_regexp_replace_flags(self): + self.assert_compile( + self.table.c.myid.regexp_replace( + "pattern", "replacement", flags="ig" + ), + "REGEXP_REPLACE(mytable.myid, %s, %s, %s)", + checkpositional=("pattern", "replacement", "ig"), + ) + + +class RegexpTestMariaDb(fixtures.TestBase, RegexpCommon): + __dialect__ = "mariadb" + + def test_regexp_match_flags(self): + self.assert_compile( + self.table.c.myid.regexp_match("pattern", flags="ig"), + "mytable.myid REGEXP CONCAT('(?', %s, ')', %s)", + checkpositional=("ig", "pattern"), + ) + + def test_not_regexp_match_flags(self): + self.assert_compile( + ~self.table.c.myid.regexp_match("pattern", flags="ig"), + "mytable.myid NOT REGEXP CONCAT('(?', %s, ')', %s)", + checkpositional=("ig", "pattern"), + ) + + def test_regexp_replace_flags(self): + self.assert_compile( + self.table.c.myid.regexp_replace( + "pattern", "replacement", flags="ig" + ), + "REGEXP_REPLACE(mytable.myid, CONCAT('(?', %s, ')', %s), %s)", + checkpositional=("ig", "pattern", "replacement"), + ) |