From fbd58bf55f5ae8b8d2d3d1a9908f4ea9237fada7 Mon Sep 17 00:00:00 2001 From: Augusto Moura Date: Thu, 4 Nov 2021 01:54:43 -0300 Subject: [PATCH 1/2] fix: add obj to argument list for get_fieldsets --- django_reverse_admin/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_reverse_admin/__init__.py b/django_reverse_admin/__init__.py index 6e9a3bf..63961be 100644 --- a/django_reverse_admin/__init__.py +++ b/django_reverse_admin/__init__.py @@ -310,7 +310,7 @@ def _changeform_view(self, request, object_id, form_url, extra_context): readonly_fields = self.get_readonly_fields(request, obj) adminForm = helpers.AdminForm(form, - list(self.get_fieldsets(request)), + list(self.get_fieldsets(request, obj)), self.prepopulated_fields, readonly_fields=readonly_fields, model_admin=self From 475b35c07bec93575ad74f4034bf7b14bb84a512 Mon Sep 17 00:00:00 2001 From: gutierri Date: Wed, 1 May 2024 19:48:15 -0300 Subject: [PATCH 2/2] add coverage tests --- tests/polls/tests/test_admin.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/polls/tests/test_admin.py b/tests/polls/tests/test_admin.py index 8326ce6..55a540d 100644 --- a/tests/polls/tests/test_admin.py +++ b/tests/polls/tests/test_admin.py @@ -1,7 +1,10 @@ +from unittest import mock + from django.contrib.auth.models import User from django.test import TestCase from django.test import Client from django.urls import reverse +from django.core.handlers.wsgi import WSGIRequest from polls.models import Address from polls.models import AddressNonId @@ -10,7 +13,7 @@ from polls.models import PersonWithTwoAddresses from polls.admin import SITE_HEADER import polls.tests.config as test_config - +from django_reverse_admin import ReverseModelAdmin class AddressAdminTest(TestCase): def setUp(self): @@ -137,3 +140,20 @@ def test_admin_header(self): change_url = reverse('admin:polls_personwithtwoaddresses_add') res = client.get(change_url) self.assertTrue(SITE_HEADER in str(res.content), 'Custom header is set') + + @mock.patch.object(ReverseModelAdmin, 'get_fieldsets') + def test_called_args(self, mock): + person = PersonWithAddressNonId.objects.create(name='Toto', home_addr=AddressNonId.objects.create()) + + client = Client() + client.login(**test_config.ADMIN_USER) + change_url = reverse('admin:polls_personwithaddressnonid_change', args=(person.pk,)) + # Ensure it doesn't raise any exception. + client.get(change_url, test_config.PERSON_WITH_TWO_ADDRESSES) + self.assertEqual(mock.call_count, 2) + self.assertEqual(2, len(mock.call_args_list[0].args)) + self.assertIsInstance(mock.call_args_list[0].args[0], WSGIRequest) + self.assertIsInstance(mock.call_args_list[0].args[1], PersonWithAddressNonId) + self.assertEqual(2, len(mock.call_args_list[1].args)) + self.assertIsInstance(mock.call_args_list[1].args[0], WSGIRequest) + self.assertIsInstance(mock.call_args_list[1].args[1], PersonWithAddressNonId)