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.management.commands.seed_catalog import BAMBU_LAB_BOOTSTRAP_KEY, SUNLU_BOOTSTRAP_KEY 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_lists_show_product_images_and_placeholder_for_missing_photo(self): image_url = "https://cdn.shopify.com/example.png" self.spool.image_url = image_url self.spool.manufacturer_color_code = "11200" self.spool.save(update_fields=["image_url", "manufacturer_color_code"]) for model in (InventoryItem, OrderItem): model.objects.create(owner=self.alice, variant=self.spool, quantity=1) model.objects.create(owner=self.alice, variant=self.refill, quantity=1) for url in ("/bestand/", "/bestellung/"): response = self.client.get(url) self.assertContains( response, 'class="variant-thumbnail"', count=1 if url == "/bestand/" else 2 ) self.assertContains(response, f'src="{image_url}"', count=1) self.assertContains(response, 'referrerpolicy="no-referrer"', count=1) self.assertContains(response, 'Artikelnr.') self.assertContains(response, '11200', count=1) self.assertContains(response, '–', count=1) self.assertIn("https://cdn.shopify.com", response["Content-Security-Policy"]) self.assertNotIn( "https://cdn.shopify.com", self.client.get("/katalog/")["Content-Security-Policy"] ) 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_material_filter_only_shows_matching_subtypes_without_brand(self): basic = ProductType.objects.create(manufacturer=self.maker, name="Bambu Lab · PLA Basic") abs_cf = ProductType.objects.create(manufacturer=self.maker, name="ABS-CF") Variant.objects.create(product_type=basic, color=self.black, spool_type="refill") Variant.objects.create(product_type=abs_cf, color=self.black, spool_type="spool") for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): response = self.client.post( url, {"manufacturer": self.maker.pk, "material": "PLA", "action": "filter"} ) form = response.context["form"] self.assertEqual( list(form.fields["material"].choices)[1:], [("ABS", "ABS"), ("PLA", "PLA")] ) self.assertEqual( {label for value, label in form.fields["product_type"].choices if value}, {"Basic", "Matte"}, ) self.assertFalse(form.fields["color"].queryset.exists()) response = self.client.post(url, self.selection() | {"material": "ABS"}) self.assertTrue(response.context["form"].errors) 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_color_preview_is_available_in_both_flows_and_remains_optional(self): self.spool.swatch = "#DE4343" self.spool.save(update_fields=["swatch"]) for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): with self.subTest(url=url): response = self.client.post( url, { "manufacturer": self.maker.pk, "material": "PLA", "product_type": self.product.pk, "action": "filter", }, ) self.assertContains(response, 'data-color-select=""') self.assertContains(response, 'data-swatch="#DE4343"', count=1) self.assertContains(response, "Kohleschwarz") self.assertFalse(response.context["form"].errors) 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, ) bambu_variants = Variant.objects.filter(product_type__manufacturer__name="Bambu Lab") self.assertEqual(bambu_variants.count(), BAMBU_LAB_VARIANT_COUNT) self.assertFalse( bambu_variants.exclude(pk__in=(self.spool.pk, self.refill.pk, self.black_spool.pk)) .filter(image_url="") .exists() ) self.assertTrue( Variant.objects.get( product_type__name="PLA Basic", manufacturer_color_code="10100", spool_type="spool" ).image_url.endswith("/pla_baisc_white.jpg") ) 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()) self.assertEqual( Variant.objects.get( product_type__name="PLA Silk Dual Color", color__name="Velvet Eclipse (Black-Red)" ).swatch, "#000000,#A34342", ) 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( bambu_variants.filter( product_type__name="PLA Translucent", manufacturer_color_code="13612" ).exists() ) self.assertTrue(CatalogBootstrap.objects.filter(key=BAMBU_LAB_BOOTSTRAP_KEY).exists()) def test_sunlu_imports_separately_into_existing_installations_and_preserves_edits(self): CatalogBootstrap.objects.create(key=BAMBU_LAB_BOOTSTRAP_KEY) call_command("seed_catalog", verbosity=0) sunlu = Manufacturer.objects.get(name="SUNLU") variants = Variant.objects.filter(product_type__manufacturer=sunlu) self.assertEqual(variants.count(), 561) self.assertEqual(ProductType.objects.filter(manufacturer=sunlu).count(), 67) self.assertEqual(Variant.objects.filter(product_type__manufacturer=self.maker).count(), 3) self.assertTrue(CatalogBootstrap.objects.filter(key=SUNLU_BOOTSTRAP_KEY).exists()) pla = variants.filter(product_type__name="PLA", color__name="Schwarz") self.assertEqual(set(pla.values_list("spool_type", flat=True)), {"spool", "refill"}) spool = pla.get(spool_type="spool") self.assertEqual(spool.swatch, "#000000") self.assertTrue(spool.image_url.startswith("https://cdn.shopify.com/")) self.assertTrue( variants.filter(product_type__name="PETG (5 kg)", net_weight_g=5000).exists() ) self.assertTrue( variants.filter(product_type__name="PA6-CF20 (500 g)", net_weight_g=500).exists() ) for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): self.assertRedirects( self.client.post(url, self.selection(spool)), url.removesuffix("hinzufuegen/"), ) refill = pla.get(spool_type="refill") refill.is_active = False refill.save(update_fields=["is_active"]) extra = variants.get(product_type__name="PETG-CF10") extra.delete() call_command("seed_catalog", verbosity=0) self.assertEqual(variants.count(), 560) self.assertFalse(variants.filter(product_type__name="PETG-CF10").exists()) refill.refresh_from_db() self.assertFalse(refill.is_active)