from django.core.management import call_command, get_commands from filament.forms import VariantSelectionForm from filament.models import 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_example_catalog_is_idempotent_and_does_not_reactivate_variants(self): self.assertIn("seed_catalog", get_commands()) call_command("seed_catalog", verbosity=0) count = Variant.objects.count() v = Variant.objects.filter(manufacturer_color_code="11100").first() self.assertIsNotNone(v) v.is_active = False v.save() call_command("seed_catalog", verbosity=0) self.assertEqual(Variant.objects.count(), count) v.refresh_from_db() self.assertFalse(v.is_active)