Example #1
0
def test_get_random_products_cache_bump():
    from shuup.front.template_helpers import general

    supplier = get_default_supplier()
    shop = get_default_shop()

    products = [create_product("sku-%d" % x, supplier=supplier, shop=shop) for x in range(2)]
    children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(2)]

    for child in children:
        child.link_to_parent(products[0])

    context = get_jinja_context()
    cache.clear()
    set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)
    def set_cache_value(key, value, timeout=None):
        if "random_products" in key:
            return set_cached_value_mock(key, value, timeout)

    with mock.patch.object(context_cache, "set_cached_value", new=set_cache_value):
        assert set_cached_value_mock.call_count == 0

        assert general.get_random_products(context, n_products=10)
        assert set_cached_value_mock.call_count == 1

        # call again, the cache should be returned instead and the set_cached_value shouldn't be called again
        assert general.get_random_products(context, n_products=10)
        assert set_cached_value_mock.call_count == 1

        # change a shop product, the cache should be bumped
        ShopProduct.objects.filter(shop=shop).first().save()
        assert general.get_random_products(context, n_products=10)
        assert set_cached_value_mock.call_count == 2
Example #2
0
def test_get_random_products_sale_only():
    from shuup.front.template_helpers import general

    supplier = get_default_supplier()
    shop = get_default_shop()
    product = create_product("sku-1", supplier=supplier, shop=shop, default_price=10)
    context = get_jinja_context()

    cache.clear()
    for cache_test in range(2):
        assert len(general.get_random_products(context, n_products=10)) == 1
        assert len(general.get_random_products(context, n_products=10, sale_items_only=True)) == 0

    # add a catalog campaign discount for the product
    from shuup.campaigns.models import CatalogCampaign
    from shuup.campaigns.models.catalog_filters import ProductFilter
    from shuup.campaigns.models.product_effects import ProductDiscountAmount
    campaign = CatalogCampaign.objects.create(shop=shop, public_name="test", name="test", active=True)
    product_filter = ProductFilter.objects.create()
    product_filter.products.add(product)
    campaign.filters.add(product_filter)
    ProductDiscountAmount.objects.create(campaign=campaign, discount_amount=0.1)

    cache.clear()
    for cache_test in range(2):
        assert len(general.get_random_products(context, n_products=10, sale_items_only=True)) == 1
        assert product in general.get_random_products(context, n_products=10, sale_items_only=True)
def test_get_random_products():
    supplier = get_default_supplier()
    shop = get_default_shop()    
    products = [create_product("sku-%d" % x, supplier=supplier, shop=shop) for x in range(2)]
    children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(2)]
    for child in children:
        child.link_to_parent(products[0])
    context = get_jinja_context()
    # only 2 parent products exist
    assert len(list(general.get_random_products(context, n_products=10))) == 2
Example #4
0
    def get_context_data(self, context):
        type = self.config.get("type", "newest")
        count = self.config.get("count", 4)
        orderable_only = self.config.get("orderable_only", True)
        if type == "newest":
            products = get_newest_products(context, count, orderable_only)
        elif type == "best_selling":
            products = get_best_selling_products(context, count, orderable_only=orderable_only)
        elif type == "random":
            products = get_random_products(context, count, orderable_only)
        else:
            products = []

        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "products": products
        }
def test_get_random_products():
    from shuup.front.template_helpers import general

    supplier = get_default_supplier()
    shop = get_default_shop()

    products = [create_product("sku-%d" % x, supplier=supplier, shop=shop) for x in range(2)]
    children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(2)]

    for child in children:
        child.link_to_parent(products[0])

    context = get_jinja_context()
    random_products = list(general.get_random_products(context, n_products=10))
    assert len(random_products) == 2

    # only 2 parent products exist
    assert products[0] in random_products
    assert products[1] in random_products
Example #6
0
    def get_context_data(self, context):
        highlight_type = self.config.get("type", HighlightType.NEWEST.value)
        count = self.config.get("count", 4)
        orderable_only = self.config.get("orderable_only", True)

        if highlight_type == HighlightType.NEWEST.value:
            products = get_newest_products(context, count, orderable_only)
        elif highlight_type == HighlightType.BEST_SELLING.value:
            products = get_best_selling_products(
                context,
                count,
                orderable_only=orderable_only,
            )
        elif highlight_type == HighlightType.RANDOM.value:
            products = get_random_products(context, count, orderable_only)
        else:
            products = []

        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "products": products
        }
def test_get_random_products():
    populate_if_required()
    context = get_jinja_context()
    assert len(list(general.get_random_products(context, n_products=4))) == 4