Skip to content

Commit

Permalink
🧹: last couple PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm committed Oct 31, 2024
1 parent 8360648 commit 9b91aaa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class SoilDataPushInputEntry(graphene.InputObjectType):
# an individual site's updates are invalid, we reject all of that site's updates
# NOTE: changing a depth interval preset causes all depth intervals for that site to be
# deleted. we haven't yet thought through the implications of when/whether to apply
# that change in the context of this mutation
# that change in the context of this mutation. this work is tracked here:
# https://github.com/techmatters/terraso-backend/issues/1527
class SoilDataPush(BaseWriteMutation):
results = graphene.Field(graphene.List(graphene.NonNull(SoilDataPushEntry)), required=True)

Expand Down
146 changes: 73 additions & 73 deletions terraso_backend/tests/graphql/mutations/test_soil_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,79 @@ def test_apply_to_all(client, project_site, project_manager):
"""


def test_push_soil_data_success(client, user):
site = mixer.blend(Site, owner=user)

site.soil_data = SoilData()
site.soil_data.save()
site.soil_data.depth_intervals.get_or_create(depth_interval_start=10, depth_interval_end=20)

soil_data_changes = {
"slopeAspect": 10,
"depthDependentData": [{"depthInterval": {"start": 0, "end": 10}, "clayPercent": 10}],
"depthIntervals": [
{
"depthInterval": {"start": 0, "end": 10},
"soilTextureEnabled": True,
}
],
"deletedDepthIntervals": [
{
"start": 10,
"end": 20,
}
],
}

client.force_login(user)
response = graphql_query(
PUSH_SOIL_DATA_QUERY,
input_data={
"soilDataEntries": [
{"siteId": str(site.id), "soilData": soil_data_changes},
]
},
client=client,
)

assert response.json()
result = response.json()["data"]["pushSoilData"]
assert result["errors"] is None
assert result["results"][0]["result"]["soilData"]["slopeAspect"] == 10

site.refresh_from_db()

assert site.soil_data.slope_aspect == 10
assert (
site.soil_data.depth_dependent_data.get(
depth_interval_start=0, depth_interval_end=10
).clay_percent
== 10
)
assert (
site.soil_data.depth_intervals.get(
depth_interval_start=0, depth_interval_end=10
).soil_texture_enabled
is True
)
assert not site.soil_data.depth_intervals.filter(
depth_interval_start=10, depth_interval_end=20
).exists()

history = SoilDataHistory.objects.get(site=site)
assert history.update_failure_reason is None
assert history.update_succeeded
assert history.soil_data_changes["slope_aspect"] == 10
assert history.soil_data_changes["depth_dependent_data"][0]["depth_interval"]["start"] == 0
assert history.soil_data_changes["depth_dependent_data"][0]["depth_interval"]["end"] == 10
assert history.soil_data_changes["depth_dependent_data"][0]["clay_percent"] == 10
assert history.soil_data_changes["depth_intervals"][0]["depth_interval"]["start"] == 0
assert history.soil_data_changes["depth_intervals"][0]["depth_interval"]["end"] == 10
assert history.soil_data_changes["depth_intervals"][0]["soil_texture_enabled"] is True
assert history.soil_data_changes["deleted_depth_intervals"][0]["start"] == 10
assert history.soil_data_changes["deleted_depth_intervals"][0]["end"] == 20


def test_push_soil_data_can_process_mixed_results(client, user):
non_user = mixer.blend(User)
user_sites = mixer.cycle(2).blend(Site, owner=user)
Expand Down Expand Up @@ -958,76 +1031,3 @@ def test_push_soil_data_can_process_mixed_results(client, user):
assert history_3.update_failure_reason == "DOES_NOT_EXIST"
assert not history_3.update_succeeded
assert history_3.soil_data_changes["slope_aspect"] == 15


def test_push_soil_data_success(client, user):
site = mixer.blend(Site, owner=user)

site.soil_data = SoilData()
site.soil_data.save()
site.soil_data.depth_intervals.get_or_create(depth_interval_start=10, depth_interval_end=20)

soil_data_changes = {
"slopeAspect": 10,
"depthDependentData": [{"depthInterval": {"start": 0, "end": 10}, "clayPercent": 10}],
"depthIntervals": [
{
"depthInterval": {"start": 0, "end": 10},
"soilTextureEnabled": True,
}
],
"deletedDepthIntervals": [
{
"start": 10,
"end": 20,
}
],
}

client.force_login(user)
response = graphql_query(
PUSH_SOIL_DATA_QUERY,
input_data={
"soilDataEntries": [
{"siteId": str(site.id), "soilData": soil_data_changes},
]
},
client=client,
)

assert response.json()
result = response.json()["data"]["pushSoilData"]
assert result["errors"] is None
assert result["results"][0]["result"]["soilData"]["slopeAspect"] == 10

site.refresh_from_db()

assert site.soil_data.slope_aspect == 10
assert (
site.soil_data.depth_dependent_data.get(
depth_interval_start=0, depth_interval_end=10
).clay_percent
== 10
)
assert (
site.soil_data.depth_intervals.get(
depth_interval_start=0, depth_interval_end=10
).soil_texture_enabled
is True
)
assert not site.soil_data.depth_intervals.filter(
depth_interval_start=10, depth_interval_end=20
).exists()

history = SoilDataHistory.objects.get(site=site)
assert history.update_failure_reason is None
assert history.update_succeeded
assert history.soil_data_changes["slope_aspect"] == 10
assert history.soil_data_changes["depth_dependent_data"][0]["depth_interval"]["start"] == 0
assert history.soil_data_changes["depth_dependent_data"][0]["depth_interval"]["end"] == 10
assert history.soil_data_changes["depth_dependent_data"][0]["clay_percent"] == 10
assert history.soil_data_changes["depth_intervals"][0]["depth_interval"]["start"] == 0
assert history.soil_data_changes["depth_intervals"][0]["depth_interval"]["end"] == 10
assert history.soil_data_changes["depth_intervals"][0]["soil_texture_enabled"] is True
assert history.soil_data_changes["deleted_depth_intervals"][0]["start"] == 10
assert history.soil_data_changes["deleted_depth_intervals"][0]["end"] == 20

0 comments on commit 9b91aaa

Please sign in to comment.