From a7dd04cec2f20edaa3cea3669ab1d2ce11f38ea5 Mon Sep 17 00:00:00 2001 From: Helen Root Date: Tue, 7 May 2024 11:17:23 +0200 Subject: [PATCH] Define siret up top so that the BadRequest error can be handled --- api/tests/files/corrupt_purchase_import.csv | 2 ++ api/tests/test_import_purchases.py | 13 +++++++++++++ api/views/purchaseimport.py | 1 + 3 files changed, 16 insertions(+) create mode 100644 api/tests/files/corrupt_purchase_import.csv diff --git a/api/tests/files/corrupt_purchase_import.csv b/api/tests/files/corrupt_purchase_import.csv new file mode 100644 index 000000000..9a566a087 --- /dev/null +++ b/api/tests/files/corrupt_purchase_import.csv @@ -0,0 +1,2 @@ +"cantine SIRET,""description"",""fournisseur"",""date"",""prix HT"",""famille"",""caractéristiques"",""definition de local""" +"82399356058716,""weird formatting"",""Le bon traiteur"",""2022-05-02"",""90.11"",""PRODUITS_LAITIERS"",""LOCAL"",""REGION""" diff --git a/api/tests/test_import_purchases.py b/api/tests/test_import_purchases.py index 20e9a63e3..081a9ae3a 100644 --- a/api/tests/test_import_purchases.py +++ b/api/tests/test_import_purchases.py @@ -165,6 +165,19 @@ def test_import_bad_purchases(self): "Format fichier : 7-8 colonnes attendues, 6 trouvées.", ) + @authenticate + def test_import_corrupt_purchases_file(self): + """ + A reasonable error should be thrown + """ + CanteenFactory.create(siret="82399356058716", managers=[authenticate.user]) + with open("./api/tests/files/corrupt_purchase_import.csv") as purchase_file: + response = self.client.post(reverse("import_purchases"), {"file": purchase_file}) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(Purchase.objects.count(), 0) + errors = response.json()["errors"] + self.assertEqual(errors.pop(0)["message"], "Format fichier : 7-8 colonnes attendues, 1 trouvées.") + @authenticate def test_warn_duplicate_file(self): """ diff --git a/api/views/purchaseimport.py b/api/views/purchaseimport.py index a9f5bfb2b..5cef4c1c7 100644 --- a/api/views/purchaseimport.py +++ b/api/views/purchaseimport.py @@ -136,6 +136,7 @@ def _process_chunk(self, chunk): csvreader = csv.reader(io.StringIO("".join(chunk)), self.dialect) for row_number, row in enumerate(csvreader, start=1): + siret = None # If header, pass if row_number == 1 and row[0].lower().__contains__("siret"): continue