fix(ISSUE-8): contd m2m save values (#11)

* feat(ISSUE-8): ISSUE-8: ManyToManyField causes error on confirmations

* feat(ISSUE-8): Update some readme and remove print statements

* feat(ISSUE-8): Generate new version of package

* feat(ISSUE-3): Adding .travis.yml

* feat(ISSUE-3): Adding coveralls

* feat(ISSUE-3): Trying github actions

* feat(ISSUE-3): remove travis

* feat(ISSUE-3): Change python versions to test

* feat(ISSUE-3): Some refactoring and trying tox

* feat(ISSUE-3): Try action matrix

* feat(ISSUE-3): Some more refactors

* feat(ISSUE-3): Fix tests

* feat(ISSUE-3): Refactor/fix tests

* feat(ISSUE-3): Remove tox

* feat(ISSUE-3): Adding pypi version badge to readme

* feat(ISSUE-3): Update readme again

* feat(ISSUE-3): Remove tox from readme

* feat(ISSUE-8): Adding some tests for m2m and fix save value issue

* feat(ISSUE-8): Updating test todos

* feat(ISSUE-8): Finish up the tests

* feat(ISSUE-8): Rename

* feat(ISSUE-8): Update gitignore

Co-authored-by: Thu Trang Pham <thu@joinmodernhealth.com>
This commit is contained in:
Thu Trang Pham
2021-02-19 21:31:30 -08:00
committed by GitHub
parent 9a9dfa75e8
commit 09744c7f2a
10 changed files with 239 additions and 21 deletions
@@ -64,7 +64,6 @@ class TestConfirmChangeAndAdd(TestCase):
]
self.assertEqual(response.template_name, expected_templates)
form_data = {"shop": str(shop.id), "item": str(item.id), "quantity": str(5)}
self.assertEqual(response.context_data["form_data"], form_data)
for k, v in form_data.items():
self.assertIn(
f'<input type="hidden" name="{ k }" value="{ v }">',
@@ -100,7 +99,6 @@ class TestConfirmChangeAndAdd(TestCase):
"id": str(item.id),
"currency": Item.VALID_CURRENCIES[0][0],
}
self.assertEqual(response.context_data["form_data"], form_data)
for k, v in form_data.items():
self.assertIn(
f'<input type="hidden" name="{ k }" value="{ v }">',
@@ -0,0 +1,183 @@
from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User
from django.urls import reverse
from tests.market.admin import InventoryAdmin, ShoppingMallAdmin
from tests.market.models import Item, Inventory, ShoppingMall
from tests.factories import ItemFactory, ShopFactory, InventoryFactory
class TestConfirmChangeAndAddM2MField(TestCase):
@classmethod
def setUpTestData(cls):
cls.superuser = User.objects.create_superuser(
username="super", email="super@email.org", password="pass"
)
def setUp(self):
self.client.force_login(self.superuser)
self.factory = RequestFactory()
def test_post_add_without_confirm_add_m2m(self):
shops = [ShopFactory() for i in range(3)]
data = {"name": "name", "shops": [s.id for s in shops]}
response = self.client.post(reverse("admin:market_shoppingmall_add"), data)
# Redirects to changelist and is added
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/admin/market/shoppingmall/")
self.assertEqual(ShoppingMall.objects.count(), 1)
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}
response = self.client.post(reverse("admin:market_shoppingmall_add"), data)
# Ensure not redirected (confirmation page does not redirect)
self.assertEqual(response.status_code, 200)
expected_templates = [
"admin/market/shoppingmall/change_confirmation.html",
"admin/market/change_confirmation.html",
"admin/change_confirmation.html",
]
self.assertEqual(response.template_name, expected_templates)
form_data = [("name", "name")] + [("shops", s.id) for s in shops]
for k, v in form_data:
self.assertIn(
f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content,
)
# Should not have been added yet
self.assertEqual(ShoppingMall.objects.count(), 0)
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",
"id": shopping_mall.id,
"_confirm_change": True,
"csrfmiddlewaretoken": "fake token",
"_save": True,
}
response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
)
# Ensure not redirected (confirmation page does not redirect)
self.assertEqual(response.status_code, 200)
expected_templates = [
"admin/market/shoppingmall/change_confirmation.html",
"admin/market/change_confirmation.html",
"admin/change_confirmation.html",
]
self.assertEqual(response.template_name, expected_templates)
form_data = {
"name": "Not My Mall",
"shops": "1",
"id": str(shopping_mall.id),
}
for k, v in form_data.items():
self.assertIn(
f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content,
)
# Hasn't changed item yet
shopping_mall.refresh_from_db()
self.assertEqual(shopping_mall.name, "My Mall")
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")
shopping_mall.shops.set(shops)
# Currently ShoppingMall configured with confirmation_fields = ['name']
data = {
"name": "Not My Mall",
"shops": ["1", "2", "3"],
"id": shopping_mall.id,
"_confirm_change": True,
"csrfmiddlewaretoken": "fake token",
"_save": True,
}
response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
)
# Ensure not redirected (confirmation page does not redirect)
self.assertEqual(response.status_code, 200)
expected_templates = [
"admin/market/shoppingmall/change_confirmation.html",
"admin/market/change_confirmation.html",
"admin/change_confirmation.html",
]
self.assertEqual(response.template_name, expected_templates)
form_data = [
("name", "Not My Mall"),
("shops", "1"),
("shops", "2"),
("shops", "3"),
("id", str(shopping_mall.id)),
]
for k, v in form_data:
self.assertIn(
f'<input type="hidden" name="{ k }" value="{ v }">',
response.rendered_content,
)
# Hasn't changed item yet
shopping_mall.refresh_from_db()
self.assertEqual(shopping_mall.name, "My Mall")
def test_post_change_without_confirm_change_m2m_value(self):
# make the m2m the confirmation_field
ShoppingMallAdmin.confirmation_fields = ["shops"]
shops = [ShopFactory() for i in range(3)]
shopping_mall = ShoppingMall.objects.create(name="name")
shopping_mall.shops.set(shops)
assert shopping_mall.shops.count() == 3
data = {"name": "name", "id": str(shopping_mall.id), "shops": ["1"]}
response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
)
# Redirects to changelist
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/admin/market/shoppingmall/")
# Shop has changed
shopping_mall.refresh_from_db()
self.assertEqual(shopping_mall.shops.count(), 1)
def test_form_invalid_m2m_value(self):
ShoppingMallAdmin.confirmation_fields = ["shops"]
shopping_mall = ShoppingMall.objects.create(name="name")
data = {
"id": shopping_mall.id,
"name": "name",
"shops": ["1", "2", "3"], # These shops don't exist
"_confirm_change": True,
"csrfmiddlewaretoken": "fake token",
}
response = self.client.post(
f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data
)
# Form invalid should show errors on form
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(response.context_data.get("errors"))
self.assertTrue(
str(response.context_data["errors"][0][0]).startswith(
"Select a valid choice."
)
)
# Should not have updated inventory
shopping_mall.refresh_from_db()
self.assertEqual(shopping_mall.shops.count(), 0)