Debian Codegraph @

tests/times_test.py

raw symbols SCIP
1 # Copyright (C) 2022 - 2024 Christian Ledermann
2 #
3 # This library is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2.1 of the License, or (at your option)
6 # any later version.
7 #
8 # This library is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 """Test the times classes."""
18
19 import datetime
20
21 import pytest
22 from dateutil.tz import gettz
23 from dateutil.tz import tzoffset
24 from dateutil.tz import tzutc
25
26 import fastkml as kml
27 from fastkml.enums import DateTimeResolution
28 from fastkml.times import KmlDateTime
29 from tests.base import Lxml
30 from tests.base import StdLibrary
31
32
33 class TestDateTime(StdLibrary):
34 """KmlDateTime implementation is independent of XML parser."""
35
36 def test_kml_datetime_year(self) -> None:
37 dt = datetime.datetime(2000, 1, 1) # noqa: DTZ001
38
39 kdt = KmlDateTime(dt, DateTimeResolution.year)
40
41 assert kdt.resolution == DateTimeResolution.year
42 assert str(kdt) == "2000"
43 assert bool(kdt)
44
45 def test_kml_datetime_year_month(self) -> None:
46 dt = datetime.datetime(2000, 3, 1) # noqa: DTZ001
47
48 kdt = KmlDateTime(dt, DateTimeResolution.year_month)
49
50 assert kdt.resolution == DateTimeResolution.year_month
51 assert str(kdt) == "2000-03"
52 assert bool(kdt)
53
54 def test_kml_datetime_date(self) -> None:
55 dt = datetime.datetime.now() # noqa: DTZ005
56
57 kdt = KmlDateTime(dt, DateTimeResolution.date)
58
59 assert kdt.resolution == DateTimeResolution.date
60 assert str(kdt) == dt.date().isoformat()
61 assert bool(kdt)
62
63 def test_kml_datetime_date_implicit(self) -> None:
64 dt = datetime.date.today() # noqa: DTZ011
65
66 kdt = KmlDateTime(dt)
67
68 assert kdt.resolution == DateTimeResolution.date
69 assert str(kdt) == dt.isoformat()
70 assert bool(kdt)
71
72 def test_kml_datetime_datetime(self) -> None:
73 dt = datetime.datetime.now() # noqa: DTZ005
74
75 kdt = KmlDateTime(dt, DateTimeResolution.datetime)
76
77 assert kdt.resolution == DateTimeResolution.datetime
78 assert str(kdt) == dt.isoformat()
79 assert bool(kdt)
80
81 def test_kml_datetime_datetime_implicit(self) -> None:
82 dt = datetime.datetime.now() # noqa: DTZ005
83
84 kdt = KmlDateTime(dt)
85
86 assert kdt.resolution == DateTimeResolution.datetime
87 assert str(kdt) == dt.isoformat()
88 assert bool(kdt)
89
90 def test_kml_datetime_no_datetime(self) -> None:
91 """When we pass dt as None bool() should return False."""
92 kdt = KmlDateTime(None) # type: ignore[arg-type]
93
94 assert kdt.resolution == DateTimeResolution.date
95 assert not bool(kdt)
96 with pytest.raises(
97 AttributeError,
98 match=r"^'NoneType' object has no attribute 'isoformat'$",
99 ):
100 str(kdt)
101
102 def test_kml_datetime_in_dst_fall_back(self) -> None:
103 dt = datetime.datetime(2016, 10, 30, 3, 30, tzinfo=gettz("Europe/Helsinki"))
104
105 kdt_dst_0 = KmlDateTime(dt)
106 kdt_dst_1 = KmlDateTime(dt.replace(fold=1))
107 kdt_no_dst = KmlDateTime(dt.replace(tzinfo=gettz("Etc/GMT-3")))
108
109 assert kdt_dst_0 != kdt_dst_1
110 assert kdt_dst_0 == kdt_no_dst
111 assert str(kdt_dst_0) != str(kdt_dst_1)
112 assert str(kdt_dst_0) == str(kdt_no_dst)
113
114 def test_parse_year(self) -> None:
115 dt = KmlDateTime.parse("2000")
116
117 assert dt
118 assert dt.resolution == DateTimeResolution.year
119 assert dt.dt == datetime.date(2000, 1, 1)
120
121 def test_parse_year_0(self) -> None:
122 with pytest.raises(
123 ValueError,
124 match=r"^year 0 is out of range$|year must be in 1..9999",
125 ):
126 KmlDateTime.parse("0000")
127
128 def test_parse_year_month(self) -> None:
129 dt = KmlDateTime.parse("2000-03")
130
131 assert dt
132 assert dt.resolution == DateTimeResolution.year_month
133 assert dt.dt == datetime.date(2000, 3, 1)
134
135 def test_parse_year_month_no_dash(self) -> None:
136 dt = KmlDateTime.parse("200004")
137
138 assert dt
139 assert dt.resolution == DateTimeResolution.year_month
140 assert dt.dt == datetime.date(2000, 4, 1)
141
142 def test_parse_year_month_0(self) -> None:
143 with pytest.raises(ValueError, match=r"month must be in 1..12"):
144 KmlDateTime.parse("2000-00")
145
146 def test_parse_year_month_13(self) -> None:
147 with pytest.raises(ValueError, match=r"month must be in 1..12"):
148 KmlDateTime.parse("2000-13")
149
150 def test_parse_year_month_day(self) -> None:
151 dt = KmlDateTime.parse("2000-03-01")
152
153 assert dt
154 assert dt.resolution == DateTimeResolution.date
155 assert dt.dt == datetime.date(2000, 3, 1)
156
157 def test_parse_year_month_day_no_dash(self) -> None:
158 dt = KmlDateTime.parse("20000401")
159
160 assert dt
161 assert dt.resolution == DateTimeResolution.date
162 assert dt.dt == datetime.date(2000, 4, 1)
163
164 def test_parse_year_month_day_0(self) -> None:
165 with pytest.raises(
166 ValueError,
167 match="day ",
168 ):
169 KmlDateTime.parse("2000-05-00")
170
171 def test_parse_datetime_utc(self) -> None:
172 dt = KmlDateTime.parse("1997-07-16T07:30:15Z")
173
174 assert dt
175 assert dt.resolution == DateTimeResolution.datetime
176 assert dt.dt == datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc())
177
178 def test_parse_datetime_with_tz(self) -> None:
179 dt = KmlDateTime.parse("1997-07-16T07:30:15+01:00")
180
181 assert dt
182 assert dt.resolution == DateTimeResolution.datetime
183 assert dt.dt == datetime.datetime(
184 1997,
185 7,
186 16,
187 7,
188 30,
189 15,
190 tzinfo=tzoffset(None, 3600),
191 )
192
193 def test_parse_datetime_with_tz_no_colon(self) -> None:
194 dt = KmlDateTime.parse("1997-07-16T07:30:15+0100")
195
196 assert dt
197 assert dt.resolution == DateTimeResolution.datetime
198 assert dt.dt == datetime.datetime(
199 1997,
200 7,
201 16,
202 7,
203 30,
204 15,
205 tzinfo=tzoffset(None, 3600),
206 )
207
208 def test_parse_datetime_no_tz(self) -> None:
209 dt = KmlDateTime.parse("1997-07-16T07:30:15")
210
211 assert dt
212 assert dt.resolution == DateTimeResolution.datetime
213 assert dt.dt == datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc())
214
215 def test_parse_datetime_empty(self) -> None:
216 with pytest.raises(
217 ValueError,
218 match=r"^Could not match input '' to any of the following formats:",
219 ):
220 KmlDateTime.parse("")
221
222 def test_parse_year_month_5(self) -> None:
223 """Test that a single digit month is invalid."""
224 with pytest.raises(
225 ValueError,
226 match=r"^Could not match input '19973' to any of the following formats:",
227 ):
228 KmlDateTime.parse("19973")
229
230
231 class TestStdLibrary(StdLibrary):
232 """Test with the standard library."""
233
234 def test_timestamp(self) -> None:
235 now = datetime.datetime.now() # noqa: DTZ005
236 dt = KmlDateTime(now)
237
238 ts = kml.TimeStamp(timestamp=dt)
239
240 assert ts.timestamp
241 assert ts.timestamp.dt == now
242 assert ts.timestamp.resolution == DateTimeResolution.datetime
243 assert "TimeStamp>" in str(ts.to_string())
244 assert "when>" in str(ts.to_string())
245 assert now.isoformat() in str(ts.to_string())
246 y2k = KmlDateTime(datetime.date(2000, 1, 1))
247 ts = kml.TimeStamp(timestamp=y2k)
248 assert ts.timestamp == y2k
249 assert "2000-01-01" in str(ts.to_string())
250
251 def test_timespan(self) -> None:
252 now = KmlDateTime(datetime.datetime.now()) # noqa: DTZ005
253 y2k = KmlDateTime(datetime.datetime(2000, 1, 1)) # noqa: DTZ001
254
255 ts = kml.TimeSpan(end=now, begin=y2k)
256
257 assert ts.end == now
258 assert ts.begin == y2k
259 assert "TimeSpan>" in str(ts.to_string())
260 assert "begin>" in str(ts.to_string())
261 assert "end>" in str(ts.to_string())
262 assert now.dt.isoformat() in str(ts.to_string())
263 assert y2k.dt.isoformat() in str(ts.to_string())
264 ts.end = None
265 assert now.dt.isoformat() not in str(ts.to_string())
266 assert y2k.dt.isoformat() in str(ts.to_string())
267 assert ts
268 ts.begin = None
269 assert not ts
270
271 def test_feature_timestamp(self) -> None:
272 now = datetime.datetime.now() # noqa: DTZ005
273 f = kml.Document()
274
275 f.times = kml.TimeStamp(timestamp=KmlDateTime(now))
276
277 assert f.time_stamp
278 assert f.time_stamp.dt == now
279 assert now.isoformat() in str(f.to_string())
280 assert "TimeStamp>" in str(f.to_string())
281 assert "when>" in str(f.to_string())
282 f.times = kml.TimeStamp(timestamp=KmlDateTime(now.date()))
283 assert now.date().isoformat() in str(f.to_string())
284 assert now.isoformat() not in str(f.to_string())
285 f.times = None
286 assert "TimeStamp>" not in str(f.to_string())
287
288 def test_feature_timespan(self) -> None:
289 now = datetime.datetime.now() # noqa: DTZ005
290 y2k = datetime.datetime(2000, 1, 1) # noqa: DTZ001
291 f = kml.Document()
292 f.times = kml.TimeSpan(begin=KmlDateTime(y2k), end=KmlDateTime(now))
293 assert f.begin == KmlDateTime(y2k)
294 assert f.end == KmlDateTime(now)
295 assert now.isoformat() in str(f.to_string())
296 assert "2000-01-01" in str(f.to_string())
297 assert "TimeSpan>" in str(f.to_string())
298 assert "begin>" in str(f.to_string())
299 assert "end>" in str(f.to_string())
300 f.times = kml.TimeSpan(begin=KmlDateTime(y2k))
301 assert now.isoformat() not in str(f.to_string())
302 assert "2000-01-01" in str(f.to_string())
303 assert "TimeSpan>" in str(f.to_string())
304 assert "begin>" in str(f.to_string())
305 assert "end>" not in str(f.to_string())
306 f.times = None
307 assert "TimeSpan>" not in str(f.to_string())
308
309 def test_read_timestamp_year(self) -> None:
310 doc = """
311 <TimeStamp>
312 <when>1997</when>
313 </TimeStamp>
314 """
315
316 ts = kml.TimeStamp.from_string(doc, ns="")
317
318 assert ts.timestamp
319 assert ts.timestamp.resolution == DateTimeResolution.year
320 assert ts.timestamp.dt == datetime.date(1997, 1, 1)
321
322 def test_read_timestamp_year_month(self) -> None:
323 doc = """
324 <TimeStamp>
325 <when>1997-07</when>
326 </TimeStamp>
327 """
328
329 ts = kml.TimeStamp.from_string(doc, ns="")
330
331 assert ts.timestamp
332 assert ts.timestamp.resolution == DateTimeResolution.year_month
333 assert ts.timestamp.dt == datetime.date(1997, 7, 1)
334
335 def test_read_timestamp_ym_no_hyphen(self) -> None:
336 doc = """
337 <TimeStamp>
338 <when>199808</when>
339 </TimeStamp>
340 """
341
342 ts = kml.TimeStamp.from_string(doc, ns="")
343
344 assert ts.timestamp
345 assert ts.timestamp.resolution == DateTimeResolution.year_month
346 assert ts.timestamp.dt == datetime.date(1998, 8, 1)
347
348 def test_read_timestamp_ymd(self) -> None:
349 doc = """
350 <TimeStamp>
351 <when>1997-07-16</when>
352 </TimeStamp>
353 """
354
355 ts = kml.TimeStamp.from_string(doc, ns="")
356
357 assert ts.timestamp
358 assert ts.timestamp.resolution == DateTimeResolution.date
359 assert ts.timestamp.dt == datetime.date(1997, 7, 16)
360
361 def test_read_timestamp_invalid(self) -> None:
362 doc = """
363 <TimeStamp>
364 <when>jan1997</when>
365 </TimeStamp>
366 """
367
368 ts = kml.TimeStamp.from_string(doc, ns="", strict=False)
369
370 assert ts.timestamp is None
371
372 def test_read_timestamp_empty(self) -> None:
373 doc = """
374 <TimeStamp>
375 <when></when>
376 </TimeStamp>
377 """
378
379 ts = kml.TimeStamp.from_string(doc, ns="")
380
381 assert ts.timestamp is None
382
383 def test_read_timestamp_utc(self) -> None:
384 # dateTime (YYYY-MM-DDThh:mm:ssZ)
385 # Here, T is the separator between the calendar and the hourly notation
386 # of time, and Z indicates UTC. (Seconds are required.)
387 doc = """
388 <TimeStamp>
389 <when>1997-07-16T07:30:15Z</when>
390 </TimeStamp>
391 """
392
393 ts = kml.TimeStamp.from_string(doc, ns="")
394
395 assert ts.timestamp
396 assert ts.timestamp.resolution == DateTimeResolution.datetime
397 assert ts.timestamp.dt == datetime.datetime(
398 1997,
399 7,
400 16,
401 7,
402 30,
403 15,
404 tzinfo=tzutc(),
405 )
406
407 def test_read_timestamp_utc_offset(self) -> None:
408 doc = """
409 <TimeStamp>
410 <when>1997-07-16T10:30:15+03:00</when>
411 </TimeStamp>
412 """
413
414 ts = kml.TimeStamp.from_string(doc, ns="")
415
416 assert ts.timestamp
417 assert ts.timestamp.resolution == DateTimeResolution.datetime
418 assert ts.timestamp.dt == datetime.datetime(
419 1997,
420 7,
421 16,
422 10,
423 30,
424 15,
425 tzinfo=tzoffset(None, 10800),
426 )
427
428 def test_read_timespan(self) -> None:
429 doc = """
430 <TimeSpan>
431 <begin>1876-08-01</begin>
432 <end>1997-07-16T07:30:15Z</end>
433 </TimeSpan>
434 """
435
436 ts = kml.TimeSpan.from_string(doc, ns="")
437
438 assert ts.begin
439 assert ts.begin.resolution == DateTimeResolution.date
440 assert ts.begin.dt == datetime.date(1876, 8, 1)
441 assert ts.end
442 assert ts.end.resolution == DateTimeResolution.datetime
443 assert ts.end.dt == datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc())
444
445 def test_feature_fromstring(self) -> None:
446 doc = """<Document xmlns="http://www.opengis.net/kml/2.2">
447 <name>Document.kml</name>
448 <open>1</open>
449 <TimeStamp>
450 <when>1997-07-16T10:30:15+03:00</when>
451 </TimeStamp>
452 <TimeSpan>
453 <begin>1876-08-01</begin>
454 <end>1997-07-16T07:30:15Z</end>
455 </TimeSpan>
456 </Document>"""
457
458 d = kml.Document.from_string(doc, ns="")
459
460 assert d.time_stamp is None
461 assert d.begin
462 assert d.begin.dt == datetime.date(1876, 8, 1)
463 assert d.end
464 assert d.end.dt == datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc())
465
466
467 class TestLxml(Lxml, TestStdLibrary):
468 """Test with lxml."""