Debian Codegraph @

tests/hypothesis/common.py

raw symbols SCIP
1 # Copyright (C) 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 """Common functionality for property based tests."""
17
18 import datetime
19 import logging
20
21 from dateutil.tz import tzfile
22 from dateutil.tz import tzutc
23 from dateutil.tz import tzwin
24 from pygeoif import GeometryCollection
25 from pygeoif import MultiLineString
26 from pygeoif import MultiPoint
27 from pygeoif import MultiPolygon
28 from pygeoif.geometry import LinearRing
29 from pygeoif.geometry import LineString
30 from pygeoif.geometry import Point
31 from pygeoif.geometry import Polygon
32
33 import fastkml
34 from fastkml.base import _XMLObject
35 from fastkml.enums import AltitudeMode
36 from fastkml.enums import ColorMode
37 from fastkml.enums import DataType
38 from fastkml.enums import DateTimeResolution
39 from fastkml.enums import DisplayMode
40 from fastkml.enums import GridOrigin
41 from fastkml.enums import PairKey
42 from fastkml.enums import RefreshMode
43 from fastkml.enums import Shape
44 from fastkml.enums import Units
45 from fastkml.enums import Verbosity
46 from fastkml.enums import ViewRefreshMode
47 from fastkml.gx import Angle
48 from fastkml.gx import TrackItem
49
50 logger = logging.getLogger(__name__)
51
52 eval_locals = {
53 "Point": Point,
54 "Polygon": Polygon,
55 "LineString": LineString,
56 "LinearRing": LinearRing,
57 "MultiPoint": MultiPoint,
58 "MultiLineString": MultiLineString,
59 "MultiPolygon": MultiPolygon,
60 "GeometryCollection": GeometryCollection,
61 "AltitudeMode": AltitudeMode,
62 "fastkml": fastkml,
63 "ViewRefreshMode": ViewRefreshMode,
64 "RefreshMode": RefreshMode,
65 "TrackItem": TrackItem,
66 "Angle": Angle,
67 "datetime": datetime,
68 "DateTimeResolution": DateTimeResolution,
69 "DataType": DataType,
70 "Units": Units,
71 "ColorMode": ColorMode,
72 "DisplayMode": DisplayMode,
73 "PairKey": PairKey,
74 "GridOrigin": GridOrigin,
75 "Shape": Shape,
76 "tzutc": tzutc,
77 "tzfile": tzfile,
78 "tzwin": tzwin,
79 }
80
81
82 def assert_repr_roundtrip(obj: _XMLObject) -> None:
83 """Test that repr(obj) can be eval'd back to obj."""
84 try:
85 assert obj == eval(repr(obj), {}, eval_locals) # noqa: S307
86 except FileNotFoundError: # pragma: no cover
87 # The timezone file may not be available on all systems.
88 logger.exception("Failed to eval repr(obj).") # pragma: no cover
89
90
91 def assert_str_roundtrip(obj: _XMLObject) -> None:
92 """
93 Test that an XML object can be serialized and deserialized without changes.
94
95 Uses default verbosity settings and validates the resulting XML structure.
96 """
97 new_object = type(obj).from_string(obj.to_string())
98
99 assert obj.to_string() == new_object.to_string()
100 assert obj == new_object
101 assert new_object.validate()
102
103
104 def assert_str_roundtrip_terse(obj: _XMLObject) -> None:
105 new_object = type(obj).from_string(
106 obj.to_string(verbosity=Verbosity.terse),
107 )
108
109 assert obj.to_string(verbosity=Verbosity.verbose) == new_object.to_string(
110 verbosity=Verbosity.verbose,
111 )
112 assert new_object.validate()
113
114
115 def assert_str_roundtrip_verbose(obj: _XMLObject) -> None:
116 new_object = type(obj).from_string(
117 obj.to_string(verbosity=Verbosity.verbose),
118 )
119
120 assert obj.to_string(verbosity=Verbosity.terse) == new_object.to_string(
121 verbosity=Verbosity.terse,
122 )
123 assert new_object.validate()