Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Restoring Fields with Errors #243

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions field_friend/automations/field.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import uuid
from dataclasses import dataclass
from typing import Any, Self

Expand Down Expand Up @@ -161,15 +162,36 @@ def shapely_polygon(self) -> shapely.geometry.Polygon:

@classmethod
def args_from_dict(cls, data: dict[str, Any]) -> dict:
return data
# Ensure all required fields exist with defaults
defaults: dict[str, Any] = {
'id': str(uuid.uuid4()),
'name': 'Field',
'first_row_start': None,
'first_row_end': None,
'row_spacing': 1,
'row_count': 1,
'outline_buffer_width': 1,
'row_support_points': [],
'bed_count': 1,
'bed_spacing': 1
}
for key in defaults:
if key in data:
defaults[key] = data[key]
return defaults

@classmethod
def from_dict(cls, data: dict[str, Any]) -> Self:
data['first_row_start'] = GeoPoint(lat=data['first_row_start']['lat'], long=data['first_row_start']['long'])
data['first_row_end'] = GeoPoint(lat=data['first_row_end']['lat'], long=data['first_row_end']['long'])
data['first_row_start'] = GeoPoint(lat=data['first_row_start']['lat'],
long=data['first_row_start']['long']) if data.get('first_row_start') else GeoPoint(lat=0, long=0)
data['first_row_end'] = GeoPoint(lat=data['first_row_end']['lat'],
long=data['first_row_end']['long']) if data.get('first_row_end') else GeoPoint(lat=1, long=1)
data['row_support_points'] = [rosys.persistence.from_dict(
RowSupportPoint, sp) for sp in data['row_support_points']] if 'row_support_points' in data else []
RowSupportPoint, sp) for sp in data['row_support_points']] if data.get('row_support_points') else []
field_data = cls(**cls.args_from_dict(data))
# if id is None, set it to a random uuid
if field_data.id is None:
field_data.id = str(uuid.uuid4())
return field_data

def get_buffered_area(self, rows: list[Row], buffer_width: float) -> list[GeoPoint]:
Expand Down
4 changes: 3 additions & 1 deletion tests/old_field_provider_persistence.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"row_spacing": 0.5,
"row_count": 10,
"outline_buffer_width": 2,
"row_support_points": []
"row_support_points": [],
"bed_count": 1,
"bed_spacing": 0.5
}
}
}
19 changes: 19 additions & 0 deletions tests/old_field_provider_persistence_with_errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"fields": {
"eb24db0f-d48e-4a88-b23a-4833cda55483": {
"id": "eb24db0f-d48e-4a88-b23a-4833cda55483",
"name": "field_1",
"first_row_start": {
"lat": 51.98333789813455,
"long": 7.434242765994318
},
"first_row_end": { "lat": 51.98334192260392, "long": 7.434293309874038 },
"row_spacing": 0.5,
"row_count": 10,
"not_existing_value": true,
"row_support_points": [],
"bed_count": 1,
"bed_spacing": 0.5
}
}
}
21 changes: 20 additions & 1 deletion tests/test_field_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from field_friend.localization import GeoPoint


def test_loading_from_old_persistence(system: System):
def test_loading_from_persistence(system: System):
system.field_provider.restore(json.loads(Path('tests/old_field_provider_persistence.json').read_text()))
assert len(system.field_provider.fields) == 1
field = system.field_provider.fields[0]
Expand All @@ -27,6 +27,25 @@ def test_loading_from_old_persistence(system: System):
assert field.first_row_end == GeoPoint(lat=51.98334192260392, long=7.434293309874038)


def test_loading_from_persistence_with_errors(system: System):
system.field_provider.restore(json.loads(Path('tests/old_field_provider_persistence_with_errors.json').read_text()))
assert len(system.field_provider.fields) == 1
field = system.field_provider.fields[0]
# should set outline_buffer_width to default value because it is missing in the persistence data
assert field.outline_buffer_width == 1
# should not write the not_existing_value to the field
assert not hasattr(field, 'not_existing_value')
assert field.row_count == 10
assert field.row_spacing == 0.5
assert len(field.outline) == 5
assert len(field.rows) == 10
assert len(field.row_support_points) == 0
for row in field.rows:
assert len(row.points) == 2
assert field.first_row_start == FIELD_FIRST_ROW_START
assert field.first_row_end == GeoPoint(lat=51.98334192260392, long=7.434293309874038)


def test_field_outline(system: System, field: Field):
field = system.field_provider.fields[0]
outline = field.outline
Expand Down