Debian Codegraph @

tests/helper_test.py

raw symbols SCIP
1 # Copyright (C) 2024 Rishit Chaudhary, 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 """Test the helper functions edge cases."""
17
18 from enum import Enum
19 from typing import Callable
20 from unittest.mock import Mock
21 from unittest.mock import patch
22
23 from fastkml.helpers import attribute_enum_kwarg
24 from fastkml.helpers import attribute_float_kwarg
25 from fastkml.helpers import subelement_bool_kwarg
26 from fastkml.helpers import subelement_enum_kwarg
27 from fastkml.helpers import subelement_float_kwarg
28 from fastkml.helpers import subelement_int_kwarg
29 from tests.base import StdLibrary
30
31
32 class Node:
33 text: str
34
35
36 class Color(Enum):
37 RED = 1
38
39
40 class TestStdLibrary(StdLibrary):
41 """Test with the standard library."""
42
43 def test_subelement_int_kwarg(self) -> None:
44 node = Node()
45 node.text = ""
46 element = Mock()
47 element.find.return_value = node
48 res = subelement_int_kwarg(
49 element=element,
50 ns="ns",
51 name_spaces={"name": "uri"},
52 node_name="node",
53 kwarg="kwarg",
54 classes=(int,),
55 strict=False,
56 )
57 assert res == {}
58
59 def test_subelement_float_kwarg(self) -> None:
60 node = Node()
61 node.text = ""
62 element = Mock()
63 element.find.return_value = node
64 res = subelement_float_kwarg(
65 element=element,
66 ns="ns",
67 name_spaces={"name": "uri"},
68 node_name="node",
69 kwarg="kwarg",
70 classes=(float,),
71 strict=False,
72 )
73 assert res == {}
74
75 @patch("fastkml.helpers.handle_error")
76 def test_attribute_float_kwarg(
77 self,
78 mock_handle_error: Callable[..., None],
79 ) -> None:
80 element = Mock()
81 element.get.return_value = "abcd"
82
83 res = attribute_float_kwarg(
84 element=element,
85 ns="ns",
86 name_spaces={"name": "uri"},
87 node_name="node",
88 kwarg="a",
89 classes=(float,),
90 strict=True,
91 )
92
93 assert res == {}
94 mock_handle_error.assert_called_once() # type: ignore[attr-defined]
95
96 def test_subelement_enum_kwarg(self) -> None:
97 node = Node()
98 node.text = ""
99 element = Mock()
100 element.find.return_value = node
101
102 res = subelement_enum_kwarg(
103 element=element,
104 ns="ns",
105 name_spaces={"name": "uri"},
106 node_name="node",
107 kwarg="a",
108 classes=(Color,),
109 strict=True,
110 )
111
112 assert res == {}
113 element.find.assert_called_once_with("nsnode")
114
115 def test_attribute_enum_kwarg(self) -> None:
116 element = Mock()
117 element.get.return_value = None
118
119 res = attribute_enum_kwarg(
120 element=element,
121 ns="ns",
122 name_spaces={"name": "uri"},
123 node_name="node",
124 kwarg="a",
125 classes=(Color,),
126 strict=True,
127 )
128
129 assert res == {}
130 element.get.assert_called_once_with("nsnode")
131
132 def test_subelement_bool_kwarg(self) -> None:
133 node = Node()
134 node.text = ""
135 element = Mock()
136 element.find.return_value = node
137 res = subelement_bool_kwarg(
138 element=element,
139 ns="ns",
140 name_spaces={"name": "uri"},
141 node_name="node",
142 kwarg="a",
143 classes=(bool,),
144 strict=True,
145 )
146
147 assert res == {}
148 element.find.assert_called_once_with("nsnode")