Skip to content

Commit

Permalink
Optimisation interface admin + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Dec 5, 2024
1 parent 4650c9c commit 5b5da43
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
8 changes: 6 additions & 2 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,16 @@ class BibListes(db.Model):

@hybrid_property
def nb_taxons(self):
return len(self.noms)
return db.session.scalar(
select([db.func.count(cor_nom_liste.c.cd_nom)]).where(
cor_nom_liste.c.id_liste == self.id_liste
)
)

@nb_taxons.expression
def nb_taxons(cls):
return (
db.select([db.func.count(cor_nom_liste.c.id_liste)])
db.select([db.func.count(cor_nom_liste.c.cd_nom)])
.where(cor_nom_liste.c.id_liste == cls.id_liste)
.label("nb_taxons")
)
Expand Down
25 changes: 15 additions & 10 deletions apptax/taxonomie/routesbiblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import os
import logging

from flask import Blueprint, request, current_app
from sqlalchemy import func, or_
from sqlalchemy.orm import joinedload
from flask import Blueprint
from sqlalchemy import select

from pypnusershub import routes as fnauth
from utils_flask_sqla.response import json_resp

from . import filemanager
from . import db
from .models import BibListes, Taxref
from .models import BibListes
from apptax.taxonomie.schemas import BibListesSchema

adresses = Blueprint("bib_listes", __name__)
Expand All @@ -29,8 +26,16 @@ def get_biblistes():
retourne les contenu de bib_listes dans "data"
et le nombre d'enregistrements dans "count"
"""
biblistes_records = db.session.query(
BibListes.id_liste, BibListes.code_liste, BibListes.nom_liste, BibListes.nb_taxons
biblistes_records = db.session.execute(

Check warning on line 29 in apptax/taxonomie/routesbiblistes.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/routesbiblistes.py#L29

Added line #L29 was not covered by tests
select(
BibListes.id_liste,
BibListes.code_liste,
BibListes.nom_liste,
BibListes.desc_liste,
BibListes.nb_taxons,
BibListes.regne,
BibListes.group2_inpn,
)
).all()
biblistes_schema = BibListesSchema()
biblistes_infos = {

Check warning on line 41 in apptax/taxonomie/routesbiblistes.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/routesbiblistes.py#L41

Added line #L41 was not covered by tests
Expand All @@ -44,10 +49,10 @@ def get_biblistes():
@adresses.route("/<regne>", methods=["GET"], defaults={"group2_inpn": None})
@adresses.route("/<regne>/<group2_inpn>", methods=["GET"])
def get_biblistesbyTaxref(regne, group2_inpn):
q = db.session.query(BibListes)
q = select(BibListes)
if regne:
q = q.where(BibListes.regne == regne)
if group2_inpn:
q = q.where(BibListes.group2_inpn == group2_inpn)
results = q.all()
results = db.session.scalars(q).all()
return BibListesSchema().dump(results, many=True)
9 changes: 8 additions & 1 deletion apptax/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ def liste():
"code_liste": "TEST_LIST_Plantae",
"nom_liste": "Liste test Plantae",
"desc_liste": "Liste description",
"regne": "Plantea",
"regne": "Plantae",
},
{
"code_liste": "TEST_LIST_Mousses",
"nom_liste": "Liste test Mousses",
"desc_liste": "Liste description",
"regne": "Plantae",
"group2_inpn": "Mousses",
},
]

Expand Down
22 changes: 10 additions & 12 deletions apptax/tests/test_biblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@

@pytest.mark.usefixtures("client_class", "temporary_transaction")
class TestApiBibListe:
schema_cor_nom_liste = Schema(
{
"items": [{"cd_nom": int, "id_liste": int}],
"total": int,
"limit": int,
"page": int,
}
)

schema_allnamebyListe = Schema(
[
{
"id_liste": int,
"code_liste": str,
"nom_liste": str,
"desc_liste": str,
"desc_liste": Or(str, None),
"regne": Or(str, None),
"group2_inpn": Or(str, None),
"nb_taxons": int,
}
]
)

def test_get_biblistes(self):
def test_get_biblistes(self, listes):
query_string = {"limit": 10}
response = self.client.get(
url_for(
Expand All @@ -45,11 +36,18 @@ def test_get_biblistes(self):
assert self.schema_allnamebyListe.is_valid(data["data"])

def test_get_biblistesbyTaxref(self, listes):

response = self.client.get(
url_for("bib_listes.get_biblistesbyTaxref", regne="Animalia", group2_inpn=None),
)
# Filter test list only
data = [d for d in response.json if d["desc_liste"] == "Liste description"]
self.schema_allnamebyListe.validate(data)
assert len(data) == 1

response = self.client.get(
url_for("bib_listes.get_biblistesbyTaxref", regne="Plantae", group2_inpn="Mousses"),
)
# Filter test list only
data = [d for d in response.json if d["desc_liste"] == "Liste description"]
self.schema_allnamebyListe.validate(data)
assert len(data) == 1

0 comments on commit 5b5da43

Please sign in to comment.