From 7c9cb1ed37354c184fe0438ecae923d5be711c86 Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Fri, 9 Jun 2017 12:36:09 -0400 Subject: [PATCH] Refs #27953 -- Removed hardcoded uses of Model.__str__() in tests. --- tests/admin_changelist/tests.py | 2 +- tests/admin_utils/models.py | 3 +++ tests/admin_utils/test_logentry.py | 16 ++++++++-------- tests/admin_views/tests.py | 26 +++++++++++++------------- tests/admin_widgets/tests.py | 8 ++++---- tests/model_fields/test_floatfield.py | 4 ++-- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 0e35cf37e0..8e2f33ea04 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -186,7 +186,7 @@ class ChangeListTests(TestCase): link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) row_html = ( 'name' - 'Parent object' % link + '%s' % (link, new_parent) ) self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output) diff --git a/tests/admin_utils/models.py b/tests/admin_utils/models.py index dafb24e342..7b9c08a2f7 100644 --- a/tests/admin_utils/models.py +++ b/tests/admin_utils/models.py @@ -18,6 +18,9 @@ class Article(models.Model): hist = models.CharField(max_length=100, verbose_name=_("History")) created = models.DateTimeField(null=True) + def __str__(self): + return self.title + def test_from_model(self): return "nothing" diff --git a/tests/admin_utils/test_logentry.py b/tests/admin_utils/test_logentry.py index ee56f6f1f5..09c09b0ea7 100644 --- a/tests/admin_utils/test_logentry.py +++ b/tests/admin_utils/test_logentry.py @@ -120,23 +120,23 @@ class LogEntryTests(TestCase): json.loads(logentry.change_message), [ {"changed": {"fields": ["domain"]}}, - {"added": {"object": "Article object", "name": "article"}}, - {"changed": {"fields": ["title"], "object": "Article object", "name": "article"}}, - {"deleted": {"object": "Article object", "name": "article"}}, + {"added": {"object": "Added article", "name": "article"}}, + {"changed": {"fields": ["title"], "object": "Changed Title", "name": "article"}}, + {"deleted": {"object": "Title second article", "name": "article"}}, ] ) self.assertEqual( logentry.get_change_message(), - 'Changed domain. Added article "Article object". ' - 'Changed title for article "Article object". Deleted article "Article object".' + 'Changed domain. Added article "Added article". ' + 'Changed title for article "Changed Title". Deleted article "Title second article".' ) with translation.override('fr'): self.assertEqual( logentry.get_change_message(), - "Modification de domain. Ajout de article « Article object ». " - "Modification de title pour l'objet article « Article object ». " - "Suppression de article « Article object »." + "Modification de domain. Ajout de article « Added article ». " + "Modification de title pour l'objet article « Changed Title ». " + "Suppression de article « Title second article »." ) def test_logentry_get_edited_object(self): diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 2f52bd9925..16cc8d4b71 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -891,11 +891,11 @@ class AdminViewBasicTest(AdminViewBasicTestCase): ModelAdmin.changelist_view shouldn't result in a NoReverseMatch if url for change_view is removed from get_urls (#20934). """ - UnchangeableObject.objects.create() + o = UnchangeableObject.objects.create() response = self.client.get(reverse('admin:admin_views_unchangeableobject_changelist')) self.assertEqual(response.status_code, 200) # Check the format of the shown object -- shouldn't contain a change link - self.assertContains(response, 'UnchangeableObject object', html=True) + self.assertContains(response, '%s' % o, html=True) def test_invalid_appindex_url(self): """ @@ -3306,7 +3306,7 @@ class AdminActionsTest(TestCase): # No 500 caused by NoReverseMatch self.assertEqual(response.status_code, 200) # The page shouldn't display a link to the nonexistent change page - self.assertContains(response, "
  • Unchangeable object: UnchangeableObject object
  • ", 1, html=True) + self.assertContains(response, "
  • Unchangeable object: %s
  • " % obj, 1, html=True) def test_custom_function_mail_action(self): "Tests a custom action defined in a function" @@ -3692,12 +3692,12 @@ class AdminCustomQuerysetTest(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(ShortMessage.objects.count(), 1) # Message should contain non-ugly model verbose name - pk = ShortMessage.objects.all()[0].pk + sm = ShortMessage.objects.all()[0] self.assertContains( response, '
  • The short message "' - 'ShortMessage object" was added successfully.
  • ' % - reverse('admin:admin_views_shortmessage_change', args=(pk,)), html=True + '%s" was added successfully.' % + (reverse('admin:admin_views_shortmessage_change', args=(sm.pk,)), sm), html=True ) def test_add_model_modeladmin_only_qs(self): @@ -3733,12 +3733,12 @@ class AdminCustomQuerysetTest(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(Paper.objects.count(), 1) # Message should contain non-ugly model verbose name - pk = Paper.objects.all()[0].pk + p = Paper.objects.all()[0] self.assertContains( response, '
  • The paper "' - 'Paper object" was added successfully.
  • ' % - reverse('admin:admin_views_paper_change', args=(pk,)), html=True + '%s" was added successfully.' % + (reverse('admin:admin_views_paper_change', args=(p.pk,)), p), html=True ) def test_edit_model_modeladmin_defer_qs(self): @@ -3786,8 +3786,8 @@ class AdminCustomQuerysetTest(TestCase): self.assertContains( response, '
  • The short message "' - 'ShortMessage object" was changed successfully.
  • ' % - reverse('admin:admin_views_shortmessage_change', args=(sm.pk,)), html=True + '%s" was changed successfully.' % + (reverse('admin:admin_views_shortmessage_change', args=(sm.pk,)), sm), html=True ) def test_edit_model_modeladmin_only_qs(self): @@ -3833,8 +3833,8 @@ class AdminCustomQuerysetTest(TestCase): self.assertContains( response, '
  • The paper "' - 'Paper object" was changed successfully.
  • ' % - reverse('admin:admin_views_paper_change', args=(p.pk,)), html=True + '%s" was changed successfully.' % + (reverse('admin:admin_views_paper_change', args=(p.pk,)), p), html=True ) def test_history_view_custom_qs(self): diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index bb8f9df63f..ca888c720b 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -520,8 +520,8 @@ class ForeignKeyRawIdWidgetTest(TestCase): self.assertHTMLEqual( w.render('honeycomb_widget', big_honeycomb.pk, attrs={}), '' - ' Honeycomb object' - % {'hcombpk': big_honeycomb.pk} + ' %(hcomb)s' + % {'hcombpk': big_honeycomb.pk, 'hcomb': big_honeycomb} ) def test_fk_to_self_model_not_in_admin(self): @@ -535,8 +535,8 @@ class ForeignKeyRawIdWidgetTest(TestCase): self.assertHTMLEqual( w.render('individual_widget', subject1.pk, attrs={}), '' - ' Individual object' - % {'subj1pk': subject1.pk} + ' %(subj1)s' + % {'subj1pk': subject1.pk, 'subj1': subject1} ) def test_proper_manager_for_label_lookup(self): diff --git a/tests/model_fields/test_floatfield.py b/tests/model_fields/test_floatfield.py index 52f61c88d5..c1c941b25c 100644 --- a/tests/model_fields/test_floatfield.py +++ b/tests/model_fields/test_floatfield.py @@ -21,9 +21,9 @@ class TestFloatField(TestCase): instance.size = instance msg = ( 'Tried to update field model_fields.FloatModel.size with a model ' - 'instance, . Use a value ' + 'instance, %r. Use a value ' 'compatible with FloatField.' - ) + ) % instance with transaction.atomic(): with self.assertRaisesMessage(TypeError, msg): instance.save()