diff --git a/terraso_backend/apps/soil_id/models/project_soil_settings.py b/terraso_backend/apps/soil_id/models/project_soil_settings.py index e7aa33103..1a0897dbd 100644 --- a/terraso_backend/apps/soil_id/models/project_soil_settings.py +++ b/terraso_backend/apps/soil_id/models/project_soil_settings.py @@ -87,7 +87,7 @@ def save(self, *args, **kwargs): # delete project intervals... ProjectDepthInterval.objects.filter(project=self).delete() # delete related soil data - DepthDependentSoilData.delete_in_project(self.id) + DepthDependentSoilData.delete_in_project(self.project.id) # create new intervals self.apply_preset() return result diff --git a/terraso_backend/tests/graphql/mutations/test_soil_data.py b/terraso_backend/tests/graphql/mutations/test_soil_data.py index 23830e95b..537e1f0e7 100644 --- a/terraso_backend/tests/graphql/mutations/test_soil_data.py +++ b/terraso_backend/tests/graphql/mutations/test_soil_data.py @@ -669,26 +669,33 @@ def test_update_project_soil_settings(client, user, project_manager, project): assert payload == new_data -def test_update_project_depth_interval_preset_no_change( - client, project, project_manager, site_with_soil_data +@pytest.mark.parametrize("depth_interval_preset", ["LANDPKS", "NRCS"]) +def test_update_project_depth_interval_preset_depth_dependent_data( + depth_interval_preset, client, project, project_manager, site_with_soil_data ): - original_depth_interval_preset = project.soil_settings.depth_interval_preset + original_preset = project.soil_settings.depth_interval_preset input_data = { "projectId": str(project.id), - "depthIntervalPreset": original_depth_interval_preset, + "depthIntervalPreset": depth_interval_preset, } client.force_login(project_manager) response = graphql_query(UPDATE_PROJECT_SETTINGS_QUERY, input_data=input_data, client=client) payload = response.json() assert "errors" not in payload intervals = match_json("*..depthIntervals", payload) - assert intervals[0] == make_intervals(LandPKSIntervalDefaults) - # make sure soil data was not deleted + expected_intervals = make_intervals( + {"LANDPKS": LandPKSIntervalDefaults, "NRCS": NRCSIntervalDefaults}[depth_interval_preset] + ) + assert intervals[0] == expected_intervals + # make sure soil data was handled correctly project.refresh_from_db() - assert project.soil_settings.depth_interval_preset == original_depth_interval_preset + assert project.soil_settings.depth_interval_preset == depth_interval_preset site_with_soil_data.refresh_from_db() - assert len(project.soil_settings.depth_intervals.all()) > 0 - assert len(site_with_soil_data.soil_data.depth_dependent_data.all()) > 0 + assert project.soil_settings.depth_intervals.exists() + if original_preset == depth_interval_preset: + assert site_with_soil_data.soil_data.depth_dependent_data.exists() + else: + assert not site_with_soil_data.soil_data.depth_dependent_data.exists() UPDATE_SOIL_DEPTH_PRESET_GRAPHQL = """