Skip to content

Commit

Permalink
test: routes for type site and post site (#57)
Browse files Browse the repository at this point in the history
* test: routes for type site and post site

Reviewed-by: andriac

* test: apply black and change query string

- Keep site beauce it's specific one cannot re use existing
- Fix mnemonique "None" with existing label_default

Reviewed-by:andriac
  • Loading branch information
andriacap committed Sep 7, 2023
1 parent 1f00887 commit f8f7a9a
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"cruved": {
"C": 1,
"U": 1,
"D": 3
},
"sorts": [
{
"prop": "base_site_name",
"dir": "asc"
}
],
"filters": {},
"label": "Gite",
"label_list": "Gites",
"genre": "M",
"geometry_type": "Point",
"display_properties": [
"base_site_name",
"base_site_code",
"roost_type",
"nb_visits",
"threat",
"recommandation",
"opening",
"owner_name",
"owner_adress",
"owner_tel",
"owner_mail",
"medias"
],
"display_list": [
"base_site_name",
"roost_type",
"nb_visits",
"owner_name"
],
"specific": {
"roost_type": {
"type_widget": "select",
"required": true,
"attribut_label": "Type de gite",
"values": [
"barrage",
"bâtiment",
"cave",
"Ebouli",
"église",
"façade",
"four",
"garage",
"Gîte artificiel",
"grange",
"grenier",
"grotte",
"maison",
"mine",
"mur",
"panneau",
"plancher",
"pont",
"ruine",
"toit",
"toit et volet",
"transformateur",
"tunnel",
"volet",
"Autre"
]
},
"place_name": {
"type_widget": "text",
"attribut_label": "Lieux-dit"
},
"owner_name": {
"type_widget": "text",
"attribut_label": "Nom propriétaire"
},
"owner_adress": {
"type_widget": "text",
"attribut_label": "Adresse propriétaire"
},
"owner_tel": {
"type_widget": "text",
"attribut_label": "Tel propriétaire"
},
"owner_mail": {
"type_widget": "text",
"attribut_label": "Email propriétaire"
},
"opening": {
"type_widget": "textarea",
"attribut_label": "Ouverture"
},
"threat": {
"type_widget": "textarea",
"attribut_label": "Menace(s)",
"rows": 3
},
"recommandation": {
"type_widget": "textarea",
"attribut_label": "Mesure(s) préconisé(s)",
"rows": 3
}
}
}
47 changes: 47 additions & 0 deletions backend/gn_module_monitoring/tests/fixtures/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from shapely.geometry import Point

from gn_module_monitoring.monitoring.models import TMonitoringSites
from gn_module_monitoring.monitoring.schemas import BibTypeSiteSchema, MonitoringSitesSchema


@pytest.fixture()
Expand Down Expand Up @@ -42,3 +43,49 @@ def sites(users, types_site, site_group_with_sites):
with db.session.begin_nested():
db.session.add_all(sites.values())
return sites


@pytest.fixture()
def site_to_post_with_types(users, types_site, site_group_without_sites):
user = users["user"]
geom_4326 = from_shape(Point(43, 24), srid=4326)
list_nomenclature_id = []
specific_dic = {"owner_name": "Propriétaire", "threat": "Menaces", "owner_tel": "0609090909"}
schema_type_site = BibTypeSiteSchema()
mock_db_type_site = [schema_type_site.dump(type) for type in types_site.values()]

for type in mock_db_type_site:
list_nomenclature_id.append(type["id_nomenclature_type_site"])

site_to_post_with_types = TMonitoringSites(
id_inventor=user.id_role,
id_digitiser=user.id_role,
base_site_name=f"New Site",
base_site_description=f"New Description",
base_site_code=f"New Code",
geom=geom_4326,
id_nomenclature_type_site=list_nomenclature_id[0],
types_site=list_nomenclature_id,
id_sites_group=site_group_without_sites.id_sites_group,
)

post_data = dict()
post_data["dataComplement"] = {}
for type_site_dic in mock_db_type_site:
copy_dic = type_site_dic.copy()
copy_dic.pop("label")
post_data["dataComplement"][type_site_dic["label"]] = copy_dic

post_data["dataComplement"]["types_site"] = list_nomenclature_id
post_data["properties"] = MonitoringSitesSchema().dump(site_to_post_with_types)
post_data["properties"]["types_site"] = list_nomenclature_id

for type_site in mock_db_type_site:
specific_config = type_site["config"]["specific"]
for key_specific in specific_config:
if key_specific in specific_dic.keys():
post_data["properties"][key_specific] = specific_dic[key_specific]
else:
post_data["properties"][key_specific] = None

return post_data
17 changes: 15 additions & 2 deletions backend/gn_module_monitoring/tests/fixtures/type_site.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import json
import os

import pytest
from geonature.utils.env import db
from pypnnomenclature.models import BibNomenclaturesTypes, TNomenclatures

from gn_module_monitoring.monitoring.models import BibTypeSite


def get_test_data(filename):
folder_path = os.path.abspath(os.path.dirname(__file__))
folder = os.path.join(folder_path, "TestData")
jsonfile = os.path.join(folder, filename)
with open(jsonfile) as file:
data = json.load(file)
return data


@pytest.fixture
def nomenclature_types_site():
mnemoniques = ("Test_Grotte", "Test_Mine")
Expand All @@ -29,9 +41,10 @@ def nomenclature_types_site():

@pytest.fixture
def types_site(nomenclature_types_site):
config_type_site = get_test_data("config_type_site.json")
types_site = {
nomenc_type_site.mnemonique: BibTypeSite(
id_nomenclature_type_site=nomenc_type_site.id_nomenclature, config={}
nomenc_type_site.label_default: BibTypeSite(
id_nomenclature_type_site=nomenc_type_site.id_nomenclature, config=config_type_site
)
for nomenc_type_site in nomenclature_types_site
}
Expand Down
40 changes: 40 additions & 0 deletions backend/gn_module_monitoring/tests/test_routes/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask import url_for

from gn_module_monitoring.monitoring.schemas import BibTypeSiteSchema, MonitoringSitesSchema
from gn_module_monitoring.monitoring.models import TMonitoringSites


@pytest.mark.usefixtures("client_class", "temporary_transaction")
Expand Down Expand Up @@ -135,3 +136,42 @@ def test_get_module_sites(self):
r = self.client.get(url_for("monitorings.get_module_sites", module_code=module_code))

assert r.json["module_code"] == module_code

def test_get_types_site_by_label(self, types_site):
schema = BibTypeSiteSchema()
mock_db_type_site = [schema.dump(type) for type in types_site.values()]
string_contains = "e"
string_missing = "a"

query_string = {
"limit": 100,
"page": 1,
"sort_label": "label_fr",
"sort_dir": "asc",
"label_fr": string_contains
}
r = self.client.get(
url_for("monitorings.get_types_site_by_label"), query_string=query_string
)
assert all([string_contains in item["label"] for item in r.json["items"]])
assert all([type in r.json["items"] for type in mock_db_type_site])

query_string["label_fr"] = string_missing
r = self.client.get(
url_for("monitorings.get_types_site_by_label"), query_string=query_string
)
assert all([type not in r.json["items"] for type in mock_db_type_site])

def test_post_sites(self, site_to_post_with_types, types_site, site_group_without_sites):

response = self.client.post(
url_for("monitorings.post_sites"), data=site_to_post_with_types
)
assert response.status_code == 201

obj_created = response.json
res = TMonitoringSites.find_by_id(obj_created["id"])
assert (
res.as_dict()["base_site_name"]
== site_to_post_with_types["properties"]["base_site_name"]
)

0 comments on commit f8f7a9a

Please sign in to comment.