|
11 | 11 | from tests.utils import TestCases
|
12 | 12 |
|
13 | 13 |
|
14 |
| -class StacIOTest(unittest.TestCase): |
15 |
| - def setUp(self) -> None: |
16 |
| - self.stac_io = StacIO.default() |
17 |
| - |
18 |
| - def test_read_write_collection(self) -> None: |
19 |
| - collection = pystac.read_file( |
20 |
| - TestCases.get_path("data-files/collections/multi-extent.json") |
21 |
| - ) |
22 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
23 |
| - dest_href = os.path.join(tmp_dir, "collection.json") |
24 |
| - pystac.write_file(collection, dest_href=dest_href) |
25 |
| - self.assertTrue(os.path.exists(dest_href), msg="File was not written.") |
26 |
| - |
27 |
| - def test_read_write_collection_with_file_protocol(self) -> None: |
28 |
| - collection = pystac.read_file( |
29 |
| - "file://" + TestCases.get_path("data-files/collections/multi-extent.json") |
30 |
| - ) |
31 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
32 |
| - dest_href = os.path.join(tmp_dir, "collection.json") |
33 |
| - pystac.write_file(collection, dest_href="file://" + dest_href) |
34 |
| - self.assertTrue(os.path.exists(dest_href), msg="File was not written.") |
35 |
| - |
36 |
| - def test_read_item(self) -> None: |
37 |
| - item = pystac.read_file(TestCases.get_path("data-files/item/sample-item.json")) |
38 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
39 |
| - dest_href = os.path.join(tmp_dir, "item.json") |
40 |
| - pystac.write_file(item, dest_href=dest_href) |
41 |
| - self.assertTrue(os.path.exists(dest_href), msg="File was not written.") |
42 |
| - |
43 |
| - def test_read_write_catalog(self) -> None: |
44 |
| - catalog = pystac.read_file( |
45 |
| - TestCases.get_path("data-files/catalogs/test-case-1/catalog.json") |
46 |
| - ) |
47 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
48 |
| - dest_href = os.path.join(tmp_dir, "catalog.json") |
49 |
| - pystac.write_file(catalog, dest_href=dest_href) |
50 |
| - self.assertTrue(os.path.exists(dest_href), msg="File was not written.") |
51 |
| - |
52 |
| - def test_read_item_collection_raises_exception(self) -> None: |
53 |
| - with self.assertRaises(pystac.STACTypeError): |
54 |
| - _ = pystac.read_file( |
55 |
| - TestCases.get_path( |
56 |
| - "data-files/item-collection/sample-item-collection.json" |
57 |
| - ) |
58 |
| - ) |
59 |
| - |
60 |
| - def test_read_item_dict(self) -> None: |
61 |
| - item_dict = self.stac_io.read_json( |
62 |
| - TestCases.get_path("data-files/item/sample-item.json") |
| 14 | +def test_read_write_collection() -> None: |
| 15 | + collection = pystac.read_file( |
| 16 | + TestCases.get_path("data-files/collections/multi-extent.json") |
| 17 | + ) |
| 18 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 19 | + dest_href = os.path.join(tmp_dir, "collection.json") |
| 20 | + pystac.write_file(collection, dest_href=dest_href) |
| 21 | + assert os.path.exists(dest_href), "File was not written." |
| 22 | + |
| 23 | + |
| 24 | +def test_read_write_collection_with_file_protocol() -> None: |
| 25 | + collection = pystac.read_file( |
| 26 | + "file://" + TestCases.get_path("data-files/collections/multi-extent.json") |
| 27 | + ) |
| 28 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 29 | + dest_href = os.path.join(tmp_dir, "collection.json") |
| 30 | + pystac.write_file(collection, dest_href="file://" + dest_href) |
| 31 | + assert os.path.exists(dest_href), "File was not written." |
| 32 | + |
| 33 | + |
| 34 | +def test_read_item() -> None: |
| 35 | + item = pystac.read_file(TestCases.get_path("data-files/item/sample-item.json")) |
| 36 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 37 | + dest_href = os.path.join(tmp_dir, "item.json") |
| 38 | + pystac.write_file(item, dest_href=dest_href) |
| 39 | + assert os.path.exists(dest_href), "File was not written." |
| 40 | + |
| 41 | + |
| 42 | +def test_read_write_catalog() -> None: |
| 43 | + catalog = pystac.read_file( |
| 44 | + TestCases.get_path("data-files/catalogs/test-case-1/catalog.json") |
| 45 | + ) |
| 46 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 47 | + dest_href = os.path.join(tmp_dir, "catalog.json") |
| 48 | + pystac.write_file(catalog, dest_href=dest_href) |
| 49 | + assert os.path.exists(dest_href), "File was not written." |
| 50 | + |
| 51 | + |
| 52 | +def test_read_item_collection_raises_exception() -> None: |
| 53 | + with pytest.raises(pystac.STACTypeError): |
| 54 | + _ = pystac.read_file( |
| 55 | + TestCases.get_path("data-files/item-collection/sample-item-collection.json") |
63 | 56 | )
|
64 |
| - item = pystac.read_dict(item_dict) |
65 |
| - self.assertIsInstance(item, pystac.Item) |
66 | 57 |
|
67 |
| - def test_read_collection_dict(self) -> None: |
68 |
| - collection_dict = self.stac_io.read_json( |
69 |
| - TestCases.get_path("data-files/collections/multi-extent.json") |
70 |
| - ) |
71 |
| - collection = pystac.read_dict(collection_dict) |
72 |
| - self.assertIsInstance(collection, pystac.Collection) |
73 |
| - |
74 |
| - def test_read_catalog_dict(self) -> None: |
75 |
| - catalog_dict = self.stac_io.read_json( |
76 |
| - TestCases.get_path("data-files/catalogs/test-case-1/catalog.json") |
77 |
| - ) |
78 |
| - catalog = pystac.read_dict(catalog_dict) |
79 |
| - self.assertIsInstance(catalog, pystac.Catalog) |
80 | 58 |
|
81 |
| - def test_read_from_stac_object(self) -> None: |
82 |
| - catalog = pystac.STACObject.from_file( |
83 |
| - TestCases.get_path("data-files/catalogs/test-case-1/catalog.json") |
84 |
| - ) |
85 |
| - self.assertIsInstance(catalog, pystac.Catalog) |
86 |
| - |
87 |
| - def test_report_duplicate_keys(self) -> None: |
88 |
| - # Directly from dict |
89 |
| - class ReportingStacIO(DefaultStacIO, DuplicateKeyReportingMixin): |
90 |
| - pass |
91 |
| - |
92 |
| - stac_io = ReportingStacIO() |
93 |
| - test_json = """{ |
94 |
| - "key": "value_1", |
95 |
| - "key": "value_2" |
96 |
| - }""" |
97 |
| - |
98 |
| - with self.assertRaises(pystac.DuplicateObjectKeyError) as excinfo: |
99 |
| - stac_io.json_loads(test_json) |
100 |
| - self.assertEqual(str(excinfo.exception), 'Found duplicate object name "key"') |
101 |
| - |
102 |
| - # From file |
103 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
104 |
| - src_href = os.path.join(tmp_dir, "test.json") |
105 |
| - with open(src_href, "w") as dst: |
106 |
| - dst.write(test_json) |
107 |
| - |
108 |
| - with self.assertRaises(pystac.DuplicateObjectKeyError) as excinfo: |
109 |
| - stac_io.read_json(src_href) |
110 |
| - self.assertEqual( |
111 |
| - str(excinfo.exception), |
112 |
| - f'Found duplicate object name "key" in {src_href}', |
113 |
| - ) |
114 |
| - |
115 |
| - @unittest.mock.patch("pystac.stac_io.urlopen") |
116 |
| - def test_headers_stac_io(self, urlopen_mock: unittest.mock.MagicMock) -> None: |
117 |
| - stac_io = DefaultStacIO(headers={"Authorization": "api-key fake-api-key-value"}) |
118 |
| - |
119 |
| - catalog = pystac.Catalog("an-id", "a description").to_dict() |
120 |
| - # required until https://github.com/stac-utils/pystac/pull/896 is merged |
121 |
| - catalog["links"] = [] |
122 |
| - urlopen_mock.return_value.__enter__.return_value.read.return_value = json.dumps( |
123 |
| - catalog |
124 |
| - ).encode("utf-8") |
125 |
| - pystac.Catalog.from_file("https://example.com/catalog.json", stac_io=stac_io) |
126 |
| - |
127 |
| - request_obj = urlopen_mock.call_args[0][0] |
128 |
| - self.assertEqual(request_obj.headers, stac_io.headers) |
| 59 | +def test_read_item_dict() -> None: |
| 60 | + stac_io = StacIO.default() |
| 61 | + item_dict = stac_io.read_json( |
| 62 | + TestCases.get_path("data-files/item/sample-item.json") |
| 63 | + ) |
| 64 | + item = pystac.read_dict(item_dict) |
| 65 | + assert isinstance(item, pystac.Item) |
| 66 | + |
| 67 | + |
| 68 | +def test_read_collection_dict() -> None: |
| 69 | + stac_io = StacIO.default() |
| 70 | + collection_dict = stac_io.read_json( |
| 71 | + TestCases.get_path("data-files/collections/multi-extent.json") |
| 72 | + ) |
| 73 | + collection = pystac.read_dict(collection_dict) |
| 74 | + assert isinstance(collection, pystac.Collection) |
| 75 | + |
| 76 | + |
| 77 | +def test_read_catalog_dict() -> None: |
| 78 | + stac_io = StacIO.default() |
| 79 | + catalog_dict = stac_io.read_json( |
| 80 | + TestCases.get_path("data-files/catalogs/test-case-1/catalog.json") |
| 81 | + ) |
| 82 | + catalog = pystac.read_dict(catalog_dict) |
| 83 | + assert isinstance(catalog, pystac.Catalog) |
| 84 | + |
| 85 | + |
| 86 | +def test_read_from_stac_object() -> None: |
| 87 | + catalog = pystac.STACObject.from_file( |
| 88 | + TestCases.get_path("data-files/catalogs/test-case-1/catalog.json") |
| 89 | + ) |
| 90 | + assert isinstance(catalog, pystac.Catalog) |
| 91 | + |
| 92 | + |
| 93 | +def test_report_duplicate_keys() -> None: |
| 94 | + # Directly from dict |
| 95 | + class ReportingStacIO(DefaultStacIO, DuplicateKeyReportingMixin): |
| 96 | + pass |
| 97 | + |
| 98 | + stac_io = ReportingStacIO() |
| 99 | + test_json = """{ |
| 100 | + "key": "value_1", |
| 101 | + "key": "value_2" |
| 102 | + }""" |
| 103 | + |
| 104 | + with pytest.raises(pystac.DuplicateObjectKeyError) as excinfo: |
| 105 | + stac_io.json_loads(test_json) |
| 106 | + assert str(excinfo.value) == 'Found duplicate object name "key"' |
| 107 | + |
| 108 | + # From file |
| 109 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 110 | + src_href = os.path.join(tmp_dir, "test.json") |
| 111 | + with open(src_href, "w") as dst: |
| 112 | + dst.write(test_json) |
| 113 | + |
| 114 | + with pytest.raises(pystac.DuplicateObjectKeyError) as excinfo: |
| 115 | + stac_io.read_json(src_href) |
| 116 | + assert str(excinfo.value), f'Found duplicate object name "key" in {src_href}' |
| 117 | + |
| 118 | + |
| 119 | +@unittest.mock.patch("pystac.stac_io.urlopen") |
| 120 | +def test_headers_stac_io(urlopen_mock: unittest.mock.MagicMock) -> None: |
| 121 | + stac_io = DefaultStacIO(headers={"Authorization": "api-key fake-api-key-value"}) |
| 122 | + |
| 123 | + catalog = pystac.Catalog("an-id", "a description").to_dict() |
| 124 | + # required until https://github.com/stac-utils/pystac/pull/896 is merged |
| 125 | + catalog["links"] = [] |
| 126 | + urlopen_mock.return_value.__enter__.return_value.read.return_value = json.dumps( |
| 127 | + catalog |
| 128 | + ).encode("utf-8") |
| 129 | + pystac.Catalog.from_file("https://example.com/catalog.json", stac_io=stac_io) |
| 130 | + |
| 131 | + request_obj = urlopen_mock.call_args[0][0] |
| 132 | + assert request_obj.headers == stac_io.headers |
129 | 133 |
|
130 | 134 |
|
131 | 135 | @pytest.mark.vcr()
|
|
0 commit comments