2
2
import json
3
3
from typing import Any , Dict , List , Tuple , Union , cast
4
4
import unittest
5
- from tempfile import TemporaryDirectory
6
5
from datetime import datetime
7
6
from collections import defaultdict
8
7
18
17
)
19
18
from pystac .extensions .label import LabelClasses , LabelExtension , LabelType
20
19
from pystac .utils import is_absolute_href , join_path_or_url , JoinType
21
- from tests .utils import TestCases , ARBITRARY_GEOM , ARBITRARY_BBOX , MockStacIO
20
+ from tests .utils import (
21
+ TestCases ,
22
+ ARBITRARY_GEOM ,
23
+ ARBITRARY_BBOX ,
24
+ MockStacIO ,
25
+ TemporaryDirectory ,
26
+ )
22
27
23
28
24
29
class CatalogTypeTest (unittest .TestCase ):
25
30
def test_determine_type_for_absolute_published (self ) -> None :
26
31
cat = TestCases .test_case_1 ()
27
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
32
+ with TemporaryDirectory () as tmp_dir :
28
33
cat .normalize_and_save (tmp_dir , catalog_type = CatalogType .ABSOLUTE_PUBLISHED )
29
34
cat_json = pystac .StacIO .default ().read_json (
30
35
os .path .join (tmp_dir , "catalog.json" )
@@ -35,7 +40,7 @@ def test_determine_type_for_absolute_published(self) -> None:
35
40
36
41
def test_determine_type_for_relative_published (self ) -> None :
37
42
cat = TestCases .test_case_2 ()
38
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
43
+ with TemporaryDirectory () as tmp_dir :
39
44
cat .normalize_and_save (tmp_dir , catalog_type = CatalogType .RELATIVE_PUBLISHED )
40
45
cat_json = pystac .StacIO .default ().read_json (
41
46
os .path .join (tmp_dir , "catalog.json" )
@@ -63,7 +68,7 @@ def test_determine_type_for_unknown(self) -> None:
63
68
64
69
class CatalogTest (unittest .TestCase ):
65
70
def test_create_and_read (self ) -> None :
66
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
71
+ with TemporaryDirectory () as tmp_dir :
67
72
cat_dir = os .path .join (tmp_dir , "catalog" )
68
73
catalog = TestCases .test_case_1 ()
69
74
@@ -283,7 +288,7 @@ def test_clone_generates_correct_links(self) -> None:
283
288
def test_save_uses_previous_catalog_type (self ) -> None :
284
289
catalog = TestCases .test_case_1 ()
285
290
assert catalog .catalog_type == CatalogType .SELF_CONTAINED
286
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
291
+ with TemporaryDirectory () as tmp_dir :
287
292
catalog .normalize_hrefs (tmp_dir )
288
293
href = catalog .self_href
289
294
catalog .save ()
@@ -360,7 +365,7 @@ def test_generate_subcatalogs_does_not_change_item_count(self) -> None:
360
365
361
366
catalog .generate_subcatalogs ("${year}/${day}" )
362
367
363
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
368
+ with TemporaryDirectory () as tmp_dir :
364
369
catalog .normalize_hrefs (tmp_dir )
365
370
catalog .save (pystac .CatalogType .SELF_CONTAINED )
366
371
@@ -489,7 +494,7 @@ def item_mapper(item: pystac.Item) -> pystac.Item:
489
494
item .properties ["ITEM_MAPPER" ] = "YEP"
490
495
return item
491
496
492
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
497
+ with TemporaryDirectory () as tmp_dir :
493
498
catalog = TestCases .test_case_1 ()
494
499
495
500
new_cat = catalog .map_items (item_mapper )
@@ -513,7 +518,7 @@ def item_mapper(item: pystac.Item) -> List[pystac.Item]:
513
518
item2 .properties ["ITEM_MAPPER_2" ] = "YEP"
514
519
return [item , item2 ]
515
520
516
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
521
+ with TemporaryDirectory () as tmp_dir :
517
522
catalog = TestCases .test_case_1 ()
518
523
catalog_items = catalog .get_all_items ()
519
524
@@ -618,7 +623,7 @@ def asset_mapper(key: str, asset: pystac.Asset) -> pystac.Asset:
618
623
619
624
return asset
620
625
621
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
626
+ with TemporaryDirectory () as tmp_dir :
622
627
catalog = TestCases .test_case_2 ()
623
628
624
629
new_cat = catalog .map_assets (asset_mapper )
@@ -651,7 +656,7 @@ def asset_mapper(
651
656
else :
652
657
return asset
653
658
654
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
659
+ with TemporaryDirectory () as tmp_dir :
655
660
catalog = TestCases .test_case_2 ()
656
661
657
662
new_cat = catalog .map_assets (asset_mapper )
@@ -691,7 +696,7 @@ def asset_mapper(
691
696
else :
692
697
return asset
693
698
694
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
699
+ with TemporaryDirectory () as tmp_dir :
695
700
catalog = TestCases .test_case_2 ()
696
701
697
702
new_cat = catalog .map_assets (asset_mapper )
@@ -766,7 +771,7 @@ def check_all_absolute(cat: Catalog) -> None:
766
771
test_cases = TestCases .all_test_catalogs ()
767
772
768
773
for catalog in test_cases :
769
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
774
+ with TemporaryDirectory () as tmp_dir :
770
775
c2 = catalog .full_copy ()
771
776
c2 .normalize_hrefs (tmp_dir )
772
777
c2 .catalog_type = CatalogType .RELATIVE_PUBLISHED
@@ -792,7 +797,7 @@ def test_extra_fields(self) -> None:
792
797
793
798
catalog .extra_fields ["type" ] = "FeatureCollection"
794
799
795
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
800
+ with TemporaryDirectory () as tmp_dir :
796
801
p = os .path .join (tmp_dir , "catalog.json" )
797
802
catalog .save_object (include_self_link = False , dest_href = p )
798
803
with open (p ) as f :
@@ -817,7 +822,7 @@ def test_validate_all(self) -> None:
817
822
item = cat .get_item ("area-1-1-labels" , recursive = True )
818
823
assert item is not None
819
824
item .geometry = {"type" : "INVALID" , "coordinates" : "NONE" }
820
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
825
+ with TemporaryDirectory () as tmp_dir :
821
826
cat .normalize_hrefs (tmp_dir )
822
827
cat .save (catalog_type = pystac .CatalogType .SELF_CONTAINED )
823
828
@@ -838,7 +843,7 @@ def test_set_hrefs_manually(self) -> None:
838
843
year += 1
839
844
month += 1
840
845
841
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
846
+ with TemporaryDirectory () as tmp_dir :
842
847
for root , _ , items in catalog .walk ():
843
848
844
849
# Set root's HREF based off the parent
@@ -928,7 +933,7 @@ def test_reading_iterating_and_writing_works_as_expected(self) -> None:
928
933
for item in cat .get_all_items ():
929
934
pass
930
935
931
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
936
+ with TemporaryDirectory () as tmp_dir :
932
937
new_stac_uri = os .path .join (tmp_dir , "test-case-6" )
933
938
cat .normalize_hrefs (new_stac_uri )
934
939
cat .save (catalog_type = CatalogType .SELF_CONTAINED )
@@ -998,7 +1003,7 @@ def check_catalog(self, c: Catalog, tag: str) -> None:
998
1003
self .check_item (item , tag )
999
1004
1000
1005
def test_full_copy_1 (self ) -> None :
1001
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
1006
+ with TemporaryDirectory () as tmp_dir :
1002
1007
cat = Catalog (id = "test" , description = "test catalog" )
1003
1008
1004
1009
item = Item (
@@ -1019,7 +1024,7 @@ def test_full_copy_1(self) -> None:
1019
1024
self .check_catalog (cat2 , "dest" )
1020
1025
1021
1026
def test_full_copy_2 (self ) -> None :
1022
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
1027
+ with TemporaryDirectory () as tmp_dir :
1023
1028
cat = Catalog (id = "test" , description = "test catalog" )
1024
1029
image_item = Item (
1025
1030
id = "Imagery" ,
@@ -1066,7 +1071,7 @@ def test_full_copy_2(self) -> None:
1066
1071
self .check_catalog (cat2 , "dest" )
1067
1072
1068
1073
def test_full_copy_3 (self ) -> None :
1069
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
1074
+ with TemporaryDirectory () as tmp_dir :
1070
1075
root_cat = TestCases .test_case_1 ()
1071
1076
root_cat .normalize_hrefs (
1072
1077
os .path .join (tmp_dir , "catalog-full-copy-3-source" )
@@ -1080,7 +1085,7 @@ def test_full_copy_3(self) -> None:
1080
1085
self .check_catalog (cat2 , "dest" )
1081
1086
1082
1087
def test_full_copy_4 (self ) -> None :
1083
- with TemporaryDirectory (dir = os . getcwd () ) as tmp_dir :
1088
+ with TemporaryDirectory () as tmp_dir :
1084
1089
root_cat = TestCases .test_case_2 ()
1085
1090
root_cat .normalize_hrefs (
1086
1091
os .path .join (tmp_dir , "catalog-full-copy-4-source" )
0 commit comments