Skip to content

Commit

Permalink
feat(taxref): taxons comparison through path
Browse files Browse the repository at this point in the history
  • Loading branch information
bouttier committed Oct 10, 2024
1 parent baa9172 commit e88bd31
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ def where_params(cls, filters=None, *, query):
query = query.filter(col.ilike(value + "%"))
return query

def __le__(self, other):
return self.tree <= other.tree


@serializable
class BibListes(db.Model):
Expand Down Expand Up @@ -546,6 +549,11 @@ class TaxrefTree(db.Model):
taxref = db.relationship(Taxref, backref=backref("tree", uselist=False))
path = db.Column(db.String, nullable=False)

def __le__(self, other):
# self <= other means taxon other is the same or a parent of self
p1, p2 = self.path.split("."), other.path.split(".")
return len(p1) >= len(p2) and p1[: len(p2)] == p2


# Taxref deffered properties

Expand Down
46 changes: 46 additions & 0 deletions apptax/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest
import sqlalchemy as sa

from .fixtures import *
from apptax.taxonomie.models import Taxref, TaxrefTree


@pytest.mark.usefixtures("client_class", "temporary_transaction")
class TestModels:
def test_taxref_tree_comparison(self):
animalia = db.session.execute(
sa.select(TaxrefTree).where(TaxrefTree.cd_nom == 183716)
).scalar_one()
capra_ibex = db.session.execute(
sa.select(TaxrefTree).where(TaxrefTree.cd_nom == 61098)
).scalar_one()
cinnamon = db.session.execute(
sa.select(TaxrefTree).where(TaxrefTree.cd_nom == 706584)
).scalar_one()

assert animalia <= animalia
assert capra_ibex <= animalia
assert not animalia <= capra_ibex
assert not cinnamon <= animalia
assert not animalia <= cinnamon
assert not cinnamon <= capra_ibex
assert not capra_ibex <= cinnamon

def test_taxref_comparison(self):
animalia = db.session.execute(
sa.select(Taxref).where(Taxref.cd_nom == 183716)
).scalar_one()
capra_ibex = db.session.execute(
sa.select(Taxref).where(Taxref.cd_nom == 61098)
).scalar_one()
cinnamon = db.session.execute(
sa.select(Taxref).where(Taxref.cd_nom == 706584)
).scalar_one()

assert animalia <= animalia
assert capra_ibex <= animalia
assert not animalia <= capra_ibex
assert not cinnamon <= animalia
assert not animalia <= cinnamon
assert not cinnamon <= capra_ibex
assert not capra_ibex <= cinnamon

0 comments on commit e88bd31

Please sign in to comment.