From 8d6eb3d61cfe2e9379dde1e2212d34b924494cbe Mon Sep 17 00:00:00 2001 From: Pablo Moreno Date: Fri, 22 Nov 2024 22:09:10 +0100 Subject: [PATCH] feat: added tests for web --- shop/filters.py | 7 ++--- ...0004_alter_product_options_product_slug.py | 2 +- shop/models.py | 7 +++-- shop/tests/api/__init__.py | 0 shop/tests/{ => api}/test_api_brands.py | 0 shop/tests/{ => api}/test_api_customers.py | 0 shop/tests/{ => api}/test_api_orders.py | 0 .../{ => api}/test_api_product_batches.py | 0 .../{ => api}/test_api_product_prices.py | 0 shop/tests/{ => api}/test_api_products.py | 0 shop/tests/{ => api}/test_api_providers.py | 0 shop/tests/{ => api}/test_api_tags.py | 0 shop/tests/{ => api}/test_api_taxes.py | 0 shop/tests/{ => api}/test_shop_models.py | 0 shop/tests/mixins.py | 31 +++++++++++++++++++ shop/tests/web/__init__.py | 0 shop/tests/web/test_index.py | 22 +++++++++++++ 17 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 shop/tests/api/__init__.py rename shop/tests/{ => api}/test_api_brands.py (100%) rename shop/tests/{ => api}/test_api_customers.py (100%) rename shop/tests/{ => api}/test_api_orders.py (100%) rename shop/tests/{ => api}/test_api_product_batches.py (100%) rename shop/tests/{ => api}/test_api_product_prices.py (100%) rename shop/tests/{ => api}/test_api_products.py (100%) rename shop/tests/{ => api}/test_api_providers.py (100%) rename shop/tests/{ => api}/test_api_tags.py (100%) rename shop/tests/{ => api}/test_api_taxes.py (100%) rename shop/tests/{ => api}/test_shop_models.py (100%) create mode 100644 shop/tests/mixins.py create mode 100644 shop/tests/web/__init__.py create mode 100644 shop/tests/web/test_index.py diff --git a/shop/filters.py b/shop/filters.py index 645bbfc..8ea187e 100644 --- a/shop/filters.py +++ b/shop/filters.py @@ -1,6 +1,7 @@ import django_filters from django.utils.text import gettext_lazy as _ from django_filters import OrderingFilter + from shop.models import Product @@ -9,11 +10,7 @@ class ProductFilter(django_filters.FilterSet): field_name="name", lookup_expr="icontains", label=_("Nombre") ) tags = django_filters.BaseInFilter(field_name="tags", label=_("Etiquetas")) - ordering = OrderingFilter( - fields={ - 'name': 'name' - } - ) + ordering = OrderingFilter(fields={"name": "name"}) class Meta: model = Product diff --git a/shop/migrations/0004_alter_product_options_product_slug.py b/shop/migrations/0004_alter_product_options_product_slug.py index ce3da5d..06df414 100644 --- a/shop/migrations/0004_alter_product_options_product_slug.py +++ b/shop/migrations/0004_alter_product_options_product_slug.py @@ -36,5 +36,5 @@ class Migration(migrations.Migration): ), migrations.RunPython( add_product_slug, - ) + ), ] diff --git a/shop/models.py b/shop/models.py index 5cf1bb0..cfc6189 100644 --- a/shop/models.py +++ b/shop/models.py @@ -3,7 +3,8 @@ from uuid import uuid4 from django.db import models from django.utils import timezone -from django.utils.text import gettext_lazy as _, slugify +from django.utils.text import gettext_lazy as _ +from django.utils.text import slugify class TimestampedModel(models.Model): @@ -70,7 +71,9 @@ class Product(TimestampedModel): on_delete=models.SET_NULL, verbose_name=_("marca"), ) - slug = models.SlugField(default="", blank=True, null=True, max_length=128, verbose_name=_('Slug')) + slug = models.SlugField( + default="", blank=True, null=True, max_length=128, verbose_name=_("Slug") + ) def __str__(self): return self.name diff --git a/shop/tests/api/__init__.py b/shop/tests/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shop/tests/test_api_brands.py b/shop/tests/api/test_api_brands.py similarity index 100% rename from shop/tests/test_api_brands.py rename to shop/tests/api/test_api_brands.py diff --git a/shop/tests/test_api_customers.py b/shop/tests/api/test_api_customers.py similarity index 100% rename from shop/tests/test_api_customers.py rename to shop/tests/api/test_api_customers.py diff --git a/shop/tests/test_api_orders.py b/shop/tests/api/test_api_orders.py similarity index 100% rename from shop/tests/test_api_orders.py rename to shop/tests/api/test_api_orders.py diff --git a/shop/tests/test_api_product_batches.py b/shop/tests/api/test_api_product_batches.py similarity index 100% rename from shop/tests/test_api_product_batches.py rename to shop/tests/api/test_api_product_batches.py diff --git a/shop/tests/test_api_product_prices.py b/shop/tests/api/test_api_product_prices.py similarity index 100% rename from shop/tests/test_api_product_prices.py rename to shop/tests/api/test_api_product_prices.py diff --git a/shop/tests/test_api_products.py b/shop/tests/api/test_api_products.py similarity index 100% rename from shop/tests/test_api_products.py rename to shop/tests/api/test_api_products.py diff --git a/shop/tests/test_api_providers.py b/shop/tests/api/test_api_providers.py similarity index 100% rename from shop/tests/test_api_providers.py rename to shop/tests/api/test_api_providers.py diff --git a/shop/tests/test_api_tags.py b/shop/tests/api/test_api_tags.py similarity index 100% rename from shop/tests/test_api_tags.py rename to shop/tests/api/test_api_tags.py diff --git a/shop/tests/test_api_taxes.py b/shop/tests/api/test_api_taxes.py similarity index 100% rename from shop/tests/test_api_taxes.py rename to shop/tests/api/test_api_taxes.py diff --git a/shop/tests/test_shop_models.py b/shop/tests/api/test_shop_models.py similarity index 100% rename from shop/tests/test_shop_models.py rename to shop/tests/api/test_shop_models.py diff --git a/shop/tests/mixins.py b/shop/tests/mixins.py new file mode 100644 index 0000000..1dcd50e --- /dev/null +++ b/shop/tests/mixins.py @@ -0,0 +1,31 @@ +from decimal import Decimal + +from django.utils.timezone import now + +from shop.models import Product, ProductPrice, Tax + + +class CreateProductsMixin: + def create_product( + self, + sku="000001", + name="Producto 1", + description="Descripción", + price=Decimal("10.00"), + ) -> Product: + tax = Tax.objects.create( + code="IVA", + value=21, + ) + product = Product.objects.create( + sku=sku, + name=name, + description=description, + ) + ProductPrice.objects.create( + price=price, + product=product, + date=now(), + tax=tax, + ) + return product diff --git a/shop/tests/web/__init__.py b/shop/tests/web/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shop/tests/web/test_index.py b/shop/tests/web/test_index.py new file mode 100644 index 0000000..2be9f88 --- /dev/null +++ b/shop/tests/web/test_index.py @@ -0,0 +1,22 @@ +from django.test import TestCase +from django.urls import reverse + +from shop.tests.mixins import CreateProductsMixin + + +class TestIndex(TestCase, CreateProductsMixin): + def setUp(self): + self.product = self.create_product() + + def test_index_view(self): + response = self.client.get(reverse("shop:index")) + assert response.status_code == 200 + + def test_product_detail_view(self): + response = self.client.get( + reverse( + "shop:product_detail", + kwargs={"pk": self.product.pk, "slug": self.product.slug}, + ) + ) + assert response.status_code == 200