summaryrefslogtreecommitdiff
path: root/test/dialect/test_sqlite.py
diff options
context:
space:
mode:
authorFederico Caselli <cfederico87@gmail.com>2020-07-16 21:32:52 +0200
committerMike Bayer <mike_mp@zzzcomputing.com>2020-08-27 17:30:18 -0400
commitb1b97ed1fcac777c4f42fdf84e05f8d59f63b679 (patch)
tree93b67e4ae3eff79d3e49bca71cddac40cf40b9bd /test/dialect/test_sqlite.py
parentfe772672b4fc00df0b66aca92e2092779a844a2d (diff)
downloadsqlalchemy-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/test_sqlite.py')
-rw-r--r--test/dialect/test_sqlite.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py
index b4813926f..cb418e99d 100644
--- a/test/dialect/test_sqlite.py
+++ b/test/dialect/test_sqlite.py
@@ -20,6 +20,7 @@ from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy import Index
from sqlalchemy import inspect
+from sqlalchemy import literal
from sqlalchemy import MetaData
from sqlalchemy import pool
from sqlalchemy import PrimaryKeyConstraint
@@ -27,6 +28,7 @@ from sqlalchemy import schema
from sqlalchemy import select
from sqlalchemy import sql
from sqlalchemy import Table
+from sqlalchemy import table
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy import tuple_
@@ -2608,3 +2610,76 @@ class TypeReflectionTest(fixtures.TestBase):
def test_round_trip_direct_type_affinity(self):
self._test_round_trip(self._type_affinity_fixture())
+
+
+class RegexpTest(fixtures.TestBase, testing.AssertsCompiledSQL):
+ __dialect__ = "sqlite"
+
+ 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 ?",
+ 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",
+ checkparams={},
+ )
+
+ def test_regexp_match_str(self):
+ self.assert_compile(
+ literal("string").regexp_match(self.table.c.name),
+ "? REGEXP mytable.name",
+ checkpositional=("string",),
+ )
+
+ def test_regexp_match_flags(self):
+ self.assert_compile(
+ self.table.c.myid.regexp_match("pattern", flags="ig"),
+ "mytable.myid REGEXP ?",
+ checkpositional=("pattern",),
+ )
+
+ def test_not_regexp_match(self):
+ self.assert_compile(
+ ~self.table.c.myid.regexp_match("pattern"),
+ "mytable.myid NOT REGEXP ?",
+ checkpositional=("pattern",),
+ )
+
+ def test_not_regexp_match_flags(self):
+ self.assert_compile(
+ ~self.table.c.myid.regexp_match("pattern", flags="ig"),
+ "mytable.myid NOT REGEXP ?",
+ 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",
+ checkparams={},
+ )
+
+ def test_not_regexp_match_str(self):
+ self.assert_compile(
+ ~literal("string").regexp_match(self.table.c.name),
+ "? NOT REGEXP mytable.name",
+ checkpositional=("string",),
+ )
+
+ def test_regexp_replace(self):
+ assert_raises_message(
+ exc.CompileError,
+ "sqlite dialect does not support regular expression replacements",
+ self.table.c.myid.regexp_replace("pattern", "rep").compile,
+ dialect=sqlite.dialect(),
+ )