Debian Codegraph @

tests/validator_test.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 """Test the validator module."""
17
18 from pathlib import Path
19 from typing import Final
20
21 import pytest
22
23 from fastkml import atom
24 from fastkml import config
25 from fastkml.validator import get_schema_parser
26 from fastkml.validator import validate
27 from tests.base import Lxml
28 from tests.base import StdLibrary
29
30 TEST_DIR: Final = Path(__file__).parent
31
32
33 class TestStdLibrary(StdLibrary):
34 def setup_method(self) -> None:
35 """Invalidate the cache before each test."""
36 get_schema_parser.cache_clear()
37 super().setup_method()
38
39 def test_validate(self) -> None:
40 assert (
41 validate(
42 file_to_validate=TEST_DIR
43 / "ogc_conformance"
44 / "data"
45 / "kml"
46 / "Document-clean.kml",
47 )
48 is None
49 )
50
51 def test_validate_require_element_or_path(self) -> None:
52 with pytest.raises(
53 ValueError,
54 match=r"^Either element or file_to_validate must be provided.$",
55 ):
56 validate()
57
58 def test_validate_mutual_exclusive_element_and_path(self) -> None:
59 with pytest.raises(
60 ValueError,
61 match=r"^Only one of element and file_to_validate can be provided.$",
62 ):
63 validate(
64 element=atom.Link().etree_element(),
65 file_to_validate=TEST_DIR / "test.xml",
66 )
67
68
69 class TestLxml(Lxml):
70 def setup_method(self) -> None:
71 """Invalidate the cache before each test."""
72 get_schema_parser.cache_clear()
73 super().setup_method()
74
75 def test_validate(self) -> None:
76 assert validate(
77 file_to_validate=TEST_DIR
78 / "ogc_conformance"
79 / "data"
80 / "kml"
81 / "Document-clean.kml",
82 )
83
84 def test_validate_element(self) -> None:
85 link = atom.Link(
86 ns="{http://www.w3.org/2005/Atom}",
87 href="#here",
88 rel="alternate",
89 type="text/html",
90 hreflang="en",
91 title="Title",
92 length=3456,
93 )
94 assert validate(element=link.etree_element())
95
96 def test_validate_invalid_element(self) -> None:
97 link = atom.Link(
98 ns="{http://www.w3.org/2005/Atom}",
99 href="",
100 rel="alternate",
101 type="text/html",
102 hreflang="en",
103 title="Title",
104 length=3456,
105 )
106
107 with pytest.raises(AssertionError):
108 validate(element=link.etree_element())
109
110 def test_get_schema_parser(self) -> None:
111 path = TEST_DIR / "ogc_conformance" / "data" / "atom-author-link.xsd"
112 assert get_schema_parser(path)
113
114 def test_validate_empty_element(self) -> None:
115 element = config.etree.Element("kml")
116 with pytest.raises(
117 AssertionError,
118 match=(
119 r"^Element 'kml': "
120 "No matching global declaration available for the validation root.$"
121 ),
122 ):
123 assert validate(element=element)