]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add tests, make CompileError testable
authorAnton Kovalevich <kai3341@gmail.com>
Thu, 1 Apr 2021 11:39:43 +0000 (14:39 +0300)
committerAnton Kovalevich <kai3341@gmail.com>
Thu, 1 Apr 2021 11:39:43 +0000 (14:39 +0300)
lib/sqlalchemy/dialects/mysql/base.py
lib/sqlalchemy/dialects/mysql/expression.py
test/dialect/mysql/test_compiler.py

index 8fbe3edde96054cf570c103911933ba45a707e8a..8410e9aeae5d6684f78fda8a1cb76d7fc38dac83 100644 (file)
@@ -1616,14 +1616,17 @@ class MySQLCompiler(compiler.SQLCompiler):
         flag_combination = (boolean_mode, natural_language, query_expansion)
 
         if flag_combination not in self.match_valid_flag_combinations:
-            flags = {
-                'mysql_boolean_mode': boolean_mode,
-                'mysql_natural_language': natural_language,
-                'mysql_query_expansion': query_expansion,
-            }
+            flags = (
+                'mysql_boolean_mode=%s' % boolean_mode,
+                'mysql_natural_language=%s' % natural_language,
+                'mysql_query_expansion=%s' % query_expansion,
+            )
+
+            flags = ", ".join(flags)
+
             raise exc.CompileError(
-                    "Flag combination does not make sence: %s." % flags
-                )
+                "Flag combination does not make sence: %s" % flags
+            )
 
         match_clause = self.process(binary.left, **kw)
         against_clause = self.process(binary.right, **kw)
index 1568258bf890c1589d843efd7f3d6d5a3e6fe9bb..ca442e115fa1ce255c6a42f009b8b530413682b7 100644 (file)
@@ -56,9 +56,7 @@ class match(ColumnElement):
     used, but allows to pass multiple columns
 
     All positional arguments passed to :func:`.match`, typically should be a
-     :class:`_expression.ColumnElement` instances or alternatively a Python
-     scalar expression to be coerced into a column expression, serving as
-     the ``MATCH`` side of expression.
+     :class:`_expression.ColumnElement` instances
 
     :param against: typically scalar expression to be coerced into a ``str``,
      but may be a :class:`_expression.ColumnElement` instance
index 84646d3802a96e58af154c154d2fb4e8da10d37f..dae2862993136d66eb9a0d2ad7b9e177bab862a3 100644 (file)
@@ -1,5 +1,7 @@
 # coding: utf-8
 
+from finctools import partial
+
 from sqlalchemy import BLOB
 from sqlalchemy import BOOLEAN
 from sqlalchemy import Boolean
@@ -430,6 +432,82 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             literal_binds=True,
         )
 
+    def test_match_compile_kw_mysql(self):
+        matchtable = table("matchtable", column("title", String))
+        self.assert_compile(
+            matchtable.c.title.match("somstr", mysql_boolean_mode=False),
+            "MATCH (matchtable.title) AGAINST (%s)",
+        )
+        self.assert_compile(
+            matchtable.c.title.match(
+                "somstr",
+                mysql_boolean_mode=False,
+                mysql_natural_language=True,
+            ),
+            "MATCH (matchtable.title) AGAINST (%s IN NATURAL LANGUAGE MODE)",
+        )
+        self.assert_compile(
+            matchtable.c.title.match(
+                "somstr",
+                mysql_boolean_mode=False,
+                mysql_query_expansion=True,
+            ),
+            "MATCH (matchtable.title) AGAINST (%s WITH QUERY EXPANSION)",
+        )
+        self.assert_compile(
+            matchtable.c.title.match(
+                "somstr",
+                mysql_boolean_mode=False,
+                mysql_natural_language=True,
+                mysql_query_expansion=True,
+            ),
+            "MATCH (matchtable.title) AGAINST "
+            "(%s IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION)",
+        )
+
+        assert_raises_message(
+            exc.CompileError,
+            "Flag combination does not make sence: "
+            "mysql_boolean_mode=True, "
+            "mysql_natural_language=True, "
+            "mysql_query_expansion=True",
+            partial(
+                matchtable.c.title.match,
+                "somstr",
+                mysql_natural_language=True,
+                mysql_query_expansion=True,
+            ),
+            dialect=mysql.dialect(),
+        )
+
+        assert_raises_message(
+            exc.CompileError,
+            "Flag combination does not make sence: "
+            "mysql_boolean_mode=True, "
+            "mysql_natural_language=False, "
+            "mysql_query_expansion=True",
+            partial(
+                matchtable.c.title.match,
+                "somstr",
+                mysql_query_expansion=True,
+            ),
+            dialect=mysql.dialect(),
+        )
+
+        assert_raises_message(
+            exc.CompileError,
+            "Flag combination does not make sence: "
+            "mysql_boolean_mode=True, "
+            "mysql_natural_language=True, "
+            "mysql_query_expansion=False",
+            partial(
+                matchtable.c.title.match,
+                "somstr",
+                mysql_natural_language=True,
+            ),
+            dialect=mysql.dialect(),
+        )
+
     def test_concat_compile_kw(self):
         expr = literal("x", type_=String) + literal("y", type_=String)
         self.assert_compile(expr, "concat('x', 'y')", literal_binds=True)