diff options
| author | Gorka Eguileor <geguileo@redhat.com> | 2015-09-29 20:29:33 +0200 |
|---|---|---|
| committer | Gorka Eguileor <geguileo@redhat.com> | 2015-09-29 20:29:33 +0200 |
| commit | 9fb76d759678558f6fd087dcc04de3f2daa3a571 (patch) | |
| tree | 4d4155a79fe49d3f97e5e734acaf20b83684d9fd /test/sql | |
| parent | fc73036865a0c0d2809b66fcfdc663ff425f4267 (diff) | |
| download | sqlalchemy-9fb76d759678558f6fd087dcc04de3f2daa3a571.tar.gz | |
Only preserve order in updates if tuple/dict
To avoid penalties for updates that do not require ordering, we will
only use OrderedDict for updates that receive a tuple or list of pairs,
and all kinds of dictionaries (dict, sqlalchemy's OrderedDict, or
collections.OrderedDict) will be treateated as unordered updates, just
like we were doing before.
This way this new feature will not change how updates behave for any
existing code and will only affect those that use the new ordered
feature.
This patch reverts update tests to how they were before as well as adds
a couple of tests to confirm that OrderedDicts are really treated like
normal dicts.
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_compiler.py | 8 | ||||
| -rw-r--r-- | test/sql/test_update.py | 67 |
2 files changed, 61 insertions, 14 deletions
diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 899af86a9..c957b2f8a 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -2764,7 +2764,7 @@ class CRUDTest(fixtures.TestBase, AssertsCompiledSQL): u.values( x=3 + bindparam('x')), - "UPDATE foo SET y=:y, x=(:param_1 + :x) WHERE foo.x = :x", + "UPDATE foo SET x=(:param_1 + :x), y=:y WHERE foo.x = :x", params={ 'x': 1, 'y': 2}) @@ -2951,9 +2951,9 @@ class InlineDefaultTest(fixtures.TestBase, AssertsCompiledSQL): ) self.assert_compile(t.update(inline=True, values={'col3': 'foo'}), - "UPDATE test SET col3=:col3, col1=foo(:foo_1), " - "col2=(SELECT coalesce(max(foo.id)) AS coalesce_1 " - "FROM foo)") + "UPDATE test SET col1=foo(:foo_1), col2=(SELECT " + "coalesce(max(foo.id)) AS coalesce_1 FROM foo), " + "col3=:col3") class SchemaTest(fixtures.TestBase, AssertsCompiledSQL): diff --git a/test/sql/test_update.py b/test/sql/test_update.py index 3dd6c99db..059c3ad6d 100644 --- a/test/sql/test_update.py +++ b/test/sql/test_update.py @@ -115,7 +115,7 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL): table1.c.myid == 12, values={table1.c.name: table1.c.myid}), 'UPDATE mytable ' - 'SET description=:description, name=mytable.myid ' + 'SET name=mytable.myid, description=:description ' 'WHERE mytable.myid = :myid_1', params={'description': 'test'}, checkparams={'description': 'test', 'myid_1': 12}) @@ -128,8 +128,7 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL): 'UPDATE mytable ' 'SET myid=:myid, description=:description ' 'WHERE mytable.myid = :myid_1', - params=util.OrderedDict(( - ('myid_1', 12), ('myid', 9), ('description', 'test')))) + params={'myid_1': 12, 'myid': 9, 'description': 'test'}) def test_update_8(self): table1 = self.tables.mytable @@ -155,17 +154,19 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL): update(table1, table1.c.myid == 12, values=v1).values(v2), 'UPDATE mytable ' 'SET ' - 'description=:description, ' - 'name=(mytable.name || :name_1) ' + 'name=(mytable.name || :name_1), ' + 'description=:description ' 'WHERE mytable.myid = :myid_1', params={'description': 'test'}) def test_update_11(self): table1 = self.tables.mytable - values = util.OrderedDict(( - (table1.c.myid, func.do_stuff(table1.c.myid, literal('hoho'))), - (table1.c.name, table1.c.name + 'lala'))) + values = { + table1.c.name: table1.c.name + 'lala', + table1.c.myid: func.do_stuff(table1.c.myid, literal('hoho')) + } + self.assert_compile( update( table1, @@ -185,8 +186,7 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL): def test_update_12(self): table1 = self.tables.mytable - # Confirm that we can pass values not only as dicts and ordered dicts, - # but as value pairs + # Confirm that we can pass values as tuple value pairs values = ( (table1.c.myid, func.do_stuff(table1.c.myid, literal('hoho'))), (table1.c.name, table1.c.name + 'lala')) @@ -206,6 +206,53 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL): 'mytable.myid = hoho(:hoho_1) AND ' 'mytable.name = :param_2 || mytable.name || :param_3') + def test_update_13(self): + table1 = self.tables.mytable + + # Confirm that we can pass values as list value pairs + values = [ + (table1.c.myid, func.do_stuff(table1.c.myid, literal('hoho'))), + (table1.c.name, table1.c.name + 'lala')] + self.assert_compile( + update( + table1, + (table1.c.myid == func.hoho(4)) & ( + table1.c.name == literal('foo') + + table1.c.name + + literal('lala')), + values=values), + 'UPDATE mytable ' + 'SET ' + 'myid=do_stuff(mytable.myid, :param_1), ' + 'name=(mytable.name || :name_1) ' + 'WHERE ' + 'mytable.myid = hoho(:hoho_1) AND ' + 'mytable.name = :param_2 || mytable.name || :param_3') + + def test_update_14(self): + table1 = self.tables.mytable + + # Confirm that ordered dicts are treated as normal dicts + values = util.OrderedDict(( + (table1.c.name, table1.c.name + 'lala'), + (table1.c.myid, func.do_stuff(table1.c.myid, literal('hoho'))))) + + self.assert_compile( + update( + table1, + (table1.c.myid == func.hoho(4)) & ( + table1.c.name == literal('foo') + + table1.c.name + + literal('lala')), + values=values), + 'UPDATE mytable ' + 'SET ' + 'myid=do_stuff(mytable.myid, :param_1), ' + 'name=(mytable.name || :name_1) ' + 'WHERE ' + 'mytable.myid = hoho(:hoho_1) AND ' + 'mytable.name = :param_2 || mytable.name || :param_3') + def test_where_empty(self): table1 = self.tables.mytable self.assert_compile( |
