103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
from decimal import Decimal
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from rest_framework.test import APITestCase as TestCase
|
|
|
|
from shop.models import Customer, Product, ProductPrice, Tax, CustomerAddress
|
|
from shop.utils import create_order, create_order_line_for_product
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class ShopModelsTest(TestCase):
|
|
def setUp(self) -> None:
|
|
self.tax = Tax.objects.create(
|
|
code="IVA",
|
|
value=21,
|
|
)
|
|
self.customer = User.objects.create_user(
|
|
username="11111111H",
|
|
first_name="Darth",
|
|
last_name="Maull",
|
|
email="darth@maul.com",
|
|
password="dathomir",
|
|
)
|
|
|
|
self.customer_shipping_address = CustomerAddress.objects.create(
|
|
user=self.customer,
|
|
address="Dathomir",
|
|
address_town="Dathomir",
|
|
address_zip="00001",
|
|
address_state="Dathomir",
|
|
address_phone="900000000",
|
|
address_type=CustomerAddress.Types.SHIPPING,
|
|
)
|
|
|
|
self.customer_billing_address = CustomerAddress.objects.create(
|
|
user=self.customer,
|
|
address="Dathomir",
|
|
address_town="Dathomir",
|
|
address_zip="00001",
|
|
address_state="Dathomir",
|
|
address_phone="900000000",
|
|
address_type=CustomerAddress.Types.BILLING,
|
|
)
|
|
self.create_products()
|
|
|
|
def create_products(self):
|
|
self.potatoes = Product.objects.create(
|
|
sku="0000001",
|
|
name="Patatas",
|
|
stock=Decimal("100.00"),
|
|
)
|
|
self.gasoline = Product.objects.create(
|
|
sku="0000002",
|
|
name="Gasolina",
|
|
stock=Decimal("800.00"),
|
|
)
|
|
self.usb_c = Product.objects.create(
|
|
sku="0000003",
|
|
name="Cable USB-C",
|
|
stock=Decimal("5.00"),
|
|
)
|
|
|
|
ProductPrice.objects.create(
|
|
product=self.potatoes, price=Decimal("0.80"), tax=self.tax
|
|
)
|
|
ProductPrice.objects.create(
|
|
product=self.gasoline, price=Decimal("1.15"), tax=self.tax
|
|
)
|
|
ProductPrice.objects.create(
|
|
product=self.usb_c, price=Decimal("9.95"), tax=self.tax
|
|
)
|
|
|
|
def test_create_order(self):
|
|
order = create_order(
|
|
customer=self.customer,
|
|
billing_address=self.customer_billing_address.address,
|
|
billing_city=self.customer_billing_address.address_town,
|
|
billing_state=self.customer_billing_address.address_state,
|
|
billing_zip=self.customer_billing_address.address_zip,
|
|
billing_country=self.customer_billing_address.address_country,
|
|
)
|
|
|
|
l1 = create_order_line_for_product(
|
|
self.potatoes,
|
|
quantity=Decimal("1.5"),
|
|
order=order,
|
|
)
|
|
l2 = create_order_line_for_product(
|
|
self.gasoline,
|
|
quantity=Decimal("40"),
|
|
order=order,
|
|
)
|
|
l3 = create_order_line_for_product(
|
|
self.usb_c,
|
|
quantity=Decimal("1.00"),
|
|
order=order,
|
|
)
|
|
order.calculate_total_from_lines()
|
|
|
|
assert order.total == l1.total + l2.total + l3.total
|
|
assert order.base_total == l1.base_total + l2.base_total + l3.base_total
|