categorias-productos #51 #52

Open
mono wants to merge 13 commits from feat/51-categorias-productos into main
3 changed files with 83 additions and 3 deletions
Showing only changes of commit 73fff016a3 - Show all commits

View File

@@ -1,5 +1,10 @@
from .catalogue_images import CatalogueImageSerializer
from .products import ProductSerializer, ListProductSerializer
from .products import (
ProductSerializer,
ListProductSerializer,
ProductCategorySerializer,
ProductCategoryBriefSerializer,
)
from .customers import CustomerSerializer, ListCustomerSerializer
from .sales import (
SaleSerializer,
@@ -26,6 +31,8 @@ __all__ = [
# Products
"ProductSerializer",
"ListProductSerializer",
"ProductCategorySerializer",
"ProductCategoryBriefSerializer",
# Customers
"CustomerSerializer",
"ListCustomerSerializer",

View File

@@ -4,11 +4,32 @@ from ..models.products import Product, ProductCategory
from ..models.provenance import Supplier
class ProductCategorySerializer(serializers.ModelSerializer):
class Meta:
model = ProductCategory
fields = [
"id",
"name",
"external_id",
"description",
"alternative_name",
"active",
]
read_only_fields = ["name", "external_id"]
class ProductCategoryBriefSerializer(serializers.ModelSerializer):
class Meta:
model = ProductCategory
fields = ["id", "name", "alternative_name", "active"]
class ProductSerializer(serializers.ModelSerializer):
catalogue_images = serializers.SerializerMethodField()
suppliers = serializers.PrimaryKeyRelatedField(
many=True, queryset=Supplier.objects.all(), required=False
)
categories = ProductCategoryBriefSerializer(many=True, read_only=True)
class Meta:
model = Product

View File

@@ -1,6 +1,6 @@
from django.test import TestCase
from ..models.products import ProductCategory
from ..models.products import Product, ProductCategory
class TestProductCategoryModel(TestCase):
@@ -22,4 +22,56 @@ class TestProductCategoryModel(TestCase):
category = ProductCategory.objects.create(name="Granos")
self.assertTrue(category.active)
self.assertEqual(category.description, "")
self.assertIsNone(category.alternative_name)
self.assertIsNone(category.alternative_name)
class TestProductCategorySerializers(TestCase):
def test_product_category_serializer_devuelve_campos_don_confiao(self):
from ..serializers import ProductCategorySerializer
category = ProductCategory.objects.create(
name="Bebidas",
external_id="8",
description="Bebidas naturales",
alternative_name="Jugos",
active=False,
)
data = ProductCategorySerializer(category).data
self.assertEqual(
data,
{
"id": category.pk,
"name": "Bebidas",
"external_id": "8",
"description": "Bebidas naturales",
"alternative_name": "Jugos",
"active": False,
},
)
def test_product_serializer_incluye_categorias_anidadas(self):
from ..serializers import ProductSerializer
category = ProductCategory.objects.create(
name="Bebidas", external_id="8", alternative_name="Jugos"
)
product = Product.objects.create(
name="Jugo de naranja",
price=2500,
measuring_unit="UNIT",
external_id="10",
)
product.categories.add(category)
data = ProductSerializer(product).data
self.assertEqual(
data["categories"],
[
{
"id": category.pk,
"name": "Bebidas",
"alternative_name": "Jugos",
"active": True,
}
],
)