Skip to content

Commit

Permalink
feat: categorie site with marshmallow
Browse files Browse the repository at this point in the history
Test and marshmallow create/refactor to
adapt for bibcategorie site paginate

WIP : Adapt load_only site_type in test to "assert"
same object when initiate BibCategorieSite

[Refs ticket]: #3
  • Loading branch information
andriacap committed Dec 23, 2022
1 parent 5d8a2ff commit 54a5b28
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
32 changes: 30 additions & 2 deletions backend/gn_module_monitoring/monitoring/schemas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json

from marshmallow import Schema, fields
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema

from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups

from pypnnomenclature.schemas import NomenclatureSchema

from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups,TMonitoringSites,BibCategorieSite,cor_site_type_categorie


def paginate_schema(schema):
Expand All @@ -26,3 +28,29 @@ class Meta:
def serialize_geojson(self, obj):
if obj.geom_geojson is not None:
return json.loads(obj.geom_geojson)


class MonitoringSitesSchema(SQLAlchemyAutoSchema):
class Meta:
model = TMonitoringSites
# load_instance = True
exclude = ("geom_geojson","geom")

# geometry = fields.Method("serialize_geojson", dump_only=True)

# def serialize_geojson(self, obj):
# if obj.geom is not None :
# return json.loads(obj.geom)




class BibCategorieSiteSchema(SQLAlchemyAutoSchema):
site_type = fields.Nested(NomenclatureSchema(only=("id_nomenclature","label_fr")), many=True, dump_only=True)
class Meta:
model = BibCategorieSite
include_fk=True
load_instance=True



19 changes: 17 additions & 2 deletions backend/gn_module_monitoring/routes/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from gn_module_monitoring.blueprint import blueprint
from gn_module_monitoring.monitoring.models import BibCategorieSite
from gn_module_monitoring.monitoring.schemas import MonitoringSitesSchema,BibCategorieSiteSchema
from gn_module_monitoring.utils.routes import filter_params, get_limit_offset, paginate


Expand All @@ -16,7 +17,14 @@ def get_categories():
query = filter_params(query=BibCategorieSite.query, params=params)
query = query.order_by(BibCategorieSite.id_categorie)

return paginate(query=query, object_name="categories", limit=limit, page=page, depth=1)
return paginate(
query=query,
schema=BibCategorieSiteSchema,
limit=limit,
page=page,
)

# return paginate(query=query, object_name="categories", limit=limit, page=page, depth=1)


@blueprint.route("/sites/categories/<int:id_categorie>", methods=["GET"])
Expand All @@ -36,7 +44,14 @@ def get_sites():
BibCategorieSite, TBaseSites.id_categorie == BibCategorieSite.id_categorie
)
query = filter_params(query=query, params=params)
return paginate(query=query, object_name="sites", limit=limit, page=page)
return paginate(
query=query,
schema=MonitoringSitesSchema,
limit=limit,
page=page,
)

# return paginate(query=query, object_name="sites", limit=limit, page=page)


@blueprint.route("/sites/module/<string:module_code>", methods=["GET"])
Expand Down
19 changes: 16 additions & 3 deletions backend/gn_module_monitoring/tests/fixtures/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
from geonature.core.gn_monitoring.models import TBaseSites
from geonature.utils.env import db
from pypnnomenclature.models import BibNomenclaturesTypes, TNomenclatures
from pypnnomenclature.schemas import NomenclatureSchema
from shapely.geometry import Point
from sqlalchemy.orm import load_only

from gn_module_monitoring.monitoring.schemas import BibCategorieSiteSchema
from gn_module_monitoring.monitoring.models import BibCategorieSite, TMonitoringSites
from gn_module_monitoring.tests.fixtures.sites_groups import sites_groups


@pytest.fixture()
def site_type():
# query_test = db.session.query(TNomenclatures).options(load_only("label_fr","id_nomenclature"))
# query=query_test.filter(
# BibNomenclaturesTypes.mnemonique == "TYPE_SITE", TNomenclatures.mnemonique == "Grotte"
# ).one()
# query=query_test.filter(
# TNomenclatures.id_type == 116, TNomenclatures.mnemonique == "Grotte"
# ).one()
return TNomenclatures.query.filter(
BibNomenclaturesTypes.mnemonique == "TYPE_SITE", TNomenclatures.mnemonique == "Grotte"
).one()

BibNomenclaturesTypes.mnemonique == "TYPE_SITE", TNomenclatures.mnemonique == "Grotte"
).one()
# @pytest.fixture()
# def site_type_schema_nomenclature(site_type):
# schema = NomenclatureSchema(only=("label_fr","id_nomenclature"))
# return schema.dump(site_type)

@pytest.fixture()
def categories(site_type):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from flask import url_for

from gn_module_monitoring.monitoring.schemas import BibCategorieSiteSchema

@pytest.mark.usefixtures("client_class", "temporary_transaction")
class TestSite:
Expand All @@ -18,13 +18,20 @@ def test_get_categories(self, categories):
r = self.client.get(url_for("monitorings.get_categories"))

assert r.json["count"] >= len(categories)
assert all([cat.as_dict(depth=1) in r.json["categories"] for cat in categories.values()])
assert all(
[
BibCategorieSiteSchema().dump(cat) in r.json["items"]
for cat in categories.values()
]
)

# assert all([cat.as_dict(depth=1) in r.json["items"] for cat in categories.values()])

def test_get_categories_label(self, categories):
label = list(categories.keys())[0]

r = self.client.get(url_for("monitorings.get_categories"), query_string={"label": label})
assert categories[label].as_dict(depth=1) in r.json["categories"]
assert categories[label].as_dict(depth=1) in r.json["items"][0]
# assert categories[label].as_dict(depth=1) in r.json["categories"]

def test_get_sites(self, sites):
r = self.client.get(url_for("monitorings.get_sites"))
Expand Down

0 comments on commit 54a5b28

Please sign in to comment.