Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test on populate bib_list #520

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading