Refs #27804 -- Used subTest() in a modeladmin test.

This commit is contained in:
Nick Pope 2017-09-04 02:20:43 +01:00 committed by Tim Graham
parent 9397d3add4
commit dea4cfb95b
1 changed files with 23 additions and 4 deletions

View File

@ -1,9 +1,10 @@
from datetime import date from datetime import date
from django import forms from django import forms
from django.contrib.admin.models import LogEntry from django.contrib.admin.models import ADDITION, CHANGE, DELETION, LogEntry
from django.contrib.admin.options import ( from django.contrib.admin.options import (
HORIZONTAL, VERTICAL, ModelAdmin, TabularInline, HORIZONTAL, VERTICAL, ModelAdmin, TabularInline,
get_content_type_for_model,
) )
from django.contrib.admin.sites import AdminSite from django.contrib.admin.sites import AdminSite
from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect
@ -615,9 +616,27 @@ class ModelAdminTests(TestCase):
ma = ModelAdmin(Band, self.site) ma = ModelAdmin(Band, self.site)
mock_request = MockRequest() mock_request = MockRequest()
mock_request.user = User.objects.create(username='bill') mock_request.user = User.objects.create(username='bill')
self.assertEqual(ma.log_addition(mock_request, self.band, 'added'), LogEntry.objects.latest('id')) content_type = get_content_type_for_model(self.band)
self.assertEqual(ma.log_change(mock_request, self.band, 'changed'), LogEntry.objects.latest('id')) tests = (
self.assertEqual(ma.log_change(mock_request, self.band, 'deleted'), LogEntry.objects.latest('id')) (ma.log_addition, ADDITION, {'added': {}}),
(ma.log_change, CHANGE, {'changed': {'fields': ['name', 'bio']}}),
(ma.log_deletion, DELETION, str(self.band)),
)
for method, flag, message in tests:
with self.subTest(name=method.__name__):
created = method(mock_request, self.band, message)
fetched = LogEntry.objects.filter(action_flag=flag).latest('id')
self.assertEqual(created, fetched)
self.assertEqual(fetched.action_flag, flag)
self.assertEqual(fetched.content_type, content_type)
self.assertEqual(fetched.object_id, str(self.band.pk))
self.assertEqual(fetched.user, mock_request.user)
if flag == DELETION:
self.assertEqual(fetched.change_message, '')
self.assertEqual(fetched.object_repr, message)
else:
self.assertEqual(fetched.change_message, str(message))
self.assertEqual(fetched.object_repr, str(self.band))
class ModelAdminPermissionTests(SimpleTestCase): class ModelAdminPermissionTests(SimpleTestCase):