From 85ae946b402c2bfb6abd9a973ff4d31d512088e8 Mon Sep 17 00:00:00 2001 From: Kurt Schwehr Date: Sun, 25 Oct 2020 00:25:08 -0700 Subject: [PATCH 1/3] test_link.py: Add more tests and cleanup - Drop RANDOM_BBOX and RANDOM_GEOM as they are not important to the methods under test - Just import datetime. Try to stick to importing just modules. - Create an item in setUp - Add test_minimal first the simplest instance creation call - Add a limited test_relative --- tests/test_link.py | 96 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 10 deletions(-) diff --git a/tests/test_link.py b/tests/test_link.py index f3794fd0d..3f1bd753a 100644 --- a/tests/test_link.py +++ b/tests/test_link.py @@ -1,22 +1,98 @@ -from datetime import datetime +import datetime import unittest import pystac -from tests.utils import (RANDOM_BBOX, RANDOM_GEOM) -TEST_DATETIME = datetime(2020, 3, 14, 16, 32) +TEST_DATETIME = datetime.datetime(2020, 3, 14, 16, 32) class LinkTest(unittest.TestCase): + item: pystac.Item + + def setUp(self): + self.item = pystac.Item(id='test-item', + geometry=None, + bbox=None, + datetime=TEST_DATETIME, + properties={}) + + def test_minimal(self): + rel = 'my rel' + target = 'https://example.com/a/b' + link = pystac.Link(rel, target) + self.assertEqual(target, link.get_href()) + self.assertEqual(target, link.get_absolute_href()) + + expected_repr = f'' + self.assertEqual(expected_repr, link.__repr__()) + + self.assertFalse(link.is_resolved()) + + expected_dict = {'rel': rel, 'href': target} + self.assertEqual(expected_dict, link.to_dict()) + + # Run the same tests on the clone. + clone = link.clone() + # TODO(schwehr): Does link need an __eq__? + self.assertNotEqual(link, clone) + + self.assertEqual(target, clone.get_href()) + self.assertEqual(target, clone.get_absolute_href()) + + self.assertEqual(expected_repr, clone.__repr__()) + + self.assertEqual(expected_dict, clone.to_dict()) + + # Try the modification methods. + self.assertIsNone(link.owner) + link.set_owner(1) # A junk value. + self.assertEqual(1, link.owner) + link.set_owner(None) + self.assertIsNone(link.owner) + + self.assertEqual(pystac.LinkType.ABSOLUTE, link.link_type) + + link.make_absolute() + self.assertEqual(pystac.LinkType.ABSOLUTE, link.link_type) + self.assertEqual(target, link.get_href()) + self.assertEqual(target, link.get_absolute_href()) + + link.make_relative() + self.assertEqual(pystac.LinkType.RELATIVE, link.link_type) + self.assertEqual(target, link.get_href()) + self.assertEqual(target, link.get_absolute_href()) + + link.set_owner(self.item) + self.assertEqual(self.item, link.owner) + # TODO(schwehr): Cannot call link.get_href() after set_owner. + + # TODO(schwehr): Test link.resolve_stac_object() + + def test_relative(self): + rel = 'my rel' + target = '../elsewhere' + mime_type = 'example/stac_thing' + link = pystac.Link(rel, + target, + mime_type, + 'a title', + properties={'a': 'b'}, + link_type=pystac.LinkType.RELATIVE) + expected_dict = { + 'rel': rel, + 'href': target, + 'type': 'example/stac_thing', + 'title': 'a title', + 'a': 'b' + } + self.assertEqual(expected_dict, link.to_dict()) + + self.assertEqual(pystac.LinkType.RELATIVE, link.link_type) + def test_link_does_not_fail_if_href_is_none(self): - """Test to ensure get_href does not fail when the href is None""" + """Test to ensure get_href does not fail when the href is None.""" catalog = pystac.Catalog(id='test', description='test desc') - item = pystac.Item(id='test-item', - geometry=RANDOM_GEOM, - bbox=RANDOM_BBOX, - datetime=datetime.utcnow(), - properties={}) - catalog.add_item(item) + catalog.add_item(self.item) catalog.set_self_href('/some/href') catalog.make_all_links_relative() From 262adfe7555342ab4620d1f5b3ec53be1b8bbea6 Mon Sep 17 00:00:00 2001 From: Kurt Schwehr Date: Mon, 26 Oct 2020 06:16:34 -0700 Subject: [PATCH 2/3] test_link.py: Add more tests (#155). - Add TODO for lines not covered by test_link.py - Add StaticLinkTest class without setUp --- tests/test_link.py | 102 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/tests/test_link.py b/tests/test_link.py index 3f1bd753a..7144067df 100644 --- a/tests/test_link.py +++ b/tests/test_link.py @@ -3,7 +3,7 @@ import pystac -TEST_DATETIME = datetime.datetime(2020, 3, 14, 16, 32) +TEST_DATETIME: datetime.datetime = datetime.datetime(2020, 3, 14, 16, 32) class LinkTest(unittest.TestCase): @@ -98,3 +98,103 @@ def test_link_does_not_fail_if_href_is_none(self): link = catalog.get_single_link('item') self.assertIsNone(link.get_href()) + + # TODO: Test get_href when href is absolute and there is an owner + # TODO: Test get_absolute_href when there is an owner + # TODO: Test when resolve_stac_object on link when target is a str. + + def test_resolve_stac_object_no_root_and_target_is_item(self): + link = pystac.Link('my rel', target=self.item) + link.resolve_stac_object() + + +class StaticLinkTest(unittest.TestCase): + def test_from_dict_round_trip(self): + test_cases = [ + { + 'rel': '', + 'href': '' + }, # Not valid, but works. + { + 'rel': 'r', + 'href': 't' + }, + { + 'rel': 'r', + 'href': '/t' + }, + { + 'rel': 'r', + 'href': 't', + 'type': 'a/b', + 'title': 't', + 'c': 'd', + 1: 2 + }, + # Special case. + { + 'rel': 'self', + 'href': 't' + }, + ] + for d in test_cases: + d2 = pystac.Link.from_dict(d).to_dict() + self.assertEqual(d, d2) + + def test_from_dict_link_type(self): + test_cases = [ + ({ + 'rel': '', + 'href': 'https://a' + }, pystac.LinkType.ABSOLUTE), + ({ + 'rel': '', + 'href': '/a' + }, pystac.LinkType.ABSOLUTE), + ({ + 'rel': '', + 'href': 'a' + }, pystac.LinkType.RELATIVE), + ({ + 'rel': '', + 'href': './a' + }, pystac.LinkType.RELATIVE), + # 'self' is a special case. + ({ + 'rel': 'self', + 'href': 'does not matter' + }, pystac.LinkType.ABSOLUTE), + ] + for case in test_cases: + item = pystac.Link.from_dict(case[0]) + self.assertEqual(case[1], item.link_type) + + def test_from_dict_failures(self): + for d in [{}, {'href': 't'}, {'rel': 'r'}]: + with self.assertRaises(KeyError): + pystac.Link.from_dict(d) + + for d in [ + { + 'rel': '', + 'href': 1 + }, + { + 'rel': '', + 'href': None + }, + ]: + with self.assertRaises(AttributeError): + pystac.Link.from_dict(d) + + def test_collection(self): + c = pystac.Collection('collection id', 'desc', extent=None) + link = pystac.Link.collection(c) + expected = {'rel': 'collection', 'href': None, 'type': 'application/json'} + self.assertEqual(expected, link.to_dict()) + + def test_child(self): + c = pystac.Collection('collection id', 'desc', extent=None) + link = pystac.Link.child(c) + expected = {'rel': 'child', 'href': None, 'type': 'application/json'} + self.assertEqual(expected, link.to_dict()) From df45327e2ea8e880569b56b7190b801ccd6c8444 Mon Sep 17 00:00:00 2001 From: Kurt Schwehr Date: Tue, 27 Oct 2020 10:13:50 -0700 Subject: [PATCH 3/3] test_link.py: Remove TODOs Made https://github.com/stac-utils/pystac/issues/218 to track them. --- tests/test_link.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test_link.py b/tests/test_link.py index 7144067df..a296650c7 100644 --- a/tests/test_link.py +++ b/tests/test_link.py @@ -33,7 +33,6 @@ def test_minimal(self): # Run the same tests on the clone. clone = link.clone() - # TODO(schwehr): Does link need an __eq__? self.assertNotEqual(link, clone) self.assertEqual(target, clone.get_href()) @@ -64,9 +63,6 @@ def test_minimal(self): link.set_owner(self.item) self.assertEqual(self.item, link.owner) - # TODO(schwehr): Cannot call link.get_href() after set_owner. - - # TODO(schwehr): Test link.resolve_stac_object() def test_relative(self): rel = 'my rel' @@ -99,10 +95,6 @@ def test_link_does_not_fail_if_href_is_none(self): link = catalog.get_single_link('item') self.assertIsNone(link.get_href()) - # TODO: Test get_href when href is absolute and there is an owner - # TODO: Test get_absolute_href when there is an owner - # TODO: Test when resolve_stac_object on link when target is a str. - def test_resolve_stac_object_no_root_and_target_is_item(self): link = pystac.Link('my rel', target=self.item) link.resolve_stac_object()