Fix: Clean Home, Crear lineas de Venta
This commit is contained in:
1
tienda_ilusion/don_confiao/tests/__init__.py
Normal file
1
tienda_ilusion/don_confiao/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
#!/usr/bin/env python3
|
||||
104
tienda_ilusion/don_confiao/tests/test_products.py
Normal file
104
tienda_ilusion/don_confiao/tests/test_products.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from django.test import Client, TestCase
|
||||
from django.conf import settings
|
||||
from ..models import ProductCategory, Product
|
||||
|
||||
import os
|
||||
import json
|
||||
|
||||
|
||||
class TestProducts(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
def test_import_products(self):
|
||||
self._import_csv()
|
||||
all_products = self._get_products()
|
||||
self.assertEqual(
|
||||
len(all_products),
|
||||
3
|
||||
)
|
||||
|
||||
def test_import_products_with_categories(self):
|
||||
self._import_csv()
|
||||
all_products = self._get_products()
|
||||
self.assertIn("Aceites", all_products[0]["categories"])
|
||||
|
||||
def test_don_repeat_categories_on_import(self):
|
||||
self._import_csv()
|
||||
categories_on_csv = ["Cafes", "Alimentos", "Aceites"]
|
||||
categories = ProductCategory.objects.all()
|
||||
self.assertCountEqual(
|
||||
[c.name for c in categories],
|
||||
categories_on_csv
|
||||
)
|
||||
|
||||
def test_update_products(self):
|
||||
self._import_csv()
|
||||
first_products = self._get_products()
|
||||
self._import_csv('example_products2.csv')
|
||||
seconds_products = self._get_products()
|
||||
self.assertEqual(len(first_products), len(seconds_products))
|
||||
|
||||
def test_preserve_id_on_import(self):
|
||||
self._import_csv()
|
||||
id_aceite = Product.objects.get(name='Aceite').id
|
||||
self._import_csv('example_products2.csv')
|
||||
id_post_updated = Product.objects.get(name='Aceite').id
|
||||
self.assertEqual(id_aceite, id_post_updated)
|
||||
|
||||
def test_update_categories_on_import(self):
|
||||
self._import_csv()
|
||||
first_products = self._get_products()
|
||||
first_categories = {p["name"]: p["categories"] for p in first_products}
|
||||
self._import_csv('example_products2.csv')
|
||||
updated_products = self._get_products()
|
||||
updated_categories = {
|
||||
p["name"]: p["categories"] for p in updated_products}
|
||||
|
||||
self.assertIn('Cafes', first_categories['Arroz'])
|
||||
self.assertNotIn('Granos', first_categories['Arroz'])
|
||||
|
||||
self.assertIn('Granos', updated_categories['Arroz'])
|
||||
self.assertNotIn('Cafes', updated_categories['Arroz'])
|
||||
|
||||
def test_update_price(self):
|
||||
self._import_csv()
|
||||
first_products = self._get_products()
|
||||
first_prices = {p["name"]: p["price_list"] for p in first_products}
|
||||
expected_first_prices = {
|
||||
"Aceite": '50000.00',
|
||||
"Café": '14000.00',
|
||||
"Arroz": '7000.00'
|
||||
}
|
||||
self.assertDictEqual(
|
||||
expected_first_prices,
|
||||
first_prices
|
||||
)
|
||||
|
||||
self._import_csv('example_products2.csv')
|
||||
updated_products = self._get_products()
|
||||
updated_prices = {p["name"]: p["price_list"] for p in updated_products}
|
||||
expected_updated_prices = {
|
||||
"Aceite": '50000.00',
|
||||
"Café": '15000.00',
|
||||
"Arroz": '6000.00'
|
||||
}
|
||||
|
||||
self.assertDictEqual(
|
||||
expected_updated_prices,
|
||||
updated_prices
|
||||
)
|
||||
|
||||
def _get_products(self):
|
||||
products_response = self.client.get("/don_confiao/productos")
|
||||
return json.loads(products_response.content.decode('utf-8'))
|
||||
|
||||
def _import_csv(self, csv_file='example_products.csv'):
|
||||
app_name = "don_confiao"
|
||||
app_dir = os.path.join(settings.BASE_DIR, app_name)
|
||||
example_csv = os.path.join(app_dir, csv_file)
|
||||
with open(example_csv, 'rb') as csv:
|
||||
self.client.post(
|
||||
"/don_confiao/importar_productos",
|
||||
{"csv_file": csv}
|
||||
)
|
||||
73
tienda_ilusion/don_confiao/tests/tests.py
Normal file
73
tienda_ilusion/don_confiao/tests/tests.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from django.test import TestCase
|
||||
from ..models import Sale, SaleLine
|
||||
|
||||
|
||||
class ConfiaoTest(TestCase):
|
||||
|
||||
def test_create_sale(self):
|
||||
sale = Sale()
|
||||
sale.customer = "Alejandro"
|
||||
sale.date = "2024-06-22"
|
||||
sale.phone = '666666666'
|
||||
sale.description = "Description"
|
||||
sale.save()
|
||||
|
||||
self.assertIsInstance(sale, Sale)
|
||||
|
||||
def test_create_sale_line(self):
|
||||
sale = Sale()
|
||||
sale.customer = "Alejandro"
|
||||
sale.date = "2024-06-22"
|
||||
sale.phone = '666666666'
|
||||
sale.description = "Description"
|
||||
|
||||
line = SaleLine()
|
||||
line.sale = sale
|
||||
line.product = 'papaya'
|
||||
line.quantity = 2
|
||||
line.unit_price = 2500
|
||||
line.amount = 5000
|
||||
sale.save()
|
||||
line.save()
|
||||
self.assertEqual(SaleLine.objects.all()[0].quantity, 2)
|
||||
|
||||
def test_create_sale_with_lines(self):
|
||||
sale = Sale()
|
||||
sale.customer = "Alejandro"
|
||||
sale.date = "2024-06-22"
|
||||
sale.phone = '666666666'
|
||||
sale.description = "Description"
|
||||
|
||||
line1 = SaleLine()
|
||||
line1.sale = sale
|
||||
line1.product = 'papaya'
|
||||
line1.quantity = 2
|
||||
line1.unit_price = 2500
|
||||
line1.amount = 5000
|
||||
|
||||
line2 = SaleLine()
|
||||
line2.sale = sale
|
||||
line2.product = 'papaya'
|
||||
line2.quantity = 2
|
||||
line2.unit_price = 2500
|
||||
line2.amount = 5000
|
||||
|
||||
sale.save()
|
||||
line1.save()
|
||||
line2.save()
|
||||
|
||||
self.assertEqual(len(SaleLine.objects.all()), 2)
|
||||
self.assertEqual(
|
||||
Sale.objects.all()[0].saleline_set.all()[0].quantity,
|
||||
2
|
||||
)
|
||||
|
||||
def test_allow_sale_without_description(self):
|
||||
sale = Sale()
|
||||
sale.customer = "Alejandro"
|
||||
sale.date = "2024-06-22"
|
||||
sale.phone = '666666666'
|
||||
sale.description = None
|
||||
sale.save()
|
||||
|
||||
self.assertEqual(len(Sale.objects.all()), 1)
|
||||
Reference in New Issue
Block a user