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_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)

    children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(5)]
    for child in children:
        child.link_to_parent(product_3)
        create_order_with_product(child, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)
    cache.clear()
    # Third product now sold in greatest quantity
    assert product_3 == general.get_best_selling_products(context, n_products=n_products)[0]
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

    # Make order unorderable
    shop_product = product.get_shop_instance(shop)
    shop_product.visibility = ShopProductVisibility.NOT_VISIBLE
    shop_product.save()
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=2))) == 0
Example #4
0
def test_get_best_selling_products():
    from shuup.front.template_helpers import general
    context = get_jinja_context()

    # No products sold
    assert len(list(general.get_best_selling_products(context, n_products=3))) == 0
    shop = get_default_shop()

    supplier = get_default_supplier()
    supplier2 = Supplier.objects.create(name="supplier2", enabled=True, is_approved=True)
    supplier3 = Supplier.objects.create(name="supplier3", enabled=True, is_approved=True)
    supplier2.shops.add(shop)
    supplier3.shops.add(shop)

    product1 = create_product("product1", shop, supplier, 10)
    product2 = create_product("product2", shop, supplier, 20)
    create_order_with_product(product1, supplier, quantity=1, taxless_base_unit_price=10, shop=shop)
    create_order_with_product(product2, supplier, quantity=2, taxless_base_unit_price=20, shop=shop)

    cache.clear()
    # Two products sold
    for cache_test in range(2):
        best_selling_products = list(general.get_best_selling_products(context, n_products=3))
        assert len(best_selling_products) == 2
        assert product1 in best_selling_products
        assert product2 in best_selling_products

    # Make order unorderable
    shop_product = product1.get_shop_instance(shop)
    shop_product.visibility = ShopProductVisibility.NOT_VISIBLE
    shop_product.save()

    cache.clear()
    for cache_test in range(2):
        best_selling_products = list(general.get_best_selling_products(context, n_products=3))
        assert len(best_selling_products) == 1
        assert product1 not in best_selling_products
        assert product2 in best_selling_products

    # add a new product with discounted amount
    product3 = create_product("product3", supplier=supplier, shop=shop, default_price=30)
    create_order_with_product(product3, supplier, quantity=1, taxless_base_unit_price=30, shop=shop)
    from shuup.customer_group_pricing.models import CgpDiscount
    CgpDiscount.objects.create(
        shop=shop,
        product=product3,
        group=AnonymousContact.get_default_group(),
        discount_amount_value=5
    )
    cache.clear()
    for cache_test in range(2):
        best_selling_products = list(general.get_best_selling_products(context, n_products=3, orderable_only=True))
        assert len(best_selling_products) == 2
        assert product1 not in best_selling_products
        assert product2 in best_selling_products
        assert product3 in best_selling_products
Example #5
0
def test_best_selling_products_with_multiple_orders():
    from shuup.front.template_helpers import general

    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, default_price=price)
    product_2 = create_product("test-sku-2", supplier=supplier, shop=shop, default_price=price)
    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)

    # Two initial products sold
    for cache_test in range(2):
        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, default_price=price)
    create_order_with_product(product_3, supplier, quantity=2, taxless_base_unit_price=price, shop=shop)

    # Third product sold in greater quantity
    cache.clear()
    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
    for cache_test in range(2):
        assert product_3 not in general.get_best_selling_products(context, n_products=n_products)

    children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(5)]
    for child in children:
        child.link_to_parent(product_3)
        create_order_with_product(child, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)

    cache.clear()
    # Third product now sold in greatest quantity
    for cache_test in range(2):
        assert product_3 == general.get_best_selling_products(context, n_products=n_products)[0]

    # add a new product with discounted amount
    product_4 = create_product("test-sku-4", supplier=supplier, shop=shop, default_price=price)
    create_order_with_product(product_4, supplier, quantity=2, taxless_base_unit_price=price, shop=shop)
    from shuup.customer_group_pricing.models import CgpDiscount
    CgpDiscount.objects.create(
        shop=shop,
        product=product_4,
        group=AnonymousContact.get_default_group(),
        discount_amount_value=(price * 0.1)
    )
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
Example #7
0
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)
Example #8
0
    def get_context_data(self, context):
        request = context["request"]
        plugin_type = self.config.get("type", HighlightType.NEWEST.value)
        count = self.config.get("count", 5)
        cutoff_days = self.config.get("cutoff_days", 30)
        cache_timeout = self.config.get("cache_timeout", 0)
        orderable_only = False

        products = []
        if request.is_ajax():
            if plugin_type == HighlightType.NEWEST.value:
                products = get_newest_products(context, count, orderable_only)
            elif plugin_type == HighlightType.BEST_SELLING.value:
                products = get_best_selling_products(context, count, cutoff_days, orderable_only)
            elif plugin_type == HighlightType.RANDOM.value:
                products = get_random_products(context, count, orderable_only)

        return {
            "request": request,
            "title": self.get_translated_value("title"),
            "products": products,
            "data_url": reverse(
                "shuup:xtheme-product-highlight",
                kwargs=dict(
                    plugin_type=plugin_type,
                    cutoff_days=cutoff_days,
                    count=count,
                    cache_timeout=cache_timeout
                )
            ),
        }
Example #9
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)
        sale_items_only = self.config.get("sale_items_only", False)

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

        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "products": products
        }
def test_get_best_selling_products():
    context = get_jinja_context()

    # No products sold
    cache.clear()
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=3))) == 0

    supplier = get_default_supplier()
    shop = get_default_shop()
    product1 = create_product("product1", shop, supplier, 10)
    product2 = create_product("product2", shop, supplier, 20)
    create_order_with_product(product1,
                              supplier,
                              quantity=1,
                              taxless_base_unit_price=10,
                              shop=shop)
    create_order_with_product(product2,
                              supplier,
                              quantity=2,
                              taxless_base_unit_price=20,
                              shop=shop)

    # Two products sold
    cache.clear()
    for time in range(2):
        best_selling_products = list(
            general.get_best_selling_products(context, n_products=3))
        assert len(best_selling_products) == 2
        assert product1 in best_selling_products
        assert product2 in best_selling_products

    # Make order unorderable
    shop_product = product1.get_shop_instance(shop)
    shop_product.visibility = ShopProductVisibility.NOT_VISIBLE
    shop_product.save()

    cache.clear()
    for time in range(2):
        best_selling_products = list(
            general.get_best_selling_products(context, n_products=3))
        assert len(best_selling_products) == 1
        assert product1 not in best_selling_products
        assert product2 in best_selling_products
Example #11
0
def test_get_best_selling_products_cache_bump():
    supplier = get_default_supplier()
    shop = get_default_shop()
    shop2 = get_shop(identifier="shop2")
    product1 = create_product("product1", shop, supplier, 10)
    product2 = create_product("product2", shop, supplier, 20)
    product3 = create_product("product3", shop2, supplier, 20)
    shop1_product1 = product1.get_shop_instance(shop)
    shop2_product3 = product3.get_shop_instance(shop2)

    create_order_with_product(product1, supplier, quantity=1, taxless_base_unit_price=10, shop=shop)
    create_order_with_product(product2, supplier, quantity=2, taxless_base_unit_price=20, shop=shop)
    create_order_with_product(product3, supplier, quantity=2, taxless_base_unit_price=30, shop=shop2)

    cache.clear()
    from shuup.front.template_helpers import general
    context = get_jinja_context()

    set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)
    def set_cache_value(key, value, timeout=None):
        if "best_selling_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_best_selling_products(context, 2, orderable_only=False)
        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_best_selling_products(context, 2, orderable_only=False)
        assert set_cached_value_mock.call_count == 1

        # save the shop2 product and see whether the cache is bumped
        shop2_product3.save()

        # neve SHOULD be changed and things should be cached
        assert general.get_best_selling_products(context, 2, orderable_only=False)
        assert set_cached_value_mock.call_count == 1

        # now change shop1 product, it should bump the cache
        shop1_product1.save()
        assert general.get_best_selling_products(context, 2, orderable_only=False)
        assert set_cached_value_mock.call_count == 2
Example #12
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
        }
Example #13
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_best_selling_products_per_supplier():
    from shuup.front.template_helpers import general

    context = get_jinja_context()

    # No products sold
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=3))) == 0
    shop = get_default_shop()

    supplier = get_default_supplier()
    supplier2 = Supplier.objects.create(name="supplier2", enabled=True)
    supplier2.shops.add(shop)

    product1 = create_product("product1", shop, supplier, 10)
    product2 = create_product("product2", shop, supplier2, 20)
    create_order_with_product(product1,
                              supplier,
                              quantity=1,
                              taxless_base_unit_price=10,
                              shop=shop)
    create_order_with_product(product2,
                              supplier2,
                              quantity=2,
                              taxless_base_unit_price=20,
                              shop=shop)

    cache.clear()
    # Two products sold, but only one supplier
    for cache_test in range(2):
        best_selling_products = list(
            general.get_best_selling_products(context,
                                              n_products=3,
                                              supplier=supplier))
        assert len(best_selling_products) == 1
        assert product1 in best_selling_products
        assert product2 not in best_selling_products

    # Two products sold, but only one supplier
    for cache_test in range(2):
        best_selling_products = list(
            general.get_best_selling_products(context,
                                              n_products=3,
                                              supplier=supplier2))
        assert len(best_selling_products) == 1
        assert product1 not in best_selling_products
        assert product2 in best_selling_products

    # Make product 1 also sold by supplier2
    shop_product = product1.get_shop_instance(shop)
    shop_product.suppliers.add(supplier2)

    cache.clear()
    for cache_test in range(2):
        best_selling_products = list(
            general.get_best_selling_products(context,
                                              n_products=3,
                                              supplier=supplier2))
        assert len(best_selling_products
                   ) == 1  # Since there isn't any orders yet for supplier 2
        assert product2 in best_selling_products

    create_order_with_product(product1,
                              supplier2,
                              quantity=2,
                              taxless_base_unit_price=20,
                              shop=shop)
    cache.clear()
    for cache_test in range(2):
        best_selling_products = list(
            general.get_best_selling_products(context,
                                              n_products=3,
                                              supplier=supplier2))
        assert len(best_selling_products) == 2
        assert product1 in best_selling_products
        assert product2 in best_selling_products
Example #15
0
def test_get_best_selling_products_per_supplier(reindex_catalog):
    from shuup.front.template_helpers import general

    context = get_jinja_context()

    reindex_catalog()

    # No products sold
    assert len(list(general.get_best_selling_products(context,
                                                      n_products=3))) == 0
    shop = get_default_shop()

    supplier = get_supplier("simple_supplier", shop, name="Supplier 1")
    supplier2 = get_supplier("simple_supplier", shop, name="Supplier 2")

    product1 = create_product("product1", shop, supplier, 10)
    product2 = create_product("product2", shop, supplier2, 20)
    create_order_with_product(product1,
                              supplier,
                              quantity=1,
                              taxless_base_unit_price=10,
                              shop=shop)
    create_order_with_product(product2,
                              supplier2,
                              quantity=2,
                              taxless_base_unit_price=20,
                              shop=shop)

    reindex_catalog()

    # Two products sold, but only one supplier
    best_selling_products = list(
        general.get_best_selling_products(context,
                                          n_products=3,
                                          supplier=supplier))
    assert len(best_selling_products) == 1
    assert product1 in best_selling_products
    assert product2 not in best_selling_products

    # Two products sold, but only one supplier
    cache.clear()
    best_selling_products = list(
        general.get_best_selling_products(context,
                                          n_products=3,
                                          supplier=supplier2))
    assert len(best_selling_products) == 1
    assert product1 not in best_selling_products
    assert product2 in best_selling_products

    # Make product 1 also sold by supplier2
    shop_product = product1.get_shop_instance(shop)
    shop_product.suppliers.add(supplier2)

    reindex_catalog()

    cache.clear()
    best_selling_products = list(
        general.get_best_selling_products(context,
                                          n_products=3,
                                          supplier=supplier2))
    assert len(best_selling_products
               ) == 1  # Since there isn't any orders yet for supplier 2
    assert product2 in best_selling_products

    # get for all suppliers
    cache.clear()
    best_selling_products = list(
        general.get_best_selling_products(context, n_products=3))
    assert len(best_selling_products) == 2
    assert product1 in best_selling_products
    assert product2 in best_selling_products