| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the kml classes.""" |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from fastkml import containers |
| 22 |
from fastkml import features |
| 23 |
from fastkml import kml |
| 24 |
from fastkml import styles |
| 25 |
from tests.base import Lxml |
| 26 |
from tests.base import StdLibrary |
| 27 |
|
| 28 |
|
| 29 |
class TestStdLibrary(StdLibrary): |
| 30 |
"""Test with the standard library.""" |
| 31 |
|
| 32 |
def test_document_boolean_visibility(self) -> None: |
| 33 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 34 |
<Document targetId="someTargetId"> |
| 35 |
<name>Document.kml</name> |
| 36 |
<visibility>true</visibility> |
| 37 |
<open>1</open> |
| 38 |
</Document> |
| 39 |
</kml>""" |
| 40 |
|
| 41 |
k = kml.KML.from_string(doc, strict=False) |
| 42 |
assert k.features[0].visibility |
| 43 |
assert k.features[0].isopen |
| 44 |
|
| 45 |
def test_document_boolean_open(self) -> None: |
| 46 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 47 |
<Document targetId="someTargetId"> |
| 48 |
<name>Document.kml</name> |
| 49 |
<visibility>0</visibility> |
| 50 |
<open>false</open> |
| 51 |
</Document> |
| 52 |
</kml>""" |
| 53 |
|
| 54 |
k = kml.KML.from_string(doc, strict=False) |
| 55 |
assert k.features[0].visibility == 0 |
| 56 |
assert k.features[0].isopen is False |
| 57 |
|
| 58 |
def test_document_boolean_visibility_invalid(self) -> None: |
| 59 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 60 |
<Document targetId="someTargetId"> |
| 61 |
<name>Document.kml</name> |
| 62 |
<visibility>invalid</visibility> |
| 63 |
<open>1</open> |
| 64 |
</Document> |
| 65 |
</kml>""" |
| 66 |
|
| 67 |
d = kml.KML.from_string(doc, strict=False) |
| 68 |
|
| 69 |
assert d.features[0].visibility is None |
| 70 |
assert d.features[0].isopen |
| 71 |
|
| 72 |
def test_container_creation(self) -> None: |
| 73 |
container = containers._Container( |
| 74 |
ns="ns", |
| 75 |
id="id", |
| 76 |
target_id="target_id", |
| 77 |
name="name", |
| 78 |
) |
| 79 |
assert container.ns == "ns" |
| 80 |
assert container.name == "name" |
| 81 |
|
| 82 |
def test_container_feature_append(self) -> None: |
| 83 |
container = containers._Container( |
| 84 |
ns="ns", |
| 85 |
id="id", |
| 86 |
target_id="target_id", |
| 87 |
name="name", |
| 88 |
) |
| 89 |
feature = features._Feature(name="new_feature") |
| 90 |
container.append(feature) |
| 91 |
assert feature in container.features |
| 92 |
with pytest.raises(ValueError, match="Cannot append self"): |
| 93 |
container.append(container) |
| 94 |
|
| 95 |
def test_document_container_get_style_url(self) -> None: |
| 96 |
document = containers.Document( |
| 97 |
name="Document", |
| 98 |
ns="ns", |
| 99 |
style_url=styles.StyleUrl(url="www.styleurl.com"), |
| 100 |
) |
| 101 |
assert document.get_style_by_url(style_url="www.styleurl.com") is None |
| 102 |
|
| 103 |
def test_document_container_get_style_url_id(self) -> None: |
| 104 |
style = styles.Style(id="style-0") |
| 105 |
document = containers.Document( |
| 106 |
name="Document", |
| 107 |
ns="ns", |
| 108 |
styles=[style], |
| 109 |
) |
| 110 |
assert document.get_style_by_url(style_url="#style-0") == style |
| 111 |
|
| 112 |
def test_get_style_by_url(self) -> None: |
| 113 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 114 |
<Document> |
| 115 |
<name>Document.kml</name> |
| 116 |
<open>1</open> |
| 117 |
<Style id="exampleStyleDocument"> |
| 118 |
<LabelStyle> |
| 119 |
<color>ff0000cc</color> |
| 120 |
</LabelStyle> |
| 121 |
</Style> |
| 122 |
<StyleMap id="styleMapExample"> |
| 123 |
<Pair> |
| 124 |
<key>normal</key> |
| 125 |
<styleUrl>#normalState</styleUrl> |
| 126 |
</Pair> |
| 127 |
<Pair> |
| 128 |
<key>highlight</key> |
| 129 |
<styleUrl>#highlightState</styleUrl> |
| 130 |
</Pair> |
| 131 |
</StyleMap> |
| 132 |
<Style id="linestyleExample"> |
| 133 |
<LineStyle> |
| 134 |
<color>7f0000ff</color> |
| 135 |
<width>4</width> |
| 136 |
</LineStyle> |
| 137 |
</Style> |
| 138 |
</Document> |
| 139 |
</kml>""" |
| 140 |
k = kml.KML.from_string(doc) |
| 141 |
assert len(k.features) == 1 |
| 142 |
document = k.features[0] |
| 143 |
|
| 144 |
style0 = document.get_style_by_url( |
| 145 |
"http://localhost:8080/somepath#exampleStyleDocument", |
| 146 |
) |
| 147 |
style1 = document.get_style_by_url("somepath#linestyleExample") |
| 148 |
style2 = document.get_style_by_url("#styleMapExample") |
| 149 |
|
| 150 |
assert isinstance(style0.styles[0], styles.LabelStyle) |
| 151 |
assert style0.id == "exampleStyleDocument" |
| 152 |
assert isinstance(style1.styles[0], styles.LineStyle) |
| 153 |
assert style1.id == "linestyleExample" |
| 154 |
assert isinstance(style2, styles.StyleMap) |
| 155 |
assert style2.id == "styleMapExample" |
| 156 |
|
| 157 |
|
| 158 |
class TestLxml(Lxml, TestStdLibrary): |
| 159 |
"""Test with lxml.""" |