def get_initialized_test_event(): get_default_product() customer = create_random_person() order = create_random_order(customer) return ATestEvent(order_language=order.language, order=order, just_some_text=random_title())
def get_initialized_test_event(): get_default_product() customer = create_random_person() order = create_random_order(customer) return ATestEvent( order_language=order.language, order=order, just_some_text=random_title() )
def test_broken_order_lines(): with pytest.raises(ValidationError): OrderLine(type=OrderLineType.PRODUCT).save() with pytest.raises(ValidationError): OrderLine(product=get_default_product(), type=OrderLineType.PRODUCT, supplier=None).save() with pytest.raises(ValidationError): OrderLine(product=get_default_product(), type=OrderLineType.OTHER).save() with pytest.raises(ValidationError): OrderLine(product=get_default_product(), type=OrderLineType.OTHER, tax_rate=3).save()
def get_order_and_source(admin_user): # create original source to tamper with source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.billing_address = MutableAddress.objects.create( name="Original Billing") source.shipping_address = MutableAddress.objects.create( name="Original Shipping") source.customer = get_person_contact(admin_user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) assert len(source.get_lines()) == 2 source.creator = admin_user creator = OrderCreator() order = creator.create_order(source) return order, source
def test_tracking_codes(): product = get_default_product() supplier = get_default_supplier() order = create_order_with_product(product, supplier=supplier, quantity=1, taxless_base_unit_price=10, tax_rate=decimal.Decimal("0.5")) _add_product_to_order(order, "duck-tape-1", 3, order.shop, supplier) _add_product_to_order(order, "water-1", 2, order.shop, supplier) order.cache_prices() order.check_all_verified() order.save() # Create shipment with tracking code for every product line. product_lines = order.lines.exclude(product_id=None) assert len(product_lines) == 3 for line in product_lines: shipment = order.create_shipment(supplier, {line.product: line.quantity}) if line.quantity != 3: shipment.tracking_code = "123FI" shipment.save() tracking_codes = order.get_tracking_codes() code_count = (len(product_lines) - 1) # We skipped that one assert len(tracking_codes) == code_count assert len([ tracking_code for tracking_code in tracking_codes if tracking_code == "123FI" ]) == code_count
def test_order_source_parentage(rf, admin_user): source = seed_source(admin_user) product = get_default_product() source.add_line( type=OrderLineType.PRODUCT, product=product, supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), line_id="parent" ) source.add_line( type=OrderLineType.OTHER, text="Child Line", sku="KIDKIDKID", quantity=1, base_unit_price=source.create_price(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
def test_tracking_codes(): product = get_default_product() supplier = get_default_supplier() order = create_order_with_product( product, supplier=supplier, quantity=1, taxless_base_unit_price=10, tax_rate=decimal.Decimal("0.5") ) _add_product_to_order(order, "duck-tape-1", 3, order.shop, supplier) _add_product_to_order(order, "water-1", 2, order.shop, supplier) order.cache_prices() order.check_all_verified() order.save() # Create shipment with tracking code for every product line. product_lines = order.lines.exclude(product_id=None) assert len(product_lines) == 3 for line in product_lines: shipment = order.create_shipment(supplier, {line.product: line.quantity}) if line.quantity != 3: shipment.tracking_code = "123FI" shipment.save() tracking_codes = order.get_tracking_codes() code_count = (len(product_lines)-1) # We skipped that one assert len(tracking_codes) == code_count assert len([tracking_code for tracking_code in tracking_codes if tracking_code == "123FI"]) == code_count
def test_basket_update_errors(): request = get_request_with_basket() basket = request.basket product = get_default_product() basket_commands.handle_add(request, basket, product_id=product.pk, quantity=1) # Hide product and now updating quantity should give errors shop_product = product.get_shop_instance(request.shop) shop_product.suppliers.clear() line_id = basket.get_lines()[0].line_id basket_commands.handle_update(request, basket, **{"q_%s" % line_id: "2"}) error_messages = messages.get_messages(request) # One warning is added to messages assert len(error_messages) == 1 assert any("not supplied" in msg.message for msg in error_messages) shop_product.visible = False shop_product.save() basket_commands.handle_update(request, basket, **{"q_%s" % line_id: "2"}) error_messages = messages.get_messages(request) # Two warnings is added to messages assert len(error_messages) == 3 assert any("not visible" in msg.message for msg in error_messages) assert all("[" not in msg.message for msg in error_messages)
def test_order_creator(rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) request = apply_request_middleware(rf.get("/")) creator = OrderCreator(request) 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_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_add_and_remove_and_clear(): product = get_default_product() supplier = get_default_supplier() request = get_request_with_basket() basket = request.basket with pytest.raises(ValidationError): basket_commands.handle_add(request, basket, product_id=product.pk, quantity=-3) # Ordering antimatter is not supported # These will get merged into one line... basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 1, "supplier_id": supplier.pk}) basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 2}) # ... so there will be 3 products but one line assert basket.product_count == 3 lines = basket.get_lines() assert len(lines) == 1 # ... and deleting that line will clear the basket... basket_commands.handle_del(request, basket, lines[0].line_id) assert basket.product_count == 0 # ... and adding another product will create a new line... basket_commands.handle_add(request, basket, product_id=product.pk, quantity=1) assert basket.product_count == 1 # ... that can be cleared. basket_commands.handle_clear(request, basket) assert basket.product_count == 0
def test_order_creation_adds_usage(rf, admin_user): request, shop, group = initialize_test(rf, False) source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True ) # add coupon coupon = Coupon.objects.create(active=True, code="asdf") campaign = BasketCampaign.objects.create( active=True, shop=shop, name="test", public_name="test", discount_percentage="0.1", coupon=coupon ) source.add_code(coupon.code) request = apply_request_middleware(rf.get("/")) creator = OrderCreator(request) order = creator.create_order(source) assert CouponUsage.objects.count() == 1
def test_ajax(): product = get_default_product() commands = get_basket_command_dispatcher(get_request_with_basket()) commands.ajax = True rv = commands.handle("add", kwargs=dict(product_id=product.pk, quantity=-3)) assert isinstance(rv, JsonResponse) assert commands.basket.product_count == 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"
def test_custom_payment_processor_cash_service(choice_identifier, expected_payment_status): shop = get_default_shop() product = get_default_product() supplier = get_default_supplier() processor = CustomPaymentProcessor.objects.create() payment_method = PaymentMethod.objects.create( shop=shop, payment_processor=processor, choice_identifier=choice_identifier, tax_class=get_default_tax_class()) order = create_order_with_product( product=product, supplier=supplier, quantity=1, taxless_base_unit_price=Decimal('5.55'), shop=shop) order.taxful_total_price = TaxfulPrice(Decimal('5.55'), u'EUR') order.payment_method = payment_method order.save() assert order.payment_status == PaymentStatus.NOT_PAID processor.process_payment_return_request(choice_identifier, order, None) assert order.payment_status == expected_payment_status processor.process_payment_return_request(choice_identifier, order, None) assert order.payment_status == expected_payment_status
def get_order_and_source(admin_user): # create original source to tamper with source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.billing_address = MutableAddress.objects.create(name="Original Billing") source.shipping_address = MutableAddress.objects.create(name="Original Shipping") source.customer = get_person_contact(admin_user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) assert len(source.get_lines()) == 2 source.creator = admin_user creator = OrderCreator() order = creator.create_order(source) return order, source
def test_order_creation_adds_usage(rf, admin_user): request, shop, group = initialize_test(rf, False) source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) # add coupon coupon = Coupon.objects.create(active=True, code="asdf") BasketCampaign.objects.create(active=True, shop=shop, name="test", public_name="test", discount_percentage="0.1", coupon=coupon) source.add_code(coupon.code) creator = OrderCreator() creator.create_order(source) assert CouponUsage.objects.count() == 1
def test_fixed_cost_with_waiving_costs(): sm = get_shipping_method(name="Fixed and waiving", price=5) sm.behavior_components.add( *[WaivingCostBehaviorComponent.objects.create( price_value=p, waive_limit_value=w) for (p, w) in [(3, 5), (7, 10), (10, 30)]]) source = BasketishOrderSource(get_default_shop()) source.shipping_method = sm def pricestr(pi): assert pi.price.unit_matches_with(source.create_price(0)) return "%.0f EUR (%.0f EUR)" % (pi.price.value, pi.base_price.value) assert pricestr(sm.get_total_cost(source)) == "25 EUR (25 EUR)" assert source.total_price.value == 25 source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.create_price(2), quantity=1) assert pricestr(sm.get_total_cost(source)) == "25 EUR (25 EUR)" assert source.total_price.value == 27 source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.create_price(3), quantity=1) assert pricestr(sm.get_total_cost(source)) == "22 EUR (25 EUR)" assert source.total_price.value == 27 source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.create_price(10), quantity=1) assert pricestr(sm.get_total_cost(source)) == "15 EUR (25 EUR)" assert source.total_price.value == 30 source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.create_price(10), quantity=1) assert pricestr(sm.get_total_cost(source)) == "15 EUR (25 EUR)" assert source.total_price.value == 40 source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.create_price(10), quantity=1) assert pricestr(sm.get_total_cost(source)) == "5 EUR (25 EUR)" assert source.total_price.value == 40
def test_basic_order(rf, admin_user): request = rf.get('/') product = get_default_product() SimpleProductPrice.objects.create(product=product, group=None, price=Decimal("100.00"), includes_tax=False) customer = get_person_contact(admin_user) for x in range(10): create_order(request, creator=admin_user, customer=customer, product=product) assert Order.objects.filter(customer=customer).count() == 10
def test_product_page(client): get_default_shop() product = get_default_product() response = client.get( reverse('shoop:product', kwargs={ 'pk': product.pk, 'slug': product.slug })) assert b'no such element' not in response.content, 'All items are not rendered correctly'
def test_basic_order(rf, admin_user): shop = get_default_shop() request = rf.get('/') request.shop = shop product = get_default_product() customer = get_person_contact(admin_user) for x in range(10): create_order(request, creator=admin_user, customer=customer, product=product) assert Order.objects.filter(customer=customer).count() == 10
def _create_order_for_stripe(): product = get_default_product() supplier = get_default_supplier() order = create_order_with_product(product, supplier=supplier, quantity=1, taxless_unit_price=100, tax_rate=0) order.cache_prices() assert order.taxless_total_price > 0 if not order.payment_data: order.payment_data = {} order.save() return order
def test_add_order_log_entry(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) assert not OrderLogEntry.objects.filter(target=order).exists() view = NewLogEntryView.as_view() test_message = "test_order" request = apply_request_middleware(rf.post("/", {"message": test_message}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 assert OrderLogEntry.objects.filter(target=order).exists() assert OrderLogEntry.objects.filter(target=order).first().message == test_message
def test_methods(admin_user, country): contact = get_person_contact(admin_user) source = BasketishOrderSource( lines=[ SourceLine( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, unit_price=TaxlessPrice(10), weight=Decimal("0.2"), ) ] ) billing_address = get_address() shipping_address = get_address(name="Shippy Doge", country=country) source.shop = get_default_shop() source.billing_address = billing_address source.shipping_address = shipping_address source.customer = contact with override_provides_for_expensive_sweden_shipping_method(): source.shipping_method = get_expensive_sweden_shipping_method() source.payment_method = PaymentMethod.objects.create( identifier="neat", module_data={"price": 4}, tax_class=get_default_tax_class() ) assert source.shipping_method_id assert source.payment_method_id errors = list(source.get_validation_errors()) if ( country == "FI" ): # "Expenseefe-a Svedee Sheepping" will not allow shipping to Finland, let's see if that holds true assert any([ve.code == "we_no_speak_finnish" for ve in errors]) return # Shouldn't try the rest if we got an error here else: assert not errors final_lines = list(source.get_final_lines()) assert any(line.type == OrderLineType.SHIPPING for line in final_lines) # TODO: (TAX) for some reason SourceLine.taxless_total_price property has been removed # I think it should be implemented back like in OrderLine / janne for line in final_lines: if line.type == OrderLineType.SHIPPING: if country == "SE": # We _are_ using Expenseefe-a Svedee Sheepping after all. assert line.taxless_total_price == TaxlessPrice("5.00") else: assert line.taxless_total_price == TaxlessPrice("4.00") assert line.text == u"Expenseefe-a Svedee Sheepping" if line.type == OrderLineType.PAYMENT: assert line.taxless_total_price == TaxlessPrice("4")
def test_basic_order(rf, admin_user, mode): prices_include_tax = mode == "taxful" shop = get_shop(prices_include_tax=prices_include_tax) request = rf.get("/") request.shop = shop product = get_default_product() customer = get_person_contact(admin_user) for x in range(10): create_order(request, creator=admin_user, customer=customer, product=product) assert Order.objects.filter(customer=customer).count() == 10
def test_product_page(client): get_default_shop() product = get_default_product() response = client.get( reverse('shoop:product', kwargs={ 'pk': product.pk, 'slug': product.slug } ) ) assert b'no such element' not in response.content, 'All items are not rendered correctly'
def test_basket_update(): request = get_request_with_basket() basket = request.basket product = get_default_product() basket_commands.handle_add(request, basket, product_id=product.pk, quantity=1) assert basket.product_count == 1 line_id = basket.get_lines()[0].line_id basket_commands.handle_update(request, basket, **{"q_%s" % line_id: "2"}) assert basket.product_count == 2 basket_commands.handle_update(request, basket, **{"delete_%s" % line_id: "1"}) assert basket.product_count == 0
def test_product_edit_view_works_at_all(rf, admin_user): get_default_shop() request = apply_request_middleware(rf.get("/"), user=admin_user) with replace_modules([ProductModule]): with admin_only_urls(): default_product = get_default_product() view_func = ProductEditView.as_view() response = view_func(request, pk=default_product.pk) assert (default_product.sku in response.rendered_content) # it's probable the SKU is there response = view_func(request, pk=None) # "new mode" assert response.rendered_content # yeah, something gets rendered
def test_order_set_status_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) order.create_shipment_of_all_products() # Need to be shipped to set complete assert order.status.role == OrderStatusRole.INITIAL complete_status = OrderStatus.objects.get_default_complete() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": complete_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == complete_status.id assert order.log_entries.filter(identifier="status_change").exists()
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)
def test_order_verification(): product = get_default_product() supplier = get_default_supplier() order = create_order_with_product(product, supplier=supplier, quantity=3, n_lines=10, taxless_base_unit_price=10) order.require_verification = True order.save() assert not order.check_all_verified(), "Nothing is verified by default" order.lines.filter(pk=order.lines.filter(verified=False).first().pk).update(verified=True) assert not order.check_all_verified(), "All is not verified even if something is" order.lines.all().update(verified=True) assert order.check_all_verified(), "All is now verified" assert not order.require_verification, "Verification complete"
def test_product_module_search(rf, admin_user): get_default_shop() request = apply_request_middleware(rf.get("/"), user=admin_user) with replace_modules([ProductModule]): with admin_only_urls(): default_product = get_default_product() model_url = get_model_url(default_product) sku = default_product.sku assert any(sr.url == model_url for sr in get_search_results(request, query=sku)) # Queries work assert any(sr.is_action for sr in get_search_results(request, query=sku[:5])) # Actions work assert empty_iterable(get_search_results(request, query=sku[:2])) # Short queries don't
def test_basic_order(rf, admin_user, mode): prices_include_tax = (mode == "taxful") shop = get_shop(prices_include_tax=prices_include_tax) request = rf.get('/') request.shop = shop apply_request_middleware(request) product = get_default_product() customer = get_person_contact(admin_user) for x in range(10): create_order(request, creator=admin_user, customer=customer, product=product) assert Order.objects.filter(customer=customer).count() == 10
def test_order_set_status_canceled_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert order.status.role == OrderStatusRole.INITIAL canceled_status = OrderStatus.objects.get_default_canceled() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": canceled_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == canceled_status.id assert order.log_entries.filter(identifier="status_change").exists()
def test_waiver(): sm = get_shipping_method(name="Waivey", price=100, waive_at=370) source = BasketishOrderSource(get_default_shop()) assert sm.get_effective_name(source) == u"Waivey" assert sm.get_total_cost(source).price == source.create_price(100) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.create_price(400), quantity=1 ) assert sm.get_total_cost(source).price == source.create_price(0)
def test_order_set_status_completed_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) order.create_shipment_of_all_products() # Need to be shipped to set complete assert order.status.role == OrderStatusRole.INITIAL complete_status = OrderStatus.objects.get_default_complete() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": complete_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == complete_status.id assert order.log_entries.filter(identifier="status_change").exists()
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
def test_add_order_log_entry(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(), )) assert not OrderLogEntry.objects.filter(target=order).exists() view = NewLogEntryView.as_view() test_message = "test_order" request = apply_request_middleware(rf.post("/", {"message": test_message}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 assert OrderLogEntry.objects.filter(target=order).exists() assert OrderLogEntry.objects.filter( target=order).first().message == test_message
def test_order_creator_supplierless_product_line_conversion_should_fail(rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=None, quantity=1, base_unit_price=source.create_price(10), ) creator = OrderCreator() with pytest.raises(ValueError): order = creator.create_order(source)
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)
def create_order(): shop = get_default_shop() product = get_default_product() supplier = get_default_supplier() order = create_order_with_product( product, shop=shop, supplier=supplier, quantity=1, taxless_base_unit_price=10, ) order.cache_prices() order.save() return order
def test_order_creator_supplierless_product_line_conversion_should_fail( rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=None, quantity=1, base_unit_price=source.create_price(10), ) creator = OrderCreator() with pytest.raises(ValueError): order = creator.create_order(source)
def test_methods(admin_user, country): contact = get_person_contact(admin_user) source = BasketishOrderSource(lines=[ SourceLine(type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, unit_price=TaxlessPrice(10), weight=Decimal("0.2")) ]) billing_address = get_address() shipping_address = get_address(name="Shippy Doge", country=country) source.shop = get_default_shop() source.billing_address = billing_address source.shipping_address = shipping_address source.customer = contact with override_provides_for_expensive_sweden_shipping_method(): source.shipping_method = get_expensive_sweden_shipping_method() source.payment_method = PaymentMethod.objects.create( identifier="neat", module_data={"price": 4}, tax_class=get_default_tax_class()) assert source.shipping_method_id assert source.payment_method_id errors = list(source.get_validation_errors()) if country == "FI": # "Expenseefe-a Svedee Sheepping" will not allow shipping to Finland, let's see if that holds true assert any([ve.code == "we_no_speak_finnish" for ve in errors]) return # Shouldn't try the rest if we got an error here else: assert not errors final_lines = list(source.get_final_lines()) assert any(line.type == OrderLineType.SHIPPING for line in final_lines) # TODO: (TAX) for some reason SourceLine.taxless_total_price property has been removed # I think it should be implemented back like in OrderLine / janne for line in final_lines: if line.type == OrderLineType.SHIPPING: if country == "SE": # We _are_ using Expenseefe-a Svedee Sheepping after all. assert line.taxless_total_price == TaxlessPrice("5.00") else: assert line.taxless_total_price == TaxlessPrice("4.00") assert line.text == u"Expenseefe-a Svedee Sheepping" if line.type == OrderLineType.PAYMENT: assert line.taxless_total_price == TaxlessPrice("4")
def test_basic_order(): PRODUCTS_TO_SEND = 10 product = get_default_product() supplier = get_default_supplier() order = create_order_with_product(product, supplier=supplier, quantity=PRODUCTS_TO_SEND, taxless_base_unit_price=10, tax_rate=Decimal("0.5")) assert order.shop.prices_include_tax is False price = order.shop.create_price currency = order.currency discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.discount_amount = price(30) assert discount_order_line.price == price(-30) discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() assert order.taxful_total_price == TaxfulPrice( PRODUCTS_TO_SEND * (10 + 5) - 30, currency) shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == PRODUCTS_TO_SEND, "All products were shipped" assert shipment.weight == product.net_weight * PRODUCTS_TO_SEND, "Gravity works" assert not order.get_unshipped_products( ), "Nothing was left in the warehouse" order.shipping_status = ShippingStatus.FULLY_SHIPPED order.create_payment(order.taxful_total_price) assert order.payments.exists(), "A payment was created" with pytest.raises(NoPaymentToCreateException): order.create_payment(Money(6, currency)) assert order.is_paid(), "Order got paid" assert order.can_set_complete(), "Finalization is possible" order.status = OrderStatus.objects.get_default_complete() assert order.is_complete(), "Finalization done" summary = order.get_tax_summary() assert len(summary) == 2 assert summary[0].tax_id is None assert summary[0].tax_code == '' assert summary[0].tax_amount == Money(0, currency) assert summary[0].tax_rate == 0 assert summary[1].tax_rate * 100 == 50 assert summary[1].based_on == Money(100, currency) assert summary[1].tax_amount == Money(50, currency) assert summary[1].taxful == summary[1].based_on + summary[1].tax_amount
def test_order_set_status_canceled_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(), )) assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert order.status.role == OrderStatusRole.INITIAL canceled_status = OrderStatus.objects.get_default_canceled() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": canceled_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == canceled_status.id assert order.log_entries.filter(identifier="status_change").exists()
def get_source(): source = BasketishOrderSource(get_default_shop()) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) return source
def test_methods(admin_user, country): contact = get_person_contact(admin_user) source = BasketishOrderSource(get_default_shop()) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), weight=Decimal("0.2") ) billing_address = get_address() shipping_address = get_address(name="Shippy Doge", country=country) source.billing_address = billing_address source.shipping_address = shipping_address source.customer = contact source.shipping_method = get_expensive_sweden_shipping_method() source.payment_method = get_payment_method(name="neat", price=4) assert source.shipping_method_id assert source.payment_method_id errors = list(source.get_validation_errors()) if country == "FI": # "Expenseefe-a Svedee Sheepping" will not allow shipping to # Finland, let's see if that holds true assert any([ve.code == "we_no_speak_finnish" for ve in errors]) assert [x.code for x in errors] == ["we_no_speak_finnish"] return # Shouldn't try the rest if we got an error here else: assert not errors final_lines = list(source.get_final_lines()) assert any(line.type == OrderLineType.SHIPPING for line in final_lines) for line in final_lines: if line.type == OrderLineType.SHIPPING: if country == "SE": # We _are_ using Expenseefe-a Svedee Sheepping after all. assert line.price == source.create_price("5.00") else: assert line.price == source.create_price("4.00") assert line.text == u"Expenseefe-a Svedee Sheepping" if line.type == OrderLineType.PAYMENT: assert line.price == source.create_price(4)