def test_product_categories(settings): with override_settings(SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES=True): shop_product = get_default_shop_product() shop_product.categories.clear() shop_product.primary_category = None shop_product.save() assert not shop_product.primary_category assert not shop_product.categories.count() category_one = CategoryFactory() category_two = CategoryFactory() shop_product.categories = Category.objects.all() assert shop_product.primary_category # this was automatically populated assert shop_product.primary_category.pk == category_one.pk # it's the first one also shop_product.categories.clear() shop_product.primary_category = category_one shop_product.save() assert shop_product.primary_category == category_one assert category_one in shop_product.categories.all() # test removing shop_product.categories.remove(category_one) shop_product.refresh_from_db() assert not shop_product.categories.exists() shop_product.categories.add(category_one) category_one.soft_delete() assert not shop_product.categories.exists() with override_settings(SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES=False): shop_product.categories.clear() shop_product.primary_category = None shop_product.save() assert not shop_product.primary_category assert not shop_product.categories.count() category_one = CategoryFactory() category_two = CategoryFactory() shop_product.categories = Category.objects.all() assert not shop_product.primary_category # this was NOT automatically populated shop_product.categories.clear() shop_product.primary_category = category_one shop_product.save() assert shop_product.primary_category == category_one assert category_one not in shop_product.categories.all()
def test_category_form_saving(rf): with transaction.atomic(): category = CategoryFactory() form_kwargs = dict(instance=category, languages=("sw",), default_language="sw") form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) form_data = get_form_data(form, prepared=True) for lang, field_map in form.trans_name_map.items(): for dst_field in field_map.values(): form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL" form_kwargs["data"] = form_data form = CategoryBaseForm(**form_kwargs) form.full_clean() form.save() category = form.instance category.set_current_language("sw") assert category.name == "IJWEHGWOHKSL"
def test_basket_category_discount(rf): """ Test that discounting based on product category works. """ request, shop, group = initialize_test(rf, False) price = shop.create_price basket = get_basket(request) supplier = get_default_supplier() category = CategoryFactory() discount_amount_value = 6 single_product_price = 10 def create_category_product(category): product = create_product(printable_gibberish(), shop, supplier, single_product_price) product.primary_category = category sp = ShopProduct.objects.get(product=product, shop=shop) sp.primary_category = category sp.categories.add(category) return product basket_condition = CategoryProductsBasketCondition.objects.create( quantity=2) basket_condition.categories.add(category) campaign = BasketCampaign.objects.create(shop=shop, public_name="test", name="test", active=True) campaign.conditions.add(basket_condition) campaign.save() DiscountFromCategoryProducts.objects.create( campaign=campaign, discount_amount=discount_amount_value, category=category) basket.save() products = [create_category_product(category) for i in range(2)] for product in products: basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) basket.shipping_method = get_shipping_method(shop=shop) basket.save() assert basket.product_count == 2 assert basket_condition.matches(basket=basket, lines=basket.get_lines()) assert campaign.rules_match(basket, basket.get_lines()) assert basket.total_price == price(single_product_price * 2) - price( discount_amount_value * 2)
def test_product_category_discount_percentage_greater_then_products( rf, include_tax): # Buy X amount of Y get Z discount from Y request, shop, _ = initialize_test(rf, include_tax) basket = get_basket(request) supplier = get_default_supplier() single_product_price = "50" discount_percentage = Decimal(1.9) # 190% quantity = 2 # the expected discount amount should not be greater than the products expected_discount_amount = basket.create_price( single_product_price) * quantity category = CategoryFactory() # create basket rule that requires 2 products in basket product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) ShopProduct.objects.get(shop=shop, product=product).categories.add(category) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=quantity) basket.shipping_method = get_shipping_method(shop=shop) basket.save() rule = ProductsInBasketCondition.objects.create(quantity=2) rule.products.add(product) rule.save() campaign = BasketCampaign.objects.create(active=True, shop=shop, name="test", public_name="test") campaign.conditions.add(rule) DiscountFromCategoryProducts.objects.create( campaign=campaign, discount_percentage=discount_percentage, category=category) assert rule.matches(basket, []) basket.uncache() final_lines = basket.get_final_lines() assert len(final_lines ) == 2 # no new lines since the effect touches original lines original_price = basket.create_price(single_product_price) * quantity line = final_lines[0] assert line.discount_amount == expected_discount_amount assert basket.total_price == original_price - expected_discount_amount
def test_category_form_saving(rf): with transaction.atomic(): shop = get_default_shop() category = CategoryFactory() form_kwargs = dict(instance=category, languages=("sw",), default_language="sw") form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) form_data = get_form_data(form, prepared=True) for lang, field_map in form.trans_name_map.items(): for dst_field in field_map.values(): form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL" form_kwargs["data"] = form_data form = CategoryBaseForm(**form_kwargs) form.full_clean() form.save() category = form.instance category.set_current_language("sw") assert category.name == "IJWEHGWOHKSL"
def test_category_form_with_parent(rf, admin_user): with transaction.atomic(): shop = get_default_shop() category1 = CategoryFactory() category2 = CategoryFactory() category2.shops.clear() assert shop not in category2.shops.all() category3 = CategoryFactory() category3.shops.clear() assert shop not in category3.shops.all() request = apply_request_middleware(rf.get("/"), user=admin_user, shop=shop) form_kwargs = dict(instance=category1, request=request, languages=("sw",), default_language="sw") form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) form_data = get_form_data(form, prepared=True) for lang, field_map in form.trans_name_map.items(): for dst_field in field_map.values(): form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL" # Make sure we have right parent options parent_bound_field = [field for field in form.visible_fields() if field.name == "parent"][0] assert len(parent_bound_field.field.choices) == 1 assert parent_bound_field.field.choices[0][0] is None category2.shops.add(shop) form_kwargs["data"] = form_data form = CategoryBaseForm(**form_kwargs) assert isinstance(form, CategoryBaseForm) # Make sure category 2 is now in parent options parent_bound_field = [field for field in form.visible_fields() if field.name == "parent"][0] assert len(parent_bound_field.field.choices) == 2 assert parent_bound_field.field.choices[1][0] == category2.id # Make sure saving the form still works form.full_clean() form.save() category = form.instance category.set_current_language("sw") assert category.name == "IJWEHGWOHKSL"
def test_undiscounted_effects(rf, include_tax): request, shop, _ = initialize_test(rf, include_tax) basket = get_basket(request) supplier = get_default_supplier(shop) single_product_price = Decimal(50) discounted_product_quantity = 4 normal_priced_product_quantity = 2 discount_percentage = Decimal(0.2) # 20% discount_amount = basket.create_price(single_product_price * normal_priced_product_quantity * discount_percentage) category = CategoryFactory() discounted_product = create_product( printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price ) second_product = create_product( printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price ) ShopProduct.objects.get(shop=shop, product=discounted_product).categories.add(category) ShopProduct.objects.get(shop=shop, product=second_product).categories.add(category) basket.add_product(supplier=supplier, shop=shop, product=discounted_product, quantity=discounted_product_quantity) basket.add_product(supplier=supplier, shop=shop, product=second_product, quantity=normal_priced_product_quantity) basket.shipping_method = get_shipping_method(shop=shop) basket.save() # Store basket price before any campaigns exists original_price = basket.total_price # CatalogCampaign catalog_campaign = CatalogCampaign.objects.create(active=True, shop=shop, name="test", public_name="test") # Limit catalog campaign to "discounted_product" product_filter = ProductFilter.objects.create() product_filter.products.add(discounted_product) catalog_campaign.filters.add(product_filter) # BasketCampaign campaign = BasketCampaign.objects.create(active=True, shop=shop, name="test2", public_name="test2") final_lines = basket.get_final_lines() assert len(final_lines) == 3 # Discount based on undiscounted product values DiscountPercentageFromUndiscounted.objects.create(campaign=campaign, discount_percentage=discount_percentage) basket.uncache() final_lines = basket.get_final_lines() assert len(final_lines) == 4 discounted_basket_price = original_price - discount_amount assert basket.total_price.as_rounded() == discounted_basket_price.as_rounded()
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_product_category_discount_amount_with_minimum_price(rf): # Buy X amount of Y get Z discount from Y request, shop, _ = initialize_test(rf, False) basket = get_basket(request) supplier = get_default_supplier(shop) single_product_price = Decimal("50") single_product_min_price = Decimal("40") discount_amount_value = Decimal("200") # will exceed the minimum price quantity = 2 # the expected discount amount should not be greater than the products expected_discount_amount = basket.create_price(single_product_price) * quantity category = CategoryFactory() # create basket rule that requires 2 products in basket product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) shop_product = ShopProduct.objects.get(shop=shop, product=product) shop_product.minimum_price_value = single_product_min_price shop_product.save() shop_product.categories.add(category) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=quantity) basket.shipping_method = get_shipping_method(shop=shop) basket.save() rule = ProductsInBasketCondition.objects.create(quantity=2) rule.products.add(product) rule.save() campaign = BasketCampaign.objects.create(active=True, shop=shop, name="test", public_name="test") campaign.conditions.add(rule) DiscountFromCategoryProducts.objects.create(campaign=campaign, discount_amount=discount_amount_value, category=category) assert rule.matches(basket, []) basket.uncache() # the discount amount should not exceed the minimum price. as the configued discount # will exceed, it should limit the discount amount final_lines = basket.get_final_lines() expected_discount_amount = basket.create_price((single_product_price - single_product_min_price) * quantity) original_price = basket.create_price(single_product_price) * quantity line = final_lines[0] assert line.discount_amount == expected_discount_amount assert basket.total_price == original_price - expected_discount_amount
def _get_shop_product_sample_data(): return { "shop": get_default_shop().id, "suppliers": [get_default_supplier().id], "visibility": ShopProductVisibility.ALWAYS_VISIBLE.value, "purchasable": False, "visibility_limit": ProductVisibility.VISIBLE_TO_ALL.value, "visibility_groups": [ create_random_contact_group().id, create_random_contact_group().id, ], "backorder_maximum": 0, "purchase_multiple": 1, "minimum_purchase_quantity": 1, "limit_shipping_methods": False, "limit_payment_methods": False, "shipping_methods": [], "payment_methods": [], "primary_category": get_default_category().id, "categories": [get_default_category().id, CategoryFactory().id], "default_price_value": 12.45, "minimum_price_value": 5.35, "translations": { "en": { "status_text": "available soon", }, "pt-br": { "status_text": "disponivel logo", } }, }
def test_discount_no_limits(rf, include_tax): # check whether is it possible to earn money buying from us # adding lots of effects request, shop, _ = initialize_test(rf, include_tax) basket = get_basket(request) supplier = get_default_supplier(shop) single_product_price = Decimal(50) quantity = 4 discount_amount = (single_product_price * quantity * 2) discount_percentage = Decimal(1.9) # 190% second_product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) # the expected discount amount should not be greater than the products expected_discount_amount = basket.create_price( single_product_price) * quantity category = CategoryFactory() # create basket rule that requires 2 products in basket product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) ShopProduct.objects.get(shop=shop, product=product).categories.add(category) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=quantity) basket.shipping_method = get_shipping_method(shop=shop) basket.save() rule = ProductsInBasketCondition.objects.create(quantity=2) rule.products.add(product) rule.save() # BasketCampaign campaign = BasketCampaign.objects.create(active=True, shop=shop, name="test", public_name="test") campaign.conditions.add(rule) # effect 1 - categories from products DiscountFromCategoryProducts.objects.create( campaign=campaign, discount_percentage=discount_percentage, category=category) # effect 2 - discount from products effect2 = DiscountFromProduct.objects.create( campaign=campaign, discount_amount=discount_amount) effect2.products.add(product) # effect 3 - basket discount BasketDiscountAmount.objects.create(campaign=campaign, discount_amount=discount_amount) # effect 4 - basket discount percetage BasketDiscountPercentage.objects.create( campaign=campaign, discount_percentage=discount_percentage) basket.uncache() final_lines = basket.get_final_lines() assert len(final_lines) == 3 original_price = basket.create_price(single_product_price) * quantity line = final_lines[0] assert line.discount_amount == expected_discount_amount assert basket.total_price == original_price - expected_discount_amount # effect free - aaaww yes, it's free effect_free = FreeProductLine.objects.create(campaign=campaign, quantity=1) effect_free.products.add(second_product) basket.uncache() final_lines = basket.get_final_lines() assert len(final_lines) == 4 line = final_lines[0] assert line.discount_amount == expected_discount_amount assert basket.total_price == original_price - expected_discount_amount # CatalogCampaign catalog_campaign = CatalogCampaign.objects.create(active=True, shop=shop, name="test2", public_name="test") product_filter = ProductFilter.objects.create() product_filter.products.add(product) catalog_campaign.filters.add(product_filter) # effect 5 - ProductDiscountAmount ProductDiscountAmount.objects.create(campaign=catalog_campaign, discount_amount=discount_amount) # effct 6 - ProductDiscountPercentage ProductDiscountPercentage.objects.create( campaign=catalog_campaign, discount_percentage=discount_percentage) basket.uncache() final_lines = basket.get_final_lines() assert len(final_lines) == 4 line = final_lines[0] assert line.discount_amount == expected_discount_amount assert basket.total_price == original_price - expected_discount_amount
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite shop.staff_members.add(admin_user) parent = create_product("ComplexVarParent", shop=shop, supplier=get_default_supplier()) sizes = [("%sL" % ("X" * x)) for x in range(4)] for size in sizes: child = create_product("ComplexVarChild-%s" % size, shop=shop, supplier=get_default_supplier()) child.link_to_parent(parent, variables={"size": size}) shop_product = parent.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content, "lxml")) # Needed for Django 1.8 tests to pass post.update({ "shop1-default_price_value": "42", "images-TOTAL_FORMS": "0", "media-TOTAL_FORMS": "0", "base-name__fi": "test", "base-name__it": "test", "base-name__ja": "test", "base-name__pt-br": "test", "base-name__zh-hans": "test", "base-name__es": "test", }) post_data = {"shop1-primary_category": [], "shop1-categories": []} post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { "shop1-default_price_value": 12, "shop1-primary_category": cat.pk, "shop1-categories": [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v with patch("django.db.transaction.on_commit", new=atomic_commit_mock): request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.primary_category # the catalog price was indexed catalog_price = ProductCatalogPrice.objects.filter( shop=shop_product.shop, product=shop_product.product).first() assert catalog_price.price_value == shop_product.default_price_value if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = {"shop1-primary_category": [], "shop1-categories": []} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = {"shop1-primary_category": [], "shop1-categories": [cat.pk]} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { "shop1-primary_category": [], "shop1-categories": [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category # Test for showing alert of validation issues view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) soup = BeautifulSoup(content, "lxml") alert = soup.find_all("div", {"class": "validation-issues-alert"}) assert not alert with override_provides( "admin_product_validator", ["shuup.testing.admin_product_validator:TestAdminProductValidator"], ): view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) soup = BeautifulSoup(content, "lxml") alert = soup.find_all( "div", {"class": "validation-issues-alert alert alert-danger"}) assert alert alert_danger = soup.find_all("div", {"class": "alert-danger"}) assert alert_danger alert = soup.find_all( "div", {"class": "validation-issues-alert alert alert-warning"}) alert_div = alert[0] strong = alert_div.find_all("strong") assert strong script = alert_div.find_all("script") assert not script
def test_category_module_search(rf): cm = CategoryModule() category = CategoryFactory() request = rf.get("/") assert not empty_iterable(cm.get_search_results(request, query=category.identifier)) assert empty_iterable(cm.get_search_results(request, query="k"))
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite product = get_default_product() shop_product = product.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content)) # Needed for Django 1.8 tests to pass post.update({ 'shop1-default_price_value': '42', 'images-TOTAL_FORMS': '0', 'media-TOTAL_FORMS': '0', 'base-name__fi': 'test', 'base-name__it': 'test', 'base-name__ja': 'test', 'base-name__pt-br': 'test', 'base-name__zh-hans': 'test', }) post_data = {'shop1-primary_category': [], 'shop1-categories': []} post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-default_price_value': 12, 'shop1-primary_category': [cat.pk], 'shop1-categories': [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = {'shop1-primary_category': [], 'shop1-categories': []} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = {'shop1-primary_category': [], 'shop1-categories': [cat.pk]} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category
def test_product_from_category_plugin(rf, reindex_catalog): 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) reindex_catalog() plugin = ProductsFromCategoryPlugin({ "category": category1.pk, "cache_timeout": 120 }) plugin_context = plugin.get_context_data(get_context(rf, is_ajax=False)) context_products = plugin_context["products"] assert len(context_products) == 0 plugin_context = plugin.get_context_data(get_context(rf)) context_data_url = plugin_context["data_url"] context_products = plugin_context["products"] assert p1 in context_products assert p2 in context_products assert p3 not in context_products check_expected_product_count(context_data_url, 2) check_expected_product_count(context_data_url, 2) # one for checking it is cached # 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, "plugin-cache_timeout": 3600, }, 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_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite shop.staff_members.add(admin_user) parent = create_product("ComplexVarParent", shop=shop, supplier=get_default_supplier()) sizes = [("%sL" % ("X" * x)) for x in range(4)] for size in sizes: child = create_product("ComplexVarChild-%s" % size, shop=shop, supplier=get_default_supplier()) child.link_to_parent(parent, variables={"size": size}) shop_product = parent.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content, "lxml")) # Needed for Django 1.8 tests to pass post.update({ 'shop1-default_price_value': '42', 'images-TOTAL_FORMS': '0', 'media-TOTAL_FORMS': '0', 'base-name__fi': 'test', 'base-name__it': 'test', 'base-name__ja': 'test', 'base-name__pt-br': 'test', 'base-name__zh-hans': 'test', }) post_data = {'shop1-primary_category': [], 'shop1-categories': []} post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-default_price_value': 12, 'shop1-primary_category': cat.pk, 'shop1-categories': [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.primary_category if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = {'shop1-primary_category': [], 'shop1-categories': []} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = {'shop1-primary_category': [], 'shop1-categories': [cat.pk]} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category