def test_category_links_plugin_with_customer(rf, show_all_categories): """ Test plugin for categories that is visible for certain group """ shop = get_default_shop() group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = rf.get("/") request.shop = get_default_shop() apply_request_middleware(request) request.customer = customer category = get_default_category() category.status = CategoryStatus.VISIBLE category.visibility = CategoryVisibility.VISIBLE_TO_GROUPS category.visibility_groups.add(group) category.shops.add(shop) category.save() vars = {"request": request} context = get_jinja_context(**vars) plugin = CategoryLinksPlugin({"categories": [category.pk], "show_all_categories": show_all_categories}) assert category.is_visible(customer) assert category in plugin.get_context_data(context)["categories"] customer_without_groups = create_random_person() customer_without_groups.groups.clear() assert not category.is_visible(customer_without_groups) request.customer = customer_without_groups context = get_jinja_context(**vars) assert category not in plugin.get_context_data(context)["categories"]
def test_get_pagination_variables(): populate_if_required() # Makes sure there is at least 30 products in db products = Product.objects.all()[:19] assert len(products) == 19 vars = {"products": products} context = get_jinja_context(**vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert variables["page"].number == 1 assert len(variables["objects"]) == 4 context = get_jinja_context(path="/?page=5", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert variables["page"].number == 5 assert len(variables["objects"]) == 3 variables = general.get_pagination_variables(context, context["products"], limit=20) assert not variables["is_paginated"] assert variables["page"].number == 1 context = get_jinja_context(path="/?page=42", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert variables["page"].number == 5 assert len(variables["objects"]) == 3 vars = {"products": []} context = get_jinja_context(path="/", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert not variables["is_paginated"]
def test_best_selling_products_with_multiple_orders(): context = get_jinja_context() supplier = get_default_supplier() shop = get_default_shop() n_products = 2 price = 10 product_1 = create_product("test-sku-1", supplier=supplier, shop=shop) product_2 = create_product("test-sku-2", supplier=supplier, shop=shop) create_order_with_product(product_1, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) create_order_with_product(product_2, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) cache.clear() # Two initial products sold assert product_1 in general.get_best_selling_products(context, n_products=n_products) assert product_2 in general.get_best_selling_products(context, n_products=n_products) product_3 = create_product("test-sku-3", supplier=supplier, shop=shop) create_order_with_product(product_3, supplier, quantity=2, taxless_base_unit_price=price, shop=shop) cache.clear() # Third product sold in greater quantity assert product_3 in general.get_best_selling_products(context, n_products=n_products) create_order_with_product(product_1, supplier, quantity=4, taxless_base_unit_price=price, shop=shop) create_order_with_product(product_2, supplier, quantity=4, taxless_base_unit_price=price, shop=shop) cache.clear() # Third product outsold by first two products assert product_3 not in general.get_best_selling_products(context, n_products=n_products)
def test_get_visible_products_orderable_only(): context = get_jinja_context() shop = get_default_shop() simple_supplier = get_simple_supplier() n_products = 2 # Create product without stock product = create_product( "test-sku", supplier=simple_supplier, shop=shop, stock_behavior=StockBehavior.STOCKED ) assert len(general.get_visible_products(context, n_products, orderable_only=True)) == 0 assert len(general.get_visible_products(context, n_products, orderable_only=False)) == 1 # Increase stock on product quantity = product.get_shop_instance(shop).minimum_purchase_quantity simple_supplier.adjust_stock(product.id, quantity) assert len(general.get_visible_products(context, n_products, orderable_only=True)) == 1 assert len(general.get_visible_products(context, n_products, orderable_only=False)) == 1 # Decrease stock on product simple_supplier.adjust_stock(product.id, -quantity) assert len(general.get_visible_products(context, n_products, orderable_only=True)) == 0 assert len(general.get_visible_products(context, n_products, orderable_only=False)) == 1
def test_social_media_plugin_ordering(): """ Test that social media plugin ordering works as expected """ context = get_jinja_context() icon_classes = SocialMediaLinksPlugin.icon_classes link_1_type = "Facebook" link_1 = { "url": "http://www.facebook.com", "ordering": 2, } link_2_type = "Twitter" link_2 = { "url": "http://www.twitter.com", "ordering": 1, } links = { link_1_type: link_1, link_2_type: link_2, } plugin = SocialMediaLinksPlugin({"links": links}) assert len(plugin.get_links()) == 2 # Make sure link 2 comes first assert plugin.get_links()[0][2] == link_2["url"]
def test_get_visible_products_filter(): context = get_jinja_context() shop = get_default_shop() supplier = get_default_supplier() product_1 = create_product( "test-sku-1", supplier=supplier, shop=shop, ) product_2 = create_product( "test-sku-2", supplier=supplier, shop=shop, ) filter_dict = {"id": product_1.id} product_list = general.get_visible_products(context, n_products=2, filter_dict=filter_dict) assert product_1 in product_list assert product_2 not in product_list # Test also with orderable_only False product_list = general.get_visible_products(context, n_products=2, filter_dict=filter_dict, orderable_only=False) assert product_1 in product_list assert product_2 not in product_list
def test_cross_sell_plugin_type(): """ Test that template helper returns correct number of cross sells when shop contains multiple relation types """ shop = get_default_shop() supplier = get_default_supplier() product = create_product("test-sku", shop=shop, supplier=supplier, stock_behavior=StockBehavior.UNSTOCKED) context = get_jinja_context(product=product) type_counts = ((ProductCrossSellType.RELATED, 1), (ProductCrossSellType.RECOMMENDED, 2), (ProductCrossSellType.BOUGHT_WITH, 3)) # Create cross sell products and relations in different quantities for type, count in type_counts: _create_cross_sell_products(product, shop, supplier, type, count) assert ProductCrossSell.objects.filter(product1=product, type=type).count() == count # Make sure quantities returned by plugin match for type, count in type_counts: assert len( list( product_helpers.get_product_cross_sells( context, product, type, count))) == count
def get_context(rf, customer=None): request = rf.get("/") request.shop = get_default_shop() apply_request_middleware(request) if customer: request.customer = customer vars = {"request": request} return get_jinja_context(**vars)
def test_cross_sell_plugin_count(): shop = get_default_shop() product = create_product("test-sku", shop=shop, stock_behavior=StockBehavior.UNSTOCKED) context = get_jinja_context(product=product) total_count = 5 trim_count = 3 _create_cross_sell_products(product, shop, "related", total_count) assert ProductCrossSell.objects.filter(product1=product, type="related").count() == total_count assert len(list(product_helpers.get_product_cross_sells(context, product, type, trim_count))) == trim_count
def test_page_links_plugin_show_all(): """ Test that show_all_pages forces plugin to return all visible pages """ context = get_jinja_context() page = create_page(eternal=True, visible_in_menu=True) plugin = PageLinksPlugin({"show_all_pages": False}) assert not plugin.get_context_data(context)["pages"] plugin = PageLinksPlugin({"show_all_pages": True}) assert page in plugin.get_context_data(context)["pages"]
def test_cross_sell_plugin_count(): shop = get_default_shop() product = create_product("test-sku", shop=shop, stock_behavior=StockBehavior.UNSTOCKED) context = get_jinja_context(product=product) total_count = 5 trim_count = 3 type = ProductCrossSellType.RELATED _create_cross_sell_products(product, shop, type, total_count) assert ProductCrossSell.objects.filter(product1=product, type=type).count() == total_count assert len(list(product_helpers.get_product_cross_sells(context, product, type, trim_count))) == trim_count
def test_get_best_selling_products(): context = get_jinja_context() cache.clear() # No products sold assert len(list(general.get_best_selling_products(context, n_products=2))) == 0 supplier = get_default_supplier() shop = get_default_shop() product = get_default_product() create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=10, shop=shop) cache.clear() # One product sold assert len(list(general.get_best_selling_products(context, n_products=2))) == 1
def test_page_links_plugin_visible_in_menu(): """ Make sure plugin correctly filters out pages that are not set to be visible in menus """ context = get_jinja_context() page = create_page(eternal=True, visible_in_menu=True) plugin = PageLinksPlugin({"pages": [page.pk]}) assert page in plugin.get_context_data(context)["pages"] page.visible_in_menu = False page.save() assert page not in plugin.get_context_data(context)["pages"]
def test_best_selling_products_with_multiple_orders(): context = get_jinja_context() supplier = get_default_supplier() shop = get_default_shop() n_products = 2 price = 10 product_1 = create_product("test-sku-1", supplier=supplier, shop=shop) product_2 = create_product("test-sku-2", supplier=supplier, shop=shop) create_order_with_product(product_1, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) create_order_with_product(product_2, supplier, quantity=1, taxless_base_unit_price=price, shop=shop) cache.clear() # Two initial products sold assert product_1 in general.get_best_selling_products( context, n_products=n_products) assert product_2 in general.get_best_selling_products( context, n_products=n_products) product_3 = create_product("test-sku-3", supplier=supplier, shop=shop) create_order_with_product(product_3, supplier, quantity=2, taxless_base_unit_price=price, shop=shop) cache.clear() # Third product sold in greater quantity assert product_3 in general.get_best_selling_products( context, n_products=n_products) create_order_with_product(product_1, supplier, quantity=4, taxless_base_unit_price=price, shop=shop) create_order_with_product(product_2, supplier, quantity=4, taxless_base_unit_price=price, shop=shop) cache.clear() # Third product outsold by first two products assert product_3 not in general.get_best_selling_products( context, n_products=n_products)
def test_cross_sell_plugin_renders(): """ Test that the plugin renders a product """ shop = get_default_shop() product = create_product("test-sku", shop=shop, stock_behavior=StockBehavior.UNSTOCKED) computed = create_product("test-computed-sku", shop=shop, stock_behavior=StockBehavior.UNSTOCKED) type = ProductCrossSellType.COMPUTED ProductCrossSell.objects.create(product1=product, product2=computed, type=type) assert ProductCrossSell.objects.filter(product1=product, type=type).count() == 1 context = get_jinja_context(product=product) rendered = ProductCrossSellsPlugin({"type": type}).render(context) assert computed.sku in rendered
def test_get_visible_products_filter(): context = get_jinja_context() shop = get_default_shop() supplier = get_default_supplier() product_1 = create_product("test-sku-1", supplier=supplier, shop=shop) product_2 = create_product("test-sku-2", supplier=supplier, shop=shop) filter_dict = {"id": product_1.id} product_list = general.get_visible_products(context, n_products=2, filter_dict=filter_dict) assert product_1 in product_list assert product_2 not in product_list # Test also with orderable_only False product_list = general.get_visible_products(context, n_products=2, filter_dict=filter_dict, orderable_only=False) assert product_1 in product_list assert product_2 not in product_list
def test_cross_sell_plugin_type(): """ Test that template helper returns correct number of cross sells when shop contains multiple relation types """ shop = get_default_shop() product = create_product("test-sku", shop=shop, stock_behavior=StockBehavior.UNSTOCKED) context = get_jinja_context(product=product) type_counts = (("related", 1), ("recommended", 2), ("computed", 3)) # Create cross sell products and relations in different quantities for type, count in type_counts: _create_cross_sell_products(product, shop, type, count) assert ProductCrossSell.objects.filter(product1=product, type=type).count() == count # Make sure quantites returned by plugin match for type, count in type_counts: assert len(list(product_helpers.get_product_cross_sells(context, product, type, count))) == count
def test_page_links_plugin_hide_expired(show_all_pages): """ Make sure plugin correctly filters out expired pages based on plugin configuration """ context = get_jinja_context() page = create_page(eternal=True, visible_in_menu=True) another_page = create_page(eternal=True, visible_in_menu=True) plugin = PageLinksPlugin({"pages": [page.pk, another_page.pk], "show_all_pages": show_all_pages}) assert page in plugin.get_context_data(context)["pages"] page.available_from = None page.available_to = None page.save() assert page in plugin.get_context_data(context)["pages"] plugin.config["hide_expired"] = True pages_in_context = plugin.get_context_data(context)["pages"] assert page not in pages_in_context assert another_page in pages_in_context
def test_page_links_plugin_hide_expired(show_all_pages): """ Make sure plugin correctly filters out expired pages based on plugin configuration """ context = get_jinja_context() page = create_page(eternal=True, visible_in_menu=True) another_page = create_page(eternal=True, visible_in_menu=True) plugin = PageLinksPlugin({ "pages": [page.pk, another_page.pk], "show_all_pages": show_all_pages }) assert page in plugin.get_context_data(context)["pages"] page.available_from = None page.available_to = None page.save() assert page in plugin.get_context_data(context)["pages"] plugin.config["hide_expired"] = True pages_in_context = plugin.get_context_data(context)["pages"] assert page not in pages_in_context assert another_page in pages_in_context
def test_get_visible_products_orderable_only(): context = get_jinja_context() shop = get_default_shop() simple_supplier = get_simple_supplier() n_products = 2 # Create product without stock product = create_product("test-sku", supplier=simple_supplier, shop=shop, stock_behavior=StockBehavior.STOCKED) assert len( general.get_visible_products(context, n_products, orderable_only=True)) == 0 assert len( general.get_visible_products(context, n_products, orderable_only=False)) == 1 # Increase stock on product quantity = product.get_shop_instance(shop).minimum_purchase_quantity simple_supplier.adjust_stock(product.id, quantity) assert len( general.get_visible_products(context, n_products, orderable_only=True)) == 1 assert len( general.get_visible_products(context, n_products, orderable_only=False)) == 1 # Decrease stock on product simple_supplier.adjust_stock(product.id, -quantity) assert len( general.get_visible_products(context, n_products, orderable_only=True)) == 0 assert len( general.get_visible_products(context, n_products, orderable_only=False)) == 1
def test_get_root_categories(): populate_if_required() context = get_jinja_context() for root in general.get_root_categories(context=context): assert not root.parent_id
def test_get_all_manufacturers(): populate_if_required() context = get_jinja_context() # TODO: This is not a good test assert len(general.get_all_manufacturers(context)) == 0
def test_get_random_products(): populate_if_required() context = get_jinja_context() assert len(list(general.get_random_products(context, n_products=4))) == 4
def test_get_root_categories(): populate_if_required() context = get_jinja_context() general.get_root_categories(context)