def create_order(request, creator, customer, product): billing_address = get_address() shipping_address = get_address(name="Shippy Doge") shipping_address.save() order = Order(creator=creator, customer=customer, shop=get_default_shop(), payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status()) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product(order_line=product_order_line, product=product, request=request, quantity=5, supplier=supplier) product_order_line.unit_price = TaxlessPrice(100) assert product_order_line.taxful_total_price.amount > 0 product_order_line.save() product_order_line.taxes.add( OrderLineTax.from_tax(get_default_tax(), product_order_line.taxless_total_price)) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.total_discount = TaxfulPrice(30) assert discount_order_line.taxful_total_discount.amount == 30 assert discount_order_line.taxful_total_price.amount == -30 assert discount_order_line.taxful_unit_price.amount == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base_amount = 5 * 100 tax_value = get_default_tax().calculate_amount(base_amount) assert order.taxful_total_price == base_amount + tax_value - 30, "Math works" shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products( ), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter( pk=order.pk).exists(), "It was paid! Honestly!"
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 seed_source(user): source = BasketishOrderSource(get_default_shop()) billing_address = get_address() shipping_address = get_address(name="Shippy Doge") source.status = get_initial_order_status() source.billing_address = billing_address source.shipping_address = shipping_address source.customer = get_person_contact(user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() assert source.payment_method_id == get_default_payment_method().id assert source.shipping_method_id == get_default_shipping_method().id return 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_address_saving_retrieving_and_immutability(): # We can save an address... address = get_address() address.save() # mutate it... address.name = u"Dog Hi" # Then set it as immutable... address.set_immutable() # We can find the immutable copy... found_address = Address.objects.try_get_exactly_like(address) assert found_address and found_address.pk == address.pk, "Can't find the address we just saved :(" # And when we try to save it again, it fails... address.name = u"Dog Yo" with pytest.raises(ImmutabilityError): address.save() # We can find the immutable copy, even if we've now changed a field... found_address = Address.objects.try_get_exactly_like(address, ignore_fields=("name",)) assert found_address and found_address.pk == address.pk, "Can't find the address we just saved :(" # But to mutate the address, we can copy it... address_copy = address.copy() assert not address_copy.pk address_copy.save() assert address_copy.pk != address.pk, "new address was saved as another entity" assert not address_copy.is_immutable, "new address is still mutable"
def fill_address_inputs(soup, with_company=False): inputs = {} test_address = get_address() for key, value in extract_form_fields(soup.find('form', id='addresses')).items(): if not value: if key in ("order-tax_number", "order-company_name"): continue if key.startswith("shipping-") or key.startswith("billing-"): bit = key.split("-")[1] value = getattr(test_address, bit, None) if not value and "email" in key: value = "*****@*****.**" % random.random() if not value: value = "test" inputs[key] = value if with_company: inputs["company-tax_number"] = "FI1234567-1" inputs["company-company_name"] = "Example Oy" else: inputs = dict((k, v) for (k, v) in inputs.items() if not k.startswith("company-")) return inputs
def create_order(request, creator, customer, product): billing_address = get_address() shipping_address = get_address(name="Shippy Doge") shipping_address.save() order = Order( creator=creator, customer=customer, shop=get_default_shop(), payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status() ) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product(order_line=product_order_line, product=product, request=request, quantity=5, supplier=supplier) product_order_line.unit_price = TaxlessPrice(100) assert product_order_line.taxful_total_price.amount > 0 product_order_line.save() product_order_line.taxes.add(OrderLineTax.from_tax(get_default_tax(), product_order_line.taxless_total_price)) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.total_discount = TaxfulPrice(30) assert discount_order_line.taxful_total_discount.amount == 30 assert discount_order_line.taxful_total_price.amount == -30 assert discount_order_line.taxful_unit_price.amount == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base_amount = 5 * 100 tax_value = get_default_tax().calculate_amount(base_amount) assert order.taxful_total_price == base_amount + tax_value - 30, "Math works" shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products(), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter(pk=order.pk).exists(), "It was paid! Honestly!"
def test_new_mutable_address(): address = get_address() new_mutable = address.to_mutable() # New address should be unsaved assert new_mutable.pk == None assert isinstance(new_mutable, MutableAddress) assert get_data_dict(address).items() == get_data_dict(new_mutable).items()
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)
def test_order_address_immutability_unsaved_address(save): billing_address = get_address() if save: billing_address.save() order = Order(shop=get_default_shop(), billing_address=billing_address.to_immutable(), order_date=now(), status=get_initial_order_status()) order.save() order.billing_address.name = "Mute Doge" with pytest.raises(ValidationError): order.billing_address.save()
def test_order_address_immutability_unsaved_address(save): billing_address = get_address() if save: billing_address.save() order = Order( shop=get_default_shop(), billing_address=billing_address, order_date=now(), status=get_initial_order_status() ) order.save() order.billing_address.name = "Mute Doge" with pytest.raises(ImmutabilityError): order.billing_address.save()
def test_immutable_address(): address = get_address() new_immutable = address.to_immutable() # New address should be saved assert new_immutable.pk != None assert isinstance(new_immutable, ImmutableAddress) assert get_data_dict(address).items() == get_data_dict(new_immutable).items() # Taking immutable for same address should return same object assert new_immutable == address.to_immutable() # Taking immutable from new_immutable should return same item assert new_immutable == new_immutable.to_immutable()
def test_address_ownership(admin_user): address = get_address() address.save() saved = SavedAddress(address=address) saved.owner = get_person_contact(admin_user) assert saved.get_title(), u"get_title does what it should even if there is no explicit title" saved.title = u"My favorite address" assert saved.get_title() == saved.title, u"get_title does what it should when there is an explicit title" assert six.text_type(saved) == saved.get_title(), u"str() is an alias for .get_title()" saved.full_clean() saved.save() assert SavedAddress.objects.for_owner(get_person_contact(admin_user)).filter(address=address).exists(), \ "contacts can save addresses" assert SavedAddress.objects.for_owner(None).count() == 0, "Ownerless saved addresses aren't a real thing"
def test_immutable_address(): address = get_address() new_immutable = address.to_immutable() # New address should be saved assert new_immutable.pk != None assert isinstance(new_immutable, ImmutableAddress) assert get_data_dict(address).items() == get_data_dict( new_immutable).items() # Taking immutable for same address should return same object assert new_immutable == address.to_immutable() # Taking immutable from new_immutable should return same item assert new_immutable == new_immutable.to_immutable()
def test_basic_address(): address = get_address() address.full_clean() string_repr = str(address) for field, value in get_data_dict(address).items(): if field == "country": # We can't test this right now, it's formatted in the repr continue if not value: continue assert value in string_repr, "Field %s is not represented in %r" % (field, string_repr) assert address.is_european_union, "Dog Fort, UK is in the EU" assert list(address.split_name) == ["Dog", "Hello"], "Names split correctly" assert address.first_name == "Dog", "Names split correctly" assert address.last_name == "Hello", "Names split correctly" assert address.full_name == "Sir Dog Hello , Esq.", "Names join correctly"
def test_address_saving_retrieving_and_immutability(): # We can save an address... address = get_address() address.save() # mutate it... address.name = u"Dog Hi" # Then set it as immutable... immutable_address = address.to_immutable() immutable_address.save() # And when we try to save it again, it fails... immutable_address.name = u"Dog Yo" with pytest.raises(ValidationError): immutable_address.save() # But to mutate the address, we can copy it... address_copy = address.to_mutable() assert not address_copy.pk address_copy.save() assert address_copy.pk != address.pk, "new address was saved as another entity"
def fill_address_inputs(soup, with_company=False): inputs = {} test_address = get_address() for key, value in extract_form_fields(soup.find(method="post")).items(): if not value: if key in ("order-vat_code", "order-company_name"): continue if key.startswith("shipping-") or key.startswith("billing-"): bit = key.split("-")[1] value = getattr(test_address, bit, None) if not value and "email" in key: value = "*****@*****.**" % random.random() if not value: value = "test" inputs[key] = value if with_company: inputs["company-vat_code"] = "FI1234567-1" inputs["company-company_name"] = "Example Oy" else: inputs = dict((k, v) for (k, v) in inputs.items() if not k.startswith("company-")) return inputs
def create_order(request, creator, customer, product): billing_address = get_address().to_immutable() shipping_address = get_address(name="Shippy Doge").to_immutable() shipping_address.save() shop = request.shop order = Order( creator=creator, customer=customer, shop=shop, payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status(), currency=shop.currency, prices_include_tax=shop.prices_include_tax, ) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product( pricing_context=request, order_line=product_order_line, product=product, quantity=5, supplier=supplier ) product_order_line.base_unit_price = shop.create_price(100) assert product_order_line.price.value > 0 product_order_line.save() line_tax = get_line_taxes_for(product_order_line)[0] product_order_line.taxes.add( OrderLineTax.from_tax(tax=line_tax.tax, base_amount=line_tax.base_amount, order_line=product_order_line) ) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.discount_amount = shop.create_price(30) assert discount_order_line.discount_amount.value == 30 assert discount_order_line.price.value == -30 assert discount_order_line.base_unit_price.value == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base = 5 * shop.create_price(100).amount discount = shop.create_price(30).amount tax_value = line_tax.amount if not order.prices_include_tax: assert order.taxless_total_price.amount == base - discount assert order.taxful_total_price.amount == base + tax_value - discount else: assert_almost_equal(order.taxless_total_price.amount, base - tax_value - discount) assert_almost_equal(order.taxful_total_price.amount, base - discount) shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products(), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter(pk=order.pk).exists(), "It was paid! Honestly!"
def create_order(request, creator, customer, product): billing_address = get_address().to_immutable() shipping_address = get_address(name="Shippy Doge").to_immutable() shipping_address.save() shop = request.shop order = Order( creator=creator, customer=customer, shop=shop, payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status(), currency=shop.currency, prices_include_tax=shop.prices_include_tax, ) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product(pricing_context=request, order_line=product_order_line, product=product, quantity=5, supplier=supplier) assert product_order_line.text == product.safe_translation_getter("name") product_order_line.base_unit_price = shop.create_price(100) assert product_order_line.price.value > 0 product_order_line.save() line_tax = get_line_taxes_for(product_order_line)[0] product_order_line.taxes.add( OrderLineTax.from_tax( tax=line_tax.tax, base_amount=line_tax.base_amount, order_line=product_order_line, )) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.discount_amount = shop.create_price(30) assert discount_order_line.discount_amount.value == 30 assert discount_order_line.price.value == -30 assert discount_order_line.base_unit_price.value == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base = 5 * shop.create_price(100).amount discount = shop.create_price(30).amount tax_value = line_tax.amount if not order.prices_include_tax: assert order.taxless_total_price.amount == base - discount assert order.taxful_total_price.amount == base + tax_value - discount else: assert_almost_equal(order.taxless_total_price.amount, base - tax_value - discount) assert_almost_equal(order.taxful_total_price.amount, base - discount) shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products( ), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter( pk=order.pk).exists(), "It was paid! Honestly!"