def test_pluginless_lcfg(rf): with plugin_override(): with override_current_theme_class(None): theme = get_current_theme(get_default_shop()) cell = LayoutCell(theme, None) assert not cell.instantiate_plugin() lcfg = LayoutCellFormGroup(layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/"))) assert "plugin" not in lcfg.forms
def test_lcfg(rf): two_thirds = int(LayoutCellGeneralInfoForm.CELL_FULL_WIDTH * 2 / 3) with plugin_override(): with override_current_theme_class(None): theme = get_current_theme(get_default_shop()) cell = LayoutCell(theme, "text", sizes={"md": two_thirds, "sm": two_thirds}) lcfg = LayoutCellFormGroup(layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/"))) assert "general" in lcfg.forms assert "plugin" in lcfg.forms assert not lcfg.is_valid() # Oh, we must've forgotten the text... lcfg = LayoutCellFormGroup( data={ "general-cell_width": "%d" % two_thirds, "general-cell_align": " ", "general-cell_extra_classes" : "newClass", "plugin-text_*": "Hello, world!" }, layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/")) ) assert lcfg.is_valid() # Let's see now! lcfg.save() assert cell.sizes["md"] == two_thirds assert cell.extra_classes == "newClass" assert cell.config["text"] == {FALLBACK_LANGUAGE_CODE: "Hello, world!"}
def test_product_from_category_plugin(rf): shop = get_default_shop() category1 = get_default_category() category2 = CategoryFactory(status=CategoryStatus.VISIBLE) category1.shops.add(shop) category2.shops.add(shop) p1 = create_product("p1", shop, get_default_supplier(), "10") p2 = create_product("p2", shop, get_default_supplier(), "20") p3 = create_product("p3", shop, get_default_supplier(), "30") sp1 = p1.get_shop_instance(shop) sp2 = p2.get_shop_instance(shop) sp3 = p3.get_shop_instance(shop) sp1.categories.add(category1) sp2.categories.add(category1) sp3.categories.add(category2) context = get_context(rf) plugin = ProductsFromCategoryPlugin({ "category": category1.pk }) context_products = plugin.get_context_data(context)["products"] assert p1 in context_products assert p2 in context_products assert p3 not in context_products # test the plugin form with override_current_theme_class(None): theme = get_current_theme(get_default_shop()) cell = LayoutCell(theme, ProductsFromCategoryPlugin.identifier, sizes={"md": 8}) lcfg = LayoutCellFormGroup(layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/"))) assert not lcfg.is_valid() lcfg = LayoutCellFormGroup( data={ "general-cell_width": "8", "general-cell_align": "pull-right", "plugin-count": 4, "plugin-category": category2.pk }, layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/")) ) assert lcfg.is_valid() lcfg.save() assert cell.config["category"] == str(category2.pk)
def test_formless_plugin_in_lcfg(rf): two_thirds = int(LayoutCellGeneralInfoForm.CELL_FULL_WIDTH * 2 / 3) with plugin_override(): with override_current_theme_class(None): theme = get_current_theme(get_default_shop()) cell = LayoutCell(theme, "inject") assert cell.instantiate_plugin() lcfg = LayoutCellFormGroup( data={ "general-cell_width": "%d" % two_thirds, "general-cell_align": "pull-right", "general-cell_extra_classes" : "newClass", }, layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/")) ) assert "plugin" not in lcfg.forms assert lcfg.is_valid() lcfg.save() assert cell.extra_classes == "newClass" assert cell.sizes["md"] == two_thirds # Something got saved even if the plugin doesn't need config
def test_product_selection_plugin(rf): shop = get_default_shop() p1 = create_product("p1", shop, get_default_supplier(), "10") p2 = create_product("p2", shop, get_default_supplier(), "20") p3 = create_product("p3", shop, get_default_supplier(), "30") p4 = create_product("p4", shop, get_default_supplier(), "40") sp1 = p1.get_shop_instance(shop) sp2 = p2.get_shop_instance(shop) sp3 = p3.get_shop_instance(shop) context = get_context(rf) plugin = ProductSelectionPlugin({ "products": [sp1.pk, sp2.pk, sp3.pk] }) context_products = plugin.get_context_data(context)["products"] assert p1 in context_products assert p2 in context_products assert p3 in context_products assert p4 not in context_products # test the plugin form with override_current_theme_class(None): theme = get_current_theme(get_default_shop()) cell = LayoutCell(theme, ProductSelectionPlugin.identifier, sizes={"md": 8}) lcfg = LayoutCellFormGroup(layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/"))) # not valid, products are required assert not lcfg.is_valid() lcfg = LayoutCellFormGroup( data={ "general-cell_width": "8", "general-cell_align": "pull-right", "plugin-products": [p1.pk, p2.pk] }, layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/")) ) assert lcfg.is_valid() lcfg.save() assert cell.config["products"] == [str(p1.pk), str(p2.pk)]
def test_product_selection_plugin(rf): shop = factories.get_default_shop() category1 = factories.CategoryFactory(status=CategoryStatus.VISIBLE) category2 = factories.CategoryFactory(status=CategoryStatus.VISIBLE) p1 = factories.create_product("p1", shop, factories.get_default_supplier(), "10") p2 = factories.create_product("p2", shop, factories.get_default_supplier(), "20") p3 = factories.create_product("p3", shop, factories.get_default_supplier(), "30") p4 = factories.create_product("p4", shop, factories.get_default_supplier(), "40") p5 = factories.create_product("p5", shop, factories.get_default_supplier(), "50") sp1 = p1.get_shop_instance(shop) sp2 = p2.get_shop_instance(shop) sp3 = p3.get_shop_instance(shop) sp4 = p4.get_shop_instance(shop) sp1.categories.add(category1, category2) sp2.categories.add(category1) sp3.categories.add(category2) sp4.categories.add(category2) # this discount should show products: p1, p2 and p5 discount1 = Discount.objects.create( name="discount1", active=True, start_datetime=now() - timedelta(days=10), end_datetime=now() + timedelta(days=1), product=p5, category=category1 ) discount1.shops = [shop] # this discount should show products: p1, p3 and p4 discount2 = Discount.objects.create( name="discount2", active=True, start_datetime=now() - timedelta(days=10), end_datetime=now() + timedelta(days=1), category=category2 ) discount2.shops = [shop] # this discount shouldn't be available for this shop discount3 = Discount.objects.create( name="discount3", active=True, start_datetime=now() - timedelta(days=10), end_datetime=now() + timedelta(days=1), category=category2 ) context = get_context(rf) # test only discount1 plugin = DiscountedProductsPlugin({"discounts": [discount1.pk], "count": 10}) context_products = plugin.get_context_data(context)["products"] assert p1 in context_products assert p2 in context_products assert p3 not in context_products assert p4 not in context_products assert p5 in context_products for status in range(2): if status == 1: discount2.active = False discount2.save() # test only discount2 plugin = DiscountedProductsPlugin({"discounts": [discount2.pk], "count": 10}) context_products = plugin.get_context_data(context)["products"] if status == 1: assert context_products == [] else: assert p1 in context_products assert p2 not in context_products assert p3 in context_products assert p4 in context_products assert p5 not in context_products # test discount3 plugin = DiscountedProductsPlugin({"discounts": [discount3.pk], "count": 10}) assert plugin.get_context_data(context)["products"] == [] discount2.active = True discount2.save() # test both discount1 and discount2 plugin = DiscountedProductsPlugin({"discounts": [discount1.pk, discount2.pk], "count": 10}) context_products = plugin.get_context_data(context)["products"] assert p1 in context_products assert p2 in context_products assert p3 in context_products assert p4 in context_products assert p5 in context_products # test the plugin form with override_current_theme_class(None): theme = get_current_theme(shop) cell = LayoutCell(theme, DiscountedProductsPlugin.identifier, sizes={"md": 8}) lcfg = LayoutCellFormGroup(layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/"))) # not valid, products are required assert not lcfg.is_valid() lcfg = LayoutCellFormGroup( data={ "general-cell_width": "8", "general-cell_align": "pull-right", "plugin-discounts": [discount1.pk, discount2.pk], "plugin-count": 6 }, layout_cell=cell, theme=theme, request=apply_request_middleware(rf.get("/")) ) assert lcfg.is_valid() lcfg.save() assert cell.config["discounts"] == [discount1.pk, discount2.pk]
def test_plugin_naming(): with plugin_override(): cell = LayoutCell(FauxTheme, TextPlugin.identifier) assert cell.plugin_name == TextPlugin.name
def test_null_cell_render(): cell = LayoutCell(FauxTheme, None) assert not cell.plugin_class assert not cell.instantiate_plugin() assert not cell.render(None) # Should render nothing whatsoever!
def test_missing_plugin_render(): plugin_id = printable_gibberish() cell = LayoutCell(FauxTheme, plugin_identifier=plugin_id) assert not cell.plugin_class assert not cell.instantiate_plugin() assert ("%s?" % plugin_id) in cell.render(None) # Should render a "whut?" comment