Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests and remove print stmt #14

Merged
merged 1 commit into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion admin_confirm/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def _get_changed_data(
if initial_value != new_value:
changed_data[name] = [initial_value, new_value]

print(changed_data)
return changed_data

def _get_form_data(self, request):
Expand Down
52 changes: 49 additions & 3 deletions admin_confirm/tests/test_confirm_change_and_add_m2m_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def test_post_add_with_confirm_add_m2m(self):
ShoppingMallAdmin.confirmation_fields = ["shops"]
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)

# Ensure not redirected (confirmation page does not redirect)
Expand All @@ -51,22 +56,39 @@ def test_post_add_with_confirm_add_m2m(self):
f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content,
)
# Submit should conserve the save action
self.assertIn(
'<input type="submit" value="Yes, I’m 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
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):
shops = [ShopFactory() for i in range(10)]
shopping_mall = ShoppingMall.objects.create(name="My Mall")
shopping_mall.shops.set(shops)
# Currently ShoppingMall configured with confirmation_fields = ['name']
data = {
"name": "Not My Mall",
"shops": "1",
"shops": ["1", "2"],
"id": shopping_mall.id,
"_confirm_change": True,
"csrfmiddlewaretoken": "fake token",
"_save": True,
"_continue": True,
}
response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
Expand All @@ -82,6 +104,7 @@ def test_m2m_field_post_change_with_confirm_change(self):
form_data = {
"name": "Not My Mall",
"shops": "1",
"shops": "2",
"id": str(shopping_mall.id),
}

Expand All @@ -90,11 +113,34 @@ def test_m2m_field_post_change_with_confirm_change(self):
f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content,
)
# Submit should conserve the save action
self.assertIn(
'<input type="submit" value="Yes, I’m 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
shopping_mall.refresh_from_db()
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):
shops = [ShopFactory() for i in range(10)]
shopping_mall = ShoppingMall.objects.create(name="My Mall")
Expand Down