forked from PnX-SI/gn_module_monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/create marshmallow schemas and remove id_module (#21)
* feat(api): remove id_module from sites_complements Co-authored-by: andriacap <[email protected]> * feat(api): create schema for sites_groups * test: add sites_group to site fixture * test: wip add test for sites_groups schemas * chore(api): remove depth parameter from paginate * test: updated to work with sites_group schema * feat: categorie site with marshmallow 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 * feat(api): route /sites/categories/id with schema Changing the route to return a dump Marshmallow schema BibCategorieSitesSchema Reviewed-by: andriac [Refs ticket]: #3 * test(api): routes get categoires label Change the "as_dict" by schema.dump in order to use the Marshmallow schema created Reviewed-by: andriac [Refs ticket]: #3 * feat(api): Sites: cols to geoserializable + schema * style(api): applied black * test(api): add test for Site Schema * style(api): applied black to test_site * refactor(api): instantiate schema once Instead of for each all() iteration * chore(api): remove useless comments * chore(api): remove useless comments and imports Co-authored-by: andriacap <[email protected]> Co-authored-by: Andria Capai <[email protected]>
- Loading branch information
1 parent
4b40ddb
commit 90f0ae5
Showing
9 changed files
with
184 additions
and
33 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...d/gn_module_monitoring/migrations/6673266fb79c_remove_id_module_from_sites_complements.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""remove_id_module_from_sites_complements | ||
Revision ID: 6673266fb79c | ||
Revises: | ||
Create Date: 2022-12-13 16:00:00.512562 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
from gn_module_monitoring import MODULE_CODE | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6673266fb79c" | ||
down_revision = "e64bafb13ce8" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
monitorings_schema = "gn_monitoring" | ||
|
||
|
||
def upgrade(): | ||
op.drop_column("t_site_complements", "id_module", schema=monitorings_schema) | ||
|
||
|
||
def downgrade(): | ||
op.add_column( | ||
"t_site_complements", | ||
sa.Column( | ||
"id_module", | ||
sa.Integer(), | ||
sa.ForeignKey( | ||
f"gn_commons.t_modules.id_module", | ||
name="fk_t_site_complements_id_module", | ||
ondelete="CASCADE", | ||
onupdate="CASCADE", | ||
), | ||
nullable=True, | ||
), | ||
schema=monitorings_schema, | ||
) | ||
# Cannot use orm here because need the model to be "downgraded" as well | ||
# Need to set nullable True above for existing rows | ||
# FIXME: find a better way because need to assign a module... | ||
statement = sa.text( | ||
f""" | ||
update {monitorings_schema}.t_site_complements | ||
set id_module = (select id_module | ||
from gn_commons.t_modules tm | ||
where module_code = :module_code); | ||
""" | ||
).bindparams(module_code=MODULE_CODE) | ||
op.execute(statement) | ||
op.alter_column("t_site_complements", "id_module", nullable=False, schema=monitorings_schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
|
||
import geojson | ||
from marshmallow import Schema, fields | ||
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema | ||
from pypnnomenclature.schemas import NomenclatureSchema | ||
|
||
from gn_module_monitoring.monitoring.models import ( | ||
BibCategorieSite, | ||
TMonitoringSites, | ||
TMonitoringSitesGroups, | ||
) | ||
|
||
|
||
def paginate_schema(schema): | ||
class PaginationSchema(Schema): | ||
count = fields.Integer() | ||
limit = fields.Integer() | ||
offset = fields.Integer() | ||
items = fields.Nested(schema, many=True, dump_only=True) | ||
|
||
return PaginationSchema | ||
|
||
|
||
class MonitoringSitesGroupsSchema(SQLAlchemyAutoSchema): | ||
class Meta: | ||
model = TMonitoringSitesGroups | ||
exclude = ("geom_geojson",) | ||
|
||
geometry = fields.Method("serialize_geojson", dump_only=True) | ||
|
||
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 | ||
exclude = ("geom_geojson", "geom") | ||
|
||
geometry = fields.Method("serialize_geojson", dump_only=True) | ||
|
||
def serialize_geojson(self, obj): | ||
if obj.geom is not None: | ||
return geojson.dumps(obj.as_geofeature().get("geometry")) | ||
|
||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 22 additions & 4 deletions
26
backend/gn_module_monitoring/tests/test_routes/test_sites_groups.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,41 @@ | ||
import pytest | ||
from flask import url_for | ||
|
||
from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups | ||
from gn_module_monitoring.monitoring.schemas import MonitoringSitesGroupsSchema | ||
|
||
|
||
@pytest.mark.usefixtures("client_class", "temporary_transaction") | ||
class TestSitesGroups: | ||
def test_get_sites_groups(self, sites_groups): | ||
r = self.client.get(url_for("monitorings.get_sites_groups")) | ||
|
||
assert r.json["count"] >= len(sites_groups) | ||
assert all([group.as_dict() in r.json["sites_groups"] for group in sites_groups.values()]) | ||
assert all( | ||
[ | ||
MonitoringSitesGroupsSchema().dump(group) in r.json["items"] | ||
for group in sites_groups.values() | ||
] | ||
) | ||
|
||
def test_get_sites_groups_filter_name(self, sites_groups): | ||
name, name_not_present = list(sites_groups.keys()) | ||
schema = MonitoringSitesGroupsSchema() | ||
|
||
r = self.client.get( | ||
url_for("monitorings.get_sites_groups"), query_string={"sites_group_name": name} | ||
) | ||
|
||
assert r.json["count"] >= 1 | ||
json_sites_groups = r.json["sites_groups"] | ||
assert sites_groups[name].as_dict() in json_sites_groups | ||
assert sites_groups[name_not_present].as_dict() not in json_sites_groups | ||
json_sites_groups = r.json["items"] | ||
assert schema.dump(sites_groups[name]) in json_sites_groups | ||
assert schema.dump(sites_groups[name_not_present]) not in json_sites_groups | ||
|
||
def test_serialize_sites_groups(self, sites_groups, sites): | ||
groups = TMonitoringSitesGroups.query.filter( | ||
TMonitoringSitesGroups.id_sites_group.in_( | ||
[s.id_sites_group for s in sites_groups.values()] | ||
) | ||
).all() | ||
schema = MonitoringSitesGroupsSchema() | ||
assert [schema.dump(site) for site in groups] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters