fix: actualizando precios al importar productos.

This commit is contained in:
Mono Mono 2024-06-29 17:20:01 -05:00
parent 64a9b75ad7
commit 524626aea3
2 changed files with 35 additions and 7 deletions

View File

@ -39,7 +39,35 @@ class TestProducts(TestCase):
first_products = self._get_products()
self._import_csv('example_products2.csv')
seconds_products = self._get_products()
self.assertCountEqual(first_products, seconds_products)
self.assertEqual(len(first_products), len(seconds_products))
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")
@ -48,7 +76,7 @@ class TestProducts(TestCase):
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, 'example_products.csv')
example_csv = os.path.join(app_dir, csv_file)
with open(example_csv, 'rb') as csv:
self.client.post(
"/don_confiao/importar_productos",

View File

@ -59,13 +59,13 @@ def handle_import_products_file(csv_file):
data = io.StringIO(csv_file.read().decode('utf-8'))
reader = csv.DictReader(data, quotechar='"')
for row in reader:
product, _ = Product.objects.get_or_create(
product, created = Product.objects.update_or_create(
name=row['producto'],
defaults={'price': row['precio']}
defaults={
'price': row['precio'],
'measuring_unit': row['unidad'],
}
)
product.price = row['precio']
product.measuring_unit = row['unidad']
product.save()
categories = [n.strip() for n in row["categorias"].split("&")]
for category in categories: