From 287ef726db822c69f36a7f89217c9175eaee5cc2 Mon Sep 17 00:00:00 2001 From: shrouxm Date: Wed, 15 Jan 2025 15:08:07 -0800 Subject: [PATCH 1/2] fix: lcc resolver --- .../apps/soil_id/graphql/soil_id/resolvers.py | 18 ++++++++++++++---- .../tests/soil_id/test_graphql_resolvers.py | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py b/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py index 8dccb4d0f..64ac96eaf 100644 --- a/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py +++ b/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py @@ -98,6 +98,19 @@ def resolve_ecological_site(ecological_site: dict): ) +def resolve_land_capability_class(site_data: dict): + def resolve_lcc_value(value): + # note that the soil ID algorithm also sometimes returns the _strings_ "None" or "nan" + if value is None or value == "None" or math.isnan(value) or value == "nan": + return "" + return value + + return LandCapabilityClass( + capability_class=resolve_lcc_value(site_data["nirrcapcl"]), + sub_class=resolve_lcc_value(site_data["nirrcapscl"]), + ) + + def resolve_soil_info(soil_match: dict): soil_id = soil_match["id"] site_data = soil_match["site"]["siteData"] @@ -109,10 +122,7 @@ def resolve_soil_info(soil_match: dict): description=soil_match["site"]["siteDescription"], full_description_url=site_data["sdeURL"], ), - land_capability_class=LandCapabilityClass( - capability_class=site_data["nirrcapcl"], - sub_class=site_data["nirrcapscl"], - ), + land_capability_class=resolve_land_capability_class(site_data), ecological_site=resolve_ecological_site(soil_match["esd"]["ESD"]), soil_data=resolve_soil_data(soil_match), ) diff --git a/terraso_backend/tests/soil_id/test_graphql_resolvers.py b/terraso_backend/tests/soil_id/test_graphql_resolvers.py index 61124fc06..f1a3dbb26 100644 --- a/terraso_backend/tests/soil_id/test_graphql_resolvers.py +++ b/terraso_backend/tests/soil_id/test_graphql_resolvers.py @@ -2,6 +2,7 @@ resolve_data_based_soil_match, resolve_data_based_soil_matches, resolve_ecological_site, + resolve_land_capability_class, resolve_location_based_soil_match, resolve_location_based_soil_matches, resolve_rock_fragment_volume, @@ -116,8 +117,8 @@ "distance": 204.0, "minCompDistance": 90.0, "slope": 0.5, - "nirrcapcl": "2", - "nirrcapscl": "s", + "nirrcapcl": "None", + "nirrcapscl": "nan", "nirrcapunit": "None", "irrcapcl": "2", "irrcapscl": "s", @@ -257,6 +258,20 @@ def test_resolve_ecological_site(): assert result.url == "" +def test_resolve_land_capability_class_success(): + result = resolve_land_capability_class({"nirrcapcl": "6", "nirrcapscl": "s"}) + + assert result.capability_class == "6" + assert result.sub_class == "s" + + +def test_resolve_land_capability_class_not_available(): + result = resolve_land_capability_class({"nirrcapcl": "None", "nirrcapscl": "nan"}) + + assert result.capability_class == "None" + assert result.sub_class == "nan" + + def test_resolve_soil_info(): result = resolve_soil_info(sample_soil_list_json[0]) From 1f2be25dd21e683b7e5f0b22c6d68db0956a5432 Mon Sep 17 00:00:00 2001 From: shrouxm Date: Thu, 16 Jan 2025 11:08:38 -0800 Subject: [PATCH 2/2] fix: tests --- terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py | 2 +- terraso_backend/tests/soil_id/test_graphql_resolvers.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py b/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py index 64ac96eaf..469069169 100644 --- a/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py +++ b/terraso_backend/apps/soil_id/graphql/soil_id/resolvers.py @@ -101,7 +101,7 @@ def resolve_ecological_site(ecological_site: dict): def resolve_land_capability_class(site_data: dict): def resolve_lcc_value(value): # note that the soil ID algorithm also sometimes returns the _strings_ "None" or "nan" - if value is None or value == "None" or math.isnan(value) or value == "nan": + if not isinstance(value, str) or value == "None" or value == "nan": return "" return value diff --git a/terraso_backend/tests/soil_id/test_graphql_resolvers.py b/terraso_backend/tests/soil_id/test_graphql_resolvers.py index f1a3dbb26..afe7743d3 100644 --- a/terraso_backend/tests/soil_id/test_graphql_resolvers.py +++ b/terraso_backend/tests/soil_id/test_graphql_resolvers.py @@ -268,8 +268,8 @@ def test_resolve_land_capability_class_success(): def test_resolve_land_capability_class_not_available(): result = resolve_land_capability_class({"nirrcapcl": "None", "nirrcapscl": "nan"}) - assert result.capability_class == "None" - assert result.sub_class == "nan" + assert result.capability_class == "" + assert result.sub_class == "" def test_resolve_soil_info():