Exemplo n.º 1
0
def test_list_view_with_multiple_suppliers(rf, admin_user):
    shop = factories.get_default_shop()

    product = factories.create_product(sku="test", shop=shop)
    shop_product = product.get_shop_instance(shop)
    shop_product.primary_category = factories.get_default_category()
    shop_product.save()
    shop_product.categories.add(shop_product.primary_category)

    # Also one product with supplier
    supplier = factories.get_default_supplier()
    product2 = factories.create_product(sku="test2", shop=shop, supplier=supplier)
    shop_product2 = product2.get_shop_instance(shop)
    shop_product2.primary_category = factories.get_default_category()
    shop_product2.save()
    shop_product2.categories.add(shop_product.primary_category)

    with override_settings(E-Commerce_ENABLE_MULTIPLE_SUPPLIERS=True):
        view = load("E-Commerce.admin.modules.products.views:ProductListView").as_view()
        request = apply_request_middleware(rf.get("/", {
            "jq": json.dumps({"perPage": 100, "page": 1})
        }), user=admin_user)
        response = view(request)
        assert 200 <= response.status_code < 300

        data = json.loads(response.content.decode("utf-8"))
        product_data = [item for item in data["items"] if item["_id"] == shop_product.pk][0]
        assert product_data["primary_category"] == factories.get_default_category().name
        assert product_data["categories"] == factories.get_default_category().name
        assert product_data["categories"] == factories.get_default_category().name
        assert product_data["suppliers"] == ""

        product_data2 = [item for item in data["items"] if item["_id"] == shop_product2.pk][0]
        assert product_data2["suppliers"] == factories.get_default_supplier().name
Exemplo n.º 2
0
def test_carousel_create(browser, admin_user, live_server, settings):
    shop = factories.get_default_shop()
    filer_image = factories.get_random_filer_image()
    factories.get_default_category()

    initialize_admin_browser_test(browser, live_server, settings, shop=shop)
    wait_until_condition(browser, lambda x: x.is_text_present("Welcome!"))

    assert not Carousel.objects.exists()

    browser.visit(live_server + "/sa/carousels/new")
    wait_until_condition(browser, lambda x: x.is_text_present("New Carousel"))
    browser.fill("base-name", "Carrot")
    click_element(browser, "button[form='carousel_form']")
    wait_until_appeared(browser, "div[class='message success']")

    assert Carousel.objects.count() == 1
    carousel = Carousel.objects.first()

    browser.visit(live_server + "/sa/carousels/%d/" % carousel.pk)
    wait_until_condition(browser, lambda x: x.is_text_present(carousel.name))
    click_element(browser, "a[href='#slides-section']")
    wait_until_appeared(browser, ".slide-add-new-panel")

    # add 1 slide
    click_element(browser, ".slide-add-new-panel")
    wait_until_condition(browser, lambda x: x.is_text_present("Slide 1"))
    wait_until_appeared(browser, "a[href='#collapse1']")
    click_element(browser, "a[href='#collapse1']")

    browser.find_by_css("#slide_1-en [name='slides-__slide_prefix__-caption__en']").fill("New Slide")
    click_element(browser, "[name='slides-__slide_prefix__-category_link'] + .select2")
    wait_until_appeared(browser, ".select2-container #select2-id_slides-__slide_prefix__-category_link-results li")
    click_element(browser, ".select2-container #select2-id_slides-__slide_prefix__-category_link-results li:last-child")

    browser.find_by_css("#slide_1-en [data-dropzone='true']").click()
    wait_until_condition(browser, lambda b: len(b.windows) == 2)
    # change to the media browser window
    browser.windows.current = browser.windows[1]
    # click to select the picture
    wait_until_appeared(browser, "a.file-preview")
    click_element(browser, "a.file-preview")
    # back to the main window
    wait_until_condition(browser, lambda b: len(b.windows) == 1)
    browser.windows.current = browser.windows[0]
    wait_until_appeared(browser, ".dz-image img[alt='%s']" % filer_image.name)

    click_element(browser, "button[form='carousel_form']")
    wait_until_appeared(browser, "div[class='message success']")
Exemplo n.º 3
0
def test_manufacturer_filter_get_fields(rf):
    cache.clear()

    shop = factories.get_default_shop()

    request = apply_request_middleware(rf.get("/"))
    assert ManufacturerProductListFilter().get_fields(request, None) is None

    manufacturer = Manufacturer.objects.create(name="Random Brand Inc")
    assert ManufacturerProductListFilter().get_fields(request, None) is None

    category = factories.get_default_category()
    product = factories.create_product("sku", shop=shop)
    shop_product = product.get_shop_instance(shop=shop)
    shop_product.primary_category = category
    shop_product.save()

    assert ManufacturerProductListFilter().get_fields(request, category) is None

    # Now once we link manufacturer to product we should get
    # form field for manufacturer
    product.manufacturer = manufacturer
    product.save()
    form_field = ManufacturerProductListFilter().get_fields(request, category)[0][1]
    assert form_field is not None
    assert form_field.label == "Manufacturers"

    with override_settings(E-Commerce_FRONT_OVERRIDE_SORTS_AND_FILTERS_LABELS_LOGIC={"manufacturers": "Brands"}):
        form_field = ManufacturerProductListFilter().get_fields(request, category)[0][1]
        assert form_field is not None
        assert form_field.label == "Brands"
Exemplo n.º 4
0
def test_category_product_discount_with_contact_group(rf):
    default_price = 10
    request, product = _init_test_for_product(rf, default_price)

    category = factories.get_default_category()
    product.get_shop_instance(request.shop).categories.add(category)
    product_discount_amount = 4
    discount = Discount.objects.create(active=True, category=category, discount_amount_value=product_discount_amount)
    discount.shops.add(request.shop)
    assert product.get_price_info(request).price == request.shop.create_price(default_price - product_discount_amount)

    # Adding contact group limitation to the discount should
    # make the discount go away
    contact_group = factories.get_default_customer_group()
    discount.contact_group = contact_group
    discount.save()
    assert product.get_price_info(request).price == request.shop.create_price(default_price)

    # Let's set contact for request with the group created
    # and let's see the discount coming back
    random_contact = factories.create_random_person()
    request.customer = random_contact
    assert product.get_price_info(request).price == request.shop.create_price(default_price)
    random_contact.groups.add(contact_group)
    assert request.customer == random_contact
    assert product.get_price_info(request).price == request.shop.create_price(default_price - product_discount_amount)
Exemplo n.º 5
0
def test_category_product_discount_with_contact(rf):
    default_price = 10
    request, product = _init_test_for_product(rf, default_price)

    category = factories.get_default_category()
    category.shop_products.add(product.get_shop_instance(request.shop))
    product_discount_amount = 4
    discount = Discount.objects.create(active=True, category=category, discount_amount_value=product_discount_amount)
    discount.shops.add(request.shop)
    assert product.get_price_info(request).price == request.shop.create_price(default_price - product_discount_amount)

    # Adding contact condition to the discount should make
    # the discount go away.
    random_contact = factories.create_random_person()
    discount.contact = random_contact
    discount.save()
    assert request.customer != random_contact
    assert product.get_price_info(request).price == request.shop.create_price(default_price)

    # Let's set the new contact as request customer and we
    # should get the discount back.
    request.customer = random_contact
    assert product.get_price_info(request).price == request.shop.create_price(default_price - product_discount_amount)

    # Another contact should still only get the price without discount
    another_contact = factories.create_random_person()
    request.customer = another_contact
    assert product.get_price_info(request).price == request.shop.create_price(default_price)
Exemplo n.º 6
0
def test_default_layout():
    vc = _get_basic_view_config()
    product = factories.create_product("test", name="Test product name")
    category = factories.get_default_category()

    placeholder_name = "wildhorse"
    context = {"request": get_request(), "product": product, "category": category}
    layout = vc.get_placeholder_layout(Layout, placeholder_name)
    assert isinstance(layout, Layout)
    assert layout.get_help_text({}) == layout.get_help_text(context)

    _add_plugin_and_test_save(vc, layout, placeholder_name, context)
Exemplo n.º 7
0
def test_price_info_cache_bump(rf):
    initial_price = 10
    shop = factories.get_default_shop()
    tax = factories.get_default_tax()
    tax_class = factories.get_default_tax_class()
    product = factories.create_product(
        "product",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=initial_price
    )
    request = apply_request_middleware(rf.get("/"))

    def assert_cache_product():
        cache_price_info(request, product, 1, product.get_price_info(request))
        assert get_cached_price_info(request, product, 1).price == shop.create_price(initial_price)

    def assert_nothing_is_cached():
        # nothing is cached
        assert get_cached_price_info(request, product, 1) is None

    # cache the item
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    tax.save()
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    tax_class.save()
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    product.save()
    assert_nothing_is_cached()
    assert_cache_product()

    shop_product = product.get_shop_instance(shop)

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.save()
    assert_nothing_is_cached()
    assert_cache_product()

    category = factories.get_default_category()

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.categories.add(category)
    assert_nothing_is_cached()
    assert_cache_product()
Exemplo n.º 8
0
def test_product_price_range_filter():
    shop = factories.get_default_shop()
    product = factories.get_default_product()
    category = factories.get_default_category()
    shop_product = product.get_shop_instance(shop)
    shop_product.default_price_value = 10
    shop_product.categories.add(category)
    shop_product.save()

    client = SmartClient()
    config = {
        "filter_products_by_price": True,
        "filter_products_by_price_range_min": 5,
        "filter_products_by_price_range_max": 15,
        "filter_products_by_price_range_size": 5
    }
    set_configuration(category=category, data=config)
    url = reverse('E-Commerce:category', kwargs={'pk': category.pk, 'slug': category.slug})
    response, soup = client.response_and_soup(url)
    assert response.status_code == 200
    assert soup.find(id="product-%d" % product.id)
    price_range_select = soup.find(id="id_price_range")
    # as the configuration is not set to override shop default configuration
    # this field shouldn't be there..
    assert price_range_select is None

    # make the category configuration override the shop's default config
    config.update({"override_default_configuration": True})
    set_configuration(category=category, data=config)
    url = reverse('E-Commerce:category', kwargs={'pk': category.pk, 'slug': category.slug})
    response, soup = client.response_and_soup(url)
    assert response.status_code == 200
    assert soup.find(id="product-%d" % product.id)
    price_range_select = soup.find(id="id_price_range")
    price_ranges = price_range_select.find_all("option")
    assert len(price_ranges) == 4

    # filter products with prices above $15
    filtered_url = "{}?price_range={}".format(url, price_ranges[-1].attrs["value"])
    response, soup = client.response_and_soup(filtered_url)
    assert response.status_code == 200
    assert not soup.find(id="product-%d" % product.id)

    # explicitly disable the override
    config.update({"override_default_configuration": False})
    set_configuration(category=category, data=config)
    url = reverse('E-Commerce:category', kwargs={'pk': category.pk, 'slug': category.slug})
    response, soup = client.response_and_soup(url)
    assert response.status_code == 200
    assert soup.find(id="product-%d" % product.id)
    price_range_select = soup.find(id="id_price_range")
    assert price_range_select is None
Exemplo n.º 9
0
def test_category_layout():
    vc = _get_basic_view_config()
    category = factories.get_default_category()

    placeholder_name = "japanese"
    context = {"category": category}
    layout = vc.get_placeholder_layout(CategoryLayout, placeholder_name, context=context)
    assert isinstance(layout, CategoryLayout)
    assert layout.get_help_text({}) == ""  # Invalid context for help text
    assert category.name in layout.get_help_text(context)
    _assert_empty_layout(layout, placeholder_name)

    _add_plugin_and_test_save(vc, layout, placeholder_name, context)
def test_supplier_filter_get_fields(rf):
    shop = factories.get_default_shop()
    request = apply_request_middleware(rf.get("/"))
    category = factories.get_default_category()

    supplier = Supplier.objects.create(name="Mike Inc")
    supplier.shops.add(shop)
    assert SupplierProductListFilter().get_fields(request, None) is None

    product = factories.create_product("sku", shop=shop)
    shop_product = product.get_shop_instance(shop=shop)
    shop_product.primary_category = category
    shop_product.save()

    assert SupplierProductListFilter().get_fields(request, category) is None

    # Now once we link manufacturer to product we should get
    # form field for manufacturer
    shop_product.suppliers.add(supplier)
    form_field = SupplierProductListFilter().get_fields(request, category)[0][1]
    assert form_field is not None
    assert form_field.label == "Suppliers"
    assert len(form_field.widget.choices) == 1

    # Add second supplier for new product
    supplier2 = Supplier.objects.create(name="K Inc")
    supplier2.shops.add(shop)
    new_product = factories.create_product("sku1", shop=shop)
    new_shop_product = new_product.get_shop_instance(shop=shop)
    new_shop_product.suppliers.add(supplier2)

    # Still one with category since shop product not linked to category
    form_field = SupplierProductListFilter().get_fields(request, category)[0][1]
    assert form_field is not None
    assert len(form_field.widget.choices) == 1

    # Without category we get two results
    form_field = SupplierProductListFilter().get_fields(request, None)[0][1]
    assert form_field is not None
    assert len(form_field.widget.choices) == 2

    new_shop_product.categories.add(category)  # primary category shouldn't be required

    # Now with or without category we get 2 results
    form_field = SupplierProductListFilter().get_fields(request, category)[0][1]
    assert form_field is not None
    assert len(form_field.widget.choices) == 2

    form_field = SupplierProductListFilter().get_fields(request, None)[0][1]
    assert form_field is not None
    assert len(form_field.widget.choices) == 2
Exemplo n.º 11
0
def test_list_view(rf, admin_user):
    shop = factories.get_default_shop()

    product = factories.create_product(sku="test", shop=shop)
    shop_product = product.get_shop_instance(shop)
    shop_product.primary_category = factories.get_default_category()
    shop_product.save()
    shop_product.categories.add(shop_product.primary_category)

    view = load("E-Commerce.admin.modules.products.views:ProductListView").as_view()
    request = apply_request_middleware(rf.get("/", {
        "jq": json.dumps({"perPage": 100, "page": 1})
    }), user=admin_user)
    response = view(request)
    assert 200 <= response.status_code < 300

    data = json.loads(response.content.decode("utf-8"))
    product_data = [item for item in data["items"] if item["_id"] == shop_product.pk][0]
    assert product_data["primary_category"] == factories.get_default_category().name
    assert product_data["categories"] == factories.get_default_category().name
    assert product_data["categories"] == factories.get_default_category().name

    # Suppliers not available by default since multiple suppliers is not enabled
    assert "suppliers" not in product_data
Exemplo n.º 12
0
def test_category_selection_excluded(rf):
    default_price = 10
    request, product = _init_test_for_product(rf, default_price)

    category = factories.get_default_category()
    product_discount_amount = 4
    discount = Discount.objects.create(
        active=True, exclude_selected_category=True, category=category,
        discount_amount_value=product_discount_amount)
    discount.shops.add(request.shop)
    assert product.get_price_info(request).price == request.shop.create_price(default_price - product_discount_amount)

    # Setting default category for product disables the discount
    product.get_shop_instance(request.shop).categories.add(category)
    assert product.get_price_info(request).price == request.shop.create_price(default_price)
Exemplo n.º 13
0
def test_suppliers_filter_get_fields(rf):
    cache.clear()
    shop = factories.get_default_shop()

    request = apply_request_middleware(rf.get("/"))
    assert SupplierProductListFilter().get_fields(request, None) is None

    supplier = Supplier.objects.create(name="Favorite brands dot com")
    supplier.shops.add(shop)
    assert Supplier.objects.enabled().exists()
    assert SupplierProductListFilter().get_fields(request, None) is None

    category = factories.get_default_category()
    product = factories.create_product("sku", shop=shop)
    shop_product = product.get_shop_instance(shop=shop)
    shop_product.primary_category = category
    shop_product.save()

    assert SupplierProductListFilter().get_fields(request, category) is None

    # Now once we link supplier to product we should get
    # form field for manufacturer
    shop_product.suppliers.add(supplier)
    form_field = SupplierProductListFilter().get_fields(request, category)[0][1]
    assert form_field is not None
    assert form_field.label == "Suppliers"

    with override_settings(E-Commerce_FRONT_OVERRIDE_SORTS_AND_FILTERS_LABELS_LOGIC={"supplier": "Filter by suppliers"}):
        form_field = SupplierProductListFilter().get_fields(request, category)[0][1]
        assert form_field is not None
        assert form_field.label == "Filter by suppliers"
        assert isinstance(form_field, forms.ModelChoiceField)

        configuration = get_configuration(shop, category)
        configuration.update({
            SupplierProductListFilter.enable_multiselect_key: True
        })

        set_configuration(shop, category, configuration)
        form_field = SupplierProductListFilter().get_fields(request, category)[0][1]
        assert form_field is not None
        assert form_field.label == "Filter by suppliers"
        assert isinstance(form_field, CommaSeparatedListField)

        cache.clear()
Exemplo n.º 14
0
def test_list_view(rf, admin_user):
    shop = factories.get_default_shop()
    supplier = get_simple_supplier()

    product = factories.create_product(sku="test", shop=shop, supplier=supplier)
    shop_product = product.get_shop_instance(shop)
    shop_product.primary_category = factories.get_default_category()
    shop_product.save()
    shop_product.categories.add(shop_product.primary_category)

    view = load("E-Commerce.simple_supplier.admin_module.views:StocksListView").as_view()
    request = apply_request_middleware(rf.get("/", {
        "jq": json.dumps({"perPage": 100, "page": 1})
    }), user=admin_user)
    response = view(request)
    assert 200 <= response.status_code < 300

    data = json.loads(response.content.decode("utf-8"))
    for item in data["items"]:
        assert item["_url"] == ""
Exemplo n.º 15
0
def test_get_placeholder_layouts():
    vc = _get_basic_view_config()
    product = factories.create_product("test", name="Test product name")
    category = factories.get_default_category()

    placeholder_name = "hermit"
    context = {"request": get_request(), "product": product, "category": category}

    provides = []
    with override_provides("xtheme_layout", provides):
        assert len(vc.get_placeholder_layouts(context, placeholder_name)) == 1  # Default layout

    provides.append("E-Commerce.xtheme.layout.ProductLayout")
    with override_provides("xtheme_layout", provides):
        assert len(vc.get_placeholder_layouts(context, placeholder_name)) == 2

    provides.append("E-Commerce.xtheme.layout.CategoryLayout")
    with override_provides("xtheme_layout", provides):
        assert len(vc.get_placeholder_layouts(context, placeholder_name)) == 3

    provides.append("E-Commerce.xtheme.layout.AnonymousContactLayout")
    with override_provides("xtheme_layout", provides):
        assert len(vc.get_placeholder_layouts(context, placeholder_name)) == 4
Exemplo n.º 16
0
def test_matching_product_discount_with_category(rf):
    default_price = 10
    request, product = _init_test_for_product(rf, default_price)

    product_discount_amount = 4
    discount = Discount.objects.create(active=True, product=product, discount_amount_value=product_discount_amount)
    discount.shops.add(request.shop)
    assert product.get_price_info(request).price == request.shop.create_price(default_price - product_discount_amount)

    another_product = factories.create_product("test1", shop=request.shop, default_price=default_price)
    category = factories.get_default_category()
    another_product.get_shop_instance(request.shop).categories.add(category)
    assert another_product.get_price_info(request).price == request.shop.create_price(default_price)

    # Let's create category with some discounts
    category_discount_amount = 8
    discount = Discount.objects.create(active=True, category=category, discount_amount_value=category_discount_amount)
    discount.shops.add(request.shop)
    assert another_product.get_price_info(request).price == (
        request.shop.create_price(default_price - category_discount_amount))

    # Category discount is bigger than product discount
    # so let's set this category for the first product too
    product.get_shop_instance(request.shop).categories.add(category)
    assert product.get_price_info(request).price == request.shop.create_price(default_price - category_discount_amount)

    # Let's create worse discount for category and make sure we have
    # multiple lines matching for these products and the best discount
    # is activated.
    discount = Discount.objects.create(
        active=True, category=category, discount_amount_value=category_discount_amount - 3)
    discount.shops.add(request.shop)

    assert another_product.get_price_info(request).price == (
           request.shop.create_price(default_price - category_discount_amount))
    assert product.get_price_info(request).price == request.shop.create_price(default_price - category_discount_amount)
Exemplo n.º 17
0
def test_category_page(client):
    factories.get_default_shop()
    category = factories.get_default_category()
    response = client.get(reverse('E-Commerce:category', kwargs={'pk': category.pk, 'slug': category.slug}))
    assert b'no such element' not in response.content, 'All items are not rendered correctly'
Exemplo n.º 18
0
def test_many_price_info_cache_bump(rf):
    initial_price = 10
    shop = factories.get_default_shop()
    tax = factories.get_default_tax()
    tax_class = factories.get_default_tax_class()
    product = factories.create_product(
        "product",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=initial_price
    )
    child1 = factories.create_product(
        "child1",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=5
    )
    child2 = factories.create_product(
        "child2",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=9
    )
    child1.link_to_parent(product, variables={"color": "red"})
    child2.link_to_parent(product, variables={"color": "blue"})

    request = apply_request_middleware(rf.get("/"))
    child1_pi = child1.get_price_info(request)
    child2_pi = child1.get_price_info(request)

    def assert_cache_products():
        cache_many_price_info(request, product, 1, [child1_pi, child2_pi])
        assert get_many_cached_price_info(request, product, 1)[0].price == child1_pi.price
        assert get_many_cached_price_info(request, product, 1)[1].price == child2_pi.price

    def assert_nothing_is_cached():
        # nothing is cached
        assert get_many_cached_price_info(request, product, 1) is None

    # cache the item
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    tax.save()
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    tax_class.save()
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    product.save()
    assert_nothing_is_cached()
    assert_cache_products()

    shop_product = product.get_shop_instance(shop)

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.save()
    assert_nothing_is_cached()
    assert_cache_products()

    category = factories.get_default_category()

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.categories.add(category)
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    supplier = shop_product.suppliers.first()
    supplier.enabled = False
    supplier.save()
    assert_nothing_is_cached()
    assert_cache_products()
def test_category_detail_multiselect_supplier_filters(client):
    shop = factories.get_default_shop()

    # Activate show supplier info for front
    assert ThemeSettings.objects.count() == 1
    theme_settings = ThemeSettings.objects.first()
    theme_settings.update_settings({"show_supplier_info": True})

    category = factories.get_default_category()

    # Important! Activate supplier filter.
    set_configuration(
        shop=shop,
        data={
            "filter_products_by_supplier": True,
            "filter_products_by_supplier_ordering": 1,
            "filter_products_by_supplier_multiselect_enabled": True
        }
    )

    supplier_data = [
        ("Johnny Inc", 0.5),
        ("Mike Inc", 0.9),
        ("Simon Inc", 0.8),
    ]

    for name, percentage_from_original_price in supplier_data:
        supplier = Supplier.objects.create(name=name)
        supplier.shops.add(shop)
        sku = name
        price_value = 10
        product = factories.create_product(sku, shop=shop, default_price=price_value)
        shop_product = product.get_shop_instance(shop)
        shop_product.suppliers.add(supplier)
        shop_product.primary_category = category
        shop_product.categories.add(category)
        shop_product.save()

        supplier_price = (percentage_from_original_price * price_value)
        SupplierPrice.objects.create(supplier=supplier, shop=shop, product=product, amount_value=supplier_price)

    strategy = "E-Commerce.testing.supplier_pricing.supplier_strategy:CheapestSupplierPriceSupplierStrategy"
    with override_settings(E-Commerce_PRICING_MODULE="supplier_pricing", E-Commerce_SHOP_PRODUCT_SUPPLIERS_STRATEGY=strategy):
        with override_current_theme_class(ClassicGrayTheme, shop):  # Ensure settings is refreshed from DB
            johnny_supplier = Supplier.objects.filter(name="Johnny Inc").first()
            mike_supplier = Supplier.objects.filter(name="Mike Inc").first()
            simon_supplier = Supplier.objects.filter(name="Simon Inc").first()

            soup = _get_category_detail_soup_multiselect(
                client, category, [johnny_supplier.pk]
            )
            assert len(soup.findAll("div", {"class": "single-product"})) == 1

            soup = _get_category_detail_soup_multiselect(
                client, category, [johnny_supplier.pk, mike_supplier.pk]
            )
            assert len(soup.findAll("div", {"class": "single-product"})) == 2

            soup = _get_category_detail_soup_multiselect(
                client, category, [johnny_supplier.pk, mike_supplier.pk, simon_supplier.pk]
            )
            assert len(soup.findAll("div", {"class": "single-product"})) == 3
def test_catalog_campaign_sync():
    shop = factories.get_default_shop()
    supplier = factories.get_default_supplier()
    default_price = 100
    product1 = factories.create_product("test1", shop=shop, supplier=supplier, default_price=default_price)
    product2 = factories.create_product("test2", shop=shop, supplier=supplier, default_price=default_price)
    product3 = factories.create_product("test3", shop=shop, supplier=supplier, default_price=default_price)
    category = factories.get_default_category()
    shop_product = product1.get_shop_instance(shop)
    shop_product.primary_category = category
    shop_product.save()
    shop_product.categories.add(category)

    contact1 = factories.create_random_person()

    contact2 = factories.create_random_person()
    contact_group = factories.get_default_customer_group()
    contact2.groups.add(contact_group)

    happy_hour1_weekdays = "0,1"  # Mon, Tue
    happy_hour1_start = datetime.time(21)
    happy_hour1_end = datetime.time(3)
    happy_hour1_condition = HourCondition.objects.create(
        days=happy_hour1_weekdays, hour_start=happy_hour1_start, hour_end=happy_hour1_end)

    happy_hour2_weekdays = "2,6"  # Wed, Sun
    happy_hour2_start = datetime.time(14)
    happy_hour2_end = datetime.time(16)
    happy_hour2_condition = HourCondition.objects.create(
        days=happy_hour2_weekdays, hour_start=happy_hour2_start, hour_end=happy_hour2_end)

    discount_amount_value = 50
    discount_percentage = decimal.Decimal("0.35")
    _create_catalog_campaign_for_products(
        shop, [product1], discount_amount_value, happy_hour1_condition)
    _create_catalog_campaign_for_products(
        shop, [product2, product3], discount_amount_value)
    _create_catalog_campaign_for_category(
        shop, category, discount_percentage)
    _create_catalog_campaign_for_contact(
        shop, product1, contact1, discount_amount_value)
    _create_catalog_campaign_for_contact_group(
        shop, [product1, product2, product3], contact_group, discount_percentage, happy_hour2_condition)

    call_command("import_catalog_campaigns", *[], **{})

    # From first campaign we should get 1 discount with happy hour
    # From second campaign we should get 2 discounts
    # From third campaign we should get 1 discount
    # From fourth campaign we should get also 1 discount
    # From last campaign we should get 3 discounts with happy hour
    assert Discount.objects.count() == 8

    # There should be 2 happy hours in total
    assert HappyHour.objects.count() == 2

    # From first happy hour there should be 4 ranges
    # Mon 21-23, Tue 0-3, Tue 21-23, Wed 0-3
    # From second happy hour there should be 2 ranges
    # Wed 14-16 and Sun 14-16
    assert TimeRange.objects.count() == 6

    # Let's go through all our 8 discounts to make sure all is good
    first_discount = Discount.objects.filter(
        product=product1, category__isnull=True, contact__isnull=True, contact_group__isnull=True).first()
    assert first_discount.happy_hours.count() == 1
    assert first_discount.discount_amount_value == discount_amount_value

    second_discount = Discount.objects.filter(
        product=product2, category__isnull=True, contact__isnull=True, contact_group__isnull=True).first()
    assert second_discount.happy_hours.count() == 0
    assert second_discount.discount_amount_value == discount_amount_value

    third_discount = Discount.objects.filter(
        product=product3, category__isnull=True, contact__isnull=True, contact_group__isnull=True).first()
    assert third_discount.happy_hours.count() == 0
    assert third_discount.discount_amount_value == discount_amount_value

    category_discount = Discount.objects.filter(
        product__isnull=True, category=category, contact__isnull=True, contact_group__isnull=True).first()
    assert category_discount.happy_hours.count() == 0
    assert category_discount.discount_percentage == discount_percentage

    contact_discount = Discount.objects.filter(
        product=product1, category__isnull=True, contact=contact1, contact_group__isnull=True).first()
    assert contact_discount.discount_amount_value == discount_amount_value

    product1_contact_group_discount = Discount.objects.filter(
        product=product1, category__isnull=True, contact__isnull=True, contact_group=contact_group).first()
    assert product1_contact_group_discount.happy_hours.count() == 1
    assert product1_contact_group_discount.discount_percentage == discount_percentage

    product2_contact_group_discount = Discount.objects.filter(
        product=product2, category__isnull=True, contact__isnull=True, contact_group=contact_group).first()
    assert product2_contact_group_discount.happy_hours.count() == 1
    assert product2_contact_group_discount.discount_percentage == discount_percentage

    product3_contact_group_discount = Discount.objects.filter(
        product=product3, category__isnull=True, contact__isnull=True, contact_group=contact_group).first()
    assert product3_contact_group_discount.happy_hours.count() == 1
    assert product3_contact_group_discount.discount_percentage == discount_percentage
def test_category_detail_filters(client):
    shop = factories.get_default_shop()

    # Activate show supplier info for front
    assert ThemeSettings.objects.count() == 1
    theme_settings = ThemeSettings.objects.first()
    theme_settings.update_settings({"show_supplier_info": True})

    category = factories.get_default_category()

    # Important! Activate supplier filter.
    set_configuration(
        shop=shop,
        data={
            "filter_products_by_supplier": True,
            "filter_products_by_supplier_ordering": 1,
        }
    )

    product_data = [
        ("laptop", 1500),
        ("keyboard", 150),
        ("mouse", 150)
    ]
    products = []
    for sku, price_value in product_data:
        products.append(factories.create_product(sku, shop=shop, default_price=price_value))

    supplier_data = [
        ("Johnny Inc", 0.5),
        ("Mike Inc", 0.9),
        ("Simon Inc", 0.8),
    ]
    for name, percentage_from_original_price in supplier_data:
        supplier = Supplier.objects.create(name=name)
        supplier.shops.add(shop)

        for product in products:
            shop_product = product.get_shop_instance(shop)
            shop_product.suppliers.add(supplier)
            shop_product.primary_category = category
            shop_product.categories.add(category)
            shop_product.save()

            supplier_price = (
                percentage_from_original_price * [price for sku, price in product_data if product.sku == sku][0])
            SupplierPrice.objects.create(supplier=supplier, shop=shop, product=product, amount_value=supplier_price)

    strategy = "E-Commerce.testing.supplier_pricing.supplier_strategy:CheapestSupplierPriceSupplierStrategy"
    with override_settings(E-Commerce_PRICING_MODULE="supplier_pricing", E-Commerce_SHOP_PRODUCT_SUPPLIERS_STRATEGY=strategy):
        with override_current_theme_class(ClassicGrayTheme, shop):  # Ensure settings is refreshed from DB

            laptop = [product for product in products if product.sku == "laptop"][0]
            keyboard = [product for product in products if product.sku == "keyboard"][0]
            mouse = [product for product in products if product.sku == "mouse"][0]
            # Let's get products for Johnny
            supplier_johnny = Supplier.objects.filter(name="Johnny Inc").first()
            soup = _get_category_detail_soup(client, category, supplier_johnny.pk)

            laptop_product_box = soup.find("div", {"id": "product-%s" % laptop.pk})
            _assert_supplier_info(laptop_product_box, "Johnny Inc")
            _assert_product_price(laptop_product_box, 750)

            # Now here when the category view is filtered based on supplier
            # the product urls should lead to supplier product url so we
            # can show details and prices for correct supplier.
            _assert_product_url(laptop_product_box, supplier_johnny, laptop)

            # Let's test rest of the products and suppliers
            keyboard_product_box = soup.find("div", {"id": "product-%s" % keyboard.pk})
            _assert_supplier_info(keyboard_product_box, "Johnny Inc")
            _assert_product_price(keyboard_product_box, 75)
            _assert_product_url(keyboard_product_box, supplier_johnny, keyboard)

            mike_supplier = Supplier.objects.filter(name="Mike Inc").first()
            soup = _get_category_detail_soup(client, category, mike_supplier.pk)
            keyboard_product_box = soup.find("div", {"id": "product-%s" % keyboard.pk})
            _assert_supplier_info(keyboard_product_box, "Mike Inc")
            _assert_product_price(keyboard_product_box, 135)
            _assert_product_url(keyboard_product_box, mike_supplier, keyboard)

            simon_supplier = Supplier.objects.filter(name="Simon Inc").first()
            soup = _get_category_detail_soup(client, category, simon_supplier.pk)
            mouse_product_box = soup.find("div", {"id": "product-%s" % mouse.pk})
            _assert_supplier_info(mouse_product_box, "Simon Inc")
            _assert_product_price(mouse_product_box, 120)
            _assert_product_url(mouse_product_box, simon_supplier, mouse)
Exemplo n.º 22
0
def test_category_detail(client):
    shop = factories.get_default_shop()

    # Activate show supplier info for front
    assert ThemeSettings.objects.count() == 1
    theme_settings = ThemeSettings.objects.first()
    theme_settings.update_settings({"show_supplier_info": True})

    # Activate supplier filters to prove they don't effect results
    # without actually filtering something. There is separate tests
    # to do the more closer tests.
    set_configuration(
        shop=shop,
        data={
            "filter_products_by_supplier": True,
            "filter_products_by_supplier_ordering": 1,
        }
    )

    category = factories.get_default_category()

    product_data = [
        ("laptop", 1500),
        ("keyboard", 150),
        ("mouse", 150)
    ]
    products = []
    for sku, price_value in product_data:
        products.append(factories.create_product(sku, shop=shop, default_price=price_value))

    supplier_data = [
        ("Johnny Inc", 0.5),
        ("Mike Inc", 0.9),
        ("Simon Inc", 0.8),
    ]
    for name, percentage_from_original_price in supplier_data:
        supplier = Supplier.objects.create(name=name)

        for product in products:
            shop_product = product.get_shop_instance(shop)
            shop_product.suppliers.add(supplier)
            shop_product.primary_category = category
            shop_product.save()

            supplier_price = (
                percentage_from_original_price * [price for sku, price in product_data if product.sku == sku][0])
            SupplierPrice.objects.create(supplier=supplier, shop=shop, product=product, amount_value=supplier_price)

    strategy = "E-Commerce.testing.supplier_pricing.supplier_strategy:CheapestSupplierPriceSupplierStrategy"
    with override_settings(E-Commerce_PRICING_MODULE="supplier_pricing", E-Commerce_SHOP_PRODUCT_SUPPLIERS_STRATEGY=strategy):
        with override_current_theme_class(ClassicGrayTheme, shop):  # Ensure settings is refreshed from DB
            soup = _get_category_detail_soup(client, category)

            # Johnny Inc has the best prices for everything
            laptop = [product for product in products if product.sku == "laptop"][0]
            laptop_product_box = soup.find("div", {"id": "product-%s" % laptop.pk})
            _assert_supplier_info(laptop_product_box, "Johnny Inc")
            _assert_product_price(laptop_product_box, 750)

            keyboard = [product for product in products if product.sku == "keyboard"][0]
            keyboard_product_box = soup.find("div", {"id": "product-%s" % keyboard.pk})
            _assert_supplier_info(keyboard_product_box, "Johnny Inc")
            _assert_product_price(keyboard_product_box, 75)

            mouse = [product for product in products if product.sku == "mouse"][0]
            mouse_product_box = soup.find("div", {"id": "product-%s" % mouse.pk})
            _assert_supplier_info(mouse_product_box, "Johnny Inc")
            _assert_product_price(mouse_product_box, 75)

            # Ok competition has done it job and the other suppliers
            # has to start adjust their prices.

            # Let's say Mike has the cheapest laptop
            mike_supplier = Supplier.objects.filter(name="Mike Inc")
            SupplierPrice.objects.filter(supplier=mike_supplier, shop=shop, product=laptop).update(amount_value=333)

            soup = _get_category_detail_soup(client, category)
            laptop_product_box = soup.find("div", {"id": "product-%s" % laptop.pk})
            _assert_supplier_info(laptop_product_box, "Mike Inc")
            _assert_product_price(laptop_product_box, 333)

            # Just to make sure Simon takes over the mouse biz
            simon_supplier = Supplier.objects.filter(name="Simon Inc")
            SupplierPrice.objects.filter(supplier=simon_supplier, shop=shop, product=mouse).update(amount_value=1)

            soup = _get_category_detail_soup(client, category)
            mouse_product_box = soup.find("div", {"id": "product-%s" % mouse.pk})
            _assert_supplier_info(mouse_product_box, "Simon Inc")
            _assert_product_price(mouse_product_box, 1)