Skip to content

Commit b9b5ab3

Browse files
add test YAML; convert datetime to str before DIFF comparison
1 parent ca7b965 commit b9b5ab3

File tree

4 files changed

+1200
-13
lines changed

4 files changed

+1200
-13
lines changed

models/ConfigData.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass, field, fields, is_dataclass
2+
from datetime import datetime, timezone
23
from enum import Enum
34

45
from .utils import update_dataclass_from_dict
@@ -140,7 +141,15 @@ def all_missing_props(self):
140141
return self._all_missing_props
141142
return []
142143

143-
def asdict_enum_safe(self, obj):
144+
def datetime_to_string(self, data: datetime):
145+
# normalize to UTC and format with Z
146+
if data.tzinfo is None:
147+
data = data.replace(tzinfo=timezone.utc)
148+
else:
149+
data = data.astimezone(timezone.utc)
150+
return data.strftime("%Y-%m-%dT%H:%M:%SZ")
151+
152+
def asdict_enum_safe(self, obj, datetime_to_str=False):
144153
"""Overwriting dataclass 'asdict' fuction to replace Enums with strings."""
145154
if is_dataclass(obj):
146155
result = {}
@@ -151,21 +160,26 @@ def asdict_enum_safe(self, obj):
151160
if key == "linked__data":
152161
key = "linked-data"
153162
if value is not None:
154-
result[key] = self.asdict_enum_safe(value)
163+
result[key] = self.asdict_enum_safe(value, datetime_to_str)
155164
return result
156165
elif isinstance(obj, Enum):
157166
return obj.value
158167
elif isinstance(obj, InlineList):
159168
return obj
160169
elif isinstance(obj, list):
161-
return [self.asdict_enum_safe(v) for v in obj]
170+
return [self.asdict_enum_safe(v, datetime_to_str) for v in obj]
162171
elif isinstance(obj, dict):
163172
return {
164-
self.asdict_enum_safe(k): self.asdict_enum_safe(v)
173+
self.asdict_enum_safe(k, datetime_to_str): self.asdict_enum_safe(
174+
v, datetime_to_str
175+
)
165176
for k, v in obj.items()
166177
}
167178
else:
168-
return obj
179+
if isinstance(obj, datetime) and datetime_to_str:
180+
return self.datetime_to_string(obj)
181+
else:
182+
return obj
169183

170184
def add_new_resource(self) -> str:
171185
"""Add a placeholder resource."""

pygeoapi_config_dialog.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,8 @@ class CustomDumper(yaml.SafeDumper):
121121
)
122122

123123
def represent_datetime_as_timestamp(dumper, data: datetime):
124-
# normalize to UTC and format with Z
125-
if data.tzinfo is None:
126-
data = data.replace(tzinfo=timezone.utc)
127-
else:
128-
data = data.astimezone(timezone.utc)
129-
value = data.strftime("%Y-%m-%dT%H:%M:%SZ")
124+
value = self.config_data.datetime_to_string(data)
125+
130126
# emit as YAML timestamp → plain scalar, no quotes
131127
return dumper.represent_scalar("tag:yaml.org,2002:timestamp", value)
132128

tests/test_yaml_save.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def test_open_file_validate_ui_data_save_file(qtbot, sample_yaml: str):
9797
dialog.open_file(
9898
sample_yaml
9999
) # now dialog.config_data has the data stored including .all_missing_props
100-
yaml1_data = deepcopy(dialog.yaml_original_data)
100+
yaml1_data = dialog.config_data.asdict_enum_safe(
101+
deepcopy(dialog.yaml_original_data), True
102+
)
101103
yaml1_missing_props = deepcopy(dialog.config_data.all_missing_props)
102104

103105
# Save YAML - EVEN THOUGH some mandatory fields might be missing and recorded as empty strings/lists
@@ -106,7 +108,9 @@ def test_open_file_validate_ui_data_save_file(qtbot, sample_yaml: str):
106108

107109
# open the new file
108110
dialog.open_file(abs_new_yaml_path) # now dialog.config_data has the data stored
109-
yaml2_data = deepcopy(dialog.yaml_original_data)
111+
yaml2_data = dialog.config_data.asdict_enum_safe(
112+
deepcopy(dialog.yaml_original_data), True
113+
)
110114

111115
# get diff between old and new data
112116
diff_data = diff_yaml_dict_remove_known_faulty_fields(

0 commit comments

Comments
 (0)