From 1dcf33e71280698007af718a2404034ef51dd1c7 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 2 Jun 2011 03:09:08 -0400 Subject: [PATCH] fix some tests --- lib/sqlalchemy/sql/expression.py | 17 ++++++++++++++++- test/sql/test_defaults.py | 2 +- test/sql/test_functions.py | 11 ++++++----- test/sql/test_types.py | 8 ++++---- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 47f19806a6..38eb71da87 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -1144,6 +1144,12 @@ func = _FunctionGenerator() >>> print func.count(1) count(:param_1) + The element is a column-oriented SQL element like any other, and is + used in that way:: + + >>> print select([func.count(table.c.id)]) + SELECT count(sometable.id) FROM sometable + Any name can be given to ``func``. If the function name is unknown to SQLAlchemy, it will be rendered exactly as is. For common SQL functions which SQLAlchemy is aware of, the name may be interpreted as a *generic @@ -1171,7 +1177,16 @@ func = _FunctionGenerator() This object meets the "column" interface, including comparison and labeling functions. The object can also be passed the :meth:`~.Connectable.execute` method of a :class:`.Connection` or :class:`.Engine`, where it will be - wrapped inside of a SELECT statement first. + wrapped inside of a SELECT statement first:: + + print connection.execute(func.current_timestamp()).scalar() + + A function can also be "bound" to a :class:`.Engine` or :class:`.Connection` + using the ``bind`` keyword argument, providing an execute() as well + as a scalar() method:: + + myfunc = func.current_timestamp(bind=some_engine) + print myfunc.scalar() Functions which are interpreted as "generic" functions know how to calculate their return type automatically. For a listing of known generic diff --git a/test/sql/test_defaults.py b/test/sql/test_defaults.py index 82ca1412bf..2bb961d4e1 100644 --- a/test/sql/test_defaults.py +++ b/test/sql/test_defaults.py @@ -68,7 +68,7 @@ class DefaultTest(fixtures.TestBase): def2 = sa.text("getdate()") else: def2 = sa.text("current_date") - ts = db.func.current_date().scalar() + ts = db.scalar(func.current_date()) else: f = len('abcdef') f2 = len('abcdefghijk') diff --git a/test/sql/test_functions.py b/test/sql/test_functions.py index f08fde1f5d..961845bac7 100644 --- a/test/sql/test_functions.py +++ b/test/sql/test_functions.py @@ -195,6 +195,7 @@ class ExecuteTest(fixtures.TestBase): def tearDown(self): pass + @testing.uses_deprecated def test_standalone_execute(self): x = testing.db.func.current_date().execute().scalar() y = testing.db.func.current_date().select().execute().scalar() @@ -300,13 +301,13 @@ class ExecuteTest(fixtures.TestBase): @testing.fails_on_everything_except('postgresql') def test_as_from(self): # TODO: shouldnt this work on oracle too ? - x = testing.db.func.current_date().execute().scalar() - y = testing.db.func.current_date().select().execute().scalar() - z = testing.db.func.current_date().scalar() - w = select(['*'], from_obj=[testing.db.func.current_date()]).scalar() + x = func.current_date(bind=testing.db).execute().scalar() + y = func.current_date(bind=testing.db).select().execute().scalar() + z = func.current_date(bind=testing.db).scalar() + w = select(['*'], from_obj=[func.current_date(bind=testing.db)]).scalar() # construct a column-based FROM object out of a function, like in [ticket:172] - s = select([sql.column('date', type_=DateTime)], from_obj=[testing.db.func.current_date()]) + s = select([sql.column('date', type_=DateTime)], from_obj=[func.current_date(bind=testing.db)]) q = s.execute().first()[s.c.date] r = s.alias('datequery').select().scalar() diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 8a8ae9cae5..6ccb67f919 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -1263,15 +1263,15 @@ class DateTest(fixtures.TestBase, AssertsExecutionResults): 'DateTest mismatch: got:%s expected:%s' % (l, insert_data)) def testtextdate(self): - x = testing.db.text( + x = testing.db.execute(text( "select user_datetime from query_users_with_date", - typemap={'user_datetime':DateTime}).execute().fetchall() + typemap={'user_datetime':DateTime})).fetchall() self.assert_(isinstance(x[0][0], datetime.datetime)) - x = testing.db.text( + x = testing.db.execute(text( "select * from query_users_with_date where user_datetime=:somedate", - bindparams=[bindparam('somedate', type_=types.DateTime)]).execute( + bindparams=[bindparam('somedate', type_=types.DateTime)]), somedate=datetime.datetime(2005, 11, 10, 11, 52, 35)).fetchall() def testdate2(self): -- 2.39.5