Loading django/contrib/humanize/tests.py +12 −12 Original line number Diff line number Diff line Loading @@ -90,8 +90,8 @@ class HumanizeTests(TransRealMixin, TestCase): '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567', None) with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False), \ translation.override('en'): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False): with translation.override('en'): self.humanize_tester(test_list, result_list, 'intcomma') def test_intcomma_without_number_grouping(self): Loading @@ -116,8 +116,8 @@ class HumanizeTests(TransRealMixin, TestCase): '100', '1000', '10123', '10311', '1000000', None) result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25', '100', '1.000', '10.123', '10.311', '1.000.000', None) with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True), \ translation.override('de'): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with translation.override('de'): self.humanize_tester(test_list, result_list, 'intcomma') def test_i18n_intword(self): Loading @@ -125,8 +125,8 @@ class HumanizeTests(TransRealMixin, TestCase): '1000000000', '2000000000', '6000000000000') result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen', '1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen') with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True), \ translation.override('de'): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with translation.override('de'): self.humanize_tester(test_list, result_list, 'intword') def test_apnumber(self): Loading Loading @@ -174,8 +174,8 @@ class HumanizeTests(TransRealMixin, TestCase): orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime try: with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True), \ translation.override('en'): with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True): with translation.override('en'): self.humanize_tester([dt], ['yesterday'], 'naturalday') finally: humanize.datetime = orig_humanize_datetime Loading tests/i18n/tests.py +5 −5 Original line number Diff line number Diff line Loading @@ -745,8 +745,8 @@ class FormattingTests(TransRealMixin, TestCase): self.assertEqual('.', get_format('DECIMAL_SEPARATOR', lang='en')) def test_get_format_modules_stability(self): with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'), \ translation.override('de', deactivate=True): with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'): with translation.override('de', deactivate=True): old = str("%r") % get_format_modules(reverse=True) new = str("%r") % get_format_modules(reverse=True) # second try self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.') Loading tests/one_to_one_regress/tests.py +21 −14 Original line number Diff line number Diff line Loading @@ -141,9 +141,11 @@ class OneToOneRegressionTests(TestCase): """ p = Place(name='Zombie Cats', address='Not sure') p.save() with self.assertNumQueries(1), self.assertRaises(Restaurant.DoesNotExist): with self.assertNumQueries(1): with self.assertRaises(Restaurant.DoesNotExist): p.restaurant with self.assertNumQueries(0), self.assertRaises(Restaurant.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(Restaurant.DoesNotExist): p.restaurant def test_reverse_object_cached_when_related_is_accessed(self): Loading Loading @@ -197,7 +199,8 @@ class OneToOneRegressionTests(TestCase): self.assertEqual(self.p1.undergroundbar, b) b.place = None b.save() with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): self.p1.undergroundbar def test_get_reverse_on_unsaved_object(self): Loading @@ -210,20 +213,23 @@ class OneToOneRegressionTests(TestCase): p = Place() # When there's no instance of the origin of the one-to-one with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): p.undergroundbar UndergroundBar.objects.create() # When there's one instance of the origin # (p.undergroundbar used to return that instance) with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): p.undergroundbar UndergroundBar.objects.create() # When there are several instances of the origin with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): p.undergroundbar def test_set_reverse_on_unsaved_object(self): Loading @@ -233,7 +239,8 @@ class OneToOneRegressionTests(TestCase): """ p = Place() b = UndergroundBar.objects.create() with self.assertNumQueries(0), self.assertRaises(ValueError): with self.assertNumQueries(0): with self.assertRaises(ValueError): p.undergroundbar = b def test_nullable_o2o_delete(self): Loading tests/test_utils/tests.py +39 −32 Original line number Diff line number Diff line Loading @@ -152,7 +152,8 @@ class CaptureQueriesContextManagerTests(TestCase): self.assertEqual(2, len(captured_queries)) def test_failure(self): with self.assertRaises(TypeError), CaptureQueriesContext(connection): with self.assertRaises(TypeError): with CaptureQueriesContext(connection): raise TypeError def test_with_client(self): Loading Loading @@ -189,11 +190,13 @@ class AssertNumQueriesContextManagerTests(TestCase): Person.objects.count() def test_failure(self): with self.assertRaises(AssertionError) as exc_info, self.assertNumQueries(2): with self.assertRaises(AssertionError) as exc_info: with self.assertNumQueries(2): Person.objects.count() self.assertIn("1 queries executed, 2 expected", str(exc_info.exception)) with self.assertRaises(TypeError), self.assertNumQueries(4000): with self.assertRaises(TypeError): with self.assertNumQueries(4000): raise TypeError def test_with_client(self): Loading Loading @@ -229,12 +232,12 @@ class AssertTemplateUsedContextManagerTests(TestCase): render_to_string('template_used/base.html') def test_nested_usage(self): with self.assertTemplateUsed('template_used/base.html'), \ self.assertTemplateUsed('template_used/include.html'): with self.assertTemplateUsed('template_used/base.html'): with self.assertTemplateUsed('template_used/include.html'): render_to_string('template_used/include.html') with self.assertTemplateUsed('template_used/extends.html'), \ self.assertTemplateUsed('template_used/base.html'): with self.assertTemplateUsed('template_used/extends.html'): with self.assertTemplateUsed('template_used/base.html'): render_to_string('template_used/extends.html') with self.assertTemplateUsed('template_used/base.html'): Loading @@ -255,33 +258,37 @@ class AssertTemplateUsedContextManagerTests(TestCase): pass def test_error_message(self): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'), \ self.assertTemplateUsed('template_used/base.html'): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'): with self.assertTemplateUsed('template_used/base.html'): pass with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'), \ self.assertTemplateUsed(template_name='template_used/base.html'): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'): with self.assertTemplateUsed(template_name='template_used/base.html'): pass with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html.*template_used/alternative\.html$'), \ self.assertTemplateUsed('template_used/base.html'): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html.*template_used/alternative\.html$'): with self.assertTemplateUsed('template_used/base.html'): render_to_string('template_used/alternative.html') def test_failure(self): with self.assertRaises(TypeError), self.assertTemplateUsed(): with self.assertRaises(TypeError): with self.assertTemplateUsed(): pass with self.assertRaises(AssertionError), self.assertTemplateUsed(''): with self.assertRaises(AssertionError): with self.assertTemplateUsed(''): pass with self.assertRaises(AssertionError), self.assertTemplateUsed(''): with self.assertRaises(AssertionError): with self.assertTemplateUsed(''): render_to_string('template_used/base.html') with self.assertRaises(AssertionError), self.assertTemplateUsed(template_name=''): with self.assertRaises(AssertionError): with self.assertTemplateUsed(template_name=''): pass with self.assertRaises(AssertionError), \ self.assertTemplateUsed('template_used/base.html'): with self.assertRaises(AssertionError): with self.assertTemplateUsed('template_used/base.html'): render_to_string('template_used/alternative.html') Loading tests/transactions/tests.py +97 −82 Original line number Diff line number Diff line Loading @@ -65,7 +65,8 @@ class AtomicTests(TransactionTestCase): self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) def test_rollback(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") self.assertQuerysetEqual(Reporter.objects.all(), []) Loading @@ -81,13 +82,15 @@ class AtomicTests(TransactionTestCase): def test_nested_commit_rollback(self): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) def test_nested_rollback_commit(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with transaction.atomic(): Reporter.objects.create(last_name="Haddock") Loading @@ -95,7 +98,8 @@ class AtomicTests(TransactionTestCase): self.assertQuerysetEqual(Reporter.objects.all(), []) def test_nested_rollback_rollback(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Loading @@ -115,15 +119,16 @@ class AtomicTests(TransactionTestCase): def test_merged_commit_rollback(self): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") # Writes in the outer block are rolled back too. self.assertQuerysetEqual(Reporter.objects.all(), []) def test_merged_rollback_commit(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with transaction.atomic(savepoint=False): Reporter.objects.create(last_name="Haddock") Loading @@ -131,7 +136,8 @@ class AtomicTests(TransactionTestCase): self.assertQuerysetEqual(Reporter.objects.all(), []) def test_merged_rollback_rollback(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Loading @@ -153,14 +159,16 @@ class AtomicTests(TransactionTestCase): atomic = transaction.atomic() with atomic: Reporter.objects.create(first_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"), atomic: with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) def test_reuse_rollback_commit(self): atomic = transaction.atomic() with six.assertRaisesRegex(self, Exception, "Oops"), atomic: with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Reporter.objects.create(last_name="Tintin") with atomic: Reporter.objects.create(last_name="Haddock") Loading @@ -169,7 +177,8 @@ class AtomicTests(TransactionTestCase): def test_reuse_rollback_rollback(self): atomic = transaction.atomic() with six.assertRaisesRegex(self, Exception, "Oops"), atomic: with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Reporter.objects.create(last_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Loading @@ -191,7 +200,8 @@ class AtomicTests(TransactionTestCase): Reporter.objects.create(first_name="Tintin") sid = transaction.savepoint() # trigger a database error inside an inner atomic without savepoint with self.assertRaises(DatabaseError), transaction.atomic(savepoint=False): with self.assertRaises(DatabaseError): with transaction.atomic(savepoint=False): connection.cursor().execute( "SELECT no_such_col FROM transactions_reporter") transaction.savepoint_rollback(sid) Loading Loading @@ -253,8 +263,8 @@ class AtomicMergeTests(TransactionTestCase): Reporter.objects.create(first_name="Tintin") with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Archibald", last_name="Haddock") with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Tournesol") raise Exception("Oops, that's his last name") # It wasn't possible to roll back Loading @@ -269,8 +279,8 @@ class AtomicMergeTests(TransactionTestCase): Reporter.objects.create(first_name="Tintin") with transaction.atomic(): Reporter.objects.create(first_name="Archibald", last_name="Haddock") with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Tournesol") raise Exception("Oops, that's his last name") # It wasn't possible to roll back Loading @@ -283,8 +293,8 @@ class AtomicMergeTests(TransactionTestCase): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") # Inner block without a savepoint fails with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") # It wasn't possible to roll back Loading @@ -306,8 +316,8 @@ class AtomicErrorsTests(TransactionTestCase): def test_atomic_prevents_setting_autocommit(self): autocommit = transaction.get_autocommit() with transaction.atomic(), \ self.assertRaises(transaction.TransactionManagementError): with transaction.atomic(): with self.assertRaises(transaction.TransactionManagementError): transaction.set_autocommit(not autocommit) # Make sure autocommit wasn't changed. self.assertEqual(connection.autocommit, autocommit) Loading Loading @@ -542,7 +552,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction The autocommit context manager works exactly the same as the default behavior. """ with self.assertRaises(Exception), transaction.autocommit(): with self.assertRaises(Exception): with transaction.autocommit(): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1) Loading @@ -552,7 +563,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ The autocommit context manager also works with a using argument. """ with self.assertRaises(Exception), transaction.autocommit(using="default"): with self.assertRaises(Exception): with transaction.autocommit(using="default"): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1) Loading @@ -563,7 +575,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction With the commit_on_success context manager, the transaction is only committed if the block doesn't throw an exception. """ with self.assertRaises(Exception), transaction.commit_on_success(): with self.assertRaises(Exception): with transaction.commit_on_success(): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 0) Loading @@ -573,7 +586,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ The commit_on_success context manager also works with a using argument. """ with self.assertRaises(Exception), transaction.commit_on_success(using="default"): with self.assertRaises(Exception): with transaction.commit_on_success(using="default"): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 0) Loading Loading @@ -619,8 +633,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ If you forget, you'll get bad errors. """ with self.assertRaises(transaction.TransactionManagementError), \ transaction.commit_manually(): with self.assertRaises(transaction.TransactionManagementError): with transaction.commit_manually(): Reporter.objects.create(first_name="Scott", last_name="Browning") @skipUnlessDBFeature('supports_transactions') Loading @@ -628,8 +642,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ The commit_manually function also works with a using argument. """ with self.assertRaises(transaction.TransactionManagementError), \ transaction.commit_manually(using="default"): with self.assertRaises(transaction.TransactionManagementError): with transaction.commit_manually(using="default"): Reporter.objects.create(first_name="Walter", last_name="Cronkite") @skipUnlessDBFeature('requires_rollback_on_dirty_transaction') Loading @@ -640,7 +654,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction be rolled back. The bug is only visible using the psycopg2 backend, though the fix is generally a good idea. """ with self.assertRaises(IntegrityError), transaction.commit_on_success(): with self.assertRaises(IntegrityError): with transaction.commit_on_success(): cursor = connection.cursor() cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');") transaction.rollback() Loading
django/contrib/humanize/tests.py +12 −12 Original line number Diff line number Diff line Loading @@ -90,8 +90,8 @@ class HumanizeTests(TransRealMixin, TestCase): '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567', None) with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False), \ translation.override('en'): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False): with translation.override('en'): self.humanize_tester(test_list, result_list, 'intcomma') def test_intcomma_without_number_grouping(self): Loading @@ -116,8 +116,8 @@ class HumanizeTests(TransRealMixin, TestCase): '100', '1000', '10123', '10311', '1000000', None) result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25', '100', '1.000', '10.123', '10.311', '1.000.000', None) with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True), \ translation.override('de'): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with translation.override('de'): self.humanize_tester(test_list, result_list, 'intcomma') def test_i18n_intword(self): Loading @@ -125,8 +125,8 @@ class HumanizeTests(TransRealMixin, TestCase): '1000000000', '2000000000', '6000000000000') result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen', '1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen') with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True), \ translation.override('de'): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with translation.override('de'): self.humanize_tester(test_list, result_list, 'intword') def test_apnumber(self): Loading Loading @@ -174,8 +174,8 @@ class HumanizeTests(TransRealMixin, TestCase): orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime try: with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True), \ translation.override('en'): with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True): with translation.override('en'): self.humanize_tester([dt], ['yesterday'], 'naturalday') finally: humanize.datetime = orig_humanize_datetime Loading
tests/i18n/tests.py +5 −5 Original line number Diff line number Diff line Loading @@ -745,8 +745,8 @@ class FormattingTests(TransRealMixin, TestCase): self.assertEqual('.', get_format('DECIMAL_SEPARATOR', lang='en')) def test_get_format_modules_stability(self): with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'), \ translation.override('de', deactivate=True): with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'): with translation.override('de', deactivate=True): old = str("%r") % get_format_modules(reverse=True) new = str("%r") % get_format_modules(reverse=True) # second try self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.') Loading
tests/one_to_one_regress/tests.py +21 −14 Original line number Diff line number Diff line Loading @@ -141,9 +141,11 @@ class OneToOneRegressionTests(TestCase): """ p = Place(name='Zombie Cats', address='Not sure') p.save() with self.assertNumQueries(1), self.assertRaises(Restaurant.DoesNotExist): with self.assertNumQueries(1): with self.assertRaises(Restaurant.DoesNotExist): p.restaurant with self.assertNumQueries(0), self.assertRaises(Restaurant.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(Restaurant.DoesNotExist): p.restaurant def test_reverse_object_cached_when_related_is_accessed(self): Loading Loading @@ -197,7 +199,8 @@ class OneToOneRegressionTests(TestCase): self.assertEqual(self.p1.undergroundbar, b) b.place = None b.save() with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): self.p1.undergroundbar def test_get_reverse_on_unsaved_object(self): Loading @@ -210,20 +213,23 @@ class OneToOneRegressionTests(TestCase): p = Place() # When there's no instance of the origin of the one-to-one with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): p.undergroundbar UndergroundBar.objects.create() # When there's one instance of the origin # (p.undergroundbar used to return that instance) with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): p.undergroundbar UndergroundBar.objects.create() # When there are several instances of the origin with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist): with self.assertNumQueries(0): with self.assertRaises(UndergroundBar.DoesNotExist): p.undergroundbar def test_set_reverse_on_unsaved_object(self): Loading @@ -233,7 +239,8 @@ class OneToOneRegressionTests(TestCase): """ p = Place() b = UndergroundBar.objects.create() with self.assertNumQueries(0), self.assertRaises(ValueError): with self.assertNumQueries(0): with self.assertRaises(ValueError): p.undergroundbar = b def test_nullable_o2o_delete(self): Loading
tests/test_utils/tests.py +39 −32 Original line number Diff line number Diff line Loading @@ -152,7 +152,8 @@ class CaptureQueriesContextManagerTests(TestCase): self.assertEqual(2, len(captured_queries)) def test_failure(self): with self.assertRaises(TypeError), CaptureQueriesContext(connection): with self.assertRaises(TypeError): with CaptureQueriesContext(connection): raise TypeError def test_with_client(self): Loading Loading @@ -189,11 +190,13 @@ class AssertNumQueriesContextManagerTests(TestCase): Person.objects.count() def test_failure(self): with self.assertRaises(AssertionError) as exc_info, self.assertNumQueries(2): with self.assertRaises(AssertionError) as exc_info: with self.assertNumQueries(2): Person.objects.count() self.assertIn("1 queries executed, 2 expected", str(exc_info.exception)) with self.assertRaises(TypeError), self.assertNumQueries(4000): with self.assertRaises(TypeError): with self.assertNumQueries(4000): raise TypeError def test_with_client(self): Loading Loading @@ -229,12 +232,12 @@ class AssertTemplateUsedContextManagerTests(TestCase): render_to_string('template_used/base.html') def test_nested_usage(self): with self.assertTemplateUsed('template_used/base.html'), \ self.assertTemplateUsed('template_used/include.html'): with self.assertTemplateUsed('template_used/base.html'): with self.assertTemplateUsed('template_used/include.html'): render_to_string('template_used/include.html') with self.assertTemplateUsed('template_used/extends.html'), \ self.assertTemplateUsed('template_used/base.html'): with self.assertTemplateUsed('template_used/extends.html'): with self.assertTemplateUsed('template_used/base.html'): render_to_string('template_used/extends.html') with self.assertTemplateUsed('template_used/base.html'): Loading @@ -255,33 +258,37 @@ class AssertTemplateUsedContextManagerTests(TestCase): pass def test_error_message(self): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'), \ self.assertTemplateUsed('template_used/base.html'): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'): with self.assertTemplateUsed('template_used/base.html'): pass with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'), \ self.assertTemplateUsed(template_name='template_used/base.html'): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'): with self.assertTemplateUsed(template_name='template_used/base.html'): pass with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html.*template_used/alternative\.html$'), \ self.assertTemplateUsed('template_used/base.html'): with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html.*template_used/alternative\.html$'): with self.assertTemplateUsed('template_used/base.html'): render_to_string('template_used/alternative.html') def test_failure(self): with self.assertRaises(TypeError), self.assertTemplateUsed(): with self.assertRaises(TypeError): with self.assertTemplateUsed(): pass with self.assertRaises(AssertionError), self.assertTemplateUsed(''): with self.assertRaises(AssertionError): with self.assertTemplateUsed(''): pass with self.assertRaises(AssertionError), self.assertTemplateUsed(''): with self.assertRaises(AssertionError): with self.assertTemplateUsed(''): render_to_string('template_used/base.html') with self.assertRaises(AssertionError), self.assertTemplateUsed(template_name=''): with self.assertRaises(AssertionError): with self.assertTemplateUsed(template_name=''): pass with self.assertRaises(AssertionError), \ self.assertTemplateUsed('template_used/base.html'): with self.assertRaises(AssertionError): with self.assertTemplateUsed('template_used/base.html'): render_to_string('template_used/alternative.html') Loading
tests/transactions/tests.py +97 −82 Original line number Diff line number Diff line Loading @@ -65,7 +65,8 @@ class AtomicTests(TransactionTestCase): self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) def test_rollback(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") self.assertQuerysetEqual(Reporter.objects.all(), []) Loading @@ -81,13 +82,15 @@ class AtomicTests(TransactionTestCase): def test_nested_commit_rollback(self): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) def test_nested_rollback_commit(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with transaction.atomic(): Reporter.objects.create(last_name="Haddock") Loading @@ -95,7 +98,8 @@ class AtomicTests(TransactionTestCase): self.assertQuerysetEqual(Reporter.objects.all(), []) def test_nested_rollback_rollback(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Loading @@ -115,15 +119,16 @@ class AtomicTests(TransactionTestCase): def test_merged_commit_rollback(self): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") # Writes in the outer block are rolled back too. self.assertQuerysetEqual(Reporter.objects.all(), []) def test_merged_rollback_commit(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with transaction.atomic(savepoint=False): Reporter.objects.create(last_name="Haddock") Loading @@ -131,7 +136,8 @@ class AtomicTests(TransactionTestCase): self.assertQuerysetEqual(Reporter.objects.all(), []) def test_merged_rollback_rollback(self): with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic(): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(): Reporter.objects.create(last_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Loading @@ -153,14 +159,16 @@ class AtomicTests(TransactionTestCase): atomic = transaction.atomic() with atomic: Reporter.objects.create(first_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"), atomic: with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) def test_reuse_rollback_commit(self): atomic = transaction.atomic() with six.assertRaisesRegex(self, Exception, "Oops"), atomic: with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Reporter.objects.create(last_name="Tintin") with atomic: Reporter.objects.create(last_name="Haddock") Loading @@ -169,7 +177,8 @@ class AtomicTests(TransactionTestCase): def test_reuse_rollback_rollback(self): atomic = transaction.atomic() with six.assertRaisesRegex(self, Exception, "Oops"), atomic: with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Reporter.objects.create(last_name="Tintin") with six.assertRaisesRegex(self, Exception, "Oops"): with atomic: Loading @@ -191,7 +200,8 @@ class AtomicTests(TransactionTestCase): Reporter.objects.create(first_name="Tintin") sid = transaction.savepoint() # trigger a database error inside an inner atomic without savepoint with self.assertRaises(DatabaseError), transaction.atomic(savepoint=False): with self.assertRaises(DatabaseError): with transaction.atomic(savepoint=False): connection.cursor().execute( "SELECT no_such_col FROM transactions_reporter") transaction.savepoint_rollback(sid) Loading Loading @@ -253,8 +263,8 @@ class AtomicMergeTests(TransactionTestCase): Reporter.objects.create(first_name="Tintin") with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Archibald", last_name="Haddock") with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Tournesol") raise Exception("Oops, that's his last name") # It wasn't possible to roll back Loading @@ -269,8 +279,8 @@ class AtomicMergeTests(TransactionTestCase): Reporter.objects.create(first_name="Tintin") with transaction.atomic(): Reporter.objects.create(first_name="Archibald", last_name="Haddock") with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Tournesol") raise Exception("Oops, that's his last name") # It wasn't possible to roll back Loading @@ -283,8 +293,8 @@ class AtomicMergeTests(TransactionTestCase): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") # Inner block without a savepoint fails with six.assertRaisesRegex(self, Exception, "Oops"), \ transaction.atomic(savepoint=False): with six.assertRaisesRegex(self, Exception, "Oops"): with transaction.atomic(savepoint=False): Reporter.objects.create(first_name="Haddock") raise Exception("Oops, that's his last name") # It wasn't possible to roll back Loading @@ -306,8 +316,8 @@ class AtomicErrorsTests(TransactionTestCase): def test_atomic_prevents_setting_autocommit(self): autocommit = transaction.get_autocommit() with transaction.atomic(), \ self.assertRaises(transaction.TransactionManagementError): with transaction.atomic(): with self.assertRaises(transaction.TransactionManagementError): transaction.set_autocommit(not autocommit) # Make sure autocommit wasn't changed. self.assertEqual(connection.autocommit, autocommit) Loading Loading @@ -542,7 +552,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction The autocommit context manager works exactly the same as the default behavior. """ with self.assertRaises(Exception), transaction.autocommit(): with self.assertRaises(Exception): with transaction.autocommit(): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1) Loading @@ -552,7 +563,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ The autocommit context manager also works with a using argument. """ with self.assertRaises(Exception), transaction.autocommit(using="default"): with self.assertRaises(Exception): with transaction.autocommit(using="default"): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1) Loading @@ -563,7 +575,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction With the commit_on_success context manager, the transaction is only committed if the block doesn't throw an exception. """ with self.assertRaises(Exception), transaction.commit_on_success(): with self.assertRaises(Exception): with transaction.commit_on_success(): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 0) Loading @@ -573,7 +586,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ The commit_on_success context manager also works with a using argument. """ with self.assertRaises(Exception), transaction.commit_on_success(using="default"): with self.assertRaises(Exception): with transaction.commit_on_success(using="default"): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 0) Loading Loading @@ -619,8 +633,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ If you forget, you'll get bad errors. """ with self.assertRaises(transaction.TransactionManagementError), \ transaction.commit_manually(): with self.assertRaises(transaction.TransactionManagementError): with transaction.commit_manually(): Reporter.objects.create(first_name="Scott", last_name="Browning") @skipUnlessDBFeature('supports_transactions') Loading @@ -628,8 +642,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction """ The commit_manually function also works with a using argument. """ with self.assertRaises(transaction.TransactionManagementError), \ transaction.commit_manually(using="default"): with self.assertRaises(transaction.TransactionManagementError): with transaction.commit_manually(using="default"): Reporter.objects.create(first_name="Walter", last_name="Cronkite") @skipUnlessDBFeature('requires_rollback_on_dirty_transaction') Loading @@ -640,7 +654,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction be rolled back. The bug is only visible using the psycopg2 backend, though the fix is generally a good idea. """ with self.assertRaises(IntegrityError), transaction.commit_on_success(): with self.assertRaises(IntegrityError): with transaction.commit_on_success(): cursor = connection.cursor() cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');") transaction.rollback()