Combine consecutive with statements
Python 2.7 allows to combine several 'with' instructions.
This commit is contained in:
parent
22c6497f99
commit
165f44aaaa
|
@ -121,17 +121,16 @@ class AuthenticationFormTest(TestCase):
|
||||||
[force_text(form.error_messages['inactive'])])
|
[force_text(form.error_messages['inactive'])])
|
||||||
|
|
||||||
def test_inactive_user_i18n(self):
|
def test_inactive_user_i18n(self):
|
||||||
with self.settings(USE_I18N=True):
|
with self.settings(USE_I18N=True), translation.override('pt-br', deactivate=True):
|
||||||
with translation.override('pt-br', deactivate=True):
|
# The user is inactive.
|
||||||
# The user is inactive.
|
data = {
|
||||||
data = {
|
'username': 'inactive',
|
||||||
'username': 'inactive',
|
'password': 'password',
|
||||||
'password': 'password',
|
}
|
||||||
}
|
form = AuthenticationForm(None, data)
|
||||||
form = AuthenticationForm(None, data)
|
self.assertFalse(form.is_valid())
|
||||||
self.assertFalse(form.is_valid())
|
self.assertEqual(form.non_field_errors(),
|
||||||
self.assertEqual(form.non_field_errors(),
|
[force_text(form.error_messages['inactive'])])
|
||||||
[force_text(form.error_messages['inactive'])])
|
|
||||||
|
|
||||||
def test_custom_login_allowed_policy(self):
|
def test_custom_login_allowed_policy(self):
|
||||||
# The user is inactive, but our custom form policy allows him to log in.
|
# The user is inactive, but our custom form policy allows him to log in.
|
||||||
|
|
|
@ -77,15 +77,14 @@ class HumanizeTests(TransRealMixin, TestCase):
|
||||||
'100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567',
|
'100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567',
|
||||||
None)
|
None)
|
||||||
|
|
||||||
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False):
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False), \
|
||||||
with translation.override('en'):
|
translation.override('en'):
|
||||||
self.humanize_tester(test_list, result_list, 'intcomma')
|
self.humanize_tester(test_list, result_list, 'intcomma')
|
||||||
|
|
||||||
def test_intcomma_without_number_grouping(self):
|
def test_intcomma_without_number_grouping(self):
|
||||||
# Regression for #17414
|
# Regression for #17414
|
||||||
with translation.override('ja'):
|
with translation.override('ja'), self.settings(USE_L10N=True):
|
||||||
with self.settings(USE_L10N=True):
|
self.humanize_tester([100], ['100'], 'intcomma')
|
||||||
self.humanize_tester([100], ['100'], 'intcomma')
|
|
||||||
|
|
||||||
def test_intword(self):
|
def test_intword(self):
|
||||||
test_list = ('100', '1000000', '1200000', '1290000',
|
test_list = ('100', '1000000', '1200000', '1290000',
|
||||||
|
@ -104,18 +103,18 @@ class HumanizeTests(TransRealMixin, TestCase):
|
||||||
'100', '1000', '10123', '10311', '1000000', None)
|
'100', '1000', '10123', '10311', '1000000', None)
|
||||||
result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25',
|
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)
|
'100', '1.000', '10.123', '10.311', '1.000.000', None)
|
||||||
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True), \
|
||||||
with translation.override('de'):
|
translation.override('de'):
|
||||||
self.humanize_tester(test_list, result_list, 'intcomma')
|
self.humanize_tester(test_list, result_list, 'intcomma')
|
||||||
|
|
||||||
def test_i18n_intword(self):
|
def test_i18n_intword(self):
|
||||||
test_list = ('100', '1000000', '1200000', '1290000',
|
test_list = ('100', '1000000', '1200000', '1290000',
|
||||||
'1000000000', '2000000000', '6000000000000')
|
'1000000000', '2000000000', '6000000000000')
|
||||||
result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen',
|
result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen',
|
||||||
'1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen')
|
'1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen')
|
||||||
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True), \
|
||||||
with translation.override('de'):
|
translation.override('de'):
|
||||||
self.humanize_tester(test_list, result_list, 'intword')
|
self.humanize_tester(test_list, result_list, 'intword')
|
||||||
|
|
||||||
def test_apnumber(self):
|
def test_apnumber(self):
|
||||||
test_list = [str(x) for x in range(1, 11)]
|
test_list = [str(x) for x in range(1, 11)]
|
||||||
|
@ -162,9 +161,9 @@ class HumanizeTests(TransRealMixin, TestCase):
|
||||||
|
|
||||||
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
||||||
try:
|
try:
|
||||||
with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True):
|
with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True), \
|
||||||
with translation.override('en'):
|
translation.override('en'):
|
||||||
self.humanize_tester([dt], ['yesterday'], 'naturalday')
|
self.humanize_tester([dt], ['yesterday'], 'naturalday')
|
||||||
finally:
|
finally:
|
||||||
humanize.datetime = orig_humanize_datetime
|
humanize.datetime = orig_humanize_datetime
|
||||||
|
|
||||||
|
|
|
@ -524,31 +524,28 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
|
||||||
if the default language is non-English but the selected language
|
if the default language is non-English but the selected language
|
||||||
is English. See #13388 and #3594 for more details.
|
is English. See #13388 and #3594 for more details.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='fr'):
|
with self.settings(LANGUAGE_CODE='fr'), translation.override('en-us'):
|
||||||
with translation.override('en-us'):
|
response = self.client.get('/test_admin/admin/jsi18n/')
|
||||||
response = self.client.get('/test_admin/admin/jsi18n/')
|
self.assertNotContains(response, 'Choisir une heure')
|
||||||
self.assertNotContains(response, 'Choisir une heure')
|
|
||||||
|
|
||||||
def testI18NLanguageNonEnglishFallback(self):
|
def testI18NLanguageNonEnglishFallback(self):
|
||||||
"""
|
"""
|
||||||
Makes sure that the fallback language is still working properly
|
Makes sure that the fallback language is still working properly
|
||||||
in cases where the selected language cannot be found.
|
in cases where the selected language cannot be found.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='fr'):
|
with self.settings(LANGUAGE_CODE='fr'), translation.override('none'):
|
||||||
with translation.override('none'):
|
response = self.client.get('/test_admin/admin/jsi18n/')
|
||||||
response = self.client.get('/test_admin/admin/jsi18n/')
|
self.assertContains(response, 'Choisir une heure')
|
||||||
self.assertContains(response, 'Choisir une heure')
|
|
||||||
|
|
||||||
def testL10NDeactivated(self):
|
def testL10NDeactivated(self):
|
||||||
"""
|
"""
|
||||||
Check if L10N is deactivated, the JavaScript i18n view doesn't
|
Check if L10N is deactivated, the JavaScript i18n view doesn't
|
||||||
return localized date/time formats. Refs #14824.
|
return localized date/time formats. Refs #14824.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='ru', USE_L10N=False):
|
with self.settings(LANGUAGE_CODE='ru', USE_L10N=False), translation.override('none'):
|
||||||
with translation.override('none'):
|
response = self.client.get('/test_admin/admin/jsi18n/')
|
||||||
response = self.client.get('/test_admin/admin/jsi18n/')
|
self.assertNotContains(response, '%d.%m.%Y %H:%M:%S')
|
||||||
self.assertNotContains(response, '%d.%m.%Y %H:%M:%S')
|
self.assertContains(response, '%Y-%m-%d %H:%M:%S')
|
||||||
self.assertContains(response, '%Y-%m-%d %H:%M:%S')
|
|
||||||
|
|
||||||
def test_disallowed_filtering(self):
|
def test_disallowed_filtering(self):
|
||||||
with patch_logger('django.security.DisallowedModelAdminLookup', 'error') as calls:
|
with patch_logger('django.security.DisallowedModelAdminLookup', 'error') as calls:
|
||||||
|
|
|
@ -295,13 +295,12 @@ class AdminSplitDateTimeWidgetTest(DjangoTestCase):
|
||||||
def test_localization(self):
|
def test_localization(self):
|
||||||
w = widgets.AdminSplitDateTime()
|
w = widgets.AdminSplitDateTime()
|
||||||
|
|
||||||
with self.settings(USE_L10N=True):
|
with self.settings(USE_L10N=True), translation.override('de-at'):
|
||||||
with translation.override('de-at'):
|
w.is_localized = True
|
||||||
w.is_localized = True
|
self.assertHTMLEqual(
|
||||||
self.assertHTMLEqual(
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
||||||
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
'<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
|
||||||
'<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class AdminURLWidgetTest(DjangoTestCase):
|
class AdminURLWidgetTest(DjangoTestCase):
|
||||||
|
|
|
@ -647,22 +647,21 @@ class DefaultFiltersI18NTests(TransRealMixin, TestCase):
|
||||||
|
|
||||||
def test_localized_filesizeformat(self):
|
def test_localized_filesizeformat(self):
|
||||||
# NOTE: \xa0 avoids wrapping between value and unit
|
# NOTE: \xa0 avoids wrapping between value and unit
|
||||||
with self.settings(USE_L10N=True):
|
with self.settings(USE_L10N=True), translation.override('de', deactivate=True):
|
||||||
with translation.override('de', deactivate=True):
|
self.assertEqual(filesizeformat(1023), '1023\xa0Bytes')
|
||||||
self.assertEqual(filesizeformat(1023), '1023\xa0Bytes')
|
self.assertEqual(filesizeformat(1024), '1,0\xa0KB')
|
||||||
self.assertEqual(filesizeformat(1024), '1,0\xa0KB')
|
self.assertEqual(filesizeformat(10*1024), '10,0\xa0KB')
|
||||||
self.assertEqual(filesizeformat(10*1024), '10,0\xa0KB')
|
self.assertEqual(filesizeformat(1024*1024-1), '1024,0\xa0KB')
|
||||||
self.assertEqual(filesizeformat(1024*1024-1), '1024,0\xa0KB')
|
self.assertEqual(filesizeformat(1024*1024), '1,0\xa0MB')
|
||||||
self.assertEqual(filesizeformat(1024*1024), '1,0\xa0MB')
|
self.assertEqual(filesizeformat(1024*1024*50), '50,0\xa0MB')
|
||||||
self.assertEqual(filesizeformat(1024*1024*50), '50,0\xa0MB')
|
self.assertEqual(filesizeformat(1024*1024*1024-1), '1024,0\xa0MB')
|
||||||
self.assertEqual(filesizeformat(1024*1024*1024-1), '1024,0\xa0MB')
|
self.assertEqual(filesizeformat(1024*1024*1024), '1,0\xa0GB')
|
||||||
self.assertEqual(filesizeformat(1024*1024*1024), '1,0\xa0GB')
|
self.assertEqual(filesizeformat(1024*1024*1024*1024), '1,0\xa0TB')
|
||||||
self.assertEqual(filesizeformat(1024*1024*1024*1024), '1,0\xa0TB')
|
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024),
|
||||||
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024),
|
'1,0\xa0PB')
|
||||||
'1,0\xa0PB')
|
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
|
||||||
self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
|
'2000,0\xa0PB')
|
||||||
'2000,0\xa0PB')
|
self.assertEqual(filesizeformat(complex(1,-1)), '0\xa0Bytes')
|
||||||
self.assertEqual(filesizeformat(complex(1,-1)), '0\xa0Bytes')
|
self.assertEqual(filesizeformat(""), '0\xa0Bytes')
|
||||||
self.assertEqual(filesizeformat(""), '0\xa0Bytes')
|
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"),
|
||||||
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"),
|
'0\xa0Bytes')
|
||||||
'0\xa0Bytes')
|
|
||||||
|
|
|
@ -286,11 +286,10 @@ class FieldsTests(SimpleTestCase):
|
||||||
n = 4.35
|
n = 4.35
|
||||||
self.assertFalse(f._has_changed(n, '4.3500'))
|
self.assertFalse(f._has_changed(n, '4.3500'))
|
||||||
|
|
||||||
with translation.override('fr'):
|
with translation.override('fr'), self.settings(USE_L10N=True):
|
||||||
with self.settings(USE_L10N=True):
|
f = FloatField(localize=True)
|
||||||
f = FloatField(localize=True)
|
localized_n = formats.localize_input(n) # -> '4,35' in French
|
||||||
localized_n = formats.localize_input(n) # -> '4,35' in French
|
self.assertFalse(f._has_changed(n, localized_n))
|
||||||
self.assertFalse(f._has_changed(n, localized_n))
|
|
||||||
|
|
||||||
# DecimalField ################################################################
|
# DecimalField ################################################################
|
||||||
|
|
||||||
|
@ -399,11 +398,10 @@ class FieldsTests(SimpleTestCase):
|
||||||
self.assertFalse(f._has_changed(d, '0.10'))
|
self.assertFalse(f._has_changed(d, '0.10'))
|
||||||
self.assertTrue(f._has_changed(d, '0.101'))
|
self.assertTrue(f._has_changed(d, '0.101'))
|
||||||
|
|
||||||
with translation.override('fr'):
|
with translation.override('fr'), self.settings(USE_L10N=True):
|
||||||
with self.settings(USE_L10N=True):
|
f = DecimalField(max_digits=2, decimal_places=2, localize=True)
|
||||||
f = DecimalField(max_digits=2, decimal_places=2, localize=True)
|
localized_d = formats.localize_input(d) # -> '0,1' in French
|
||||||
localized_d = formats.localize_input(d) # -> '0,1' in French
|
self.assertFalse(f._has_changed(d, localized_d))
|
||||||
self.assertFalse(f._has_changed(d, localized_d))
|
|
||||||
|
|
||||||
# DateField ###################################################################
|
# DateField ###################################################################
|
||||||
|
|
||||||
|
|
|
@ -746,11 +746,11 @@ class FormattingTests(TransRealMixin, TestCase):
|
||||||
self.assertEqual('.', get_format('DECIMAL_SEPARATOR', lang='en'))
|
self.assertEqual('.', get_format('DECIMAL_SEPARATOR', lang='en'))
|
||||||
|
|
||||||
def test_get_format_modules_stability(self):
|
def test_get_format_modules_stability(self):
|
||||||
with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'):
|
with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'), \
|
||||||
with translation.override('de', deactivate=True):
|
translation.override('de', deactivate=True):
|
||||||
old = str("%r") % get_format_modules(reverse=True)
|
old = str("%r") % get_format_modules(reverse=True)
|
||||||
new = str("%r") % get_format_modules(reverse=True) # second try
|
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.')
|
self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.')
|
||||||
|
|
||||||
def test_localize_templatetag_and_filter(self):
|
def test_localize_templatetag_and_filter(self):
|
||||||
"""
|
"""
|
||||||
|
@ -1062,9 +1062,8 @@ class MultipleLocaleActivationTests(TransRealMixin, TestCase):
|
||||||
def test_multiple_locale_filter(self):
|
def test_multiple_locale_filter(self):
|
||||||
with translation.override('de'):
|
with translation.override('de'):
|
||||||
t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}")
|
t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}")
|
||||||
with translation.override(self._old_language):
|
with translation.override(self._old_language), translation.override('nl'):
|
||||||
with translation.override('nl'):
|
self.assertEqual(t.render(Context({})), 'nee')
|
||||||
self.assertEqual(t.render(Context({})), 'nee')
|
|
||||||
|
|
||||||
def test_multiple_locale_filter_deactivate(self):
|
def test_multiple_locale_filter_deactivate(self):
|
||||||
with translation.override('de', deactivate=True):
|
with translation.override('de', deactivate=True):
|
||||||
|
@ -1083,9 +1082,8 @@ class MultipleLocaleActivationTests(TransRealMixin, TestCase):
|
||||||
def test_multiple_locale(self):
|
def test_multiple_locale(self):
|
||||||
with translation.override('de'):
|
with translation.override('de'):
|
||||||
t = Template("{{ _('No') }}")
|
t = Template("{{ _('No') }}")
|
||||||
with translation.override(self._old_language):
|
with translation.override(self._old_language), translation.override('nl'):
|
||||||
with translation.override('nl'):
|
self.assertEqual(t.render(Context({})), 'Nee')
|
||||||
self.assertEqual(t.render(Context({})), 'Nee')
|
|
||||||
|
|
||||||
def test_multiple_locale_deactivate(self):
|
def test_multiple_locale_deactivate(self):
|
||||||
with translation.override('de', deactivate=True):
|
with translation.override('de', deactivate=True):
|
||||||
|
@ -1104,9 +1102,8 @@ class MultipleLocaleActivationTests(TransRealMixin, TestCase):
|
||||||
def test_multiple_locale_loadi18n(self):
|
def test_multiple_locale_loadi18n(self):
|
||||||
with translation.override('de'):
|
with translation.override('de'):
|
||||||
t = Template("{% load i18n %}{{ _('No') }}")
|
t = Template("{% load i18n %}{{ _('No') }}")
|
||||||
with translation.override(self._old_language):
|
with translation.override(self._old_language), translation.override('nl'):
|
||||||
with translation.override('nl'):
|
self.assertEqual(t.render(Context({})), 'Nee')
|
||||||
self.assertEqual(t.render(Context({})), 'Nee')
|
|
||||||
|
|
||||||
def test_multiple_locale_loadi18n_deactivate(self):
|
def test_multiple_locale_loadi18n_deactivate(self):
|
||||||
with translation.override('de', deactivate=True):
|
with translation.override('de', deactivate=True):
|
||||||
|
@ -1125,9 +1122,8 @@ class MultipleLocaleActivationTests(TransRealMixin, TestCase):
|
||||||
def test_multiple_locale_trans(self):
|
def test_multiple_locale_trans(self):
|
||||||
with translation.override('de'):
|
with translation.override('de'):
|
||||||
t = Template("{% load i18n %}{% trans 'No' %}")
|
t = Template("{% load i18n %}{% trans 'No' %}")
|
||||||
with translation.override(self._old_language):
|
with translation.override(self._old_language), translation.override('nl'):
|
||||||
with translation.override('nl'):
|
self.assertEqual(t.render(Context({})), 'Nee')
|
||||||
self.assertEqual(t.render(Context({})), 'Nee')
|
|
||||||
|
|
||||||
def test_multiple_locale_deactivate_trans(self):
|
def test_multiple_locale_deactivate_trans(self):
|
||||||
with translation.override('de', deactivate=True):
|
with translation.override('de', deactivate=True):
|
||||||
|
@ -1146,9 +1142,8 @@ class MultipleLocaleActivationTests(TransRealMixin, TestCase):
|
||||||
def test_multiple_locale_btrans(self):
|
def test_multiple_locale_btrans(self):
|
||||||
with translation.override('de'):
|
with translation.override('de'):
|
||||||
t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}")
|
t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}")
|
||||||
with translation.override(self._old_language):
|
with translation.override(self._old_language), translation.override('nl'):
|
||||||
with translation.override('nl'):
|
self.assertEqual(t.render(Context({})), 'Nee')
|
||||||
self.assertEqual(t.render(Context({})), 'Nee')
|
|
||||||
|
|
||||||
def test_multiple_locale_deactivate_btrans(self):
|
def test_multiple_locale_deactivate_btrans(self):
|
||||||
with translation.override('de', deactivate=True):
|
with translation.override('de', deactivate=True):
|
||||||
|
|
|
@ -355,20 +355,19 @@ class SettingsConfigureLogging(TestCase):
|
||||||
self.assertTrue(dictConfig.called)
|
self.assertTrue(dictConfig.called)
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(DEBUG=True)
|
||||||
class SecurityLoggerTest(TestCase):
|
class SecurityLoggerTest(TestCase):
|
||||||
|
|
||||||
urls = 'logging_tests.urls'
|
urls = 'logging_tests.urls'
|
||||||
|
|
||||||
def test_suspicious_operation_creates_log_message(self):
|
def test_suspicious_operation_creates_log_message(self):
|
||||||
with self.settings(DEBUG=True):
|
with patch_logger('django.security.SuspiciousOperation', 'error') as calls:
|
||||||
with patch_logger('django.security.SuspiciousOperation', 'error') as calls:
|
response = self.client.get('/suspicious/')
|
||||||
response = self.client.get('/suspicious/')
|
self.assertEqual(len(calls), 1)
|
||||||
self.assertEqual(len(calls), 1)
|
self.assertEqual(calls[0], 'dubious')
|
||||||
self.assertEqual(calls[0], 'dubious')
|
|
||||||
|
|
||||||
def test_suspicious_operation_uses_sublogger(self):
|
def test_suspicious_operation_uses_sublogger(self):
|
||||||
with self.settings(DEBUG=True):
|
with patch_logger('django.security.DisallowedHost', 'error') as calls:
|
||||||
with patch_logger('django.security.DisallowedHost', 'error') as calls:
|
response = self.client.get('/suspicious_spec/')
|
||||||
response = self.client.get('/suspicious_spec/')
|
self.assertEqual(len(calls), 1)
|
||||||
self.assertEqual(len(calls), 1)
|
self.assertEqual(calls[0], 'dubious')
|
||||||
self.assertEqual(calls[0], 'dubious')
|
|
||||||
|
|
|
@ -141,12 +141,10 @@ class OneToOneRegressionTests(TestCase):
|
||||||
"""
|
"""
|
||||||
p = Place(name='Zombie Cats', address='Not sure')
|
p = Place(name='Zombie Cats', address='Not sure')
|
||||||
p.save()
|
p.save()
|
||||||
with self.assertNumQueries(1):
|
with self.assertNumQueries(1), self.assertRaises(Restaurant.DoesNotExist):
|
||||||
with self.assertRaises(Restaurant.DoesNotExist):
|
p.restaurant
|
||||||
p.restaurant
|
with self.assertNumQueries(0), self.assertRaises(Restaurant.DoesNotExist):
|
||||||
with self.assertNumQueries(0):
|
p.restaurant
|
||||||
with self.assertRaises(Restaurant.DoesNotExist):
|
|
||||||
p.restaurant
|
|
||||||
|
|
||||||
def test_reverse_object_cached_when_related_is_accessed(self):
|
def test_reverse_object_cached_when_related_is_accessed(self):
|
||||||
"""
|
"""
|
||||||
|
@ -199,9 +197,8 @@ class OneToOneRegressionTests(TestCase):
|
||||||
self.assertEqual(self.p1.undergroundbar, b)
|
self.assertEqual(self.p1.undergroundbar, b)
|
||||||
b.place = None
|
b.place = None
|
||||||
b.save()
|
b.save()
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist):
|
||||||
with self.assertRaises(UndergroundBar.DoesNotExist):
|
self.p1.undergroundbar
|
||||||
self.p1.undergroundbar
|
|
||||||
|
|
||||||
def test_get_reverse_on_unsaved_object(self):
|
def test_get_reverse_on_unsaved_object(self):
|
||||||
"""
|
"""
|
||||||
|
@ -213,24 +210,21 @@ class OneToOneRegressionTests(TestCase):
|
||||||
p = Place()
|
p = Place()
|
||||||
|
|
||||||
# When there's no instance of the origin of the one-to-one
|
# When there's no instance of the origin of the one-to-one
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist):
|
||||||
with self.assertRaises(UndergroundBar.DoesNotExist):
|
p.undergroundbar
|
||||||
p.undergroundbar
|
|
||||||
|
|
||||||
UndergroundBar.objects.create()
|
UndergroundBar.objects.create()
|
||||||
|
|
||||||
# When there's one instance of the origin
|
# When there's one instance of the origin
|
||||||
# (p.undergroundbar used to return that instance)
|
# (p.undergroundbar used to return that instance)
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist):
|
||||||
with self.assertRaises(UndergroundBar.DoesNotExist):
|
p.undergroundbar
|
||||||
p.undergroundbar
|
|
||||||
|
|
||||||
UndergroundBar.objects.create()
|
UndergroundBar.objects.create()
|
||||||
|
|
||||||
# When there are several instances of the origin
|
# When there are several instances of the origin
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0), self.assertRaises(UndergroundBar.DoesNotExist):
|
||||||
with self.assertRaises(UndergroundBar.DoesNotExist):
|
p.undergroundbar
|
||||||
p.undergroundbar
|
|
||||||
|
|
||||||
def test_set_reverse_on_unsaved_object(self):
|
def test_set_reverse_on_unsaved_object(self):
|
||||||
"""
|
"""
|
||||||
|
@ -239,6 +233,5 @@ class OneToOneRegressionTests(TestCase):
|
||||||
"""
|
"""
|
||||||
p = Place()
|
p = Place()
|
||||||
b = UndergroundBar.objects.create()
|
b = UndergroundBar.objects.create()
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0), self.assertRaises(ValueError):
|
||||||
with self.assertRaises(ValueError):
|
p.undergroundbar = b
|
||||||
p.undergroundbar = b
|
|
||||||
|
|
|
@ -152,9 +152,8 @@ class CaptureQueriesContextManagerTests(TestCase):
|
||||||
self.assertEqual(2, len(captured_queries))
|
self.assertEqual(2, len(captured_queries))
|
||||||
|
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError), CaptureQueriesContext(connection):
|
||||||
with CaptureQueriesContext(connection):
|
raise TypeError
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
def test_with_client(self):
|
def test_with_client(self):
|
||||||
with CaptureQueriesContext(connection) as captured_queries:
|
with CaptureQueriesContext(connection) as captured_queries:
|
||||||
|
@ -190,14 +189,12 @@ class AssertNumQueriesContextManagerTests(TestCase):
|
||||||
Person.objects.count()
|
Person.objects.count()
|
||||||
|
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
with self.assertRaises(AssertionError) as exc_info:
|
with self.assertRaises(AssertionError) as exc_info, self.assertNumQueries(2):
|
||||||
with self.assertNumQueries(2):
|
Person.objects.count()
|
||||||
Person.objects.count()
|
|
||||||
self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
|
self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
|
||||||
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError), self.assertNumQueries(4000):
|
||||||
with self.assertNumQueries(4000):
|
raise TypeError
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
def test_with_client(self):
|
def test_with_client(self):
|
||||||
person = Person.objects.create(name="test")
|
person = Person.objects.create(name="test")
|
||||||
|
@ -232,13 +229,13 @@ class AssertTemplateUsedContextManagerTests(TestCase):
|
||||||
render_to_string('template_used/base.html')
|
render_to_string('template_used/base.html')
|
||||||
|
|
||||||
def test_nested_usage(self):
|
def test_nested_usage(self):
|
||||||
with self.assertTemplateUsed('template_used/base.html'):
|
with self.assertTemplateUsed('template_used/base.html'), \
|
||||||
with self.assertTemplateUsed('template_used/include.html'):
|
self.assertTemplateUsed('template_used/include.html'):
|
||||||
render_to_string('template_used/include.html')
|
render_to_string('template_used/include.html')
|
||||||
|
|
||||||
with self.assertTemplateUsed('template_used/extends.html'):
|
with self.assertTemplateUsed('template_used/extends.html'), \
|
||||||
with self.assertTemplateUsed('template_used/base.html'):
|
self.assertTemplateUsed('template_used/base.html'):
|
||||||
render_to_string('template_used/extends.html')
|
render_to_string('template_used/extends.html')
|
||||||
|
|
||||||
with self.assertTemplateUsed('template_used/base.html'):
|
with self.assertTemplateUsed('template_used/base.html'):
|
||||||
with self.assertTemplateUsed('template_used/alternative.html'):
|
with self.assertTemplateUsed('template_used/alternative.html'):
|
||||||
|
@ -258,38 +255,34 @@ class AssertTemplateUsedContextManagerTests(TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_error_message(self):
|
def test_error_message(self):
|
||||||
with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'):
|
with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'), \
|
||||||
with self.assertTemplateUsed('template_used/base.html'):
|
self.assertTemplateUsed('template_used/base.html'):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'):
|
with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html'), \
|
||||||
with self.assertTemplateUsed(template_name='template_used/base.html'):
|
self.assertTemplateUsed(template_name='template_used/base.html'):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html.*template_used/alternative\.html$'):
|
with six.assertRaisesRegex(self, AssertionError, r'^template_used/base\.html.*template_used/alternative\.html$'), \
|
||||||
with self.assertTemplateUsed('template_used/base.html'):
|
self.assertTemplateUsed('template_used/base.html'):
|
||||||
render_to_string('template_used/alternative.html')
|
render_to_string('template_used/alternative.html')
|
||||||
|
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError), self.assertTemplateUsed():
|
||||||
with self.assertTemplateUsed():
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError), self.assertTemplateUsed(''):
|
||||||
with self.assertTemplateUsed(''):
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError), self.assertTemplateUsed(''):
|
||||||
with self.assertTemplateUsed(''):
|
render_to_string('template_used/base.html')
|
||||||
render_to_string('template_used/base.html')
|
|
||||||
|
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError), self.assertTemplateUsed(template_name=''):
|
||||||
with self.assertTemplateUsed(template_name=''):
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError), \
|
||||||
with self.assertTemplateUsed('template_used/base.html'):
|
self.assertTemplateUsed('template_used/base.html'):
|
||||||
render_to_string('template_used/alternative.html')
|
render_to_string('template_used/alternative.html')
|
||||||
|
|
||||||
|
|
||||||
class HTMLEqualTests(TestCase):
|
class HTMLEqualTests(TestCase):
|
||||||
|
|
|
@ -65,10 +65,9 @@ class AtomicTests(TransactionTestCase):
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
|
self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
|
||||||
|
|
||||||
def test_rollback(self):
|
def test_rollback(self):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic():
|
||||||
with transaction.atomic():
|
Reporter.objects.create(first_name="Haddock")
|
||||||
Reporter.objects.create(first_name="Haddock")
|
raise Exception("Oops, that's his last name")
|
||||||
raise Exception("Oops, that's his last name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_nested_commit_commit(self):
|
def test_nested_commit_commit(self):
|
||||||
|
@ -82,30 +81,27 @@ class AtomicTests(TransactionTestCase):
|
||||||
def test_nested_commit_rollback(self):
|
def test_nested_commit_rollback(self):
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic():
|
||||||
with transaction.atomic():
|
Reporter.objects.create(first_name="Haddock")
|
||||||
Reporter.objects.create(first_name="Haddock")
|
raise Exception("Oops, that's his last name")
|
||||||
raise Exception("Oops, that's his last name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
|
self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
|
||||||
|
|
||||||
def test_nested_rollback_commit(self):
|
def test_nested_rollback_commit(self):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic():
|
||||||
|
Reporter.objects.create(last_name="Tintin")
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Reporter.objects.create(last_name="Tintin")
|
Reporter.objects.create(last_name="Haddock")
|
||||||
with transaction.atomic():
|
raise Exception("Oops, that's his first name")
|
||||||
Reporter.objects.create(last_name="Haddock")
|
|
||||||
raise Exception("Oops, that's his first name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_nested_rollback_rollback(self):
|
def test_nested_rollback_rollback(self):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic():
|
||||||
with transaction.atomic():
|
Reporter.objects.create(last_name="Tintin")
|
||||||
Reporter.objects.create(last_name="Tintin")
|
with six.assertRaisesRegex(self, Exception, "Oops"):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with transaction.atomic():
|
||||||
with transaction.atomic():
|
Reporter.objects.create(first_name="Haddock")
|
||||||
Reporter.objects.create(first_name="Haddock")
|
raise Exception("Oops, that's his last name")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his first name")
|
||||||
raise Exception("Oops, that's his first name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_merged_commit_commit(self):
|
def test_merged_commit_commit(self):
|
||||||
|
@ -119,31 +115,29 @@ class AtomicTests(TransactionTestCase):
|
||||||
def test_merged_commit_rollback(self):
|
def test_merged_commit_rollback(self):
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), \
|
||||||
with transaction.atomic(savepoint=False):
|
transaction.atomic(savepoint=False):
|
||||||
Reporter.objects.create(first_name="Haddock")
|
Reporter.objects.create(first_name="Haddock")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his last name")
|
||||||
# Writes in the outer block are rolled back too.
|
# Writes in the outer block are rolled back too.
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_merged_rollback_commit(self):
|
def test_merged_rollback_commit(self):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic():
|
||||||
with transaction.atomic():
|
Reporter.objects.create(last_name="Tintin")
|
||||||
Reporter.objects.create(last_name="Tintin")
|
with transaction.atomic(savepoint=False):
|
||||||
with transaction.atomic(savepoint=False):
|
Reporter.objects.create(last_name="Haddock")
|
||||||
Reporter.objects.create(last_name="Haddock")
|
raise Exception("Oops, that's his first name")
|
||||||
raise Exception("Oops, that's his first name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_merged_rollback_rollback(self):
|
def test_merged_rollback_rollback(self):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), transaction.atomic():
|
||||||
with transaction.atomic():
|
Reporter.objects.create(last_name="Tintin")
|
||||||
Reporter.objects.create(last_name="Tintin")
|
with six.assertRaisesRegex(self, Exception, "Oops"):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with transaction.atomic(savepoint=False):
|
||||||
with transaction.atomic(savepoint=False):
|
Reporter.objects.create(first_name="Haddock")
|
||||||
Reporter.objects.create(first_name="Haddock")
|
raise Exception("Oops, that's his last name")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his first name")
|
||||||
raise Exception("Oops, that's his first name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_reuse_commit_commit(self):
|
def test_reuse_commit_commit(self):
|
||||||
|
@ -159,32 +153,29 @@ class AtomicTests(TransactionTestCase):
|
||||||
atomic = transaction.atomic()
|
atomic = transaction.atomic()
|
||||||
with atomic:
|
with atomic:
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), atomic:
|
||||||
with atomic:
|
Reporter.objects.create(first_name="Haddock")
|
||||||
Reporter.objects.create(first_name="Haddock")
|
raise Exception("Oops, that's his last name")
|
||||||
raise Exception("Oops, that's his last name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
|
self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>'])
|
||||||
|
|
||||||
def test_reuse_rollback_commit(self):
|
def test_reuse_rollback_commit(self):
|
||||||
atomic = transaction.atomic()
|
atomic = transaction.atomic()
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), atomic:
|
||||||
|
Reporter.objects.create(last_name="Tintin")
|
||||||
with atomic:
|
with atomic:
|
||||||
Reporter.objects.create(last_name="Tintin")
|
Reporter.objects.create(last_name="Haddock")
|
||||||
with atomic:
|
raise Exception("Oops, that's his first name")
|
||||||
Reporter.objects.create(last_name="Haddock")
|
|
||||||
raise Exception("Oops, that's his first name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_reuse_rollback_rollback(self):
|
def test_reuse_rollback_rollback(self):
|
||||||
atomic = transaction.atomic()
|
atomic = transaction.atomic()
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), atomic:
|
||||||
with atomic:
|
Reporter.objects.create(last_name="Tintin")
|
||||||
Reporter.objects.create(last_name="Tintin")
|
with six.assertRaisesRegex(self, Exception, "Oops"):
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with atomic:
|
||||||
with atomic:
|
Reporter.objects.create(first_name="Haddock")
|
||||||
Reporter.objects.create(first_name="Haddock")
|
raise Exception("Oops, that's his last name")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his first name")
|
||||||
raise Exception("Oops, that's his first name")
|
|
||||||
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
self.assertQuerysetEqual(Reporter.objects.all(), [])
|
||||||
|
|
||||||
def test_force_rollback(self):
|
def test_force_rollback(self):
|
||||||
|
@ -200,10 +191,9 @@ class AtomicTests(TransactionTestCase):
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
sid = transaction.savepoint()
|
sid = transaction.savepoint()
|
||||||
# trigger a database error inside an inner atomic without savepoint
|
# trigger a database error inside an inner atomic without savepoint
|
||||||
with self.assertRaises(DatabaseError):
|
with self.assertRaises(DatabaseError), transaction.atomic(savepoint=False):
|
||||||
with transaction.atomic(savepoint=False):
|
connection.cursor().execute(
|
||||||
connection.cursor().execute(
|
"SELECT no_such_col FROM transactions_reporter")
|
||||||
"SELECT no_such_col FROM transactions_reporter")
|
|
||||||
transaction.savepoint_rollback(sid)
|
transaction.savepoint_rollback(sid)
|
||||||
# atomic block should rollback, but prevent it, as we just did it.
|
# atomic block should rollback, but prevent it, as we just did it.
|
||||||
self.assertTrue(transaction.get_rollback())
|
self.assertTrue(transaction.get_rollback())
|
||||||
|
@ -263,10 +253,10 @@ class AtomicMergeTests(TransactionTestCase):
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
with transaction.atomic(savepoint=False):
|
with transaction.atomic(savepoint=False):
|
||||||
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), \
|
||||||
with transaction.atomic(savepoint=False):
|
transaction.atomic(savepoint=False):
|
||||||
Reporter.objects.create(first_name="Tournesol")
|
Reporter.objects.create(first_name="Tournesol")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his last name")
|
||||||
# It wasn't possible to roll back
|
# It wasn't possible to roll back
|
||||||
self.assertEqual(Reporter.objects.count(), 3)
|
self.assertEqual(Reporter.objects.count(), 3)
|
||||||
# It wasn't possible to roll back
|
# It wasn't possible to roll back
|
||||||
|
@ -279,10 +269,10 @@ class AtomicMergeTests(TransactionTestCase):
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), \
|
||||||
with transaction.atomic(savepoint=False):
|
transaction.atomic(savepoint=False):
|
||||||
Reporter.objects.create(first_name="Tournesol")
|
Reporter.objects.create(first_name="Tournesol")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his last name")
|
||||||
# It wasn't possible to roll back
|
# It wasn't possible to roll back
|
||||||
self.assertEqual(Reporter.objects.count(), 3)
|
self.assertEqual(Reporter.objects.count(), 3)
|
||||||
# The first block with a savepoint must roll back
|
# The first block with a savepoint must roll back
|
||||||
|
@ -293,10 +283,10 @@ class AtomicMergeTests(TransactionTestCase):
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Reporter.objects.create(first_name="Tintin")
|
Reporter.objects.create(first_name="Tintin")
|
||||||
# Inner block without a savepoint fails
|
# Inner block without a savepoint fails
|
||||||
with six.assertRaisesRegex(self, Exception, "Oops"):
|
with six.assertRaisesRegex(self, Exception, "Oops"), \
|
||||||
with transaction.atomic(savepoint=False):
|
transaction.atomic(savepoint=False):
|
||||||
Reporter.objects.create(first_name="Haddock")
|
Reporter.objects.create(first_name="Haddock")
|
||||||
raise Exception("Oops, that's his last name")
|
raise Exception("Oops, that's his last name")
|
||||||
# It wasn't possible to roll back
|
# It wasn't possible to roll back
|
||||||
self.assertEqual(Reporter.objects.count(), 2)
|
self.assertEqual(Reporter.objects.count(), 2)
|
||||||
# Inner block with a savepoint succeeds
|
# Inner block with a savepoint succeeds
|
||||||
|
@ -316,9 +306,9 @@ class AtomicErrorsTests(TransactionTestCase):
|
||||||
|
|
||||||
def test_atomic_prevents_setting_autocommit(self):
|
def test_atomic_prevents_setting_autocommit(self):
|
||||||
autocommit = transaction.get_autocommit()
|
autocommit = transaction.get_autocommit()
|
||||||
with transaction.atomic():
|
with transaction.atomic(), \
|
||||||
with self.assertRaises(transaction.TransactionManagementError):
|
self.assertRaises(transaction.TransactionManagementError):
|
||||||
transaction.set_autocommit(not autocommit)
|
transaction.set_autocommit(not autocommit)
|
||||||
# Make sure autocommit wasn't changed.
|
# Make sure autocommit wasn't changed.
|
||||||
self.assertEqual(connection.autocommit, autocommit)
|
self.assertEqual(connection.autocommit, autocommit)
|
||||||
|
|
||||||
|
@ -552,9 +542,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction
|
||||||
The autocommit context manager works exactly the same as the default
|
The autocommit context manager works exactly the same as the default
|
||||||
behavior.
|
behavior.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(Exception):
|
with self.assertRaises(Exception), transaction.autocommit():
|
||||||
with transaction.autocommit():
|
self.create_reporter_and_fail()
|
||||||
self.create_reporter_and_fail()
|
|
||||||
|
|
||||||
self.assertEqual(Reporter.objects.count(), 1)
|
self.assertEqual(Reporter.objects.count(), 1)
|
||||||
|
|
||||||
|
@ -563,9 +552,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction
|
||||||
"""
|
"""
|
||||||
The autocommit context manager also works with a using argument.
|
The autocommit context manager also works with a using argument.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(Exception):
|
with self.assertRaises(Exception), transaction.autocommit(using="default"):
|
||||||
with transaction.autocommit(using="default"):
|
self.create_reporter_and_fail()
|
||||||
self.create_reporter_and_fail()
|
|
||||||
|
|
||||||
self.assertEqual(Reporter.objects.count(), 1)
|
self.assertEqual(Reporter.objects.count(), 1)
|
||||||
|
|
||||||
|
@ -575,9 +563,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction
|
||||||
With the commit_on_success context manager, the transaction is only
|
With the commit_on_success context manager, the transaction is only
|
||||||
committed if the block doesn't throw an exception.
|
committed if the block doesn't throw an exception.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(Exception):
|
with self.assertRaises(Exception), transaction.commit_on_success():
|
||||||
with transaction.commit_on_success():
|
self.create_reporter_and_fail()
|
||||||
self.create_reporter_and_fail()
|
|
||||||
|
|
||||||
self.assertEqual(Reporter.objects.count(), 0)
|
self.assertEqual(Reporter.objects.count(), 0)
|
||||||
|
|
||||||
|
@ -586,9 +573,8 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction
|
||||||
"""
|
"""
|
||||||
The commit_on_success context manager also works with a using argument.
|
The commit_on_success context manager also works with a using argument.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(Exception):
|
with self.assertRaises(Exception), transaction.commit_on_success(using="default"):
|
||||||
with transaction.commit_on_success(using="default"):
|
self.create_reporter_and_fail()
|
||||||
self.create_reporter_and_fail()
|
|
||||||
|
|
||||||
self.assertEqual(Reporter.objects.count(), 0)
|
self.assertEqual(Reporter.objects.count(), 0)
|
||||||
|
|
||||||
|
@ -633,18 +619,18 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction
|
||||||
"""
|
"""
|
||||||
If you forget, you'll get bad errors.
|
If you forget, you'll get bad errors.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(transaction.TransactionManagementError):
|
with self.assertRaises(transaction.TransactionManagementError), \
|
||||||
with transaction.commit_manually():
|
transaction.commit_manually():
|
||||||
Reporter.objects.create(first_name="Scott", last_name="Browning")
|
Reporter.objects.create(first_name="Scott", last_name="Browning")
|
||||||
|
|
||||||
@skipUnlessDBFeature('supports_transactions')
|
@skipUnlessDBFeature('supports_transactions')
|
||||||
def test_manually_managed_with_using(self):
|
def test_manually_managed_with_using(self):
|
||||||
"""
|
"""
|
||||||
The commit_manually function also works with a using argument.
|
The commit_manually function also works with a using argument.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(transaction.TransactionManagementError):
|
with self.assertRaises(transaction.TransactionManagementError), \
|
||||||
with transaction.commit_manually(using="default"):
|
transaction.commit_manually(using="default"):
|
||||||
Reporter.objects.create(first_name="Walter", last_name="Cronkite")
|
Reporter.objects.create(first_name="Walter", last_name="Cronkite")
|
||||||
|
|
||||||
@skipUnlessDBFeature('requires_rollback_on_dirty_transaction')
|
@skipUnlessDBFeature('requires_rollback_on_dirty_transaction')
|
||||||
def test_bad_sql(self):
|
def test_bad_sql(self):
|
||||||
|
@ -654,8 +640,7 @@ class TransactionContextManagerTests(IgnoreDeprecationWarningsMixin, Transaction
|
||||||
be rolled back. The bug is only visible using the psycopg2 backend,
|
be rolled back. The bug is only visible using the psycopg2 backend,
|
||||||
though the fix is generally a good idea.
|
though the fix is generally a good idea.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(IntegrityError):
|
with self.assertRaises(IntegrityError), transaction.commit_on_success():
|
||||||
with transaction.commit_on_success():
|
cursor = connection.cursor()
|
||||||
cursor = connection.cursor()
|
cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
|
||||||
cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
|
|
||||||
transaction.rollback()
|
transaction.rollback()
|
||||||
|
|
|
@ -85,20 +85,18 @@ class JsI18NTests(TestCase):
|
||||||
languages and you've set settings.LANGUAGE_CODE to some other language
|
languages and you've set settings.LANGUAGE_CODE to some other language
|
||||||
than English.
|
than English.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='es'):
|
with self.settings(LANGUAGE_CODE='es'), override('en-us'):
|
||||||
with override('en-us'):
|
response = self.client.get('/views/jsi18n/')
|
||||||
response = self.client.get('/views/jsi18n/')
|
self.assertNotContains(response, 'esto tiene que ser traducido')
|
||||||
self.assertNotContains(response, 'esto tiene que ser traducido')
|
|
||||||
|
|
||||||
def test_jsi18n_fallback_language(self):
|
def test_jsi18n_fallback_language(self):
|
||||||
"""
|
"""
|
||||||
Let's make sure that the fallback language is still working properly
|
Let's make sure that the fallback language is still working properly
|
||||||
in cases where the selected language cannot be found.
|
in cases where the selected language cannot be found.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='fr'):
|
with self.settings(LANGUAGE_CODE='fr'), override('fi'):
|
||||||
with override('fi'):
|
response = self.client.get('/views/jsi18n/')
|
||||||
response = self.client.get('/views/jsi18n/')
|
self.assertContains(response, 'il faut le traduire')
|
||||||
self.assertContains(response, 'il faut le traduire')
|
|
||||||
|
|
||||||
def testI18NLanguageNonEnglishDefault(self):
|
def testI18NLanguageNonEnglishDefault(self):
|
||||||
"""
|
"""
|
||||||
|
@ -107,10 +105,9 @@ class JsI18NTests(TestCase):
|
||||||
is English and there is not 'en' translation available. See #13388,
|
is English and there is not 'en' translation available. See #13388,
|
||||||
#3594 and #13726 for more details.
|
#3594 and #13726 for more details.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='fr'):
|
with self.settings(LANGUAGE_CODE='fr'), override('en-us'):
|
||||||
with override('en-us'):
|
response = self.client.get('/views/jsi18n/')
|
||||||
response = self.client.get('/views/jsi18n/')
|
self.assertNotContains(response, 'Choisir une heure')
|
||||||
self.assertNotContains(response, 'Choisir une heure')
|
|
||||||
|
|
||||||
def test_nonenglish_default_english_userpref(self):
|
def test_nonenglish_default_english_userpref(self):
|
||||||
"""
|
"""
|
||||||
|
@ -119,20 +116,19 @@ class JsI18NTests(TestCase):
|
||||||
with the proper English translations. See #13726 for more details.
|
with the proper English translations. See #13726 for more details.
|
||||||
"""
|
"""
|
||||||
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app0']
|
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app0']
|
||||||
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps):
|
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps), \
|
||||||
with override('en-us'):
|
override('en-us'):
|
||||||
response = self.client.get('/views/jsi18n_english_translation/')
|
response = self.client.get('/views/jsi18n_english_translation/')
|
||||||
self.assertContains(response, javascript_quote('this app0 string is to be translated'))
|
self.assertContains(response, javascript_quote('this app0 string is to be translated'))
|
||||||
|
|
||||||
def testI18NLanguageNonEnglishFallback(self):
|
def testI18NLanguageNonEnglishFallback(self):
|
||||||
"""
|
"""
|
||||||
Makes sure that the fallback language is still working properly
|
Makes sure that the fallback language is still working properly
|
||||||
in cases where the selected language cannot be found.
|
in cases where the selected language cannot be found.
|
||||||
"""
|
"""
|
||||||
with self.settings(LANGUAGE_CODE='fr'):
|
with self.settings(LANGUAGE_CODE='fr'), override('none'):
|
||||||
with override('none'):
|
response = self.client.get('/views/jsi18n/')
|
||||||
response = self.client.get('/views/jsi18n/')
|
self.assertContains(response, 'Choisir une heure')
|
||||||
self.assertContains(response, 'Choisir une heure')
|
|
||||||
|
|
||||||
|
|
||||||
class JsI18NTestsMultiPackage(TestCase):
|
class JsI18NTestsMultiPackage(TestCase):
|
||||||
|
@ -149,10 +145,11 @@ class JsI18NTestsMultiPackage(TestCase):
|
||||||
#3594 and #13514 for more details.
|
#3594 and #13514 for more details.
|
||||||
"""
|
"""
|
||||||
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app1', 'view_tests.app2']
|
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app1', 'view_tests.app2']
|
||||||
with self.settings(LANGUAGE_CODE='en-us', INSTALLED_APPS=extended_apps):
|
with self.settings(LANGUAGE_CODE='en-us', INSTALLED_APPS=extended_apps), \
|
||||||
with override('fr'):
|
override('fr'):
|
||||||
response = self.client.get('/views/jsi18n_multi_packages1/')
|
response = self.client.get('/views/jsi18n_multi_packages1/')
|
||||||
self.assertContains(response, javascript_quote('il faut traduire cette chaîne de caractères de app1'))
|
self.assertContains(response,
|
||||||
|
javascript_quote('il faut traduire cette chaîne de caractères de app1'))
|
||||||
|
|
||||||
def testI18NDifferentNonEnLangs(self):
|
def testI18NDifferentNonEnLangs(self):
|
||||||
"""
|
"""
|
||||||
|
@ -160,20 +157,21 @@ class JsI18NTestsMultiPackage(TestCase):
|
||||||
English.
|
English.
|
||||||
"""
|
"""
|
||||||
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app3', 'view_tests.app4']
|
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app3', 'view_tests.app4']
|
||||||
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps):
|
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps), \
|
||||||
with override('es-ar'):
|
override('es-ar'):
|
||||||
response = self.client.get('/views/jsi18n_multi_packages2/')
|
response = self.client.get('/views/jsi18n_multi_packages2/')
|
||||||
self.assertContains(response, javascript_quote('este texto de app3 debe ser traducido'))
|
self.assertContains(response,
|
||||||
|
javascript_quote('este texto de app3 debe ser traducido'))
|
||||||
|
|
||||||
def testI18NWithLocalePaths(self):
|
def testI18NWithLocalePaths(self):
|
||||||
extended_locale_paths = settings.LOCALE_PATHS + (
|
extended_locale_paths = settings.LOCALE_PATHS + (
|
||||||
path.join(path.dirname(
|
path.join(path.dirname(
|
||||||
path.dirname(path.abspath(upath(__file__)))), 'app3', 'locale'),)
|
path.dirname(path.abspath(upath(__file__)))), 'app3', 'locale'),)
|
||||||
with self.settings(LANGUAGE_CODE='es-ar', LOCALE_PATHS=extended_locale_paths):
|
with self.settings(LANGUAGE_CODE='es-ar', LOCALE_PATHS=extended_locale_paths), \
|
||||||
with override('es-ar'):
|
override('es-ar'):
|
||||||
response = self.client.get('/views/jsi18n/')
|
response = self.client.get('/views/jsi18n/')
|
||||||
self.assertContains(response,
|
self.assertContains(response,
|
||||||
javascript_quote('este texto de app3 debe ser traducido'))
|
javascript_quote('este texto de app3 debe ser traducido'))
|
||||||
|
|
||||||
|
|
||||||
skip_selenium = not os.environ.get('DJANGO_SELENIUM_TESTS', False)
|
skip_selenium = not os.environ.get('DJANGO_SELENIUM_TESTS', False)
|
||||||
|
|
Loading…
Reference in New Issue