|
1 |
| -from datetime import datetime |
| 1 | +import datetime |
2 | 2 | import unittest
|
3 | 3 |
|
4 | 4 | import pystac
|
5 |
| -from tests.utils import (RANDOM_BBOX, RANDOM_GEOM) |
6 | 5 |
|
7 |
| -TEST_DATETIME = datetime(2020, 3, 14, 16, 32) |
| 6 | +TEST_DATETIME: datetime.datetime = datetime.datetime(2020, 3, 14, 16, 32) |
8 | 7 |
|
9 | 8 |
|
10 | 9 | class LinkTest(unittest.TestCase):
|
| 10 | + item: pystac.Item |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + self.item = pystac.Item(id='test-item', |
| 14 | + geometry=None, |
| 15 | + bbox=None, |
| 16 | + datetime=TEST_DATETIME, |
| 17 | + properties={}) |
| 18 | + |
| 19 | + def test_minimal(self): |
| 20 | + rel = 'my rel' |
| 21 | + target = 'https://example.com/a/b' |
| 22 | + link = pystac.Link(rel, target) |
| 23 | + self.assertEqual(target, link.get_href()) |
| 24 | + self.assertEqual(target, link.get_absolute_href()) |
| 25 | + |
| 26 | + expected_repr = f'<Link rel={rel} target={target}>' |
| 27 | + self.assertEqual(expected_repr, link.__repr__()) |
| 28 | + |
| 29 | + self.assertFalse(link.is_resolved()) |
| 30 | + |
| 31 | + expected_dict = {'rel': rel, 'href': target} |
| 32 | + self.assertEqual(expected_dict, link.to_dict()) |
| 33 | + |
| 34 | + # Run the same tests on the clone. |
| 35 | + clone = link.clone() |
| 36 | + self.assertNotEqual(link, clone) |
| 37 | + |
| 38 | + self.assertEqual(target, clone.get_href()) |
| 39 | + self.assertEqual(target, clone.get_absolute_href()) |
| 40 | + |
| 41 | + self.assertEqual(expected_repr, clone.__repr__()) |
| 42 | + |
| 43 | + self.assertEqual(expected_dict, clone.to_dict()) |
| 44 | + |
| 45 | + # Try the modification methods. |
| 46 | + self.assertIsNone(link.owner) |
| 47 | + link.set_owner(1) # A junk value. |
| 48 | + self.assertEqual(1, link.owner) |
| 49 | + link.set_owner(None) |
| 50 | + self.assertIsNone(link.owner) |
| 51 | + |
| 52 | + self.assertEqual(pystac.LinkType.ABSOLUTE, link.link_type) |
| 53 | + |
| 54 | + link.make_absolute() |
| 55 | + self.assertEqual(pystac.LinkType.ABSOLUTE, link.link_type) |
| 56 | + self.assertEqual(target, link.get_href()) |
| 57 | + self.assertEqual(target, link.get_absolute_href()) |
| 58 | + |
| 59 | + link.make_relative() |
| 60 | + self.assertEqual(pystac.LinkType.RELATIVE, link.link_type) |
| 61 | + self.assertEqual(target, link.get_href()) |
| 62 | + self.assertEqual(target, link.get_absolute_href()) |
| 63 | + |
| 64 | + link.set_owner(self.item) |
| 65 | + self.assertEqual(self.item, link.owner) |
| 66 | + |
| 67 | + def test_relative(self): |
| 68 | + rel = 'my rel' |
| 69 | + target = '../elsewhere' |
| 70 | + mime_type = 'example/stac_thing' |
| 71 | + link = pystac.Link(rel, |
| 72 | + target, |
| 73 | + mime_type, |
| 74 | + 'a title', |
| 75 | + properties={'a': 'b'}, |
| 76 | + link_type=pystac.LinkType.RELATIVE) |
| 77 | + expected_dict = { |
| 78 | + 'rel': rel, |
| 79 | + 'href': target, |
| 80 | + 'type': 'example/stac_thing', |
| 81 | + 'title': 'a title', |
| 82 | + 'a': 'b' |
| 83 | + } |
| 84 | + self.assertEqual(expected_dict, link.to_dict()) |
| 85 | + |
| 86 | + self.assertEqual(pystac.LinkType.RELATIVE, link.link_type) |
| 87 | + |
11 | 88 | def test_link_does_not_fail_if_href_is_none(self):
|
12 |
| - """Test to ensure get_href does not fail when the href is None""" |
| 89 | + """Test to ensure get_href does not fail when the href is None.""" |
13 | 90 | catalog = pystac.Catalog(id='test', description='test desc')
|
14 |
| - item = pystac.Item(id='test-item', |
15 |
| - geometry=RANDOM_GEOM, |
16 |
| - bbox=RANDOM_BBOX, |
17 |
| - datetime=datetime.utcnow(), |
18 |
| - properties={}) |
19 |
| - catalog.add_item(item) |
| 91 | + catalog.add_item(self.item) |
20 | 92 | catalog.set_self_href('/some/href')
|
21 | 93 | catalog.make_all_links_relative()
|
22 | 94 |
|
23 | 95 | link = catalog.get_single_link('item')
|
24 | 96 | self.assertIsNone(link.get_href())
|
| 97 | + |
| 98 | + def test_resolve_stac_object_no_root_and_target_is_item(self): |
| 99 | + link = pystac.Link('my rel', target=self.item) |
| 100 | + link.resolve_stac_object() |
| 101 | + |
| 102 | + |
| 103 | +class StaticLinkTest(unittest.TestCase): |
| 104 | + def test_from_dict_round_trip(self): |
| 105 | + test_cases = [ |
| 106 | + { |
| 107 | + 'rel': '', |
| 108 | + 'href': '' |
| 109 | + }, # Not valid, but works. |
| 110 | + { |
| 111 | + 'rel': 'r', |
| 112 | + 'href': 't' |
| 113 | + }, |
| 114 | + { |
| 115 | + 'rel': 'r', |
| 116 | + 'href': '/t' |
| 117 | + }, |
| 118 | + { |
| 119 | + 'rel': 'r', |
| 120 | + 'href': 't', |
| 121 | + 'type': 'a/b', |
| 122 | + 'title': 't', |
| 123 | + 'c': 'd', |
| 124 | + 1: 2 |
| 125 | + }, |
| 126 | + # Special case. |
| 127 | + { |
| 128 | + 'rel': 'self', |
| 129 | + 'href': 't' |
| 130 | + }, |
| 131 | + ] |
| 132 | + for d in test_cases: |
| 133 | + d2 = pystac.Link.from_dict(d).to_dict() |
| 134 | + self.assertEqual(d, d2) |
| 135 | + |
| 136 | + def test_from_dict_link_type(self): |
| 137 | + test_cases = [ |
| 138 | + ({ |
| 139 | + 'rel': '', |
| 140 | + 'href': 'https://a' |
| 141 | + }, pystac.LinkType.ABSOLUTE), |
| 142 | + ({ |
| 143 | + 'rel': '', |
| 144 | + 'href': '/a' |
| 145 | + }, pystac.LinkType.ABSOLUTE), |
| 146 | + ({ |
| 147 | + 'rel': '', |
| 148 | + 'href': 'a' |
| 149 | + }, pystac.LinkType.RELATIVE), |
| 150 | + ({ |
| 151 | + 'rel': '', |
| 152 | + 'href': './a' |
| 153 | + }, pystac.LinkType.RELATIVE), |
| 154 | + # 'self' is a special case. |
| 155 | + ({ |
| 156 | + 'rel': 'self', |
| 157 | + 'href': 'does not matter' |
| 158 | + }, pystac.LinkType.ABSOLUTE), |
| 159 | + ] |
| 160 | + for case in test_cases: |
| 161 | + item = pystac.Link.from_dict(case[0]) |
| 162 | + self.assertEqual(case[1], item.link_type) |
| 163 | + |
| 164 | + def test_from_dict_failures(self): |
| 165 | + for d in [{}, {'href': 't'}, {'rel': 'r'}]: |
| 166 | + with self.assertRaises(KeyError): |
| 167 | + pystac.Link.from_dict(d) |
| 168 | + |
| 169 | + for d in [ |
| 170 | + { |
| 171 | + 'rel': '', |
| 172 | + 'href': 1 |
| 173 | + }, |
| 174 | + { |
| 175 | + 'rel': '', |
| 176 | + 'href': None |
| 177 | + }, |
| 178 | + ]: |
| 179 | + with self.assertRaises(AttributeError): |
| 180 | + pystac.Link.from_dict(d) |
| 181 | + |
| 182 | + def test_collection(self): |
| 183 | + c = pystac.Collection('collection id', 'desc', extent=None) |
| 184 | + link = pystac.Link.collection(c) |
| 185 | + expected = {'rel': 'collection', 'href': None, 'type': 'application/json'} |
| 186 | + self.assertEqual(expected, link.to_dict()) |
| 187 | + |
| 188 | + def test_child(self): |
| 189 | + c = pystac.Collection('collection id', 'desc', extent=None) |
| 190 | + link = pystac.Link.child(c) |
| 191 | + expected = {'rel': 'child', 'href': None, 'type': 'application/json'} |
| 192 | + self.assertEqual(expected, link.to_dict()) |
0 commit comments