Skip to content

Commit 5a4f363

Browse files
authored
Merge branch 'fzi-forschungszentrum-informatik:main' into main
2 parents a1df16c + fc87f13 commit 5a4f363

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

source/esg/models/metadata.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
from typing import Optional
2929

3030
from pydantic import Field
31+
from pydantic import field_validator
3132
from pydantic import HttpUrl
33+
from pydantic import model_validator
3234

3335
from esg.models.base import _BaseModel
3436

@@ -235,6 +237,25 @@ class Coverage(_BaseModel):
235237
),
236238
)
237239

240+
@field_validator("from_time")
241+
def validate_from_time_has_timezone(cls, v):
242+
if v.tzinfo is None:
243+
raise ValueError("`from_time` must have timezone specified.")
244+
return v
245+
246+
@field_validator("to_time")
247+
def validate_to_time_has_timezone(cls, v):
248+
if v.tzinfo is None:
249+
raise ValueError("`to_time` must have timezone specified.")
250+
return v
251+
252+
@model_validator(mode="after")
253+
def validate_from_time_lte_to_time(cls, data):
254+
error_message = "`from_time` must be larger or equal `to_time`."
255+
if data.from_time > data.to_time:
256+
raise ValueError(error_message)
257+
return data
258+
238259

239260
class CoverageDelta(_BaseModel):
240261
"""

source/esg/test/data.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,8 +1544,8 @@
15441544
]
15451545

15461546
coverages = [
1547+
# Minimal valid example of a simple 24h coverage.
15471548
{
1548-
# Minimal valid example of a simple 24h coverage.
15491549
"Python": {
15501550
"from_time": datetime(2022, 5, 1, 0, tzinfo=timezone.utc),
15511551
"to_time": datetime(2022, 5, 2, 0, tzinfo=timezone.utc),
@@ -1556,8 +1556,8 @@
15561556
"available_at": None,
15571557
},
15581558
},
1559+
# With available at set.
15591560
{
1560-
# With available at set..
15611561
"Python": {
15621562
"from_time": datetime(2022, 5, 1, 0, tzinfo=timezone.utc),
15631563
"to_time": datetime(2022, 5, 2, 0, tzinfo=timezone.utc),
@@ -1569,6 +1569,18 @@
15691569
"available_at": "2022-05-01T00:00:00Z",
15701570
},
15711571
},
1572+
# from_time and to_time can be identical.
1573+
{
1574+
"Python": {
1575+
"from_time": datetime(2022, 5, 1, 0, tzinfo=timezone.utc),
1576+
"to_time": datetime(2022, 5, 1, 0, tzinfo=timezone.utc),
1577+
},
1578+
"JSONable": {
1579+
"from_time": "2022-05-01T00:00:00Z",
1580+
"to_time": "2022-05-01T00:00:00Z",
1581+
"available_at": None,
1582+
},
1583+
},
15721584
]
15731585

15741586
invalid_coverages = [
@@ -1596,6 +1608,27 @@
15961608
"coverage_to": None,
15971609
},
15981610
},
1611+
# `to_time` must be larger then `from_time`.
1612+
{
1613+
"JSONable": {
1614+
"from_time": "2022-05-03T00:00:00Z",
1615+
"to_time": "2022-05-02T00:00:00Z",
1616+
},
1617+
},
1618+
# `from_time` must have timezone.
1619+
{
1620+
"JSONable": {
1621+
"from_time": "2022-05-03T00:00:00",
1622+
"to_time": "2022-05-02T00:00:00Z",
1623+
},
1624+
},
1625+
# `to_time` must have timezone.
1626+
{
1627+
"JSONable": {
1628+
"from_time": "2022-05-03T00:00:00Z",
1629+
"to_time": "2022-05-02T00:00:00",
1630+
},
1631+
},
15991632
]
16001633

16011634

0 commit comments

Comments
 (0)