Files
Fil-A-Management/filament/tests/test_catalog.py
T

128 lines
5.5 KiB
Python

from django.core.management import call_command, get_commands
from filament.bambu_catalog import BAMBU_LAB_PRODUCT_COUNT, BAMBU_LAB_VARIANT_COUNT
from filament.forms import VariantSelectionForm
from filament.models import (
CatalogBootstrap,
Color,
InventoryItem,
Manufacturer,
OrderItem,
ProductType,
Variant,
)
from .base import CatalogTestCase
class CatalogTests(CatalogTestCase):
def setUp(self):
self.client.force_login(self.alice)
def test_shared_catalog_displays_optional_variant_data(self):
self.spool.manufacturer_color_code = "11200"
self.spool.net_weight_g = 1000
self.spool.diameter_mm = "1.75"
self.spool.save()
response = self.client.get("/katalog/")
self.assertEqual(response.status_code, 200)
for text in (
"Bambu Lab",
"PLA Matte",
"Scharlachrot",
"11200",
"1000",
"1,75",
"Mit Spule",
"Refill (ohne Spule)",
):
self.assertContains(response, text)
def test_dropdowns_only_offer_existing_active_combinations(self):
other_maker = Manufacturer.objects.create(name="Anderer Hersteller")
other_product = ProductType.objects.create(manufacturer=other_maker, name="PLA Basic")
other_color = Color.objects.create(name="Blau")
Variant.objects.create(product_type=other_product, color=other_color, spool_type="refill")
form = VariantSelectionForm(selection=self.selection())
self.assertEqual(list(form.fields["product_type"].queryset), [self.product])
self.assertEqual(set(form.fields["color"].queryset), {self.red, self.black})
self.assertEqual({s for s, _ in form.fields["spool_type"].choices}, {"", "spool", "refill"})
invalid = self.selection() | {"product_type": other_product.pk, "color": other_color.pk}
self.assertFalse(VariantSelectionForm(invalid).is_valid())
response = self.client.post("/bestand/hinzufuegen/", invalid)
self.assertEqual(response.status_code, 200)
self.assertFalse(InventoryItem.objects.exists())
def test_filter_button_works_without_javascript_or_full_selection(self):
response = self.client.post(
"/bestand/hinzufuegen/", {"manufacturer": self.maker.pk, "action": "filter"}
)
self.assertEqual(response.status_code, 200)
self.assertFalse(response.context["form"].errors)
self.assertContains(response, "formnovalidate")
self.assertFalse(InventoryItem.objects.exists())
def test_inactive_variants_stay_visible_in_existing_positions_but_cannot_be_added(self):
stock = InventoryItem.objects.create(owner=self.alice, variant=self.spool, quantity=2)
OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=1)
self.spool.is_active = False
self.spool.save()
for url in ("/bestand/", "/bestellung/"):
self.assertContains(self.client.get(url), "Deaktiviert")
for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"):
self.assertEqual(self.client.post(url, self.selection()).status_code, 200)
stock.refresh_from_db()
self.assertEqual(stock.quantity, 2)
self.assertEqual(OrderItem.objects.count(), 1)
self.assertEqual(
self.client.post(f"/bestand/{stock.pk}/bearbeiten/", {"quantity": 1}).status_code, 302
)
self.assertNotIn(
"spool",
dict(VariantSelectionForm(selection=self.selection()).fields["spool_type"].choices),
)
def test_bambu_catalog_is_complete_one_time_bootstrap(self):
self.assertIn("seed_catalog", get_commands())
call_command("seed_catalog", verbosity=0)
self.assertEqual(
ProductType.objects.filter(manufacturer__name="Bambu Lab").count(),
BAMBU_LAB_PRODUCT_COUNT,
)
self.assertEqual(Variant.objects.count(), BAMBU_LAB_VARIANT_COUNT)
for product, color, code, spool_type, weight in (
("PLA Basic", "Jade White", "10100", "refill", 1000),
("PLA Translucent", "Teal", "13612", "spool", 1000),
("PAHT-CF (500 g)", "Black", "70100", "spool", 500),
("PAHT-CF (1 kg)", "Black", "70100", "spool", 1000),
("Support for PLA/PETG", "Nature", "65102", "spool", 500),
):
self.assertTrue(
Variant.objects.filter(
product_type__name=product,
color__name=color,
manufacturer_color_code=code,
spool_type=spool_type,
net_weight_g=weight,
diameter_mm="1.75",
).exists(),
(product, color, spool_type),
)
self.assertFalse(ProductType.objects.filter(name__icontains="bundle").exists())
count = Variant.objects.count()
inactive = Variant.objects.filter(manufacturer_color_code="11100").first()
removed = Variant.objects.get(
product_type__name="PLA Translucent",
manufacturer_color_code="13612",
spool_type="spool",
)
inactive.is_active = False
inactive.save()
removed.delete()
call_command("seed_catalog", verbosity=0)
self.assertEqual(Variant.objects.count(), count - 1)
inactive.refresh_from_db()
self.assertFalse(inactive.is_active)
self.assertFalse(Variant.objects.filter(pk=removed.pk).exists())
self.assertEqual(CatalogBootstrap.objects.count(), 1)