Debian Codegraph @

tests/atom_test.py

raw symbols SCIP
1 # Copyright (C) 2021 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 Atom classes."""
18
19 from fastkml import atom
20 from tests.base import Lxml
21 from tests.base import StdLibrary
22
23
24 class TestStdLibrary(StdLibrary):
25 """Test with the standard library."""
26
27 def test_atom_link_ns(self) -> None:
28 link = atom.Link()
29 assert link.to_string().startswith(
30 '<atom:link xmlns:atom="http://www.w3.org/2005/Atom"',
31 )
32
33 def test_atom_link(self) -> None:
34 link = atom.Link(
35 ns="{http://www.w3.org/2005/Atom}",
36 href="#here",
37 rel="alternate",
38 type="text/html",
39 hreflang="en",
40 title="Title",
41 length=3456,
42 )
43
44 serialized = link.to_string()
45
46 assert '<atom:link xmlns:atom="http://www.w3.org/2005/Atom"' in serialized
47 assert 'href="#here"' in serialized
48 assert 'rel="alternate"' in serialized
49 assert 'type="text/html"' in serialized
50 assert 'hreflang="en"' in serialized
51 assert 'title="Title"' in serialized
52 assert 'length="3456"' in serialized
53
54 def test_atom_link_round_trip(self) -> None:
55 link = atom.Link(
56 ns="{http://www.w3.org/2005/Atom}",
57 href="#here",
58 rel="alternate",
59 type="text/html",
60 hreflang="en",
61 title="Title",
62 length=3456,
63 )
64
65 link2 = atom.Link.from_string(link.to_string())
66
67 assert link == link2
68 assert link.to_string() == link2.to_string()
69 assert repr(link) == repr(link2)
70
71 def test_atom_link_read(self) -> None:
72 link = atom.Link.from_string(
73 '<atom:link xmlns:atom="http://www.w3.org/2005/Atom" '
74 'href="#here" rel="alternate" type="text/html" hreflang="en" '
75 'title="Title" length="3456" />',
76 )
77 assert link.href == "#here"
78 assert link.rel == "alternate"
79 assert link.type == "text/html"
80 assert link.hreflang == "en"
81 assert link.title == "Title"
82 assert link.length == 3456
83
84 def test_atom_link_read_no_href(self) -> None:
85 link = atom.Link.from_string(
86 '<atom:link xmlns:atom="http://www.w3.org/2005/Atom" '
87 'rel="alternate" type="text/html" hreflang="en" '
88 'title="Title" length="3456" />',
89 )
90 assert link.href is None
91
92 def test_atom_person_ns(self) -> None:
93 ns = "{http://www.opengis.net/kml/2.2}"
94 p = atom._Person(ns=ns)
95 assert p.ns == ns
96
97 def test_atom_author(self) -> None:
98 a = atom.Author(
99 ns="{http://www.w3.org/2005/Atom}",
100 name="Nobody",
101 uri="http://localhost",
102 email="[email protected]",
103 )
104
105 serialized = a.to_string()
106 assert '<atom:author xmlns:atom="http://www.w3.org/2005/Atom">' in serialized
107 assert "<atom:name>Nobody</atom:name>" in serialized
108 assert "<atom:uri>http://localhost</atom:uri>" in serialized
109 assert "<atom:email>[email protected]</atom:email>" in serialized
110 assert "</atom:author>" in serialized
111
112 def test_atom_author_read(self) -> None:
113 a = atom.Author.from_string(
114 '<atom:author xmlns:atom="http://www.w3.org/2005/Atom">'
115 "<atom:name>Nobody</atom:name><atom:uri>http://localhost</atom:uri>"
116 "<atom:email>[email protected]</atom:email></atom:author>",
117 ns="{http://www.w3.org/2005/Atom}",
118 )
119
120 assert a.name == "Nobody"
121 assert a.uri == "http://localhost"
122 assert a.email == "[email protected]"
123
124 def test_atom_author_round_trip(self) -> None:
125 a = atom.Author(
126 ns="{http://www.w3.org/2005/Atom}",
127 name="Nobody",
128 uri="http://localhost",
129 email="[email protected]",
130 )
131
132 a2 = atom.Author.from_string(a.to_string())
133
134 assert a == a2
135 assert a.to_string() == a2.to_string()
136 assert repr(a) == repr(a2)
137
138 def test_atom_contributor_read_no_name(self) -> None:
139 a = atom.Contributor.from_string(
140 '<atom:contributor xmlns:atom="http://www.w3.org/2005/Atom">'
141 "<atom:uri>http://localhost</atom:uri>"
142 "<atom:email>[email protected]</atom:email></atom:contributor>",
143 ns="{http://www.w3.org/2005/Atom}",
144 )
145
146 assert a.name is None
147 assert a.uri == "http://localhost"
148 assert a.email == "[email protected]"
149
150 def test_atom_contributor_no_name(self) -> None:
151 a = atom.Contributor(uri="http://localhost", email="[email protected]")
152
153 assert a.name is None
154 assert "atom:name" not in a.to_string()
155
156 def test_atom_contributor_roundtrip(self) -> None:
157 a = atom.Contributor(
158 ns="{http://www.w3.org/2005/Atom}",
159 name="Nobody",
160 uri="http://localhost",
161 email="[email protected]",
162 )
163
164 a2 = atom.Contributor.from_string(a.to_string())
165
166 assert a == a2
167 assert a.to_string() == a2.to_string()
168 assert repr(a) == repr(a2)
169
170
171 class TestLxml(Lxml, TestStdLibrary):
172 """Test with lxml."""