fix: actualizando precios al importar productos.
This commit is contained in:
parent
64a9b75ad7
commit
524626aea3
@ -39,7 +39,35 @@ class TestProducts(TestCase):
|
|||||||
first_products = self._get_products()
|
first_products = self._get_products()
|
||||||
self._import_csv('example_products2.csv')
|
self._import_csv('example_products2.csv')
|
||||||
seconds_products = self._get_products()
|
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):
|
def _get_products(self):
|
||||||
products_response = self.client.get("/don_confiao/productos")
|
products_response = self.client.get("/don_confiao/productos")
|
||||||
@ -48,7 +76,7 @@ class TestProducts(TestCase):
|
|||||||
def _import_csv(self, csv_file='example_products.csv'):
|
def _import_csv(self, csv_file='example_products.csv'):
|
||||||
app_name = "don_confiao"
|
app_name = "don_confiao"
|
||||||
app_dir = os.path.join(settings.BASE_DIR, app_name)
|
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:
|
with open(example_csv, 'rb') as csv:
|
||||||
self.client.post(
|
self.client.post(
|
||||||
"/don_confiao/importar_productos",
|
"/don_confiao/importar_productos",
|
||||||
|
@ -59,13 +59,13 @@ def handle_import_products_file(csv_file):
|
|||||||
data = io.StringIO(csv_file.read().decode('utf-8'))
|
data = io.StringIO(csv_file.read().decode('utf-8'))
|
||||||
reader = csv.DictReader(data, quotechar='"')
|
reader = csv.DictReader(data, quotechar='"')
|
||||||
for row in reader:
|
for row in reader:
|
||||||
product, _ = Product.objects.get_or_create(
|
product, created = Product.objects.update_or_create(
|
||||||
name=row['producto'],
|
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("&")]
|
categories = [n.strip() for n in row["categorias"].split("&")]
|
||||||
for category in categories:
|
for category in categories:
|
||||||
|
Loading…
Reference in New Issue
Block a user