Update tests and remove print stmt (#14)

Co-authored-by: Thu Trang Pham <thu@joinmodernhealth.com>
main
Thu Trang Pham 2021-02-20 08:14:31 -08:00 committed by GitHub
parent 5b93ed5eb7
commit 6f41e6cfdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 4 deletions

View File

@ -135,7 +135,6 @@ class AdminConfirmMixin:
if initial_value != new_value: if initial_value != new_value:
changed_data[name] = [initial_value, new_value] changed_data[name] = [initial_value, new_value]
print(changed_data)
return changed_data return changed_data
def _get_form_data(self, request): def _get_form_data(self, request):

View File

@ -34,7 +34,12 @@ class TestConfirmChangeAndAddM2MField(TestCase):
ShoppingMallAdmin.confirmation_fields = ["shops"] ShoppingMallAdmin.confirmation_fields = ["shops"]
shops = [ShopFactory() for i in range(3)] shops = [ShopFactory() for i in range(3)]
data = {"name": "name", "shops": [s.id for s in shops], "_confirm_add": True} data = {
"name": "name",
"shops": [s.id for s in shops],
"_confirm_add": True,
"_save": True,
}
response = self.client.post(reverse("admin:market_shoppingmall_add"), data) response = self.client.post(reverse("admin:market_shoppingmall_add"), data)
# Ensure not redirected (confirmation page does not redirect) # Ensure not redirected (confirmation page does not redirect)
@ -51,10 +56,27 @@ class TestConfirmChangeAndAddM2MField(TestCase):
f'<input type="hidden" name="{ k }" value="{ v }">', f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content, response.rendered_content,
) )
# Submit should conserve the save action
self.assertIn(
'<input type="submit" value="Yes, Im sure" name="_save">',
response.rendered_content,
)
# There should not be _confirm_add sent in the form on confirmaiton page
self.assertNotIn("_confirm_add", response.rendered_content)
# Should not have been added yet # Should not have been added yet
self.assertEqual(ShoppingMall.objects.count(), 0) self.assertEqual(ShoppingMall.objects.count(), 0)
# Confirmation page would not have the _confirm_add sent on submit
del data["_confirm_add"]
# Selecting to "Yes, I'm sure" on the confirmation page
# Would post to the same endpoint
response = self.client.post(reverse("admin:market_shoppingmall_add"), data)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/admin/market/shoppingmall/")
self.assertEqual(ShoppingMall.objects.count(), 1)
self.assertEqual(ShoppingMall.objects.all().first().shops.count(), 3)
def test_m2m_field_post_change_with_confirm_change(self): def test_m2m_field_post_change_with_confirm_change(self):
shops = [ShopFactory() for i in range(10)] shops = [ShopFactory() for i in range(10)]
shopping_mall = ShoppingMall.objects.create(name="My Mall") shopping_mall = ShoppingMall.objects.create(name="My Mall")
@ -62,11 +84,11 @@ class TestConfirmChangeAndAddM2MField(TestCase):
# Currently ShoppingMall configured with confirmation_fields = ['name'] # Currently ShoppingMall configured with confirmation_fields = ['name']
data = { data = {
"name": "Not My Mall", "name": "Not My Mall",
"shops": "1", "shops": ["1", "2"],
"id": shopping_mall.id, "id": shopping_mall.id,
"_confirm_change": True, "_confirm_change": True,
"csrfmiddlewaretoken": "fake token", "csrfmiddlewaretoken": "fake token",
"_save": True, "_continue": True,
} }
response = self.client.post( response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
@ -82,6 +104,7 @@ class TestConfirmChangeAndAddM2MField(TestCase):
form_data = { form_data = {
"name": "Not My Mall", "name": "Not My Mall",
"shops": "1", "shops": "1",
"shops": "2",
"id": str(shopping_mall.id), "id": str(shopping_mall.id),
} }
@ -90,11 +113,34 @@ class TestConfirmChangeAndAddM2MField(TestCase):
f'<input type="hidden" name="{ k }" value="{ v }">', f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content, response.rendered_content,
) )
# Submit should conserve the save action
self.assertIn(
'<input type="submit" value="Yes, Im sure" name="_continue">',
response.rendered_content,
)
# There should not be _confirm_change sent in the form on confirmaiton page
self.assertNotIn("_confirm_change", response.rendered_content)
# Hasn't changed item yet # Hasn't changed item yet
shopping_mall.refresh_from_db() shopping_mall.refresh_from_db()
self.assertEqual(shopping_mall.name, "My Mall") self.assertEqual(shopping_mall.name, "My Mall")
# Selecting to "Yes, I'm sure" on the confirmation page
# Would post to the same endpoint
del data["_confirm_change"]
response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
)
# will show the change page for this shopping_mall
self.assertEqual(response.status_code, 302)
self.assertEqual(
response.url, f"/admin/market/shoppingmall/{shopping_mall.id}/change/"
)
# Should not be the confirmation page, we already confirmed change
self.assertNotEqual(response.templates, expected_templates)
self.assertEqual(ShoppingMall.objects.count(), 1)
self.assertEqual(ShoppingMall.objects.all().first().shops.count(), 2)
def test_m2m_field_post_change_with_confirm_change_multiple_selected(self): def test_m2m_field_post_change_with_confirm_change_multiple_selected(self):
shops = [ShopFactory() for i in range(10)] shops = [ShopFactory() for i in range(10)]
shopping_mall = ShoppingMall.objects.create(name="My Mall") shopping_mall = ShoppingMall.objects.create(name="My Mall")