Commit 5578cd8b authored by MOREAU Ulysse's avatar MOREAU Ulysse
Browse files

Add tests

parent f9cf3246
......@@ -4,6 +4,7 @@ from unittest.mock import Mock
from unittest.mock import patch
from django.templatetags.static import static
from django.utils import timezone
from . import CategoryTestCase
from . import OrderItemTestCase
......@@ -722,3 +723,132 @@ class OrderTest(PaymentTestCase, OrderItemTestCase):
with self.assertRaises(ValueError):
self.order.delete()
def test_delete_illegal_items_on_ordered_order(self) -> None:
hidden_item = self.create_item()
ood_item = self.create_item()
pack_with_illegal_children = self.create_item()
legal_item = self.create_item()
illegal_children = self.create_item()
legal_children = self.create_item()
illegal_children.hidden = True
illegal_children.save()
illegal_children.refresh_from_db()
legal_children.save()
legal_children.refresh_from_db()
hidden_item.hidden = True
hidden_item.save()
hidden_item.refresh_from_db()
# YES, this would have failed anywhere near the year 1970
ood_item.end_date = datetime.datetime(timezone.now().year - 2, 1, 1, 0, 0)
ood_item.save()
ood_item.refresh_from_db()
legal_item.save()
legal_item.refresh_from_db()
pack_with_illegal_children.children.add(illegal_children)
pack_with_illegal_children.children.add(legal_children)
pack_with_illegal_children.save()
pack_with_illegal_children.refresh_from_db()
hidden_item.add_to_order(self.order)
ood_item.add_to_order(self.order)
legal_item.add_to_order(self.order)
pack_with_illegal_children.add_to_order(self.order)
self.order.ordered = True
self.order.save()
self.order.refresh_from_db()
self.assertTrue(self.order.ordered)
self.assertTrue(hidden_item.contains_illegal_items)
self.assertTrue(ood_item.contains_illegal_items)
# You could have illegal items in a legal pack, this should still be valid
# e.g you could hide an item outside of a pack
self.assertFalse(pack_with_illegal_children.contains_illegal_items)
self.assertFalse(legal_item.contains_illegal_items)
items_in_pack = [oi.item for oi in self.order.items.all()]
deleted_count = self.order.clean_illegal_items()
self.assertEqual(deleted_count, 0)
self.order.save()
self.order.refresh_from_db()
items_in_order = [oi.item for oi in self.order.items.all()]
self.assertTrue(hidden_item in items_in_pack)
self.assertTrue(ood_item in items_in_pack)
self.assertTrue(pack_with_illegal_children in items_in_pack)
self.assertTrue(legal_item in items_in_pack)
def test_delete_illegal_items_on_unordered_order(self) -> None:
hidden_item = self.create_item()
ood_item = self.create_item()
pack_with_illegal_children = self.create_item()
legal_item = self.create_item()
illegal_children = self.create_item()
legal_children = self.create_item()
illegal_children.hidden = True
illegal_children.save()
illegal_children.refresh_from_db()
legal_children.save()
legal_children.refresh_from_db()
hidden_item.hidden = True
hidden_item.save()
hidden_item.refresh_from_db()
# YES, this would have failed anywhere near the year 1970
ood_item.end_date = datetime.datetime(timezone.now().year - 2, 1, 1, 0, 0)
ood_item.save()
ood_item.refresh_from_db()
legal_item.save()
legal_item.refresh_from_db()
pack_with_illegal_children.children.add(illegal_children)
pack_with_illegal_children.children.add(legal_children)
pack_with_illegal_children.save()
pack_with_illegal_children.refresh_from_db()
hidden_item.add_to_order(self.order)
ood_item.add_to_order(self.order)
legal_item.add_to_order(self.order)
pack_with_illegal_children.add_to_order(self.order)
self.order.save()
self.order.refresh_from_db()
self.assertFalse(self.order.ordered)
self.assertTrue(hidden_item.contains_illegal_items)
self.assertTrue(ood_item.contains_illegal_items)
# You could have illegal items in a legal pack, this should still be valid
# e.g you could hide an item outside of a pack
self.assertFalse(pack_with_illegal_children.contains_illegal_items)
self.assertFalse(legal_item.contains_illegal_items)
items_in_order = [oi.item for oi in self.order.items.all()]
self.assertTrue(hidden_item in items_in_order)
self.assertTrue(ood_item in items_in_order)
self.assertTrue(pack_with_illegal_children in items_in_order)
self.assertTrue(legal_item in items_in_order)
deleted_count = self.order.clean_illegal_items()
self.assertEqual(deleted_count, 2)
self.order.save()
self.order.refresh_from_db()
items_in_order = [oi.item for oi in self.order.items.all()]
self.assertFalse(hidden_item in items_in_order)
self.assertFalse(ood_item in items_in_order)
self.assertTrue(pack_with_illegal_children in items_in_order)
self.assertTrue(legal_item in items_in_order)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment