From: Anton Kovalevich Date: Thu, 1 Apr 2021 11:39:43 +0000 (+0300) Subject: Add tests, make CompileError testable X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=16941e780a530c76a829ac74f9364a9b82f273f2;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add tests, make CompileError testable --- diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 8fbe3edde9..8410e9aeae 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -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) diff --git a/lib/sqlalchemy/dialects/mysql/expression.py b/lib/sqlalchemy/dialects/mysql/expression.py index 1568258bf8..ca442e115f 100644 --- a/lib/sqlalchemy/dialects/mysql/expression.py +++ b/lib/sqlalchemy/dialects/mysql/expression.py @@ -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 diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index 84646d3802..dae2862993 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -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)