def test_wb_execute_insert_returning_not_used_with_old_postgres(self): """Shouldn't try to use RETURNING with PostgreSQL < 8.2.""" column1 = Column("id1", "returning_test") column2 = Column("id2", "returning_test") variable1 = IntVariable() variable2 = IntVariable() insert = Insert({}, primary_columns=(column1, column2), primary_variables=(variable1, variable2)) self.database._version = 80109 self.connection.execute(insert) self.assertFalse(variable1.is_defined()) self.assertFalse(variable2.is_defined()) result = self.connection.execute("SELECT * FROM returning_test") self.assertEquals(result.get_one(), (123, 456))
def test_execute_insert_returning_without_columns(self): """Without primary_columns, the RETURNING system won't be used.""" column1 = Column("id1", "returning_test") variable1 = IntVariable() insert = Insert({column1: 123}, primary_variables=(variable1,)) self.connection.execute(insert) self.assertFalse(variable1.is_defined()) result = self.connection.execute("SELECT * FROM returning_test") self.assertEquals(result.get_one(), (123, 456))
def test_execute_insert_returning(self): if self.database._version < 80200: return # Can't run this test with old PostgreSQL versions. column1 = Column("id1", "returning_test") column2 = Column("id2", "returning_test") variable1 = IntVariable() variable2 = IntVariable() insert = Insert({}, primary_columns=(column1, column2), primary_variables=(variable1, variable2)) self.connection.execute(insert) self.assertTrue(variable1.is_defined()) self.assertTrue(variable2.is_defined()) self.assertEquals(variable1.get(), 123) self.assertEquals(variable2.get(), 456) result = self.connection.execute("SELECT * FROM returning_test") self.assertEquals(result.get_one(), (123, 456))
def test_execute_insert_returning_without_columns(self): """Without primary_columns, the RETURNING system won't be used.""" column1 = Column("id1", "returning_test") variable1 = IntVariable() insert = Insert({column1: 123}, primary_variables=(variable1, )) self.connection.execute(insert) self.assertFalse(variable1.is_defined()) result = self.connection.execute("SELECT * FROM returning_test") self.assertEquals(result.get_one(), (123, 456))
def test_execute_insert_auto_increment_primary_key(self): id_column = Column("id", "test") id_variable = IntVariable() title_column = Column("title", "test") title_variable = UnicodeVariable(u"testing") # This is not part of the table. It is just used to show that # only one primary key variable is set from the insert ID. dummy_column = Column("dummy", "test") dummy_variable = IntVariable() insert = Insert({title_column: title_variable}, primary_columns=(id_column, dummy_column), primary_variables=(id_variable, dummy_variable)) self.connection.execute(insert) self.assertTrue(id_variable.is_defined()) self.assertFalse(dummy_variable.is_defined()) # The newly inserted row should have the maximum id value for # the table. result = self.connection.execute("SELECT MAX(id) FROM test") self.assertEqual(result.get_one()[0], id_variable.get())