feat: added tests for web
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Brand
|
||||
|
||||
|
||||
class TestBrandsAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "brand"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
|
||||
def create_brands(self):
|
||||
Brand.objects.create(
|
||||
name="Marca",
|
||||
)
|
||||
|
||||
def test_fetch_brands(self):
|
||||
self.create_brands()
|
||||
self.login()
|
||||
response = self.client.get("/api/v1/shop/brands/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
def test_create_retrieve_brand(self):
|
||||
self.login()
|
||||
response = self.client.post("/api/v1/shop/brands/", {"name": "Marca 1"})
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/brands/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_brand(self):
|
||||
self.login()
|
||||
response = self.client.post("/api/v1/shop/brands/", {"name": "Marca 1"})
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/brands/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(f"/api/v1/shop/brands/{pk}/", {"name": "Marca 2"})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/brands/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
assert response.data.get("name") == "Marca 2"
|
||||
@@ -0,0 +1,66 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
|
||||
|
||||
class TestCustomersAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "customer"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
|
||||
def test_create_retrieve_customer(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/customers/",
|
||||
{
|
||||
"vat_id": "11111111H",
|
||||
"first_name": "Darth",
|
||||
"last_name": "Maull",
|
||||
"email": "darth@maul.com",
|
||||
"address": "Dathomir",
|
||||
"city": "Dathomir",
|
||||
"state": "Dathomir",
|
||||
"country": "Dathomir",
|
||||
"zip": "00001",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
response = self.client.get(f"/api/v1/shop/customers/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_customer(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/customers/",
|
||||
{
|
||||
"vat_id": "11111111H",
|
||||
"first_name": "Darth",
|
||||
"last_name": "Maull",
|
||||
"email": "darth@maul.com",
|
||||
"address": "Dathomir",
|
||||
"city": "Dathomir",
|
||||
"state": "Dathomir",
|
||||
"country": "Dathomir",
|
||||
"zip": "00001",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
response = self.client.get(f"/api/v1/shop/customers/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/customers/{pk}/",
|
||||
{
|
||||
"address": "Mandalore",
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
response = self.client.get(f"/api/v1/shop/customers/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("address") == "Mandalore"
|
||||
@@ -0,0 +1,187 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Customer, Product, ProductPrice, Tax
|
||||
|
||||
|
||||
class TestOrdersAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "productprice"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
self.tax = Tax.objects.create(
|
||||
code="IVA",
|
||||
value=21,
|
||||
)
|
||||
self.customer = Customer.objects.create(
|
||||
vat_id="11111111H",
|
||||
first_name="Darth",
|
||||
last_name="Maull",
|
||||
email="darth@maul.com",
|
||||
address="Dathomir",
|
||||
city="Dathomir",
|
||||
state="Dathomir",
|
||||
country="Dathomir",
|
||||
zip="00001",
|
||||
)
|
||||
self.create_products()
|
||||
|
||||
def create_products(self):
|
||||
self.product = Product.objects.create(
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
|
||||
ProductPrice.objects.create(
|
||||
product=self.product,
|
||||
price=Decimal("1.20"),
|
||||
tax=self.tax,
|
||||
)
|
||||
|
||||
def test_create_retrieve_order(self):
|
||||
self.login()
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/orders/",
|
||||
{
|
||||
"customer": self.customer.pk,
|
||||
"billing_address": self.customer.address,
|
||||
"billing_city": self.customer.city,
|
||||
"billing_state": self.customer.state,
|
||||
"billing_country": self.customer.country,
|
||||
"billing_zip": self.customer.zip,
|
||||
"shipping_address": self.customer.address,
|
||||
"shipping_city": self.customer.city,
|
||||
"shipping_state": self.customer.state,
|
||||
"shipping_country": self.customer.country,
|
||||
"shipping_zip": self.customer.zip,
|
||||
"contact_phone": "612345678",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("uuid")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_retrieve_order_with_lines(self):
|
||||
self.login()
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/orders/",
|
||||
{
|
||||
"customer": self.customer.pk,
|
||||
"billing_address": self.customer.address,
|
||||
"billing_city": self.customer.city,
|
||||
"billing_state": self.customer.state,
|
||||
"billing_country": self.customer.country,
|
||||
"billing_zip": self.customer.zip,
|
||||
"shipping_address": self.customer.address,
|
||||
"shipping_city": self.customer.city,
|
||||
"shipping_state": self.customer.state,
|
||||
"shipping_country": self.customer.country,
|
||||
"shipping_zip": self.customer.zip,
|
||||
"contact_phone": "612345678",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("uuid")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert Decimal(response.data.get("base_total")) == Decimal("0.00")
|
||||
assert Decimal(response.data.get("total")) == Decimal("0.00")
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/orders/{pk}/lines/",
|
||||
{
|
||||
"product": self.product.pk,
|
||||
"quantity": "5",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/lines/")
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
assert Decimal(response.data.get("base_total")) == Decimal("6.00")
|
||||
assert Decimal(response.data.get("total")) == Decimal("6.00") + (
|
||||
Decimal("6.00") * Decimal("0.21")
|
||||
)
|
||||
|
||||
def test_create_delete_order_with_lines(self):
|
||||
self.login()
|
||||
|
||||
# Create order
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/orders/",
|
||||
{
|
||||
"customer": self.customer.pk,
|
||||
"billing_address": self.customer.address,
|
||||
"billing_city": self.customer.city,
|
||||
"billing_state": self.customer.state,
|
||||
"billing_country": self.customer.country,
|
||||
"billing_zip": self.customer.zip,
|
||||
"shipping_address": self.customer.address,
|
||||
"shipping_city": self.customer.city,
|
||||
"shipping_state": self.customer.state,
|
||||
"shipping_country": self.customer.country,
|
||||
"shipping_zip": self.customer.zip,
|
||||
"contact_phone": "612345678",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("uuid")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
# Assert order is created with 0.00€
|
||||
assert Decimal(response.data.get("base_total")) == Decimal("0.00")
|
||||
assert Decimal(response.data.get("total")) == Decimal("0.00")
|
||||
|
||||
# Create order line
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/orders/{pk}/lines/",
|
||||
{
|
||||
"product": self.product.pk,
|
||||
"quantity": "5",
|
||||
},
|
||||
)
|
||||
|
||||
line_pk = response.data.get("id")
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/lines/")
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
# Assert order totals are set
|
||||
assert Decimal(response.data.get("base_total")) == Decimal("6.00")
|
||||
assert Decimal(response.data.get("total")) == Decimal("6.00") + (
|
||||
Decimal("6.00") * Decimal("0.21")
|
||||
)
|
||||
|
||||
# Delete line
|
||||
response = self.client.delete(f"/api/v1/shop/orders/{pk}/lines/{line_pk}/")
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/orders/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
# Assert order totals are 0.00
|
||||
assert Decimal(response.data.get("base_total")) == Decimal("0.00")
|
||||
assert Decimal(response.data.get("total")) == Decimal("0.00")
|
||||
@@ -0,0 +1,94 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Product, Provider
|
||||
|
||||
|
||||
class TestProductBatchesAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "productbatch"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
|
||||
def create_provider(self):
|
||||
return Provider.objects.create(
|
||||
vat_id="11111111H",
|
||||
name="Mandalorians",
|
||||
email="din@djarin.com",
|
||||
phone="612345678",
|
||||
address="Mandalore",
|
||||
city="Mandalore",
|
||||
state="Mandalore",
|
||||
country="Mandalore",
|
||||
zip="12345",
|
||||
)
|
||||
|
||||
def create_product(self):
|
||||
self.product = Product.objects.create(
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
|
||||
def test_create_retrieve_product_batch(self):
|
||||
self.login()
|
||||
self.create_product()
|
||||
provider = self.create_provider()
|
||||
response = self.client.post(
|
||||
"/api/v1/shop/product-batches/",
|
||||
{
|
||||
"code": "B00001",
|
||||
"product": self.product.pk,
|
||||
"quantity": "5",
|
||||
"provider": provider.pk,
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/product-batches/{pk}/")
|
||||
self.product.refresh_from_db()
|
||||
assert self.product.stock == Decimal("5")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_product_batch(self):
|
||||
self.login()
|
||||
self.create_product()
|
||||
provider = self.create_provider()
|
||||
|
||||
response = self.client.post(
|
||||
"/api/v1/shop/product-batches/",
|
||||
{
|
||||
"code": "B00001",
|
||||
"product": self.product.pk,
|
||||
"quantity": "5",
|
||||
"provider": provider.pk,
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/product-batches/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
self.product.refresh_from_db()
|
||||
assert self.product.stock == Decimal("5")
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/product-batches/{pk}/",
|
||||
{
|
||||
"quantity": "10",
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
self.product.refresh_from_db()
|
||||
assert self.product.stock == Decimal("10")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/product-batches/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert Decimal(response.data.get("quantity")) == Decimal("10")
|
||||
@@ -0,0 +1,116 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Product, ProductPrice, Tax
|
||||
|
||||
|
||||
class TestProductPricessAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "productprice"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
self.tax = Tax.objects.create(
|
||||
code="IVA",
|
||||
value=21,
|
||||
)
|
||||
|
||||
def create_products(self):
|
||||
self.product = Product.objects.create(
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
|
||||
ProductPrice.objects.create(
|
||||
product=self.product,
|
||||
price=Decimal("1.20"),
|
||||
tax=self.tax,
|
||||
)
|
||||
|
||||
def test_fetch_product_prices(self):
|
||||
self.login()
|
||||
self.create_products()
|
||||
response = self.client.get(f"/api/v1/shop/products/{self.product.pk}/prices/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
def test_create_product_prices(self):
|
||||
self.login()
|
||||
|
||||
product = Product.objects.create(
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/products/{product.pk}/prices/",
|
||||
{
|
||||
"price": "1.20",
|
||||
"tax": self.tax.pk,
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
response = self.client.get(f"/api/v1/shop/products/{product.pk}/prices/")
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
def test_create_update_product_prices(self):
|
||||
self.login()
|
||||
|
||||
product = Product.objects.create(
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/products/{product.pk}/prices/",
|
||||
{
|
||||
"price": "1.20",
|
||||
"tax": self.tax.pk,
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/products/{product.pk}/prices/{pk}/",
|
||||
{
|
||||
"price": "1.10",
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
response = self.client.get(f"/api/v1/shop/products/{product.pk}/prices/{pk}/")
|
||||
assert response.data.get("price") == "1.10"
|
||||
|
||||
def test_create_delete_product_prices(self):
|
||||
self.login()
|
||||
|
||||
product = Product.objects.create(
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/products/{product.pk}/prices/",
|
||||
{
|
||||
"price": "1.20",
|
||||
"tax": self.tax.pk,
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.delete(
|
||||
f"/api/v1/shop/products/{product.pk}/prices/{pk}/"
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/products/{product.pk}/prices/{pk}/")
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
@@ -0,0 +1,176 @@
|
||||
import random
|
||||
import string
|
||||
from decimal import Decimal
|
||||
|
||||
from django.utils import timezone
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Product, ProductBatch, ProductPrice, Tag, Tax
|
||||
|
||||
|
||||
class TestProductsAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "product"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
self.tax = Tax.objects.create(
|
||||
code="IVA",
|
||||
value=21,
|
||||
)
|
||||
self.tag = Tag.objects.create(name="Alimentos")
|
||||
|
||||
def get_random_sku(self):
|
||||
return "".join(random.choices(string.ascii_uppercase, k=16))
|
||||
|
||||
def create_products(self):
|
||||
self.product = Product.objects.create(
|
||||
sku=self.get_random_sku(),
|
||||
name="Papafritas",
|
||||
description="Las mejores papafritas",
|
||||
is_digital_asset=False,
|
||||
url="",
|
||||
)
|
||||
|
||||
self.product_price = ProductPrice.objects.create(
|
||||
product=self.product,
|
||||
price=Decimal("1.20"),
|
||||
tax=self.tax,
|
||||
date=timezone.now(),
|
||||
)
|
||||
|
||||
self.batch = ProductBatch.objects.create(
|
||||
code="B0001",
|
||||
product=self.product,
|
||||
quantity=Decimal("10"),
|
||||
)
|
||||
|
||||
def test_fetch_products(self):
|
||||
self.login()
|
||||
self.create_products()
|
||||
response = self.client.get("/api/v1/shop/products/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
def test_create_product(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
"/api/v1/shop/products/",
|
||||
{
|
||||
"sku": self.get_random_sku(),
|
||||
"name": "Papafritas",
|
||||
"description": "Las mejores papafritas",
|
||||
"is_digital_asset": False,
|
||||
"url": "",
|
||||
"price": "1.20",
|
||||
"stock": "100",
|
||||
"tax": self.tax.pk,
|
||||
"tags": [self.tag.pk],
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_product(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
"/api/v1/shop/products/",
|
||||
{
|
||||
"sku": self.get_random_sku(),
|
||||
"name": "Papafritas",
|
||||
"description": "Las mejores papafritas",
|
||||
"is_digital_asset": False,
|
||||
"url": "",
|
||||
"price": "1.20",
|
||||
"stock": "100",
|
||||
"tax": self.tax.pk,
|
||||
"tags": [
|
||||
self.tag.pk,
|
||||
],
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/products/{pk}/", {"name": "Papafritas pafritas"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.data.get("name") == "Papafritas pafritas"
|
||||
|
||||
def test_create_delete_product(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
"/api/v1/shop/products/",
|
||||
{
|
||||
"sku": self.get_random_sku(),
|
||||
"name": "Papafritas",
|
||||
"description": "Las mejores papafritas",
|
||||
"is_digital_asset": False,
|
||||
"url": "",
|
||||
"price": "1.20",
|
||||
"stock": "100",
|
||||
"tax": self.tax.pk,
|
||||
"tags": [
|
||||
self.tag.pk,
|
||||
],
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.delete(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
def test_create_cannot_delete_product_with_a_batch(self):
|
||||
self.login()
|
||||
self.create_products()
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/products/{self.product.pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.delete(f"/api/v1/shop/products/{self.product.pk}/")
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
|
||||
def test_create_update_product_tags(self):
|
||||
self.login()
|
||||
tag = Tag.objects.create(name="Alimentación")
|
||||
response = self.client.post(
|
||||
"/api/v1/shop/products/",
|
||||
{
|
||||
"sku": self.get_random_sku(),
|
||||
"name": "Papafritas",
|
||||
"description": "Las mejores papafritas",
|
||||
"is_digital_asset": False,
|
||||
"url": "",
|
||||
"price": "1.20",
|
||||
"stock": "100",
|
||||
"tax": self.tax.pk,
|
||||
"tags": [
|
||||
self.tag.pk,
|
||||
],
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(f"/api/v1/shop/products/{pk}/", {"tags": [tag.pk]})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
response = self.client.get(f"/api/v1/shop/products/{pk}/")
|
||||
assert response.data.get("tags", [])[0] == tag.pk
|
||||
@@ -0,0 +1,66 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
|
||||
|
||||
class TestProvidersAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "provider"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
|
||||
def test_create_retrieve_provider(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/providers/",
|
||||
{
|
||||
"vat_id": "11111111H",
|
||||
"name": "Darth Maul",
|
||||
"email": "darth@maul.com",
|
||||
"address": "Dathomir",
|
||||
"city": "Dathomir",
|
||||
"state": "Dathomir",
|
||||
"country": "Dathomir",
|
||||
"zip": "00001",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/providers/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_provider(self):
|
||||
self.login()
|
||||
response = self.client.post(
|
||||
f"/api/v1/shop/providers/",
|
||||
{
|
||||
"vat_id": "11111111H",
|
||||
"name": "Darth Maul",
|
||||
"email": "darth@maul.com",
|
||||
"address": "Dathomir",
|
||||
"city": "Dathomir",
|
||||
"state": "Dathomir",
|
||||
"country": "Dathomir",
|
||||
"zip": "00001",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
response = self.client.get(f"/api/v1/shop/providers/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/providers/{pk}/",
|
||||
{
|
||||
"address": "Mandalore",
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
response = self.client.get(f"/api/v1/shop/providers/{pk}/")
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("address") == "Mandalore"
|
||||
@@ -0,0 +1,50 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Tag
|
||||
|
||||
|
||||
class TestTagsAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "tag"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
|
||||
def create_tags(self):
|
||||
Tag.objects.create(name="Deportes")
|
||||
|
||||
def test_fetch_taxes(self):
|
||||
self.create_tags()
|
||||
self.login()
|
||||
response = self.client.get("/api/v1/shop/tags/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
def test_create_retrieve_tax(self):
|
||||
self.login()
|
||||
response = self.client.post("/api/v1/shop/tags/", {"name": "Videojuegos"})
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/tags/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_tax(self):
|
||||
self.login()
|
||||
response = self.client.post("/api/v1/shop/tags/", {"name": "Videojuegos"})
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/tags/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/tags/{pk}/", {"name": "Juegos de mesa"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/tags/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
assert response.data.get("name") == "Juegos de mesa"
|
||||
@@ -0,0 +1,54 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from config.tests.mixins import TestUserAuthenticationMixin
|
||||
from shop.models import Tax
|
||||
|
||||
|
||||
class TestTaxesAPI(APITestCase, TestUserAuthenticationMixin):
|
||||
model_name = "tax"
|
||||
|
||||
def setUp(self):
|
||||
self.create_user()
|
||||
|
||||
def create_taxes(self):
|
||||
Tax.objects.create(
|
||||
code="IVA",
|
||||
value=21,
|
||||
)
|
||||
|
||||
def test_fetch_taxes(self):
|
||||
self.create_taxes()
|
||||
self.login()
|
||||
response = self.client.get("/api/v1/shop/taxes/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert len(response.data.get("results")) == 1
|
||||
|
||||
def test_create_retrieve_tax(self):
|
||||
self.login()
|
||||
response = self.client.post("/api/v1/shop/taxes/", {"code": "IVA5", "value": 5})
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/taxes/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_create_update_tax(self):
|
||||
self.login()
|
||||
response = self.client.post("/api/v1/shop/taxes/", {"code": "IVA5", "value": 5})
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
pk = response.data.get("id")
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/taxes/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/v1/shop/taxes/{pk}/", {"code": "IVA10", "value": 10}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response = self.client.get(f"/api/v1/shop/taxes/{pk}/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
assert response.data.get("code") == "IVA10"
|
||||
assert response.data.get("value") == 10
|
||||
@@ -0,0 +1,83 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from rest_framework.test import APITestCase as TestCase
|
||||
|
||||
from shop.models import Customer, Product, ProductPrice, Tax
|
||||
from shop.utils import create_order, create_order_line_for_product
|
||||
|
||||
|
||||
class ShopModelsTest(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.tax = Tax.objects.create(
|
||||
code="IVA",
|
||||
value=21,
|
||||
)
|
||||
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):
|
||||
self.customer = Customer.objects.create(
|
||||
first_name="Luke",
|
||||
last_name="Skywalker",
|
||||
vat_id="11111111H",
|
||||
address="Farm Skywalker",
|
||||
city="Desert",
|
||||
state="Mos-Eisley",
|
||||
zip="00001",
|
||||
country="Tatooine",
|
||||
)
|
||||
|
||||
order = create_order(
|
||||
customer=self.customer,
|
||||
billing_address=self.customer.address,
|
||||
billing_city=self.customer.city,
|
||||
billing_state=self.customer.state,
|
||||
billing_zip=self.customer.zip,
|
||||
billing_country=self.customer.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
|
||||
Reference in New Issue
Block a user