def test_cross_sell_plugin_cache_bump():
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("test-sku", shop=shop, supplier=supplier)
    context = get_jinja_context(product=product)
    total_count = 5
    trim_count = 3

    type = ProductCrossSellType.RELATED
    _create_cross_sell_products(product, shop, supplier, type, total_count)
    assert ProductCrossSell.objects.filter(product1=product, type=type).count() == total_count

    set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)
    def set_cache_value(key, value, timeout=None):
        if "product_cross_sells" 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 product_helpers.get_product_cross_sells(context, product, type, trim_count)
        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 product_helpers.get_product_cross_sells(context, product, type, trim_count)
        assert set_cached_value_mock.call_count == 1

        # bump caches
        ProductCrossSell.objects.filter(product1=product, type=type).first().save()
        assert product_helpers.get_product_cross_sells(context, product, type, trim_count)
        assert set_cached_value_mock.call_count == 2
Esempio n. 2
0
def test_cross_sell_plugin_cache_bump():
    shop = get_default_shop()
    supplier = get_default_supplier()
    product = create_product("test-sku", shop=shop, supplier=supplier)
    context = get_jinja_context(product=product)
    total_count = 5
    trim_count = 3

    type = ProductCrossSellType.RELATED
    _create_cross_sell_products(product, shop, supplier, type, total_count)
    assert ProductCrossSell.objects.filter(product1=product, type=type).count() == total_count

    set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)

    def set_cache_value(key, value, timeout=None):
        if "product_cross_sells" 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 product_helpers.get_product_cross_sells(context, product, type, trim_count)
        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 product_helpers.get_product_cross_sells(context, product, type, trim_count)
        assert set_cached_value_mock.call_count == 1

        # bump caches
        ProductCrossSell.objects.filter(product1=product, type=type).first().save()
        assert product_helpers.get_product_cross_sells(context, product, type, trim_count)
        assert set_cached_value_mock.call_count == 2
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)
    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:
        cache.clear()
        assert len(
            list(
                product_helpers.get_product_cross_sells(
                    context, product, type, count))) == count
def test_cross_sell_plugin_count():
    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)
    total_count = 5
    trim_count = 3

    type = ProductCrossSellType.RELATED
    _create_cross_sell_products(product, shop, supplier, 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_cross_sell_plugin_count():
    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)
    total_count = 5
    trim_count = 3

    type = ProductCrossSellType.RELATED
    _create_cross_sell_products(product, shop, supplier, 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
Esempio n. 6
0
    def get_context_data(self, context):
        request = context["request"]
        products = []
        use_variation_parents = self.config.get("use_variation_parents", False)
        count = self.config.get("count", 5)
        cache_timeout = self.config.get("cache_timeout", 0)
        product = context.get("product", self.config.get("product"))
        orderable_only = self.config.get("orderable_only", False)
        relation_type = self.config.get("type")

        try:
            relation_type = map_relation_type(relation_type)
        except LookupError:
            relation_type = ProductCrossSellType.RELATED

        if request.is_ajax() and product:
            if not isinstance(product, Product):
                product = Product.objects.filter(id=product).first()

            if product:
                products = get_product_cross_sells(
                    context,
                    product,
                    relation_type,
                    count=count,
                    orderable_only=orderable_only,
                    use_variation_parents=use_variation_parents,
                )

        return {
            "request":
            context["request"],
            "title":
            self.get_translated_value("title"),
            "products":
            products,
            "orderable_only":
            self.config.get("orderable_only", False),
            "data_url":
            reverse(
                "shuup:xtheme-product-cross-sells-highlight",
                kwargs=dict(
                    product_id=product.id
                    if isinstance(product, Product) else product,
                    relation_type=relation_type.label,
                    use_parents=(1 if use_variation_parents else 0),
                    count=count,
                    cache_timeout=cache_timeout,
                ),
            ) if product else "/",
        }
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