本文整理汇总了Python中shoop.testing.factories.create_product函数的典型用法代码示例。如果您正苦于以下问题:Python create_product函数的具体用法?Python create_product怎么用?Python create_product使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_product函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multivariable_variation
def test_multivariable_variation():
parent = create_product("SuperComplexVarParent")
color_var = ProductVariationVariable.objects.create(product=parent, identifier="color")
size_var = ProductVariationVariable.objects.create(product=parent, identifier="size")
for color in ("yellow", "blue", "brown"):
ProductVariationVariableValue.objects.create(variable=color_var, identifier=color)
for size in ("small", "medium", "large", "huge"):
ProductVariationVariableValue.objects.create(variable=size_var, identifier=size)
combinations = list(parent.get_all_available_combinations())
assert len(combinations) == (3 * 4)
for combo in combinations:
assert not combo["result_product_pk"]
# Elide a combination (yellow/small) for testing:
if combo["variable_to_value"][color_var].identifier == "yellow" and combo["variable_to_value"][size_var].identifier == "small":
continue
child = create_product("xyz-%s" % combo["sku_part"])
child.link_to_parent(parent, combo["variable_to_value"])
assert parent.mode == ProductMode.VARIABLE_VARIATION_PARENT
# Elided product should not yield a result
yellow_color_value = ProductVariationVariableValue.objects.get(variable=color_var, identifier="yellow")
small_size_value = ProductVariationVariableValue.objects.get(variable=size_var, identifier="small")
assert not ProductVariationResult.resolve(parent, {color_var: yellow_color_value, size_var: small_size_value})
# Anything else should
brown_color_value = ProductVariationVariableValue.objects.get(variable=color_var, identifier="brown")
result1 = ProductVariationResult.resolve(parent, {color_var: brown_color_value, size_var: small_size_value})
result2 = ProductVariationResult.resolve(parent, {color_var.pk: brown_color_value.pk, size_var.pk: small_size_value.pk})
assert result1 and result2
assert result1.pk == result2.pk
assert len(parent.get_available_variation_results()) == (3 * 4 - 1)
示例2: init_test
def init_test(request, shop, prices):
apply_request_middleware(request)
parent = create_product("parent_product", shop=shop)
children = [create_product("child-%d" % price, shop=shop, default_price=price) for price in prices]
for child in children:
child.link_to_parent(parent)
return parent
示例3: test_simple_variation
def test_simple_variation():
shop = get_default_shop()
parent = create_product("SimpleVarParent")
children = [create_product("SimpleVarChild-%d" % x) for x in range(10)]
for child in children:
child.link_to_parent(parent)
sp = ShopProduct.objects.create(shop=shop, product=child, listed=True)
assert child.is_variation_child()
assert not sp.is_list_visible() # Variation children are not list visible
assert parent.mode == ProductMode.SIMPLE_VARIATION_PARENT
assert not list(parent.get_all_available_combinations()) # Simple variations can't have these.
# Validation tests
dummy = create_product("InvalidSimpleVarChild")
with pytest.raises(ValueError):
dummy.link_to_parent(parent, variables={"size": "XL"})
with pytest.raises(ValueError):
parent.link_to_parent(dummy)
with pytest.raises(ValueError):
dummy.link_to_parent(children[0])
# Unlinkage
for child in children:
child.unlink_from_parent()
assert not child.is_variation_child()
assert child.mode == ProductMode.NORMAL
assert not parent.is_variation_parent()
assert parent.variation_children.count() == 0
示例4: test_admin_form
def test_admin_form(rf, admin_user):
supplier = get_simple_supplier()
shop = get_default_shop()
product = create_product("simple-test-product", shop, supplier)
request = rf.get("/")
request.user = admin_user
frm = SimpleSupplierForm(product=product, request=request)
# Form contains 1 product even if the product is not stocked
assert len(frm.products) == 1
assert not frm.products[0].is_stocked()
product.stock_behavior = StockBehavior.STOCKED # Make product stocked
product.save()
# Now since product is stocked it should be in the form
frm = SimpleSupplierForm(product=product, request=request)
assert len(frm.products) == 1
# Add stocked children for product
child_product = create_product("child-test-product", shop, supplier)
child_product.stock_behavior = StockBehavior.STOCKED
child_product.save()
child_product.link_to_parent(product)
# Admin form should now contain only child products for product
frm = SimpleSupplierForm(product=product, request=request)
assert len(frm.products) == 1
assert frm.products[0] == child_product
示例5: test_price_infos_are_discounted
def test_price_infos_are_discounted(rf):
request = initialize_test(rf, True)
price = request.shop.create_price
product_one = create_product("Product_1", request.shop, default_price=150)
product_two = create_product("Product_2", request.shop, default_price=250)
spp = DiscountedProductPrice(product=product_one, shop=request.shop, price_value=100)
spp.save()
spp = DiscountedProductPrice(product=product_two, shop=request.shop, price_value=200)
spp.save()
product_ids = [product_one.pk, product_two.pk]
dpm = get_pricing_module()
pricing_context = dpm.get_context_from_request(request)
price_infos = dpm.get_price_infos(pricing_context, product_ids)
assert len(price_infos) == 2
assert product_one.pk in price_infos
assert product_two.pk in price_infos
first_price_info = price_infos[product_one.pk]
second_price_info = price_infos[product_two.pk]
assert first_price_info.price == price(100)
assert first_price_info.base_price == price(150)
assert first_price_info.is_discounted
assert second_price_info.price == price(200)
assert second_price_info.base_price == price(250)
assert second_price_info.is_discounted
示例6: test_best_selling_products_with_multiple_orders
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)
示例7: test_price_infos
def test_price_infos(rf):
request, shop, group = initialize_test(rf, True)
price = shop.create_price
product_one = create_product("Product_1", shop, default_price=150)
product_two = create_product("Product_2", shop, default_price=250)
spp = SimpleProductPrice(product=product_one, shop=shop, group=group, price_value=100)
spp.save()
spp = SimpleProductPrice(product=product_two, shop=shop, group=group, price_value=200)
spp.save()
product_ids = [product_one.pk, product_two.pk]
spm = get_pricing_module()
assert isinstance(spm, SimplePricingModule)
pricing_context = spm.get_context_from_request(request)
price_infos = spm.get_price_infos(pricing_context, product_ids)
assert len(price_infos) == 2
assert product_one.pk in price_infos
assert product_two.pk in price_infos
assert price_infos[product_one.pk].price == price(100)
assert price_infos[product_two.pk].price == price(200)
assert price_infos[product_one.pk].base_price == price(100)
assert price_infos[product_two.pk].base_price == price(200)
示例8: test_variable_variation
def test_variable_variation():
parent = create_product("ComplexVarParent")
sizes_and_children = [("%sL" % ("X" * x), create_product("ComplexVarChild-%d" % x)) for x in range(4)]
for size, child in sizes_and_children:
child.link_to_parent(parent, variables={"size": size})
assert parent.mode == ProductMode.VARIABLE_VARIATION_PARENT
assert all(child.is_variation_child() for (size, child) in sizes_and_children)
# Validation tests
dummy = create_product("InvalidComplexVarChild")
with pytest.raises(ValueError):
dummy.link_to_parent(parent)
with pytest.raises(ValueError):
parent.link_to_parent(dummy)
with pytest.raises(ValueError):
dummy.link_to_parent(sizes_and_children[0][1])
# Variable tests
size_attr = parent.variation_variables.get(identifier="size")
for size, child in sizes_and_children:
size_val = size_attr.values.get(identifier=size)
result_product = ProductVariationResult.resolve(parent, {size_attr: size_val})
assert result_product == child
示例9: test_basket_shipping_error
def test_basket_shipping_error(rf):
StoredBasket.objects.all().delete()
shop = get_default_shop()
supplier = get_default_supplier()
shipped_product = create_product(
printable_gibberish(), shop=shop, supplier=supplier, default_price=50,
shipping_mode=ShippingMode.SHIPPED
)
unshipped_product = create_product(
printable_gibberish(), shop=shop, supplier=supplier, default_price=50,
shipping_mode=ShippingMode.NOT_SHIPPED
)
request = rf.get("/")
request.session = {}
request.shop = shop
apply_request_middleware(request)
basket = get_basket(request)
# With a shipped product but no shipping methods, we oughta get an error
basket.add_product(supplier=supplier, shop=shop, product=shipped_product, quantity=1)
assert any(ve.code == "no_common_shipping" for ve in basket.get_validation_errors())
basket.clear_all()
# But with an unshipped product, we should not
basket.add_product(supplier=supplier, shop=shop, product=unshipped_product, quantity=1)
assert not any(ve.code == "no_common_shipping" for ve in basket.get_validation_errors())
示例10: test_complex_variation
def test_complex_variation():
request = get_request_with_basket()
basket = request.basket
shop = get_default_shop()
supplier = get_default_supplier()
parent = create_product("SuperComplexVarParent", shop=shop, supplier=supplier)
color_var = ProductVariationVariable.objects.create(product=parent, identifier="color")
size_var = ProductVariationVariable.objects.create(product=parent, identifier="size")
ProductVariationVariableValue.objects.create(variable=color_var, identifier="yellow")
ProductVariationVariableValue.objects.create(variable=size_var, identifier="small")
combinations = list(parent.get_all_available_combinations())
for combo in combinations:
child = create_product("xyz-%s" % combo["sku_part"], shop=shop, supplier=supplier)
child.link_to_parent(parent, combo["variable_to_value"])
# Elided product should not yield a result
yellow_color_value = ProductVariationVariableValue.objects.get(variable=color_var, identifier="yellow")
small_size_value = ProductVariationVariableValue.objects.get(variable=size_var, identifier="small")
# add to basket yellow + small
kwargs = {"var_%d" % color_var.pk: yellow_color_value.pk, "var_%d" % size_var.pk: small_size_value.pk}
basket_commands.handle_add_var(request, basket, 1, **kwargs)
assert basket.get_product_ids_and_quantities()[child.pk] == 1
with pytest.raises(ValidationError):
kwargs = {"var_%d" % color_var.pk: yellow_color_value.pk, "var_%d" % size_var.pk: small_size_value.pk + 1}
basket_commands.handle_add_var(request, basket, 1, **kwargs)
示例11: test_protected_fields
def test_protected_fields():
activate("en")
shop = Shop.objects.create(
name="testshop",
identifier="testshop",
status=ShopStatus.ENABLED,
public_name="test shop",
domain="derp",
currency="EUR"
)
assert shop.name == "testshop"
assert shop.currency == "EUR"
shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES)
assert not shop_form._get_protected_fields() # No protected fields just yet, right?
data = get_form_data(shop_form, prepared=True)
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
_test_cleanliness(shop_form)
shop_form.save()
# Now let's make it protected!
create_product(printable_gibberish(), shop=shop, supplier=get_default_supplier())
order = create_random_order(customer=create_random_person(), shop=shop)
assert order.shop == shop
# And try again...
data["currency"] = "XBT" # Bitcoins!
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
assert shop_form._get_protected_fields() # So protected!
_test_cleanliness(shop_form)
shop = shop_form.save()
assert shop.currency == "EUR" # But the shop form ignored the change . . .
示例12: test_form_populate_initial_data
def test_form_populate_initial_data(rf, admin_user):
shop = get_default_shop()
supplier = get_default_supplier()
initial_discount_amount = 20
campaign = BasketCampaign.objects.create(shop=shop)
BasketDiscountAmount.objects.create(campaign=campaign, discount_amount=initial_discount_amount)
# Test that correct initial value is returned for non-many-to-many field
product_amount_initial = 10
product_amount_condition = BasketTotalProductAmountCondition(product_count=product_amount_initial)
product_amount_condition.save()
campaign.conditions.add(product_amount_condition)
products_count_initial = 5
for i in range(products_count_initial):
create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price="20")
products_initial = Product.objects.all()[:products_count_initial]
assert len(products_initial) == products_count_initial
# Test that correct initial value is returned for many-to-many field
products_in_basket_condition = ProductsInBasketCondition.objects.create()
products_in_basket_condition.values = products_initial
products_in_basket_condition.save()
campaign.conditions.add(products_in_basket_condition)
assert len(campaign.conditions.all()) == 2
assert campaign.effects.count() == 1
request=apply_request_middleware(rf.get("/"), user=admin_user)
form = BasketCampaignForm(request=request, instance=campaign)
assert form.fields["basket_product_condition"].initial == product_amount_initial
assert set(form.fields["basket_products_condition"].initial) == set([p.pk for p in products_initial])
assert form.fields["discount_amount_effect"].initial == initial_discount_amount
示例13: test_package
def test_package():
shop = get_default_shop()
supplier = get_default_supplier()
package_product = create_product("PackageParent", shop=shop, supplier=supplier)
assert not package_product.get_package_child_to_quantity_map()
children = [create_product("PackageChild-%d" % x, shop=shop, supplier=supplier) for x in range(4)]
package_def = {child: 1 + i for (i, child) in enumerate(children)}
package_product.make_package(package_def)
assert package_product.mode == ProductMode.PACKAGE_PARENT
package_product.save()
sp = package_product.get_shop_instance(shop)
assert not list(sp.get_orderability_errors(supplier=supplier, quantity=1, customer=AnonymousContact()))
with pytest.raises(ValueError): # Test re-packaging fails
package_product.make_package(package_def)
# Check that OrderCreator can deal with packages
source = BasketishOrderSource()
source.lines.append(SourceLine(
type=OrderLineType.PRODUCT,
product=package_product,
supplier=get_default_supplier(),
quantity=10,
unit_price=TaxlessPrice(10),
))
source.shop = get_default_shop()
source.status = get_initial_order_status()
creator = OrderCreator(request=None)
order = creator.create_order(source)
pids_to_quantities = order.get_product_ids_and_quantities()
for child, quantity in six.iteritems(package_def):
assert pids_to_quantities[child.pk] == 10 * quantity
示例14: test_limited_methods
def test_limited_methods():
"""
Test that products can declare that they limit available shipping methods.
"""
unique_shipping_method = get_shipping_method(name="unique", price=0)
shop = get_default_shop()
common_product = create_product(sku="SH_COMMON", shop=shop) # A product that does not limit shipping methods
unique_product = create_product(sku="SH_UNIQUE", shop=shop) # A product that only supports unique_shipping_method
unique_shop_product = unique_product.get_shop_instance(shop)
unique_shop_product.limit_shipping_methods = True
unique_shop_product.shipping_methods.add(unique_shipping_method)
unique_shop_product.save()
impossible_product = create_product(sku="SH_IMP", shop=shop) # A product that can't be shipped at all
imp_shop_product = impossible_product.get_shop_instance(shop)
imp_shop_product.limit_shipping_methods = True
imp_shop_product.save()
for product_ids, method_ids in [
((common_product.pk, unique_product.pk), (unique_shipping_method.pk,)),
((common_product.pk,), ShippingMethod.objects.values_list("pk", flat=True)),
((unique_product.pk,), (unique_shipping_method.pk,)),
((unique_product.pk, impossible_product.pk,), ()),
((common_product.pk, impossible_product.pk,), ()),
]:
product_ids = set(product_ids)
assert ShippingMethod.objects.available_ids(shop=shop, products=product_ids) == set(method_ids)
示例15: get_frontend_order_state
def get_frontend_order_state(contact, valid_lines=True):
"""
Get a dict structure mirroring what the frontend JavaScript would submit.
:type contact: Contact|None
"""
translation.activate("en")
shop = get_default_shop()
tax = Tax.objects.create(code="test_code", rate=decimal.Decimal("0.20"), name="Default")
tax_class = TaxClass.objects.create(identifier="test_tax_class", name="Default")
rule = TaxRule.objects.create(tax=tax)
rule.tax_classes.add(tax_class)
rule.save()
product = create_product(
sku=printable_gibberish(),
supplier=get_default_supplier(),
shop=shop
)
product.tax_class = tax_class
product.save()
if valid_lines:
lines = [
{"id": "x", "type": "product", "product": {"id": product.id}, "quantity": "32", "baseUnitPrice": 50},
{"id": "y", "type": "other", "sku": "hello", "text": "A greeting", "quantity": 1, "unitPrice": "5.5"},
{"id": "z", "type": "text", "text": "This was an order!", "quantity": 0},
]
else:
unshopped_product = create_product(sku=printable_gibberish(), supplier=get_default_supplier())
not_visible_product = create_product(
sku=printable_gibberish(),
supplier=get_default_supplier(),
shop=shop
)
not_visible_shop_product = not_visible_product.get_shop_instance(shop)
not_visible_shop_product.visible = False
not_visible_shop_product.save()
lines = [
{"id": "x", "type": "product"}, # no product?
{"id": "x", "type": "product", "product": {"id": unshopped_product.id}}, # not in this shop?
{"id": "y", "type": "product", "product": {"id": -product.id}}, # invalid product?
{"id": "z", "type": "other", "quantity": 1, "unitPrice": "q"}, # what's that price?
{"id": "rr", "type": "product", "quantity": 1, "product": {"id": not_visible_product.id}} # not visible
]
state = {
"customer": {"id": contact.id if contact else None},
"lines": lines,
"methods": {
"shippingMethod": {"id": get_default_shipping_method().id},
"paymentMethod": {"id": get_default_payment_method().id},
},
"shop": {
"selected": {
"id": shop.id,
"name": shop.name,
"currency": shop.currency,
"priceIncludeTaxes": shop.prices_include_tax
}
}
}
return state