Skip to content

Commit

Permalink
remove from_element, activate skipped tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 25, 2023
1 parent 816c283 commit 1264e2a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 88 deletions.
6 changes: 1 addition & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ repos:
rev: v1.7.0
hooks:
- id: mypy
additional_dependencies: [pygeoif]
additional_dependencies: [pygeoif, arrow]
# - repo: https://github.com/mgedmin/check-manifest
# rev: "0.49"
# hooks:
Expand All @@ -86,8 +86,4 @@ repos:
# rev: v1.0.1
# hooks:
# - id: rst-linter
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v0.910
# hooks:
# - id: mypy
...
30 changes: 0 additions & 30 deletions fastkml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,6 @@ def etree_element(
)
return element

def from_element(self, element: Element) -> None:
"""
Load the KML Object from an Element.
This implementation is deprecated and will be replaced by class_from_element
making it a classmethod.
"""
if f"{self.ns}{self.__name__}" != element.tag:
msg = "Call of abstract base class, subclasses implement this!"
raise TypeError(msg)

def from_string(self, xml_string: str) -> None:
"""
Load the KML Object from serialized xml.
This implementation is deprecated and will be replaced by class_from_string
making it a classmethod.
"""
self.from_element(
cast(Element, config.etree.XML(xml_string)), # type: ignore[attr-defined]
)

def to_string(
self,
*,
Expand Down Expand Up @@ -252,14 +230,6 @@ def etree_element(
element.set("targetId", self.target_id)
return element

def from_element(self, element: Element) -> None:
"""Load the KML Object from an Element."""
super().from_element(element)
if element.get("id"):
self.id = element.get("id")
if element.get("targetId"):
self.target_id = element.get("targetId")

@classmethod
def _get_id(cls, element: Element, strict: bool) -> str:
return element.get("id") or ""
Expand Down
36 changes: 4 additions & 32 deletions tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test the base classes."""
from typing import cast

import pytest

from fastkml import base
from fastkml import config
from fastkml import types
from tests.base import Lxml
from tests.base import StdLibrary

Expand All @@ -48,16 +45,12 @@ def test_to_str_empty_ns(self) -> None:
) == '<test id="id-0" targetId="target-id-0" />'.replace(" ", "")

def test_from_string(self) -> None:
be = base._BaseObject()
be.__name__ = "test"

be.from_string(
xml_string=(
be = base._BaseObject.class_from_string(
string=(
'<kml:test xmlns:kml="http://www.opengis.net/kml/2.2" '
'id="id-0" targetId="target-id-0" />'
),
)

assert be.id == "id-0"
assert be.target_id == "target-id-0"

Expand All @@ -74,24 +67,6 @@ class Test(base._BaseObject):
with pytest.raises(NotImplementedError):
Test().etree_element()

def test_base_from_element_raises(self) -> None:
be = base._BaseObject()
element = cast(
types.Element,
config.etree.Element(config.KMLNS + "Base"), # type: ignore[attr-defined]
)

with pytest.raises(TypeError):
be.from_element(element=element)

def test_base_from_string_raises(self) -> None:
be = base._BaseObject()

with pytest.raises(TypeError):
be.from_string(
'<kml:test xmlns:kml="http://www.opengis.net/kml/2.2" id="id-0" />',
)

def test_base_class_from_string(self) -> None:
be = base._BaseObject.class_from_string('<test id="id-0" targetId="td-00" />')

Expand Down Expand Up @@ -120,11 +95,8 @@ def test_to_string(self) -> None:
)

def test_from_string(self) -> None:
be = base._BaseObject()
be.__name__ = "test"

be.from_string(
xml_string=(
be = base._BaseObject.class_from_string(
string=(
'<kml:test xmlns:kml="http://www.opengis.net/kml/2.2" '
'id="id-0" targetId="target-id-0"/>\n'
),
Expand Down
16 changes: 1 addition & 15 deletions tests/oldunit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@

class TestBaseClasses:
"""
BaseClasses must raise a NotImplementedError on etree_element
and a TypeError on from_element.
BaseClasses must raise a NotImplementedError on etree_element.
"""

def setup_method(self) -> None:
Expand All @@ -62,19 +61,6 @@ def test_base_object(self) -> None:
assert bo.id is None
assert not bo.ns
pytest.raises(NotImplementedError, bo.etree_element)
element = config.etree.Element(f"{config.KMLNS}Base")
pytest.raises(TypeError, bo.from_element)
pytest.raises(TypeError, bo.from_element, element)
bo.__name__ = "NotABaseObject"
pytest.raises(TypeError, bo.from_element, element)
# Note that we can coax baseclasses not to throw errors
bo.__name__ = "Base"
bo.ns = config.KMLNS
bo.from_element(element)
assert bo.id is None
assert bo.ns == config.KMLNS
assert bo.etree_element() is not None
assert len(bo.to_string()) > 1

def test_overlay(self) -> None:
o = overlays._Overlay(name="An Overlay")
Expand Down
20 changes: 14 additions & 6 deletions tests/times_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,18 @@ def test_timespan(self) -> None:
ts.begin = None
pytest.raises(ValueError, ts.to_string)

@pytest.mark.skip(reason="not yet implemented")
def test_feature_timestamp(self) -> None:
now = datetime.datetime.now()
f = kml.Document()
f.time_stamp = KmlDateTime(now)
f._times = kml.TimeStamp(timestamp=KmlDateTime(now))
assert f.time_stamp.dt == now
assert now.isoformat() in str(f.to_string())
assert "TimeStamp>" in str(f.to_string())
assert "when>" in str(f.to_string())
f.time_stamp = KmlDateTime(now.date())
f._times = kml.TimeStamp(timestamp=KmlDateTime(now.date()))
assert now.date().isoformat() in str(f.to_string())
assert now.isoformat() not in str(f.to_string())
f.time_stamp = None
f._times = None
assert "TimeStamp>" not in str(f.to_string())

@pytest.mark.skip(reason="not yet implemented")
Expand Down Expand Up @@ -367,7 +366,6 @@ def test_read_timespan(self) -> None:
assert ts.end.dt == datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc())

def test_featurefromstring(self) -> None:
d = kml.Document(ns="")
doc = """<Document>
<name>Document.kml</name>
<open>1</open>
Expand All @@ -380,7 +378,17 @@ def test_featurefromstring(self) -> None:
</TimeSpan>
</Document>"""

d.from_string(doc)
d = kml.Document.class_from_string(doc, ns="")

assert d.time_stamp.dt == datetime.datetime(
1997,
7,
16,
10,
30,
15,
tzinfo=tzoffset(None, 10800),
)


class TestLxml(Lxml, TestStdLibrary):
Expand Down

0 comments on commit 1264e2a

Please sign in to comment.