summaryrefslogtreecommitdiff
path: root/test/sql/test_cte.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-02-20 15:00:09 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2019-02-21 11:18:19 -0500
commit4ca3092c0a89855cd740bafb4e0fb4c99051f89e (patch)
treee8a42e8391cf1b30f5b20ba10da1f3fa9f97e17c /test/sql/test_cte.py
parent7a4c40ff6b9d278529735c792c3ddfda60bd4a85 (diff)
downloadsqlalchemy-4ca3092c0a89855cd740bafb4e0fb4c99051f89e.tar.gz
Prevent __init__ from being called for Alias, subclasses
The :class:`.Alias` class and related subclasses :class:`.CTE`, :class:`.Lateral` and :class:`.TableSample` have been reworked so that it is not possible for a user to construct the objects directly. These constructs require that the standalone construction function or selectable-bound method be used to instantiate new objects. Fixes: #4509 Change-Id: I74ae4786cb3ae625dab33b00bfd6bdc4e1219139
Diffstat (limited to 'test/sql/test_cte.py')
-rw-r--r--test/sql/test_cte.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/sql/test_cte.py b/test/sql/test_cte.py
index ed2787b0f..7008bc1cc 100644
--- a/test/sql/test_cte.py
+++ b/test/sql/test_cte.py
@@ -4,12 +4,14 @@ from sqlalchemy.exc import CompileError
from sqlalchemy.sql import and_
from sqlalchemy.sql import bindparam
from sqlalchemy.sql import column
+from sqlalchemy.sql import cte
from sqlalchemy.sql import exists
from sqlalchemy.sql import func
from sqlalchemy.sql import literal
from sqlalchemy.sql import select
from sqlalchemy.sql import table
from sqlalchemy.sql.elements import quoted_name
+from sqlalchemy.sql.selectable import CTE
from sqlalchemy.sql.visitors import cloned_traverse
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import AssertsCompiledSQL
@@ -1126,3 +1128,28 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL):
"UPDATE products SET id=:id, price=:price FROM pd "
"WHERE products.price = pd.price",
)
+
+ def test_standalone_function(self):
+ a = table("a", column("x"))
+ a_stmt = select([a])
+
+ stmt = select([cte(a_stmt)])
+
+ self.assert_compile(
+ stmt,
+ "WITH anon_1 AS (SELECT a.x AS x FROM a) "
+ "SELECT anon_1.x FROM anon_1",
+ )
+
+ def test_no_alias_construct(self):
+ a = table("a", column("x"))
+ a_stmt = select([a])
+
+ assert_raises_message(
+ NotImplementedError,
+ "The CTE class is not intended to be constructed directly. "
+ r"Please use the cte\(\) standalone function",
+ CTE,
+ a_stmt,
+ "foo",
+ )