From b432ff8c4f230c4521ee26b5acdfdc72838845d8 Mon Sep 17 00:00:00 2001 From: Robin Thomas Date: Wed, 13 Apr 2016 17:04:40 -0400 Subject: working, test-included implementation of PostgreSQL 9.5's INSERT ... ON CONFLICT ... for common use cases. --- test/dialect/postgresql/test_compiler.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'test/dialect/postgresql/test_compiler.py') diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index 87e48d3f2..ae8907180 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -16,6 +16,7 @@ from sqlalchemy.sql import table, column, operators, literal_column from sqlalchemy.sql import util as sql_util from sqlalchemy.util import u from sqlalchemy.dialects.postgresql import aggregate_order_by +from sqlalchemy.dialects.postgresql.on_conflict import DoNothing, DoUpdate class SequenceTest(fixtures.TestBase, AssertsCompiledSQL): @@ -90,6 +91,28 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): 'RETURNING length(mytable.name) AS length_1', dialect=dialect) + def test_insert_on_conflict(self): + dialect = postgresql.dialect() + table1 = table('mytable', + column('myid', Integer), + column('name', String(128)), + column('description', String(128)), + ) + + i = insert( + table1, + values=dict( + name='foo'), postgresql_on_conflict=DoNothing()) + self.assert_compile(i, + 'INSERT INTO mytable (name) VALUES ' + '(%(name)s) ON CONFLICT DO NOTHING', + dialect=dialect) + i = insert(table1, values=dict(name='foo'), postgresql_on_conflict=DoUpdate(table1.c.myid).with_excluded('foo')) + self.assert_compile(i, + 'INSERT INTO mytable (name) VALUES ' + '(%(name)s) ON CONFLICT (myid) DO UPDATE SET foo = excluded.foo', + dialect=dialect) + def test_insert_returning(self): dialect = postgresql.dialect() table1 = table('mytable', -- cgit v1.2.1