Skip to content

Commit

Permalink
Add test on populate bib_list (#520)
Browse files Browse the repository at this point in the history
Co-authored-by: TheoLechemia <lechemia.theo@gmail.com>
  • Loading branch information
TheoLechemia and TheoLechemia committed Aug 8, 2024
1 parent b8c8231 commit 5e7c047
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
6 changes: 5 additions & 1 deletion apptax/admin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
11 changes: 11 additions & 0 deletions apptax/tests/assets/cd_nom_list_valid.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cd_nom
212
2852
3429
3429
4582
530157

64197

10977
10 changes: 10 additions & 0 deletions apptax/tests/assets/cd_nom_list_valid_extra_cols.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cd_nom;extra_cols
212;lala
2852;
3429;
4582;lala
530157;
;
64197;
;lala
10977;
7 changes: 7 additions & 0 deletions apptax/tests/assets/cd_nom_list_with_error.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cd_nom;extra_cols
212;lala
2852;
3429;
pas entier;
;lala
530157;
File renamed without changes
58 changes: 56 additions & 2 deletions apptax/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 5e7c047

Please sign in to comment.