diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-08-27 15:11:53 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-08-27 15:11:53 -0400 |
| commit | 326f2e4f60744d8073eaa4eda69d1dbb46bc9f50 (patch) | |
| tree | 1564027071fa60d1f656edf59e84d8361238b7e7 /test/sql | |
| parent | 8a307726b34d4adb374b0b618205dbcac31a15f5 (diff) | |
| download | sqlalchemy-326f2e4f60744d8073eaa4eda69d1dbb46bc9f50.tar.gz | |
- [feature] The "required" flag is set to
True by default, if not passed explicitly,
on bindparam() if the "value" or "callable"
parameters are not passed.
This will cause statement execution to check
for the parameter being present in the final
collection of bound parameters, rather than
implicitly assigning None. [ticket:2556]
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_compiler.py | 26 | ||||
| -rw-r--r-- | test/sql/test_functions.py | 2 | ||||
| -rw-r--r-- | test/sql/test_query.py | 63 |
3 files changed, 83 insertions, 8 deletions
diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 55b583071..40d29f222 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -1,5 +1,15 @@ #! coding:utf-8 +""" +compiler tests. + +These tests are among the very first that were written when SQLAlchemy +began in 2005. As a result the testing style here is very dense; +it's an ongoing job to break these into much smaller tests with correct pep8 +styling and coherent test organization. + +""" + from test.lib.testing import eq_, is_, assert_raises, assert_raises_message import datetime, re, operator, decimal from sqlalchemy import * @@ -1446,21 +1456,24 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): # test Text embedded within select_from(), using binds generate_series = text( "generate_series(:x, :y, :z) as s(a)", - bindparams=[bindparam('x'), bindparam('y'), bindparam('z')] + bindparams=[bindparam('x', None), + bindparam('y', None), bindparam('z', None)] ) - s =select([ + s = select([ (func.current_date() + literal_column("s.a")).label("dates") ]).select_from(generate_series) self.assert_compile( s, - "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)", + "SELECT CURRENT_DATE + s.a AS dates FROM " + "generate_series(:x, :y, :z) as s(a)", checkparams={'y': None, 'x': None, 'z': None} ) self.assert_compile( s.params(x=5, y=6, z=7), - "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)", + "SELECT CURRENT_DATE + s.a AS dates FROM " + "generate_series(:x, :y, :z) as s(a)", checkparams={'y': 6, 'x': 5, 'z': 7} ) @@ -1879,7 +1892,6 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): "UNION (SELECT foo, bar FROM bat INTERSECT SELECT foo, bar FROM bat)" ) - @testing.uses_deprecated() def test_binds(self): for ( stmt, @@ -1947,13 +1959,15 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): {'myid_1':5, 'myid_2': 6}, {'myid_1':5, 'myid_2':6}, [5,6] ), ( - bindparam('test', type_=String) + text("'hi'"), + bindparam('test', type_=String, required=False) + text("'hi'"), ":test || 'hi'", "? || 'hi'", {'test':None}, [None], {}, {'test':None}, [None] ), ( + # testing select.params() here - bindparam() objects + # must get required flag set to False select([table1], or_(table1.c.myid==bindparam('myid'), table2.c.otherid==bindparam('myotherid'))).\ params({'myid':8, 'myotherid':7}), diff --git a/test/sql/test_functions.py b/test/sql/test_functions.py index f0fcd4b72..8e5c6bc58 100644 --- a/test/sql/test_functions.py +++ b/test/sql/test_functions.py @@ -268,7 +268,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): def test_functions_with_cols(self): users = table('users', column('id'), column('name'), column('fullname')) calculate = select([column('q'), column('z'), column('r')], - from_obj=[func.calculate(bindparam('x'), bindparam('y'))]) + from_obj=[func.calculate(bindparam('x', None), bindparam('y', None))]) self.assert_compile(select([users], users.c.id > calculate.c.z), "SELECT users.id, users.name, users.fullname " diff --git a/test/sql/test_query.py b/test/sql/test_query.py index e79bf32e3..670fb2c64 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -1,4 +1,4 @@ -from test.lib.testing import eq_, assert_raises_message, assert_raises +from test.lib.testing import eq_, assert_raises_message, assert_raises, is_ import datetime from sqlalchemy import * from sqlalchemy import exc, sql, util @@ -1216,6 +1216,67 @@ class QueryTest(fixtures.TestBase): r = s.execute().fetchall() assert len(r) == 1 +class RequiredBindTest(fixtures.TablesTest): + run_create_tables = None + run_deletes = None + + @classmethod + def define_tables(cls, metadata): + Table('foo', metadata, + Column('id', Integer, primary_key=True), + Column('data', String(50)), + Column('x', Integer) + ) + + def _assert_raises(self, stmt, params): + assert_raises_message( + exc.StatementError, + "A value is required for bind parameter 'x'", + testing.db.execute, stmt, **params) + + assert_raises_message( + exc.StatementError, + "A value is required for bind parameter 'x'", + testing.db.execute, stmt, params) + + def test_insert(self): + stmt = self.tables.foo.insert().values(x=bindparam('x'), + data=bindparam('data')) + self._assert_raises( + stmt, {'data': 'data'} + ) + + def test_select_where(self): + stmt = select([self.tables.foo]).\ + where(self.tables.foo.c.data == bindparam('data')).\ + where(self.tables.foo.c.x == bindparam('x')) + self._assert_raises( + stmt, {'data': 'data'} + ) + + def test_select_columns(self): + stmt = select([bindparam('data'), bindparam('x')]) + self._assert_raises( + stmt, {'data': 'data'} + ) + + def test_text(self): + stmt = text("select * from foo where x=:x and data=:data1") + self._assert_raises( + stmt, {'data1': 'data'} + ) + + def test_required_flag(self): + is_(bindparam('foo').required, True) + is_(bindparam('foo', required=False).required, False) + is_(bindparam('foo', 'bar').required, False) + is_(bindparam('foo', 'bar', required=True).required, True) + + c = lambda: None + is_(bindparam('foo', callable_=c, required=True).required, True) + is_(bindparam('foo', callable_=c).required, False) + is_(bindparam('foo', callable_=c, required=False).required, False) + class TableInsertTest(fixtures.TablesTest): """test for consistent insert behavior across dialects regarding the inline=True flag, lower-case 't' tables. |
