Example #1
0
def test_applied_attributes():
    product = get_default_product()
    for spec in ATTR_SPECS:  # This loop sets each attribute twice. That's okay.
        attr = Attribute.objects.get(identifier=spec["identifier"])
        pa, _ = ProductAttribute.objects.get_or_create(product=product, attribute=attr)
        _populate_applied_attribute(pa)
        pa.save()
        if not attr.is_translated:
            product.set_attribute_value(attr.identifier, pa.value)

    assert product.get_attribute_value("bogomips") == 320, "integer attribute loaded neatly"
    product.set_attribute_value("bogomips", 480)
    assert product.get_attribute_value("bogomips") == 480, "integer attribute updated neatly"
    Product.cache_attributes_for_targets(
        applied_attr_cls=ProductAttribute,
        targets=[product],
        attribute_identifiers=[a["identifier"] for a in ATTR_SPECS],
        language=get_language()
    )
    assert (get_language(), "bogomips",) in product._attr_cache, "integer attribute in cache"
    assert product.get_attribute_value("bogomips") == 480, "integer attribute value in cache"
    assert product.get_attribute_value("ba:gelmips", default="Britta") == "Britta", "non-existent attributes return default value"
    assert product._attr_cache[(get_language(), "ba:gelmips")] is NoSuchAttributeHere, "cache miss saved"
    attr_info = product.get_all_attribute_info(language=get_language(), visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE)
    assert set(attr_info.keys()) <= set(a["identifier"] for a in ATTR_SPECS), "get_all_attribute_info gets all attribute info"
Example #2
0
def create_product(sku, shop=None, supplier=None, default_price=None):
    if default_price is not None:
        default_price = shop.create_price(default_price)

    product = Product(
        type=get_default_product_type(),
        tax_class=get_default_tax_class(),
        sku=sku,
        name=sku.title(),
        width=100,
        height=100,
        depth=100,
        net_weight=100,
        gross_weight=100,
        sales_unit=get_default_sales_unit(),
        stock_behavior=StockBehavior.UNSTOCKED
    )
    product.full_clean()
    product.save()
    if shop:
        sp = ShopProduct.objects.create(product=product, shop=shop, default_price=default_price)
        if supplier:
            sp.suppliers.add(supplier)
        sp.save()

    return product
Example #3
0
def test_model_url():
    with admin_only_urls():
        with pytest.raises(NoModelUrl):
            get_model_url(Counter)  # That's silly!
        p = Product()
        p.pk = 3
        assert get_model_url(p)
Example #4
0
def test_formatted_decimal_field():
    """
    Test that FormattedDecimalField doesn't return value in scientific
    notation.
    """
    class TestModelForm(ModelForm):
        class Meta:
            model = Product
            fields = ["width"]

    values = [
        "0E-9", "0E-30", "1E-9", "123E-10", "-123E-10", "1.12345666666666E20"
    ]

    for value in values:
        product = Product(width=Decimal(value))
        form = TestModelForm(instance=product)
        rendered_form = force_text(form)
        rendered_value = re.search('value="(.*)"', rendered_form).group(1)
        rendered_step = re.search('step="(.*)"', rendered_form).group(1)
        assert rendered_value and "E" not in rendered_value
        assert rendered_step and "E" not in rendered_step

    # Extremely large exponents should raise an exception so as not to
    # produce excessively large files
    large_value = "1.23E-10000"
    product = Product(width=Decimal(large_value))
    with pytest.raises(ValueError):
        form = TestModelForm(instance=product)
Example #5
0
def cache_product_things(request, products, language=None, attribute_identifiers=("author",)):
    # Cache necessary things for products. WARNING: This will cause queryset iteration.
    language = language or get_language()
    # TODO: Should we cache prices here?
    if attribute_identifiers:
        Product.cache_attributes_for_targets(
            ProductAttribute, products,
            attribute_identifiers=attribute_identifiers,
            language=language)
    products = cache_translations(products, (language,))
    return products
def test_modelform_persistence():
    with translation.override("en"):
        test_product = Product(barcode="666", stock_behavior=StockBehavior.STOCKED)
        test_product.set_current_language("en")
        test_product.name = "foo"
        frm = MultiProductForm(languages=["en"], instance=test_product, default_language="en")
        assert frm["barcode"].value() == test_product.barcode
        stock_behavior_field = Product._meta.get_field_by_name("stock_behavior")[0]
        assert stock_behavior_field.to_python(frm["stock_behavior"].value()) is test_product.stock_behavior
        assert 'value="1" selected="selected"' in six.text_type(frm["stock_behavior"].as_widget())
        assert frm.initial["name"] == test_product.name
Example #7
0
def cache_product_things(request,
                         products,
                         language=None,
                         attribute_identifiers=("author", )):
    # Cache necessary things for products. WARNING: This will cause queryset iteration.
    language = language or get_language()
    # TODO: Should we cache prices here?
    if attribute_identifiers:
        Product.cache_attributes_for_targets(
            ProductAttribute,
            products,
            attribute_identifiers=attribute_identifiers,
            language=language)
    products = cache_translations(products, (language, ))
    return products
Example #8
0
def test_modelform_persistence():
    with translation.override("en"):
        test_product = Product(barcode="666",
                               stock_behavior=StockBehavior.STOCKED)
        test_product.set_current_language("en")
        test_product.name = "foo"
        frm = TestMultiProductForm(languages=["en"],
                                   instance=test_product,
                                   default_language="en")
        assert frm["barcode"].value() == test_product.barcode
        stock_behavior_field = Product._meta.get_field_by_name(
            "stock_behavior")[0]
        assert stock_behavior_field.to_python(
            frm["stock_behavior"].value()) is test_product.stock_behavior
        assert 'value="1" selected="selected"' in six.text_type(
            frm["stock_behavior"].as_widget())
        assert frm.initial["name"] == test_product.name
Example #9
0
def test_pricing_module_is_active():
    """
    Make sure that our custom pricing module is active.
    """
    shop = Shop(currency='USD', prices_include_tax=False)
    customer = AnonymousContact()
    product = Product(sku='6.0745')

    pricing_mod = get_pricing_module()
    pricing_ctx = pricing_mod.get_context_from_data(shop, customer)

    pi = product.get_price_info(pricing_ctx, quantity=2)

    price = shop.create_price
    assert pi.price == price('12.149')
    assert pi.base_price == price('48.596')
    assert pi.quantity == 2
    assert pi.discounted_unit_price == price('6.0745')
    assert pi.base_unit_price == price('24.298')
    assert pi.discount_rate == Decimal('0.75')
Example #10
0
def test_convert_taxness_without_conversion(taxes, price_cls):
    request = get_request()
    item = Product()
    priceful = _get_price_info(price_cls)
    calcs_done_before = DummyTaxModule.calculations_done
    result = convert_taxness(request, item, priceful, with_taxes=taxes)
    calcs_done_after = DummyTaxModule.calculations_done
    assert result == priceful
    assert result.price == price_cls(480, 'USD')
    assert result.base_price == price_cls(660, 'USD')
    assert result.quantity == 2
    assert calcs_done_after == calcs_done_before
def test_product_caching_object_type_validation():
    with pytest.raises(TypeError):
        pco = ProductCachingObject()
        pco.product_id = "yeah"

    with pytest.raises(TypeError):
        pco = ProductCachingObject()
        pco.product = "yeahhh"

    with pytest.raises(ValueError):
        pco = ProductCachingObject()
        pco.product = Product()
Example #12
0
def create_product(sku, shop=None, supplier=None, default_price=None):
    if default_price is not None:
        default_price = shop.create_price(default_price)

    product = Product(type=get_default_product_type(),
                      tax_class=get_default_tax_class(),
                      sku=sku,
                      name=sku.title(),
                      width=100,
                      height=100,
                      depth=100,
                      net_weight=100,
                      gross_weight=100,
                      sales_unit=get_default_sales_unit(),
                      stock_behavior=StockBehavior.UNSTOCKED)
    product.full_clean()
    product.save()
    if shop:
        sp = ShopProduct.objects.create(product=product,
                                        shop=shop,
                                        default_price=default_price)
        if supplier:
            sp.suppliers.add(supplier)
        sp.save()

    return product
Example #13
0
def _get_order_line(request):
    order = Order(
        shop=request.shop,
        currency=request.shop.currency,
        prices_include_tax=request.shop.prices_include_tax,
    )
    pi = _get_price_info(request.shop, Product(sku='6.0745'), quantity=2)
    return OrderLine(
        order=order,
        base_unit_price=pi.base_unit_price,
        discount_amount=pi.discount_amount,
        quantity=pi.quantity,
    )
def test_applied_attributes():
    product = get_default_product()
    for spec in ATTR_SPECS:  # This loop sets each attribute twice. That's okay.
        attr = Attribute.objects.get(identifier=spec["identifier"])
        pa, _ = ProductAttribute.objects.get_or_create(product=product,
                                                       attribute=attr)
        _populate_applied_attribute(pa)
        pa.save()
        if not attr.is_translated:
            product.set_attribute_value(attr.identifier, pa.value)

    assert product.get_attribute_value(
        "bogomips") == 320, "integer attribute loaded neatly"
    product.set_attribute_value("bogomips", 480)
    assert product.get_attribute_value(
        "bogomips") == 480, "integer attribute updated neatly"
    Product.cache_attributes_for_targets(
        applied_attr_cls=ProductAttribute,
        targets=[product],
        attribute_identifiers=[a["identifier"] for a in ATTR_SPECS],
        language=get_language())
    assert (
        get_language(),
        "bogomips",
    ) in product._attr_cache, "integer attribute in cache"
    assert product.get_attribute_value(
        "bogomips") == 480, "integer attribute value in cache"
    assert product.get_attribute_value(
        "ba:gelmips", default="Britta"
    ) == "Britta", "non-existent attributes return default value"
    assert product._attr_cache[(get_language(), "ba:gelmips"
                                )] is NoSuchAttributeHere, "cache miss saved"
    attr_info = product.get_all_attribute_info(
        language=get_language(),
        visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE)
    assert set(attr_info.keys()) <= set(
        a["identifier"]
        for a in ATTR_SPECS), "get_all_attribute_info gets all attribute info"
Example #15
0
def test_convert_taxness_taxful_to_taxless():
    request = get_request()
    tax_class = TaxClass()
    item = Product(tax_class=tax_class)
    priceful = _get_price_info(TaxfulPrice)
    calcs_done_before = DummyTaxModule.calculations_done
    result = convert_taxness(request, item, priceful, with_taxes=False)
    calcs_done_after = DummyTaxModule.calculations_done
    assert result != priceful
    assert (result.price - TaxlessPrice(400, 'USD')).value < 0.00001
    assert result.base_price == TaxlessPrice(550, 'USD')
    assert result.quantity == 2
    assert result.tax_amount == Money(80, 'USD')
    assert result.taxless_price == result.price
    assert result.taxful_price == priceful.price
    assert calcs_done_after == calcs_done_before + 2
Example #16
0
def _get_template_engine_and_context():
    engine = django.template.engines['jinja2']
    assert isinstance(engine, django_jinja.backend.Jinja2)

    request = RequestFactory().get('/')
    request.shop = Shop(currency='USD', prices_include_tax=False)
    request.customer = AnonymousContact()
    request.person = request.customer

    context = {
        'request': request,
        'prod': Product(sku='6.0745'),
        # TODO: Test also with variant products
        'sline': _get_source_line(request),
        'bline': _get_basket_line(request),
        'oline': _get_order_line(request),
    }

    return (engine, context)
Example #17
0
def _get_source_line(request):
    source = OrderSource(request.shop)
    return _create_line(source, Product(sku='6.0745'))
Example #18
0
def _get_basket_line(request):
    basket = BaseBasket(request)
    return _create_line(basket, Product(sku='6.0745'))
Example #19
0
def _get_price_info(shop, product=None, quantity=2):
    if not product:
        product = Product(sku='6.0745')
    # SKU of product defines the price :)
    price = shop.create_price(product.sku)
    return PriceInfo(quantity * price, quantity * 4 * price, quantity)