diff --git a/terraso_backend/tests/graphql/mutations/test_soil_data.py b/terraso_backend/tests/graphql/mutations/test_soil_data.py index 65a418754..fb588c3d5 100644 --- a/terraso_backend/tests/graphql/mutations/test_soil_data.py +++ b/terraso_backend/tests/graphql/mutations/test_soil_data.py @@ -30,6 +30,7 @@ SoilData, SoilDataDepthInterval, ) +from apps.soil_id.models.soil_data_history import SoilDataHistory pytestmark = pytest.mark.django_db @@ -780,3 +781,100 @@ def test_apply_to_all(client, project_site, project_manager): db_intervals = SoilDataDepthInterval.objects.filter(soil_data=project_site.soil_data).all() for interval in db_intervals: assert interval.soil_texture_enabled + + +PUSH_SOIL_DATA_QUERY = """ + mutation PushSoilDataMutation($input: SoilDataPushInput!) { + pushSoilData(input: $input) { + results { + siteId + result { + __typename + ... on SoilDataPushEntryFailure { + reason + } + ... on SoilDataPushEntrySuccess { + site { + soilData { + slopeAspect + depthDependentData { + depthInterval { + start + end + } + clayPercent + } + } + } + } + } + } + errors + } + } +""" + + +def test_push_soil_data(client, user): + sites = mixer.cycle(2).blend(Site, owner=user) + + client.force_login(user) + response = graphql_query( + PUSH_SOIL_DATA_QUERY, + input_data={ + "soilDataEntries": [ + { + "siteId": str(sites[0].id), + "soilData": { + "slopeAspect": 10, + "depthDependentData": [], + "depthIntervals": [], + "deletedDepthIntervals": [], + }, + }, + { + "siteId": str(sites[1].id), + "soilData": { + "depthDependentData": [ + {"depthInterval": {"start": 0, "end": 10}, "clayPercent": 5} + ], + "depthIntervals": [], + "deletedDepthIntervals": [], + }, + }, + { + "siteId": str("c9df7deb-6b9d-4c55-8ba6-641acc47dbb2"), + "soilData": { + "depthDependentData": [], + "depthIntervals": [], + "deletedDepthIntervals": [], + }, + }, + ] + }, + client=client, + ) + + print(response.json()) + result = response.json()["data"]["pushSoilData"] + assert result["errors"] is None + assert result["results"][2]["result"]["reason"] == "DOES_NOT_EXIST" + + assert response.json() + + sites[0].refresh_from_db() + sites[1].refresh_from_db() + + assert sites[0].soil_data.slope_aspect == 10 + assert ( + sites[1] + .soil_data.depth_dependent_data.get(depth_interval_start=0, depth_interval_end=10) + .clay_percent + == 5 + ) + + history_1 = SoilDataHistory.objects.get(site=sites[0]) + assert history_1.soil_data_changes["soil_data"]["slope_aspect"] == 10 + + history_2 = SoilDataHistory.objects.get(site=sites[1]) + assert history_2.soil_data_changes["soil_data"]["depth_dependent_data"][0]["clay_percent"] == 5