Debian Codegraph @

tests/base_test.py

raw symbols SCIP
1 # Copyright (C) 2021 - 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 base classes."""
18
19 from fastkml import base
20 from fastkml import kml_base
21 from tests.base import Lxml
22 from tests.base import StdLibrary
23
24
25 class TestStdLibrary(StdLibrary):
26 """Test the base object with the standard library."""
27
28 def test_to_string(self) -> None:
29 obj = kml_base._BaseObject(id="id-0", target_id="target-id-0")
30
31 assert obj.to_string() == (
32 '<kml:_BaseObject xmlns:kml="http://www.opengis.net/kml/2.2" '
33 'id="id-0" targetId="target-id-0" />'
34 )
35
36 def test_str(self) -> None:
37 obj = kml_base._BaseObject(id="id-0", target_id="target-id-0")
38 assert str(obj) == obj.to_string()
39
40 def test_custom_kwargs(self) -> None:
41 obj = kml_base._BaseObject(
42 id="id-0",
43 target_id="target-id-0",
44 custom="custom",
45 altkw=2,
46 )
47
48 assert obj.custom == "custom" # type: ignore[attr-defined]
49 assert obj.altkw == 2 # type: ignore[attr-defined]
50
51 def test_custom_kwargs_splat(self) -> None:
52 obj = kml_base._BaseObject(
53 id="id-0",
54 target_id="target-id-0",
55 custom="custom",
56 altkw=2,
57 )
58
59 assert obj._get_splat() == {"custom": "custom", "altkw": 2}
60
61 def test_eq(self) -> None:
62 obj1 = kml_base._BaseObject(id="id-0", target_id="target-id-0")
63 obj2 = kml_base._BaseObject(id="id-0", target_id="target-id-0")
64 assert obj1 == obj2
65
66 def test_ne(self) -> None:
67 obj1 = kml_base._BaseObject(id="id-0", target_id="target-id-0")
68 obj2 = kml_base._BaseObject(id="id-1", target_id="target-id-0")
69 assert obj1 != obj2
70
71 def test_ne_basexml(self) -> None:
72 obj1 = object()
73 obj2 = base._XMLObject()
74 assert obj1 != obj2
75
76 def test_ne_basexml_2(self) -> None:
77 obj1 = base._XMLObject()
78 obj2 = kml_base._BaseObject()
79 assert obj1 != obj2
80
81 def test_to_str_empty_ns(self) -> None:
82 obj = kml_base._BaseObject(ns="", id="id-0", target_id="target-id-0")
83
84 assert obj.to_string().replace(" ", "").replace(
85 "\n",
86 "",
87 ) == '<_BaseObject id="id-0" targetId="target-id-0" />'.replace(" ", "")
88
89 def test_from_string(self) -> None:
90 be = kml_base._BaseObject.from_string(
91 string=(
92 '<kml:test xmlns:kml="http://www.opengis.net/kml/2.2" '
93 'id="id-0" targetId="target-id-0" />'
94 ),
95 )
96 assert be.id == "id-0"
97 assert be.target_id == "target-id-0"
98
99 def test_from_string_attr_ns_prefix(self) -> None:
100 be = kml_base._BaseObject.from_string(
101 string=(
102 '<kml:test xmlns:kml="http://www.opengis.net/kml/2.2" '
103 'kml:id="id-0" kml:targetId="target-id-0" />'
104 ),
105 )
106 assert be.id == "id-0"
107 assert be.target_id == "target-id-0"
108
109 def test_base_class_from_string(self) -> None:
110 be = kml_base._BaseObject.from_string(
111 '<test id="id-0" targetId="td-00" />',
112 )
113
114 assert be.id == "id-0"
115 assert be.target_id == "td-00"
116 assert be.ns == "{http://www.opengis.net/kml/2.2}"
117
118 def test_base_class_from_empty_string(self) -> None:
119 be = kml_base._BaseObject.from_string("<test/>")
120
121 assert be.id == ""
122 assert be.target_id == ""
123 assert be.ns == "{http://www.opengis.net/kml/2.2}"
124
125 def test_xml_object_roundtrip(self) -> None:
126 obj = base._XMLObject()
127 obj2 = base._XMLObject.from_string(obj.to_string(), ns="")
128
129 assert obj == obj2
130 assert str(obj) == obj2.to_string()
131 assert repr(obj) == repr(obj2)
132
133 def test_base_object_roundtrip(self) -> None:
134 obj = kml_base._BaseObject(id="id-0", target_id="target-id-0")
135
136 obj2 = kml_base._BaseObject.from_string(obj.to_string())
137
138 assert obj == obj2
139 assert str(obj) == obj2.to_string()
140 assert repr(obj) == repr(obj2)
141
142
143 class TestLxml(Lxml, TestStdLibrary):
144 """Test the base object with lxml."""
145
146 def test_to_string(self) -> None:
147 obj = kml_base._BaseObject(id="id-0")
148
149 assert obj.to_string() == (
150 '<kml:_BaseObject xmlns:kml="http://www.opengis.net/kml/2.2" id="id-0"/>\n'
151 )
152
153 def test_from_string(self) -> None:
154 be = kml_base._BaseObject.from_string(
155 string=(
156 '<kml:test xmlns:kml="http://www.opengis.net/kml/2.2" '
157 'id="id-0" targetId="target-id-0"/>\n'
158 ),
159 )
160
161 assert be.id == "id-0"
162 assert be.target_id == "target-id-0"