1
1
import os
2
2
import json
3
- from tempfile import TemporaryDirectory
3
+ import tempfile
4
4
from typing import Any , Dict , List , Tuple , Union , cast
5
5
import unittest
6
6
from datetime import datetime
29
29
class CatalogTypeTest (unittest .TestCase ):
30
30
def test_determine_type_for_absolute_published (self ) -> None :
31
31
cat = TestCases .test_case_1 ()
32
- with TemporaryDirectory () as tmp_dir :
32
+ with tempfile . TemporaryDirectory () as tmp_dir :
33
33
cat .normalize_and_save (tmp_dir , catalog_type = CatalogType .ABSOLUTE_PUBLISHED )
34
34
cat_json = pystac .StacIO .default ().read_json (
35
35
os .path .join (tmp_dir , "catalog.json" )
@@ -40,7 +40,7 @@ def test_determine_type_for_absolute_published(self) -> None:
40
40
41
41
def test_determine_type_for_relative_published (self ) -> None :
42
42
cat = TestCases .test_case_2 ()
43
- with TemporaryDirectory () as tmp_dir :
43
+ with tempfile . TemporaryDirectory () as tmp_dir :
44
44
cat .normalize_and_save (tmp_dir , catalog_type = CatalogType .RELATIVE_PUBLISHED )
45
45
cat_json = pystac .StacIO .default ().read_json (
46
46
os .path .join (tmp_dir , "catalog.json" )
@@ -68,7 +68,7 @@ def test_determine_type_for_unknown(self) -> None:
68
68
69
69
class CatalogTest (unittest .TestCase ):
70
70
def test_create_and_read (self ) -> None :
71
- with TemporaryDirectory () as tmp_dir :
71
+ with tempfile . TemporaryDirectory () as tmp_dir :
72
72
cat_dir = os .path .join (tmp_dir , "catalog" )
73
73
catalog = TestCases .test_case_1 ()
74
74
@@ -288,7 +288,7 @@ def test_clone_generates_correct_links(self) -> None:
288
288
def test_save_uses_previous_catalog_type (self ) -> None :
289
289
catalog = TestCases .test_case_1 ()
290
290
assert catalog .catalog_type == CatalogType .SELF_CONTAINED
291
- with TemporaryDirectory () as tmp_dir :
291
+ with tempfile . TemporaryDirectory () as tmp_dir :
292
292
catalog .normalize_hrefs (tmp_dir )
293
293
href = catalog .self_href
294
294
catalog .save ()
@@ -365,7 +365,7 @@ def test_generate_subcatalogs_does_not_change_item_count(self) -> None:
365
365
366
366
catalog .generate_subcatalogs ("${year}/${day}" )
367
367
368
- with TemporaryDirectory () as tmp_dir :
368
+ with tempfile . TemporaryDirectory () as tmp_dir :
369
369
catalog .normalize_hrefs (tmp_dir )
370
370
catalog .save (pystac .CatalogType .SELF_CONTAINED )
371
371
@@ -494,7 +494,7 @@ def item_mapper(item: pystac.Item) -> pystac.Item:
494
494
item .properties ["ITEM_MAPPER" ] = "YEP"
495
495
return item
496
496
497
- with TemporaryDirectory () as tmp_dir :
497
+ with tempfile . TemporaryDirectory () as tmp_dir :
498
498
catalog = TestCases .test_case_1 ()
499
499
500
500
new_cat = catalog .map_items (item_mapper )
@@ -518,7 +518,7 @@ def item_mapper(item: pystac.Item) -> List[pystac.Item]:
518
518
item2 .properties ["ITEM_MAPPER_2" ] = "YEP"
519
519
return [item , item2 ]
520
520
521
- with TemporaryDirectory () as tmp_dir :
521
+ with tempfile . TemporaryDirectory () as tmp_dir :
522
522
catalog = TestCases .test_case_1 ()
523
523
catalog_items = catalog .get_all_items ()
524
524
@@ -623,7 +623,7 @@ def asset_mapper(key: str, asset: pystac.Asset) -> pystac.Asset:
623
623
624
624
return asset
625
625
626
- with TemporaryDirectory () as tmp_dir :
626
+ with tempfile . TemporaryDirectory () as tmp_dir :
627
627
catalog = TestCases .test_case_2 ()
628
628
629
629
new_cat = catalog .map_assets (asset_mapper )
@@ -656,7 +656,7 @@ def asset_mapper(
656
656
else :
657
657
return asset
658
658
659
- with TemporaryDirectory () as tmp_dir :
659
+ with tempfile . TemporaryDirectory () as tmp_dir :
660
660
catalog = TestCases .test_case_2 ()
661
661
662
662
new_cat = catalog .map_assets (asset_mapper )
@@ -696,7 +696,7 @@ def asset_mapper(
696
696
else :
697
697
return asset
698
698
699
- with TemporaryDirectory () as tmp_dir :
699
+ with tempfile . TemporaryDirectory () as tmp_dir :
700
700
catalog = TestCases .test_case_2 ()
701
701
702
702
new_cat = catalog .map_assets (asset_mapper )
@@ -771,7 +771,7 @@ def check_all_absolute(cat: Catalog) -> None:
771
771
test_cases = TestCases .all_test_catalogs ()
772
772
773
773
for catalog in test_cases :
774
- with TemporaryDirectory () as tmp_dir :
774
+ with tempfile . TemporaryDirectory () as tmp_dir :
775
775
c2 = catalog .full_copy ()
776
776
c2 .normalize_hrefs (tmp_dir )
777
777
c2 .catalog_type = CatalogType .RELATIVE_PUBLISHED
@@ -797,7 +797,7 @@ def test_extra_fields(self) -> None:
797
797
798
798
catalog .extra_fields ["custom_field" ] = "Special content"
799
799
800
- with TemporaryDirectory () as tmp_dir :
800
+ with tempfile . TemporaryDirectory () as tmp_dir :
801
801
p = os .path .join (tmp_dir , "catalog.json" )
802
802
catalog .save_object (include_self_link = False , dest_href = p )
803
803
with open (p ) as f :
@@ -822,7 +822,7 @@ def test_validate_all(self) -> None:
822
822
item = cat .get_item ("area-1-1-labels" , recursive = True )
823
823
assert item is not None
824
824
item .geometry = {"type" : "INVALID" , "coordinates" : "NONE" }
825
- with TemporaryDirectory () as tmp_dir :
825
+ with tempfile . TemporaryDirectory () as tmp_dir :
826
826
cat .normalize_hrefs (tmp_dir )
827
827
cat .save (catalog_type = pystac .CatalogType .SELF_CONTAINED )
828
828
@@ -843,7 +843,7 @@ def test_set_hrefs_manually(self) -> None:
843
843
year += 1
844
844
month += 1
845
845
846
- with TemporaryDirectory () as tmp_dir :
846
+ with tempfile . TemporaryDirectory () as tmp_dir :
847
847
for root , _ , items in catalog .walk ():
848
848
849
849
# Set root's HREF based off the parent
@@ -933,7 +933,7 @@ def test_reading_iterating_and_writing_works_as_expected(self) -> None:
933
933
for item in cat .get_all_items ():
934
934
pass
935
935
936
- with TemporaryDirectory () as tmp_dir :
936
+ with tempfile . TemporaryDirectory () as tmp_dir :
937
937
new_stac_uri = os .path .join (tmp_dir , "test-case-6" )
938
938
cat .normalize_hrefs (new_stac_uri )
939
939
cat .save (catalog_type = CatalogType .SELF_CONTAINED )
@@ -1003,7 +1003,7 @@ def check_catalog(self, c: Catalog, tag: str) -> None:
1003
1003
self .check_item (item , tag )
1004
1004
1005
1005
def test_full_copy_1 (self ) -> None :
1006
- with TemporaryDirectory () as tmp_dir :
1006
+ with tempfile . TemporaryDirectory () as tmp_dir :
1007
1007
cat = Catalog (id = "test" , description = "test catalog" )
1008
1008
1009
1009
item = Item (
@@ -1024,7 +1024,7 @@ def test_full_copy_1(self) -> None:
1024
1024
self .check_catalog (cat2 , "dest" )
1025
1025
1026
1026
def test_full_copy_2 (self ) -> None :
1027
- with TemporaryDirectory () as tmp_dir :
1027
+ with tempfile . TemporaryDirectory () as tmp_dir :
1028
1028
cat = Catalog (id = "test" , description = "test catalog" )
1029
1029
image_item = Item (
1030
1030
id = "Imagery" ,
@@ -1071,7 +1071,7 @@ def test_full_copy_2(self) -> None:
1071
1071
self .check_catalog (cat2 , "dest" )
1072
1072
1073
1073
def test_full_copy_3 (self ) -> None :
1074
- with TemporaryDirectory () as tmp_dir :
1074
+ with tempfile . TemporaryDirectory () as tmp_dir :
1075
1075
root_cat = TestCases .test_case_1 ()
1076
1076
root_cat .normalize_hrefs (
1077
1077
os .path .join (tmp_dir , "catalog-full-copy-3-source" )
@@ -1085,7 +1085,7 @@ def test_full_copy_3(self) -> None:
1085
1085
self .check_catalog (cat2 , "dest" )
1086
1086
1087
1087
def test_full_copy_4 (self ) -> None :
1088
- with TemporaryDirectory () as tmp_dir :
1088
+ with tempfile . TemporaryDirectory () as tmp_dir :
1089
1089
root_cat = TestCases .test_case_2 ()
1090
1090
root_cat .normalize_hrefs (
1091
1091
os .path .join (tmp_dir , "catalog-full-copy-4-source" )
0 commit comments