1
from test.lib.testing import eq_
1
from sqlalchemy.testing import eq_
2
2
from sqlalchemy import *
4
from test.lib.schema import Table, Column
3
from sqlalchemy import testing
4
from sqlalchemy.testing.schema import Table, Column
5
5
from sqlalchemy.types import TypeDecorator
6
from test.lib import fixtures
6
from sqlalchemy.testing import fixtures, AssertsExecutionResults, engines, \
8
from sqlalchemy import exc as sa_exc
8
10
class ReturningTest(fixtures.TestBase, AssertsExecutionResults):
9
11
__requires__ = 'returning',
66
64
@testing.fails_on('firebird', "fb/kintersbasdb can't handle the bind params")
67
65
@testing.fails_on('oracle+zxjdbc', "JDBC driver bug")
68
@testing.exclude('firebird', '<', (2, 0), '2.0+ feature')
69
@testing.exclude('postgresql', '<', (8, 2), '8.2+ feature')
70
66
def test_anon_expressions(self):
71
67
result = table.insert().values(goofy="someOTHERgoofy").\
72
68
returning(func.lower(table.c.goofy, type_=GoofyType)).execute()
73
69
row = result.first()
74
assert row[0] == "foosomeothergoofyBAR"
70
eq_(row[0], "foosomeothergoofyBAR")
76
72
result = table.insert().values(persons=12).\
77
73
returning(table.c.persons + 18).execute()
78
74
row = result.first()
81
@testing.exclude('firebird', '<', (2, 1), '2.1+ feature')
82
@testing.exclude('postgresql', '<', (8, 2), '8.2+ feature')
83
77
def test_update_returning(self):
84
78
table.insert().execute([{'persons': 5, 'full': False}, {'persons': 3, 'full': False}])
87
81
eq_(result.fetchall(), [(1,)])
89
83
result2 = select([table.c.id, table.c.full]).order_by(table.c.id).execute()
90
eq_(result2.fetchall(), [(1,True),(2,False)])
84
eq_(result2.fetchall(), [(1, True), (2, False)])
92
@testing.exclude('firebird', '<', (2, 0), '2.0+ feature')
93
@testing.exclude('postgresql', '<', (8, 2), '8.2+ feature')
94
86
def test_insert_returning(self):
95
87
result = table.insert().returning(table.c.id).execute({'persons': 1, 'full': False})
97
89
eq_(result.fetchall(), [(1,)])
99
@testing.fails_on('postgresql', '')
100
@testing.fails_on('oracle+cx_oracle', '')
101
@testing.crashes('mssql+mxodbc', 'Raises an error')
102
def test_executemany():
103
# return value is documented as failing with psycopg2/executemany
104
result2 = table.insert().returning(table).execute(
105
[{'persons': 2, 'full': False}, {'persons': 3, 'full': True}])
107
if testing.against('mssql+zxjdbc'):
108
# jtds apparently returns only the first row
109
eq_(result2.fetchall(), [(2, 2, False, None)])
110
elif testing.against('firebird', 'mssql', 'oracle'):
111
# Multiple inserts only return the last row
112
eq_(result2.fetchall(), [(3, 3, True, None)])
114
# nobody does this as far as we know (pg8000?)
115
eq_(result2.fetchall(), [(2, 2, False, None), (3, 3, True, None)])
121
@testing.exclude('firebird', '<', (2, 1), '2.1+ feature')
122
@testing.exclude('postgresql', '<', (8, 2), '8.2+ feature')
91
@testing.requires.multivalues_inserts
92
def test_multirow_returning(self):
93
ins = table.insert().returning(table.c.id, table.c.persons).values(
95
{'persons': 1, 'full': False},
96
{'persons': 2, 'full': True},
97
{'persons': 3, 'full': False},
100
result = testing.db.execute(ins)
103
[(1, 1), (2, 2), (3, 3)]
106
def test_no_ipk_on_returning(self):
107
result = testing.db.execute(
108
table.insert().returning(table.c.id),
109
{'persons': 1, 'full': False}
111
assert_raises_message(
112
sa_exc.InvalidRequestError,
113
"Can't call inserted_primary_key when returning\(\) is used.",
114
getattr, result, "inserted_primary_key"
123
117
@testing.fails_on_everything_except('postgresql', 'firebird')
124
118
def test_literal_returning(self):
125
119
if testing.against("postgresql"):
131
125
'values (5, 10, %s) returning persons' % literal_true)
132
126
eq_([dict(row) for row in result4], [{'persons': 10}])
134
@testing.exclude('firebird', '<', (2, 1), '2.1+ feature')
135
@testing.exclude('postgresql', '<', (8, 2), '8.2+ feature')
136
128
def test_delete_returning(self):
137
129
table.insert().execute([{'persons': 5, 'full': False}, {'persons': 3, 'full': False}])
140
132
eq_(result.fetchall(), [(1,)])
142
134
result2 = select([table.c.id, table.c.full]).order_by(table.c.id).execute()
143
eq_(result2.fetchall(), [(2,False),])
135
eq_(result2.fetchall(), [(2, False),])
145
137
class SequenceReturningTest(fixtures.TestBase):
146
__requires__ = 'returning',
138
__requires__ = 'returning', 'sequences'
149
141
meta = MetaData(testing.db)