| 181 | |
| 182 | @skipUnless(connection.vendor=='postgres', 'Needs Postgres...') |
| 183 | def test_tx(self): |
| 184 | from psycopg2 import extensions |
| 185 | IN_TX = extensions.TRANSACTION_STATUS_IN_TX |
| 186 | with transaction.commit_on_success(): |
| 187 | cursor = connection.cursor() |
| 188 | cursor.execute("INSERT INTO transactions_reporter (first_name, last_name, email) VALUES ('Douglas', 'Adams', 'da@example.com');") |
| 189 | # commit_on_success commits raw SQL |
| 190 | transaction.rollback() |
| 191 | self.assertTrue(Reporter.objects.filter(last_name='Adams').exists()) |
| 192 | Reporter.objects.filter(last_name='Adams').delete() |
| 193 | |
| 194 | with transaction.autocommit(): |
| 195 | cursor = connection.cursor() |
| 196 | cursor.execute("INSERT INTO transactions_reporter (first_name, last_name, email) VALUES ('Douglas', 'Adams', 'da@example.com');") |
| 197 | # autocommit doesn't |
| 198 | transaction.rollback() |
| 199 | self.assertFalse(Reporter.objects.filter(last_name='Adams').exists()) |
| 200 | Reporter.objects.filter(last_name='Adams').delete() |
| 201 | |
| 202 | with transaction.autocommit(): |
| 203 | Reporter.objects.create(first_name='Douglas', last_name='Adams', |
| 204 | email='da@example.com') |
| 205 | # But it does commit when writing using Django's ORM |
| 207 | self.assertTrue(Reporter.objects.filter(last_name='Adams').exists()) |
| 208 | |
| 209 | with transaction.commit_on_success(): |
| 210 | Reporter.objects.filter(last_name='Adams').exists() |
| 211 | # commit_on_success also commits when only reading |
| 212 | self.assertNotEqual(connection.connection.get_transaction_status(), IN_TX) |
| 213 | self.assertFalse(transaction.is_dirty()) |
| 214 | with transaction.autocommit(): |
| 215 | Reporter.objects.filter(last_name='Adams').exists() |
| 216 | # Using autocommit we are in transaction... |
| 217 | self.assertEqual(connection.connection.get_transaction_status(), IN_TX) |
| 218 | # But the transaction isn't dirty |
| 219 | self.assertFalse(transaction.is_dirty()) |
| 220 | |