From 87cdda008673e01e2c32049f103e9cdebd2a5d77 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 1 Dec 2018 14:28:57 -0500 Subject: Move CRUDTest, InlineDefaultTest from test_compiler test_compiler is mostly related to SELECT statements as well as smaller SQL elements. While it still has some DDL related tests, move out all the remaining insert/update tests into the already present test_insert.py, test_update.py Fixes: #2630 Change-Id: I4167618543fd1235d12d1717c8c629d2374b325a --- test/sql/test_insert.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) (limited to 'test/sql/test_insert.py') diff --git a/test/sql/test_insert.py b/test/sql/test_insert.py index 6ea5b4f37..729c420c0 100644 --- a/test/sql/test_insert.py +++ b/test/sql/test_insert.py @@ -6,7 +6,7 @@ from sqlalchemy import Column, Integer, MetaData, String, Table,\ from sqlalchemy.dialects import mysql, postgresql from sqlalchemy.engine import default from sqlalchemy.testing import AssertsCompiledSQL,\ - assert_raises_message, fixtures, eq_, expect_warnings + assert_raises_message, fixtures, eq_, expect_warnings, assert_raises from sqlalchemy.sql import crud @@ -31,6 +31,91 @@ class _InsertTestBase(object): class InsertTest(_InsertTestBase, fixtures.TablesTest, AssertsCompiledSQL): __dialect__ = 'default' + def test_binds_that_match_columns(self): + """test bind params named after column names + replace the normal SET/VALUES generation.""" + + t = table('foo', column('x'), column('y')) + + i = t.insert().values(x=3 + bindparam('x')) + self.assert_compile(i, + "INSERT INTO foo (x) VALUES ((:param_1 + :x))") + self.assert_compile( + i, + "INSERT INTO foo (x, y) VALUES ((:param_1 + :x), :y)", + params={ + 'x': 1, + 'y': 2}) + + i = t.insert().values(x=bindparam('y')) + self.assert_compile(i, "INSERT INTO foo (x) VALUES (:y)") + + i = t.insert().values(x=bindparam('y'), y=5) + assert_raises(exc.CompileError, i.compile) + + i = t.insert().values(x=3 + bindparam('y'), y=5) + assert_raises(exc.CompileError, i.compile) + + i = t.insert().values(x=3 + bindparam('x2')) + self.assert_compile(i, + "INSERT INTO foo (x) VALUES ((:param_1 + :x2))") + self.assert_compile( + i, + "INSERT INTO foo (x) VALUES ((:param_1 + :x2))", + params={}) + self.assert_compile( + i, + "INSERT INTO foo (x, y) VALUES ((:param_1 + :x2), :y)", + params={ + 'x': 1, + 'y': 2}) + self.assert_compile( + i, + "INSERT INTO foo (x, y) VALUES ((:param_1 + :x2), :y)", + params={ + 'x2': 1, + 'y': 2}) + + def test_insert_literal_binds(self): + table1 = self.tables.mytable + stmt = table1.insert().values(myid=3, name='jack') + + self.assert_compile( + stmt, + "INSERT INTO mytable (myid, name) VALUES (3, 'jack')", + literal_binds=True) + + def test_insert_literal_binds_sequence_notimplemented(self): + table = Table('x', MetaData(), Column('y', Integer, Sequence('y_seq'))) + dialect = default.DefaultDialect() + dialect.supports_sequences = True + + stmt = table.insert().values(myid=3, name='jack') + + assert_raises( + NotImplementedError, + stmt.compile, + compile_kwargs=dict(literal_binds=True), dialect=dialect + ) + + def test_inline_defaults(self): + m = MetaData() + foo = Table('foo', m, + Column('id', Integer)) + + t = Table('test', m, + Column('col1', Integer, default=func.foo(1)), + Column('col2', Integer, default=select( + [func.coalesce(func.max(foo.c.id))])), + ) + + self.assert_compile( + t.insert( + inline=True, values={}), + "INSERT INTO test (col1, col2) VALUES (foo(:foo_1), " + "(SELECT coalesce(max(foo.id)) AS coalesce_1 FROM " + "foo))") + def test_generic_insert_bind_params_all_columns(self): table1 = self.tables.mytable -- cgit v1.2.1