tests.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. from __future__ import absolute_import
  2. import sys
  3. import warnings
  4. from django.db import connection, transaction, IntegrityError
  5. from django.test import TransactionTestCase, skipUnlessDBFeature
  6. from django.utils import six
  7. from django.utils.unittest import skipIf, skipUnless
  8. from .models import Reporter
  9. @skipUnless(connection.features.uses_savepoints,
  10. "'atomic' requires transactions and savepoints.")
  11. class AtomicTests(TransactionTestCase):
  12. """
  13. Tests for the atomic decorator and context manager.
  14. The tests make assertions on internal attributes because there isn't a
  15. robust way to ask the database for its current transaction state.
  16. Since the decorator syntax is converted into a context manager (see the
  17. implementation), there are only a few basic tests with the decorator
  18. syntax and the bulk of the tests use the context manager syntax.
  19. """
  20. def test_decorator_syntax_commit(self):
  21. @transaction.atomic
  22. def make_reporter():
  23. Reporter.objects.create(first_name="Tintin")
  24. make_reporter()
  25. self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
  26. def test_decorator_syntax_rollback(self):
  27. @transaction.atomic
  28. def make_reporter():
  29. Reporter.objects.create(first_name="Haddock")
  30. raise Exception("Oops, that's his last name")
  31. with six.assertRaisesRegex(self, Exception, "Oops"):
  32. make_reporter()
  33. self.assertQuerysetEqual(Reporter.objects.all(), [])
  34. def test_alternate_decorator_syntax_commit(self):
  35. @transaction.atomic()
  36. def make_reporter():
  37. Reporter.objects.create(first_name="Tintin")
  38. make_reporter()
  39. self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
  40. def test_alternate_decorator_syntax_rollback(self):
  41. @transaction.atomic()
  42. def make_reporter():
  43. Reporter.objects.create(first_name="Haddock")
  44. raise Exception("Oops, that's his last name")
  45. with six.assertRaisesRegex(self, Exception, "Oops"):
  46. make_reporter()
  47. self.assertQuerysetEqual(Reporter.objects.all(), [])
  48. def test_commit(self):
  49. with transaction.atomic():
  50. Reporter.objects.create(first_name="Tintin")
  51. self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
  52. def test_rollback(self):
  53. with six.assertRaisesRegex(self, Exception, "Oops"):
  54. with transaction.atomic():
  55. Reporter.objects.create(first_name="Haddock")
  56. raise Exception("Oops, that's his last name")
  57. self.assertQuerysetEqual(Reporter.objects.all(), [])
  58. def test_nested_commit_commit(self):
  59. with transaction.atomic():
  60. Reporter.objects.create(first_name="Tintin")
  61. with transaction.atomic():
  62. Reporter.objects.create(first_name="Archibald", last_name="Haddock")
  63. self.assertQuerysetEqual(Reporter.objects.all(),
  64. ['<Reporter: Archibald Haddock>', '<Reporter: Tintin>'])
  65. def test_nested_commit_rollback(self):
  66. with transaction.atomic():
  67. Reporter.objects.create(first_name="Tintin")
  68. with six.assertRaisesRegex(self, Exception, "Oops"):
  69. with transaction.atomic():
  70. Reporter.objects.create(first_name="Haddock")
  71. raise Exception("Oops, that's his last name")
  72. self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
  73. def test_nested_rollback_commit(self):
  74. with six.assertRaisesRegex(self, Exception, "Oops"):
  75. with transaction.atomic():
  76. Reporter.objects.create(last_name="Tintin")
  77. with transaction.atomic():
  78. Reporter.objects.create(last_name="Haddock")
  79. raise Exception("Oops, that's his first name")
  80. self.assertQuerysetEqual(Reporter.objects.all(), [])
  81. def test_nested_rollback_rollback(self):
  82. with six.assertRaisesRegex(self, Exception, "Oops"):
  83. with transaction.atomic():
  84. Reporter.objects.create(last_name="Tintin")
  85. with six.assertRaisesRegex(self, Exception, "Oops"):
  86. with transaction.atomic():
  87. Reporter.objects.create(first_name="Haddock")
  88. raise Exception("Oops, that's his last name")
  89. raise Exception("Oops, that's his first name")
  90. self.assertQuerysetEqual(Reporter.objects.all(), [])
  91. def test_merged_commit_commit(self):
  92. with transaction.atomic():
  93. Reporter.objects.create(first_name="Tintin")
  94. with transaction.atomic(savepoint=False):
  95. Reporter.objects.create(first_name="Archibald", last_name="Haddock")
  96. self.assertQuerysetEqual(Reporter.objects.all(),
  97. ['<Reporter: Archibald Haddock>', '<Reporter: Tintin>'])
  98. def test_merged_commit_rollback(self):
  99. with transaction.atomic():
  100. Reporter.objects.create(first_name="Tintin")
  101. with six.assertRaisesRegex(self, Exception, "Oops"):
  102. with transaction.atomic(savepoint=False):
  103. Reporter.objects.create(first_name="Haddock")
  104. raise Exception("Oops, that's his last name")
  105. # Writes in the outer block are rolled back too.
  106. self.assertQuerysetEqual(Reporter.objects.all(), [])
  107. def test_merged_rollback_commit(self):
  108. with six.assertRaisesRegex(self, Exception, "Oops"):
  109. with transaction.atomic():
  110. Reporter.objects.create(last_name="Tintin")
  111. with transaction.atomic(savepoint=False):
  112. Reporter.objects.create(last_name="Haddock")
  113. raise Exception("Oops, that's his first name")
  114. self.assertQuerysetEqual(Reporter.objects.all(), [])
  115. def test_merged_rollback_rollback(self):
  116. with six.assertRaisesRegex(self, Exception, "Oops"):
  117. with transaction.atomic():
  118. Reporter.objects.create(last_name="Tintin")
  119. with six.assertRaisesRegex(self, Exception, "Oops"):
  120. with transaction.atomic(savepoint=False):
  121. Reporter.objects.create(first_name="Haddock")
  122. raise Exception("Oops, that's his last name")
  123. raise Exception("Oops, that's his first name")
  124. self.assertQuerysetEqual(Reporter.objects.all(), [])
  125. def test_reuse_commit_commit(self):
  126. atomic = transaction.atomic()
  127. with atomic:
  128. Reporter.objects.create(first_name="Tintin")
  129. with atomic:
  130. Reporter.objects.create(first_name="Archibald", last_name="Haddock")
  131. self.assertQuerysetEqual(Reporter.objects.all(),
  132. ['<Reporter: Archibald Haddock>', '<Reporter: Tintin>'])
  133. def test_reuse_commit_rollback(self):
  134. atomic = transaction.atomic()
  135. with atomic:
  136. Reporter.objects.create(first_name="Tintin")
  137. with six.assertRaisesRegex(self, Exception, "Oops"):
  138. with atomic:
  139. Reporter.objects.create(first_name="Haddock")
  140. raise Exception("Oops, that's his last name")
  141. self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
  142. def test_reuse_rollback_commit(self):
  143. atomic = transaction.atomic()
  144. with six.assertRaisesRegex(self, Exception, "Oops"):
  145. with atomic:
  146. Reporter.objects.create(last_name="Tintin")
  147. with atomic:
  148. Reporter.objects.create(last_name="Haddock")
  149. raise Exception("Oops, that's his first name")
  150. self.assertQuerysetEqual(Reporter.objects.all(), [])
  151. def test_reuse_rollback_rollback(self):
  152. atomic = transaction.atomic()
  153. with six.assertRaisesRegex(self, Exception, "Oops"):
  154. with atomic:
  155. Reporter.objects.create(last_name="Tintin")
  156. with six.assertRaisesRegex(self, Exception, "Oops"):
  157. with atomic:
  158. Reporter.objects.create(first_name="Haddock")
  159. raise Exception("Oops, that's his last name")
  160. raise Exception("Oops, that's his first name")
  161. self.assertQuerysetEqual(Reporter.objects.all(), [])
  162. class AtomicInsideTransactionTests(AtomicTests):
  163. """All basic tests for atomic should also pass within an existing transaction."""
  164. def setUp(self):
  165. self.atomic = transaction.atomic()
  166. self.atomic.__enter__()
  167. def tearDown(self):
  168. self.atomic.__exit__(*sys.exc_info())
  169. @skipIf(connection.features.autocommits_when_autocommit_is_off,
  170. "This test requires a non-autocommit mode that doesn't autocommit.")
  171. class AtomicWithoutAutocommitTests(AtomicTests):
  172. """All basic tests for atomic should also pass when autocommit is turned off."""
  173. def setUp(self):
  174. transaction.set_autocommit(False)
  175. def tearDown(self):
  176. # The tests access the database after exercising 'atomic', initiating
  177. # a transaction ; a rollback is required before restoring autocommit.
  178. transaction.rollback()
  179. transaction.set_autocommit(True)
  180. @skipIf(connection.features.autocommits_when_autocommit_is_off,
  181. "This test requires a non-autocommit mode that doesn't autocommit.")
  182. class AtomicInsideLegacyTransactionManagementTests(AtomicTests):
  183. def setUp(self):
  184. transaction.enter_transaction_management()
  185. def tearDown(self):
  186. # The tests access the database after exercising 'atomic', making the
  187. # connection dirty; a rollback is required to make it clean.
  188. transaction.rollback()
  189. transaction.leave_transaction_management()
  190. @skipUnless(connection.features.uses_savepoints,
  191. "'atomic' requires transactions and savepoints.")
  192. class AtomicMergeTests(TransactionTestCase):
  193. """Test merging transactions with savepoint=False."""
  194. def test_merged_outer_rollback(self):
  195. with transaction.atomic():
  196. Reporter.objects.create(first_name="Tintin")
  197. with transaction.atomic(savepoint=False):
  198. Reporter.objects.create(first_name="Archibald", last_name="Haddock")
  199. with six.assertRaisesRegex(self, Exception, "Oops"):
  200. with transaction.atomic(savepoint=False):
  201. Reporter.objects.create(first_name="Tournesol")
  202. raise Exception("Oops, that's his last name")
  203. # It wasn't possible to roll back
  204. self.assertEqual(Reporter.objects.count(), 3)
  205. # It wasn't possible to roll back
  206. self.assertEqual(Reporter.objects.count(), 3)
  207. # The outer block must roll back
  208. self.assertQuerysetEqual(Reporter.objects.all(), [])
  209. def test_merged_inner_savepoint_rollback(self):
  210. with transaction.atomic():
  211. Reporter.objects.create(first_name="Tintin")
  212. with transaction.atomic():
  213. Reporter.objects.create(first_name="Archibald", last_name="Haddock")
  214. with six.assertRaisesRegex(self, Exception, "Oops"):
  215. with transaction.atomic(savepoint=False):
  216. Reporter.objects.create(first_name="Tournesol")
  217. raise Exception("Oops, that's his last name")
  218. # It wasn't possible to roll back
  219. self.assertEqual(Reporter.objects.count(), 3)
  220. # The first block with a savepoint must roll back
  221. self.assertEqual(Reporter.objects.count(), 1)
  222. self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
  223. def test_merged_outer_rollback_after_inner_failure_and_inner_success(self):
  224. with transaction.atomic():
  225. Reporter.objects.create(first_name="Tintin")
  226. # Inner block without a savepoint fails
  227. with six.assertRaisesRegex(self, Exception, "Oops"):
  228. with transaction.atomic(savepoint=False):
  229. Reporter.objects.create(first_name="Haddock")
  230. raise Exception("Oops, that's his last name")
  231. # It wasn't possible to roll back
  232. self.assertEqual(Reporter.objects.count(), 2)
  233. # Inner block with a savepoint succeeds
  234. with transaction.atomic(savepoint=False):
  235. Reporter.objects.create(first_name="Archibald", last_name="Haddock")
  236. # It still wasn't possible to roll back
  237. self.assertEqual(Reporter.objects.count(), 3)
  238. # The outer block must rollback
  239. self.assertQuerysetEqual(Reporter.objects.all(), [])
  240. @skipUnless(connection.features.uses_savepoints,
  241. "'atomic' requires transactions and savepoints.")
  242. class AtomicErrorsTests(TransactionTestCase):
  243. def test_atomic_prevents_setting_autocommit(self):
  244. autocommit = transaction.get_autocommit()
  245. with transaction.atomic():
  246. with self.assertRaises(transaction.TransactionManagementError):
  247. transaction.set_autocommit(not autocommit)
  248. # Make sure autocommit wasn't changed.
  249. self.assertEqual(connection.autocommit, autocommit)
  250. def test_atomic_prevents_calling_transaction_methods(self):
  251. with transaction.atomic():
  252. with self.assertRaises(transaction.TransactionManagementError):
  253. transaction.commit()
  254. with self.assertRaises(transaction.TransactionManagementError):
  255. transaction.rollback()
  256. def test_atomic_prevents_calling_transaction_management_methods(self):
  257. with transaction.atomic():
  258. with self.assertRaises(transaction.TransactionManagementError):
  259. transaction.enter_transaction_management()
  260. with self.assertRaises(transaction.TransactionManagementError):
  261. transaction.leave_transaction_management()
  262. class AtomicMiscTests(TransactionTestCase):
  263. def test_wrap_callable_instance(self):
  264. # Regression test for #20028
  265. class Callable(object):
  266. def __call__(self):
  267. pass
  268. # Must not raise an exception
  269. transaction.atomic(Callable())
  270. class IgnorePendingDeprecationWarningsMixin(object):
  271. def setUp(self):
  272. super(IgnorePendingDeprecationWarningsMixin, self).setUp()
  273. self.catch_warnings = warnings.catch_warnings()
  274. self.catch_warnings.__enter__()
  275. warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
  276. def tearDown(self):
  277. self.catch_warnings.__exit__(*sys.exc_info())
  278. super(IgnorePendingDeprecationWarningsMixin, self).tearDown()
  279. class TransactionTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
  280. def create_a_reporter_then_fail(self, first, last):
  281. a = Reporter(first_name=first, last_name=last)
  282. a.save()
  283. raise Exception("I meant to do that")
  284. def remove_a_reporter(self, first_name):
  285. r = Reporter.objects.get(first_name="Alice")
  286. r.delete()
  287. def manually_managed(self):
  288. r = Reporter(first_name="Dirk", last_name="Gently")
  289. r.save()
  290. transaction.commit()
  291. def manually_managed_mistake(self):
  292. r = Reporter(first_name="Edward", last_name="Woodward")
  293. r.save()
  294. # Oops, I forgot to commit/rollback!
  295. @skipUnlessDBFeature('supports_transactions')
  296. def test_autocommit(self):
  297. """
  298. The default behavior is to autocommit after each save() action.
  299. """
  300. self.assertRaises(Exception,
  301. self.create_a_reporter_then_fail,
  302. "Alice", "Smith"
  303. )
  304. # The object created before the exception still exists
  305. self.assertEqual(Reporter.objects.count(), 1)
  306. @skipUnlessDBFeature('supports_transactions')
  307. def test_autocommit_decorator(self):
  308. """
  309. The autocommit decorator works exactly the same as the default behavior.
  310. """
  311. autocomitted_create_then_fail = transaction.autocommit(
  312. self.create_a_reporter_then_fail
  313. )
  314. self.assertRaises(Exception,
  315. autocomitted_create_then_fail,
  316. "Alice", "Smith"
  317. )
  318. # Again, the object created before the exception still exists
  319. self.assertEqual(Reporter.objects.count(), 1)
  320. @skipUnlessDBFeature('supports_transactions')
  321. def test_autocommit_decorator_with_using(self):
  322. """
  323. The autocommit decorator also works with a using argument.
  324. """
  325. autocomitted_create_then_fail = transaction.autocommit(using='default')(
  326. self.create_a_reporter_then_fail
  327. )
  328. self.assertRaises(Exception,
  329. autocomitted_create_then_fail,
  330. "Alice", "Smith"
  331. )
  332. # Again, the object created before the exception still exists
  333. self.assertEqual(Reporter.objects.count(), 1)
  334. @skipUnlessDBFeature('supports_transactions')
  335. def test_commit_on_success(self):
  336. """
  337. With the commit_on_success decorator, the transaction is only committed
  338. if the function doesn't throw an exception.
  339. """
  340. committed_on_success = transaction.commit_on_success(
  341. self.create_a_reporter_then_fail)
  342. self.assertRaises(Exception, committed_on_success, "Dirk", "Gently")
  343. # This time the object never got saved
  344. self.assertEqual(Reporter.objects.count(), 0)
  345. @skipUnlessDBFeature('supports_transactions')
  346. def test_commit_on_success_with_using(self):
  347. """
  348. The commit_on_success decorator also works with a using argument.
  349. """
  350. using_committed_on_success = transaction.commit_on_success(using='default')(
  351. self.create_a_reporter_then_fail
  352. )
  353. self.assertRaises(Exception,
  354. using_committed_on_success,
  355. "Dirk", "Gently"
  356. )
  357. # This time the object never got saved
  358. self.assertEqual(Reporter.objects.count(), 0)
  359. @skipUnlessDBFeature('supports_transactions')
  360. def test_commit_on_success_succeed(self):
  361. """
  362. If there aren't any exceptions, the data will get saved.
  363. """
  364. Reporter.objects.create(first_name="Alice", last_name="Smith")
  365. remove_comitted_on_success = transaction.commit_on_success(
  366. self.remove_a_reporter
  367. )
  368. remove_comitted_on_success("Alice")
  369. self.assertEqual(list(Reporter.objects.all()), [])
  370. @skipUnlessDBFeature('supports_transactions')
  371. def test_commit_on_success_exit(self):
  372. @transaction.autocommit()
  373. def gen_reporter():
  374. @transaction.commit_on_success
  375. def create_reporter():
  376. Reporter.objects.create(first_name="Bobby", last_name="Tables")
  377. create_reporter()
  378. # Much more formal
  379. r = Reporter.objects.get()
  380. r.first_name = "Robert"
  381. r.save()
  382. gen_reporter()
  383. r = Reporter.objects.get()
  384. self.assertEqual(r.first_name, "Robert")
  385. @skipUnlessDBFeature('supports_transactions')
  386. def test_manually_managed(self):
  387. """
  388. You can manually manage transactions if you really want to, but you
  389. have to remember to commit/rollback.
  390. """
  391. manually_managed = transaction.commit_manually(self.manually_managed)
  392. manually_managed()
  393. self.assertEqual(Reporter.objects.count(), 1)
  394. @skipUnlessDBFeature('supports_transactions')
  395. def test_manually_managed_mistake(self):
  396. """
  397. If you forget, you'll get bad errors.
  398. """
  399. manually_managed_mistake = transaction.commit_manually(
  400. self.manually_managed_mistake
  401. )
  402. self.assertRaises(transaction.TransactionManagementError,
  403. manually_managed_mistake)
  404. @skipUnlessDBFeature('supports_transactions')
  405. def test_manually_managed_with_using(self):
  406. """
  407. The commit_manually function also works with a using argument.
  408. """
  409. using_manually_managed_mistake = transaction.commit_manually(using='default')(
  410. self.manually_managed_mistake
  411. )
  412. self.assertRaises(transaction.TransactionManagementError,
  413. using_manually_managed_mistake
  414. )
  415. class TransactionRollbackTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
  416. def execute_bad_sql(self):
  417. cursor = connection.cursor()
  418. cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
  419. @skipUnlessDBFeature('requires_rollback_on_dirty_transaction')
  420. def test_bad_sql(self):
  421. """
  422. Regression for #11900: If a function wrapped by commit_on_success
  423. writes a transaction that can't be committed, that transaction should
  424. be rolled back. The bug is only visible using the psycopg2 backend,
  425. though the fix is generally a good idea.
  426. """
  427. execute_bad_sql = transaction.commit_on_success(self.execute_bad_sql)
  428. self.assertRaises(IntegrityError, execute_bad_sql)
  429. transaction.rollback()
  430. class TransactionContextManagerTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
  431. def create_reporter_and_fail(self):
  432. Reporter.objects.create(first_name="Bob", last_name="Holtzman")
  433. raise Exception
  434. @skipUnlessDBFeature('supports_transactions')
  435. def test_autocommit(self):
  436. """
  437. The default behavior is to autocommit after each save() action.
  438. """
  439. with self.assertRaises(Exception):
  440. self.create_reporter_and_fail()
  441. # The object created before the exception still exists
  442. self.assertEqual(Reporter.objects.count(), 1)
  443. @skipUnlessDBFeature('supports_transactions')
  444. def test_autocommit_context_manager(self):
  445. """
  446. The autocommit context manager works exactly the same as the default
  447. behavior.
  448. """
  449. with self.assertRaises(Exception):
  450. with transaction.autocommit():
  451. self.create_reporter_and_fail()
  452. self.assertEqual(Reporter.objects.count(), 1)
  453. @skipUnlessDBFeature('supports_transactions')
  454. def test_autocommit_context_manager_with_using(self):
  455. """
  456. The autocommit context manager also works with a using argument.
  457. """
  458. with self.assertRaises(Exception):
  459. with transaction.autocommit(using="default"):
  460. self.create_reporter_and_fail()
  461. self.assertEqual(Reporter.objects.count(), 1)
  462. @skipUnlessDBFeature('supports_transactions')
  463. def test_commit_on_success(self):
  464. """
  465. With the commit_on_success context manager, the transaction is only
  466. committed if the block doesn't throw an exception.
  467. """
  468. with self.assertRaises(Exception):
  469. with transaction.commit_on_success():
  470. self.create_reporter_and_fail()
  471. self.assertEqual(Reporter.objects.count(), 0)
  472. @skipUnlessDBFeature('supports_transactions')
  473. def test_commit_on_success_with_using(self):
  474. """
  475. The commit_on_success context manager also works with a using argument.
  476. """
  477. with self.assertRaises(Exception):
  478. with transaction.commit_on_success(using="default"):
  479. self.create_reporter_and_fail()
  480. self.assertEqual(Reporter.objects.count(), 0)
  481. @skipUnlessDBFeature('supports_transactions')
  482. def test_commit_on_success_succeed(self):
  483. """
  484. If there aren't any exceptions, the data will get saved.
  485. """
  486. Reporter.objects.create(first_name="Alice", last_name="Smith")
  487. with transaction.commit_on_success():
  488. Reporter.objects.filter(first_name="Alice").delete()
  489. self.assertQuerysetEqual(Reporter.objects.all(), [])
  490. @skipUnlessDBFeature('supports_transactions')
  491. def test_commit_on_success_exit(self):
  492. with transaction.autocommit():
  493. with transaction.commit_on_success():
  494. Reporter.objects.create(first_name="Bobby", last_name="Tables")
  495. # Much more formal
  496. r = Reporter.objects.get()
  497. r.first_name = "Robert"
  498. r.save()
  499. r = Reporter.objects.get()
  500. self.assertEqual(r.first_name, "Robert")
  501. @skipUnlessDBFeature('supports_transactions')
  502. def test_manually_managed(self):
  503. """
  504. You can manually manage transactions if you really want to, but you
  505. have to remember to commit/rollback.
  506. """
  507. with transaction.commit_manually():
  508. Reporter.objects.create(first_name="Libby", last_name="Holtzman")
  509. transaction.commit()
  510. self.assertEqual(Reporter.objects.count(), 1)
  511. @skipUnlessDBFeature('supports_transactions')
  512. def test_manually_managed_mistake(self):
  513. """
  514. If you forget, you'll get bad errors.
  515. """
  516. with self.assertRaises(transaction.TransactionManagementError):
  517. with transaction.commit_manually():
  518. Reporter.objects.create(first_name="Scott", last_name="Browning")
  519. @skipUnlessDBFeature('supports_transactions')
  520. def test_manually_managed_with_using(self):
  521. """
  522. The commit_manually function also works with a using argument.
  523. """
  524. with self.assertRaises(transaction.TransactionManagementError):
  525. with transaction.commit_manually(using="default"):
  526. Reporter.objects.create(first_name="Walter", last_name="Cronkite")
  527. @skipUnlessDBFeature('requires_rollback_on_dirty_transaction')
  528. def test_bad_sql(self):
  529. """
  530. Regression for #11900: If a block wrapped by commit_on_success
  531. writes a transaction that can't be committed, that transaction should
  532. be rolled back. The bug is only visible using the psycopg2 backend,
  533. though the fix is generally a good idea.
  534. """
  535. with self.assertRaises(IntegrityError):
  536. with transaction.commit_on_success():
  537. cursor = connection.cursor()
  538. cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
  539. transaction.rollback()