Beispiel #1
0
def test_order_creator(admin_user):
    source = seed_source(admin_user)
    source.lines.append(
        SourceLine(
            type=OrderLineType.PRODUCT,
            product=get_default_product(),
            supplier=get_default_supplier(),
            quantity=1,
            unit_price=TaxlessPrice(10),
        ))
    source.lines.append(
        SourceLine(
            type=OrderLineType.OTHER,
            quantity=1,
            unit_price=TaxlessPrice(10),
            require_verification=True,
        ))

    creator = OrderCreator(request=None)
    order = creator.create_order(source)
    assert get_data_dict(source.billing_address) == get_data_dict(
        order.billing_address)
    assert get_data_dict(source.shipping_address) == get_data_dict(
        order.shipping_address)
    assert source.customer == order.customer
    assert source.payment_method == order.payment_method
    assert source.shipping_method == order.shipping_method
    assert order.pk
def test_order_source_parentage(rf, admin_user):
    source = seed_source(admin_user)
    product = get_default_product()
    source.lines.append(SourceLine(
        type=OrderLineType.PRODUCT,
        product=product,
        supplier=get_default_supplier(),
        quantity=1,
        unit_price=TaxlessPrice(10),
        line_id="parent"
    ))
    source.lines.append(SourceLine(
        type=OrderLineType.OTHER,
        text="Child Line",
        sku="KIDKIDKID",
        quantity=1,
        unit_price=TaxlessPrice(5),
        parent_line_id="parent"
    ))
    request = apply_request_middleware(rf.get("/"))

    creator = OrderCreator(request)
    order = Order.objects.get(pk=creator.create_order(source).pk)
    kid_line = order.lines.filter(sku="KIDKIDKID").first()
    assert kid_line
    assert kid_line.parent_line.product_id == product.pk
Beispiel #3
0
def create_random_order(customer=None, products=(), completion_probability=0):
    if not customer:
        customer = Contact.objects.all().order_by("?").first()

    if not customer:
        raise ValueError("No valid contacts")

    shop = get_default_shop()

    request = apply_request_middleware(RequestFactory().get("/"),
                                       customer=customer)

    context = PriceTaxContext.from_request(request)
    source = OrderSource()
    source.customer = customer
    source.customer_comment = "Mock Order"

    if customer.default_billing_address and customer.default_shipping_address:
        source.billing_address = customer.default_billing_address
        source.shipping_address = customer.default_shipping_address
    else:
        source.billing_address = create_random_address()
        source.shipping_address = create_random_address()
    source.order_date = now() - datetime.timedelta(days=random.uniform(0, 400))

    source.shop = shop
    source.language = customer.language
    source.status = get_initial_order_status()

    if not products:
        products = list(
            Product.objects.list_visible(source.shop,
                                         customer).order_by("?")[:40])

    source.lines = []
    for i in range(random.randint(3, 10)):
        product = random.choice(products)
        quantity = random.randint(1, 5)
        price = product.get_price(context, quantity=quantity)
        shop_product = product.get_shop_instance(source.shop)
        supplier = shop_product.suppliers.first()
        line = SourceLine(type=OrderLineType.PRODUCT,
                          product=product,
                          supplier=supplier,
                          quantity=quantity,
                          unit_price=price,
                          sku=product.sku,
                          text=product.safe_translation_getter(
                              "name", any_language=True))
        source.lines.append(line)
    with atomic():
        oc = OrderCreator(request)
        order = oc.create_order(source)
        if random.random() < completion_probability:
            order.create_shipment_of_all_products()
            # also set complete
            order.status = OrderStatus.objects.get_default_complete()
            order.save(update_fields=("status", ))
        return order
Beispiel #4
0
def test_order_creator_supplierless_product_line_conversion_should_fail(
        admin_user):
    source = seed_source(admin_user)
    source.lines.append(
        SourceLine(
            type=OrderLineType.PRODUCT,
            product=get_default_product(),
            supplier=None,
            quantity=1,
            unit_price=TaxlessPrice(10),
        ))
    creator = OrderCreator(request=None)
    with pytest.raises(ValueError):
        order = creator.create_order(source)
Beispiel #5
0
    def get_source_lines(self, source):
        from shoop.core.order_creator import SourceLine

        price_info = self.get_effective_price_info(source)
        assert price_info.quantity == 1
        yield SourceLine(
            source=source,
            quantity=1,
            type=self.method.line_type,
            text=self.get_effective_name(source),
            base_unit_price=price_info.base_unit_price,
            discount_amount=price_info.discount_amount,
            tax_class=self.method.tax_class,
        )
def test_invalid_source_line_updating():
    source = OrderSource(Shop())
    with pytest.raises(TypeError):  # Test forbidden keys
        SourceLine(source).update({"update": True})
Beispiel #7
0
def test_invalid_source_line_updating():
    with pytest.raises(TypeError):  # Test forbidden keys
        SourceLine().update({"update": True})