PageRenderTime 36ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/transactions/tests_25.py

https://code.google.com/p/mango-py/
Python | 137 lines | 106 code | 10 blank | 21 comment | 5 complexity | e2e001f9a0b6371af0fc6cdaae6f683b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from __future__ import with_statement
  2. from django.db import connection, transaction, IntegrityError
  3. from django.test import TransactionTestCase, skipUnlessDBFeature
  4. from models import Reporter
  5. class TransactionContextManagerTests(TransactionTestCase):
  6. def create_reporter_and_fail(self):
  7. Reporter.objects.create(first_name="Bob", last_name="Holtzman")
  8. raise Exception
  9. @skipUnlessDBFeature('supports_transactions')
  10. def test_autocommit(self):
  11. """
  12. The default behavior is to autocommit after each save() action.
  13. """
  14. with self.assertRaises(Exception):
  15. self.create_reporter_and_fail()
  16. # The object created before the exception still exists
  17. self.assertEqual(Reporter.objects.count(), 1)
  18. @skipUnlessDBFeature('supports_transactions')
  19. def test_autocommit_context_manager(self):
  20. """
  21. The autocommit context manager works exactly the same as the default
  22. behavior.
  23. """
  24. with self.assertRaises(Exception):
  25. with transaction.autocommit():
  26. self.create_reporter_and_fail()
  27. self.assertEqual(Reporter.objects.count(), 1)
  28. @skipUnlessDBFeature('supports_transactions')
  29. def test_autocommit_context_manager_with_using(self):
  30. """
  31. The autocommit context manager also works with a using argument.
  32. """
  33. with self.assertRaises(Exception):
  34. with transaction.autocommit(using="default"):
  35. self.create_reporter_and_fail()
  36. self.assertEqual(Reporter.objects.count(), 1)
  37. @skipUnlessDBFeature('supports_transactions')
  38. def test_commit_on_success(self):
  39. """
  40. With the commit_on_success context manager, the transaction is only
  41. committed if the block doesn't throw an exception.
  42. """
  43. with self.assertRaises(Exception):
  44. with transaction.commit_on_success():
  45. self.create_reporter_and_fail()
  46. self.assertEqual(Reporter.objects.count(), 0)
  47. @skipUnlessDBFeature('supports_transactions')
  48. def test_commit_on_success_with_using(self):
  49. """
  50. The commit_on_success context manager also works with a using argument.
  51. """
  52. with self.assertRaises(Exception):
  53. with transaction.commit_on_success(using="default"):
  54. self.create_reporter_and_fail()
  55. self.assertEqual(Reporter.objects.count(), 0)
  56. @skipUnlessDBFeature('supports_transactions')
  57. def test_commit_on_success_succeed(self):
  58. """
  59. If there aren't any exceptions, the data will get saved.
  60. """
  61. Reporter.objects.create(first_name="Alice", last_name="Smith")
  62. with transaction.commit_on_success():
  63. Reporter.objects.filter(first_name="Alice").delete()
  64. self.assertQuerysetEqual(Reporter.objects.all(), [])
  65. @skipUnlessDBFeature('supports_transactions')
  66. def test_commit_on_success_exit(self):
  67. with transaction.autocommit():
  68. with transaction.commit_on_success():
  69. Reporter.objects.create(first_name="Bobby", last_name="Tables")
  70. # Much more formal
  71. r = Reporter.objects.get()
  72. r.first_name = "Robert"
  73. r.save()
  74. r = Reporter.objects.get()
  75. self.assertEqual(r.first_name, "Robert")
  76. @skipUnlessDBFeature('supports_transactions')
  77. def test_manually_managed(self):
  78. """
  79. You can manually manage transactions if you really want to, but you
  80. have to remember to commit/rollback.
  81. """
  82. with transaction.commit_manually():
  83. Reporter.objects.create(first_name="Libby", last_name="Holtzman")
  84. transaction.commit()
  85. self.assertEqual(Reporter.objects.count(), 1)
  86. @skipUnlessDBFeature('supports_transactions')
  87. def test_manually_managed_mistake(self):
  88. """
  89. If you forget, you'll get bad errors.
  90. """
  91. with self.assertRaises(transaction.TransactionManagementError):
  92. with transaction.commit_manually():
  93. Reporter.objects.create(first_name="Scott", last_name="Browning")
  94. @skipUnlessDBFeature('supports_transactions')
  95. def test_manually_managed_with_using(self):
  96. """
  97. The commit_manually function also works with a using argument.
  98. """
  99. with self.assertRaises(transaction.TransactionManagementError):
  100. with transaction.commit_manually(using="default"):
  101. Reporter.objects.create(first_name="Walter", last_name="Cronkite")
  102. @skipUnlessDBFeature('requires_rollback_on_dirty_transaction')
  103. def test_bad_sql(self):
  104. """
  105. Regression for #11900: If a block wrapped by commit_on_success
  106. writes a transaction that can't be committed, that transaction should
  107. be rolled back. The bug is only visible using the psycopg2 backend,
  108. though the fix is generally a good idea.
  109. """
  110. with self.assertRaises(IntegrityError):
  111. with transaction.commit_on_success():
  112. cursor = connection.cursor()
  113. cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
  114. transaction.set_dirty()
  115. transaction.rollback()