From 4a0ea7318e06ea70a6e6484bd4a89d37312c60bd Mon Sep 17 00:00:00 2001
From: TheoLechemia <lechemia.theo@gmail.com>
Date: Wed, 7 Aug 2024 11:16:06 +0200
Subject: [PATCH] Add test on populate bib_list

---
 apptax/admin/utils.py                         |   6 +-
 apptax/tests/assets/cd_nom_list_valid.csv     |  11 ++++
 .../assets/cd_nom_list_valid_extra_cols.csv   |  10 +++
 .../tests/assets/cd_nom_list_with_error.csv   |   7 +++
 apptax/tests/{ => assets}/coccinelle.jpg      | Bin
 apptax/tests/test_admin.py                    |  58 +++++++++++++++++-
 6 files changed, 89 insertions(+), 3 deletions(-)
 create mode 100644 apptax/tests/assets/cd_nom_list_valid.csv
 create mode 100644 apptax/tests/assets/cd_nom_list_valid_extra_cols.csv
 create mode 100644 apptax/tests/assets/cd_nom_list_with_error.csv
 rename apptax/tests/{ => assets}/coccinelle.jpg (100%)

diff --git a/apptax/admin/utils.py b/apptax/admin/utils.py
index 47c4baedc..2c594434d 100644
--- a/apptax/admin/utils.py
+++ b/apptax/admin/utils.py
@@ -54,6 +54,7 @@ def populate_bib_liste(id_list, delimiter, with_header, file):
     if with_header:
         next(inputcsv, None)
 
+    taxa_set = set()
     for row in inputcsv:
         # Si la ligne est vide
         if not row:
@@ -75,6 +76,9 @@ def populate_bib_liste(id_list, delimiter, with_header, file):
 
         tax = Taxref.query.get(cd_nom)
         if tax:
-            tax.listes.append(bibliste)
+            # add in a set to avoid doublon -> integrity error
+            taxa_set.add(tax)
+    for new_name in taxa_set:
+        bibliste.noms.append(new_name)
 
     db.session.commit()
diff --git a/apptax/tests/assets/cd_nom_list_valid.csv b/apptax/tests/assets/cd_nom_list_valid.csv
new file mode 100644
index 000000000..8de730399
--- /dev/null
+++ b/apptax/tests/assets/cd_nom_list_valid.csv
@@ -0,0 +1,11 @@
+cd_nom
+212
+2852
+3429
+3429
+4582
+530157
+
+64197
+
+10977
diff --git a/apptax/tests/assets/cd_nom_list_valid_extra_cols.csv b/apptax/tests/assets/cd_nom_list_valid_extra_cols.csv
new file mode 100644
index 000000000..18bb984ce
--- /dev/null
+++ b/apptax/tests/assets/cd_nom_list_valid_extra_cols.csv
@@ -0,0 +1,10 @@
+cd_nom;extra_cols
+212;lala
+2852;
+3429;
+4582;lala
+530157;
+;
+64197;
+;lala
+10977;
diff --git a/apptax/tests/assets/cd_nom_list_with_error.csv b/apptax/tests/assets/cd_nom_list_with_error.csv
new file mode 100644
index 000000000..17a3e714b
--- /dev/null
+++ b/apptax/tests/assets/cd_nom_list_with_error.csv
@@ -0,0 +1,7 @@
+cd_nom;extra_cols
+212;lala
+2852;
+3429;
+pas entier;
+;lala
+530157;
diff --git a/apptax/tests/coccinelle.jpg b/apptax/tests/assets/coccinelle.jpg
similarity index 100%
rename from apptax/tests/coccinelle.jpg
rename to apptax/tests/assets/coccinelle.jpg
diff --git a/apptax/tests/test_admin.py b/apptax/tests/test_admin.py
index 0afacd895..17c0c97ac 100644
--- a/apptax/tests/test_admin.py
+++ b/apptax/tests/test_admin.py
@@ -1,14 +1,23 @@
 import json
 import os
+import csv
 
 import pytest
+from pathlib import Path
+
 from flask import url_for, current_app
 
 from apptax.database import db
 from apptax.taxonomie.models import BibListes, BibAttributs, Taxref, BibAttributs
 from pypnusershub.tests.utils import set_logged_user_cookie
 
-from .fixtures import noms_example, users, attribut_example, nom_with_media, liste
+from .fixtures import (
+    noms_example,
+    users,
+    attribut_example,
+    nom_with_media,
+    liste,
+)
 
 form_bibliste = {
     "regne": "Animalia",
@@ -65,7 +74,7 @@ def test_insert_taxref(self, users, attribut_example, liste):
 
         attr_key = f"attr.{attribut_example.id_attribut}"
 
-        with open(os.path.join("apptax/tests", "coccinelle.jpg"), "rb") as f:
+        with open(os.path.join("apptax/tests/assets", "coccinelle.jpg"), "rb") as f:
             form_taxref = {
                 attr_key: "val1",
                 "listes": liste.id_liste,
@@ -202,3 +211,48 @@ def test_filter_animalia(self):
         )
         for tax in results:
             assert tax.regne == "Animalia"
+
+    def test_insert_list(self, users, liste):
+        set_logged_user_cookie(self.client, users["admin"])
+        with open(Path("apptax/tests/assets/cd_nom_list_valid.csv"), "rb") as f:
+            req = self.client.post(
+                f"biblistes/import_cd_nom/?id={liste.id_liste}",
+                data={
+                    "delimiter": ";",
+                    "with_header": True,
+                    "upload": (f, "cd_nom_list_valid.csv"),
+                },
+                content_type="multipart/form-data",
+            )
+            assert req.status_code == 302
+
+        updated_liste = db.session.get(BibListes, liste.id_liste)
+        # must reopen th file to read it ...
+        with open(Path("apptax/tests/assets/cd_nom_list_valid.csv"), "r") as f:
+            reader = csv.DictReader(f, delimiter=";")
+            # test the csv cd_nom imported = cd_nom in liste
+            {nom.cd_nom for nom in updated_liste.noms} == {row["cd_nom"] for row in reader}
+
+        with open(Path("apptax/tests/assets/cd_nom_list_valid_extra_cols.csv"), "rb") as f:
+            req = self.client.post(
+                f"biblistes/import_cd_nom/?id={liste.id_liste}",
+                data={
+                    "delimiter": ";",
+                    "with_header": True,
+                    "upload": (f, "cd_nom_list_valid.csv"),
+                },
+                content_type="multipart/form-data",
+            )
+            assert req.status_code == 302
+
+        with open(Path("apptax/tests/assets/cd_nom_list_with_error.csv"), "rb") as f:
+            req = self.client.post(
+                f"biblistes/import_cd_nom/?id={liste.id_liste}",
+                data={
+                    "delimiter": ";",
+                    "with_header": True,
+                    "upload": (f, "cd_nom_list_valid.csv"),
+                },
+                content_type="multipart/form-data",
+            )
+            assert req.status_code == 200