| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
"""Test the utils module.""" |
| 17 |
|
| 18 |
from fastkml import Schema |
| 19 |
from fastkml import SchemaData |
| 20 |
from fastkml import kml |
| 21 |
from fastkml.utils import find |
| 22 |
from fastkml.utils import find_all |
| 23 |
from tests.base import Lxml |
| 24 |
from tests.base import StdLibrary |
| 25 |
|
| 26 |
|
| 27 |
class TestFindAll(StdLibrary): |
| 28 |
"""Test the find_all function.""" |
| 29 |
|
| 30 |
def test_find_all(self) -> None: |
| 31 |
class A: |
| 32 |
def __init__(self, x: int) -> None: |
| 33 |
self.x = x |
| 34 |
|
| 35 |
class B: |
| 36 |
def __init__(self, y: int) -> None: |
| 37 |
self.y = y |
| 38 |
|
| 39 |
class C: |
| 40 |
def __init__(self, z: int) -> None: |
| 41 |
self.z = z |
| 42 |
|
| 43 |
class D: |
| 44 |
def __init__(self, a: A, b: B, c: C) -> None: |
| 45 |
self.a = a |
| 46 |
self.b = b |
| 47 |
self.c = c |
| 48 |
|
| 49 |
a1 = A(1) |
| 50 |
a2 = A(2) |
| 51 |
b1 = B(1) |
| 52 |
b2 = B(2) |
| 53 |
c1 = C(1) |
| 54 |
c2 = C(2) |
| 55 |
d1 = D(a1, b1, c1) |
| 56 |
d2 = D(a2, b2, c2) |
| 57 |
|
| 58 |
result = list(find_all(d1, of_type=A)) |
| 59 |
assert result == [a1] |
| 60 |
|
| 61 |
result = list(find_all(d1, of_type=B)) |
| 62 |
assert result == [b1] |
| 63 |
|
| 64 |
result = list(find_all(d1, of_type=C)) |
| 65 |
assert result == [c1] |
| 66 |
|
| 67 |
result = list(find_all(d1, of_type=D)) |
| 68 |
assert result == [d1] |
| 69 |
|
| 70 |
result = list(find_all(d1, of_type=A, x=1)) |
| 71 |
assert result == [a1] |
| 72 |
|
| 73 |
result = list(find_all(d1, of_type=A, x=2)) |
| 74 |
assert not result |
| 75 |
|
| 76 |
result = list(find_all(d2, of_type=A, x=2)) |
| 77 |
assert result == [a2] |
| 78 |
|
| 79 |
def test_find_all_empty(self) -> None: |
| 80 |
result = list(find_all(None, of_type=None)) |
| 81 |
assert result == [None] |
| 82 |
|
| 83 |
def test_find_all_no_type(self) -> None: |
| 84 |
class A: |
| 85 |
def __init__(self, x: int) -> None: |
| 86 |
self.x = x |
| 87 |
|
| 88 |
a1 = A(1) |
| 89 |
|
| 90 |
result = list(find_all(a1, of_type=None)) |
| 91 |
assert result == [a1, 1] |
| 92 |
|
| 93 |
def test_find_all_no_type_attr(self) -> None: |
| 94 |
class A: |
| 95 |
def __init__(self, x: int, y: int) -> None: |
| 96 |
self.x = x |
| 97 |
self.y = y |
| 98 |
|
| 99 |
class B: |
| 100 |
def __init__(self, a: list[A]) -> None: |
| 101 |
self.a = a |
| 102 |
|
| 103 |
a1 = A(1, 0) |
| 104 |
a2 = A(0, 1) |
| 105 |
a3 = A(1, 1) |
| 106 |
b = B([a1, a2, a3]) |
| 107 |
|
| 108 |
assert list(find_all(b, x=1)) == [a1, a3] |
| 109 |
assert list(find_all(b, y=1)) == [a2, a3] |
| 110 |
assert list(find_all(b, x=0)) == [a2] |
| 111 |
assert list(find_all(b, x=1, y=1)) == [a3] |
| 112 |
|
| 113 |
def test_find_no_type_attr(self) -> None: |
| 114 |
class A: |
| 115 |
def __init__(self, x: int, y: int) -> None: |
| 116 |
self.x = x |
| 117 |
self.y = y |
| 118 |
|
| 119 |
class B: |
| 120 |
def __init__(self, a: list[A]) -> None: |
| 121 |
self.a = a |
| 122 |
|
| 123 |
a1 = A(1, 0) |
| 124 |
a2 = A(0, 1) |
| 125 |
a3 = A(1, 1) |
| 126 |
b = B([a1, a2, a3]) |
| 127 |
|
| 128 |
assert find(b, x=1) == a1 |
| 129 |
assert find(b, y=1) == a2 |
| 130 |
assert find(b, x=0) == a2 |
| 131 |
assert find(b, x=1, y=1) == a3 |
| 132 |
|
| 133 |
def test_find_schema_by_url(self) -> None: |
| 134 |
doc = ( |
| 135 |
'<kml xmlns="http://www.opengis.net/kml/2.2">' |
| 136 |
"<Document>" |
| 137 |
"<name>ExtendedData+SchemaData</name>" |
| 138 |
"<open>1</open>" |
| 139 |
"<!-- Create a balloon template referring to the user-defined type -->" |
| 140 |
'<Style id="trailhead-balloon-template">' |
| 141 |
"<BalloonStyle>" |
| 142 |
"<text>" |
| 143 |
"<![CDATA[" |
| 144 |
"<h2>My favorite trails!</h2>" |
| 145 |
"<br/><br/>" |
| 146 |
"The $[TrailHeadType/TrailHeadName/displayName] is " |
| 147 |
"<i>$[TrailHeadType/TrailHeadName]</i>." |
| 148 |
"The trail is $[TrailHeadType/TrailLength] miles.<br/>" |
| 149 |
"The climb is $[TrailHeadType/ElevationGain] meters.<br/><br/>" |
| 150 |
"]]>" |
| 151 |
"</text>" |
| 152 |
"</BalloonStyle>" |
| 153 |
"</Style>" |
| 154 |
'<!-- Declare the type "TrailHeadType" with 3 fields -->' |
| 155 |
'<Schema name="TrailHeadType" id="TrailHeadTypeId">' |
| 156 |
'<SimpleField type="string" name="TrailHeadName">' |
| 157 |
"<displayName><![CDATA[<b>Trail Head Name</b>]]></displayName>" |
| 158 |
"</SimpleField>" |
| 159 |
'<SimpleField type="double" name="TrailLength">' |
| 160 |
"<displayName><![CDATA[<i>The length in miles</i>]]></displayName>" |
| 161 |
"</SimpleField>" |
| 162 |
'<SimpleField type="int" name="ElevationGain">' |
| 163 |
"<displayName><![CDATA[<i>change in altitude</i>]]></displayName>" |
| 164 |
"</SimpleField>" |
| 165 |
"</Schema>" |
| 166 |
"<!-- Instantiate some Placemarks extended with TrailHeadType fields -->" |
| 167 |
"<Placemark>" |
| 168 |
"<name>Easy trail</name>" |
| 169 |
"<styleUrl>#trailhead-balloon-template</styleUrl>" |
| 170 |
"<ExtendedData>" |
| 171 |
'<SchemaData schemaUrl="#TrailHeadTypeId">' |
| 172 |
'<SimpleData name="TrailHeadName">Pi in the sky</SimpleData>' |
| 173 |
'<SimpleData name="TrailLength">3.14159</SimpleData>' |
| 174 |
'<SimpleData name="ElevationGain">10</SimpleData>' |
| 175 |
"</SchemaData>" |
| 176 |
"</ExtendedData>" |
| 177 |
"<Point>" |
| 178 |
"<coordinates>-122.000,37.002</coordinates>" |
| 179 |
"</Point>" |
| 180 |
"</Placemark>" |
| 181 |
"<Placemark>" |
| 182 |
"<name>Difficult trail</name>" |
| 183 |
"<styleUrl>#trailhead-balloon-template</styleUrl>" |
| 184 |
"<ExtendedData>" |
| 185 |
'<SchemaData schemaUrl="#TrailHeadTypeId">' |
| 186 |
'<SimpleData name="TrailHeadName">Mount Everest</SimpleData>' |
| 187 |
'<SimpleData name="TrailLength">347.45</SimpleData>' |
| 188 |
'<SimpleData name="ElevationGain">10000</SimpleData>' |
| 189 |
"</SchemaData>" |
| 190 |
"</ExtendedData>" |
| 191 |
"<Point>" |
| 192 |
"<coordinates>-121.998,37.0078</coordinates>" |
| 193 |
"</Point>" |
| 194 |
"</Placemark>" |
| 195 |
"</Document>" |
| 196 |
"</kml>" |
| 197 |
) |
| 198 |
k = kml.KML.from_string(doc, strict=False) |
| 199 |
|
| 200 |
schema = find(k, of_type=Schema, id="TrailHeadTypeId") |
| 201 |
schema_data = list(find_all(k, of_type=SchemaData)) |
| 202 |
|
| 203 |
assert isinstance(schema, Schema) |
| 204 |
assert schema.name == "TrailHeadType" |
| 205 |
assert schema.id == "TrailHeadTypeId" |
| 206 |
assert len(schema_data) == 2 |
| 207 |
for data in schema_data: |
| 208 |
assert isinstance(data, SchemaData) |
| 209 |
assert data.schema_url == "#TrailHeadTypeId" |
| 210 |
|
| 211 |
|
| 212 |
class TestFindAllLxml(Lxml): |
| 213 |
"""Run the tests using lxml.""" |