3
3
# This is for the type checking on GridTest.test_clear_code
4
4
# mypy: warn_unused_ignores=False
5
5
6
- import unittest
7
6
from datetime import datetime
8
7
from typing import Any
9
8
19
18
code = "MGRS-4CFJ"
20
19
21
20
22
- def make_item () -> pystac .Item :
21
+ @pytest .fixture
22
+ def sentinel2_example_item () -> pystac .Item :
23
+ return pystac .Item .from_file (
24
+ TestCases .get_path ("data-files/grid/example-sentinel2.json" )
25
+ )
26
+
27
+
28
+ @pytest .fixture
29
+ def item () -> pystac .Item :
23
30
"""Create basic test items that are only slightly different."""
24
31
asset_id = "an/asset"
25
32
start = datetime (2018 , 1 , 2 )
@@ -31,118 +38,115 @@ def make_item() -> pystac.Item:
31
38
return item
32
39
33
40
34
- class GridTest (unittest .TestCase ):
35
- def setUp (self ) -> None :
36
- super ().setUp ()
37
- self .item = make_item ()
38
- self .sentinel_example_uri = TestCases .get_path (
39
- "data-files/grid/example-sentinel2.json"
40
- )
41
-
42
- def test_stac_extensions (self ) -> None :
43
- self .assertTrue (GridExtension .has_extension (self .item ))
44
-
45
- def test_item_repr (self ) -> None :
46
- grid_item_ext = GridExtension .ext (self .item )
47
- self .assertEqual (
48
- f"<ItemGridExtension Item id={ self .item .id } >" , grid_item_ext .__repr__ ()
49
- )
50
-
51
- @pytest .mark .vcr ()
52
- def test_attributes (self ) -> None :
53
- GridExtension .ext (self .item ).apply (code )
54
- self .assertEqual (code , GridExtension .ext (self .item ).code )
55
- self .item .validate ()
56
-
57
- def test_invalid_code_value (self ) -> None :
58
- with self .assertRaises (ValueError ):
59
- GridExtension .ext (self .item ).apply ("not_a_valid_code" )
60
-
61
- @pytest .mark .vcr ()
62
- def test_modify (self ) -> None :
63
- GridExtension .ext (self .item ).apply (code )
64
- GridExtension .ext (self .item ).apply (code + "a" )
65
- self .assertEqual (code + "a" , GridExtension .ext (self .item ).code )
66
- self .item .validate ()
67
-
68
- def test_from_dict (self ) -> None :
69
- d : dict [str , Any ] = {
70
- "type" : "Feature" ,
71
- "stac_version" : "1.1.0" ,
72
- "id" : "an/asset" ,
73
- "properties" : {
74
- "grid:code" : code ,
75
- "datetime" : "2018-01-02T00:00:00Z" ,
76
- },
77
- "geometry" : None ,
78
- "links" : [],
79
- "assets" : {},
80
- "stac_extensions" : [GridExtension .get_schema_uri ()],
81
- }
82
- item = pystac .Item .from_dict (d )
83
- self .assertEqual (code , GridExtension .ext (item ).code )
84
-
85
- def test_to_from_dict (self ) -> None :
86
- GridExtension .ext (self .item ).apply (code )
87
- d = self .item .to_dict ()
88
- self .assertEqual (code , d ["properties" ][grid .CODE_PROP ])
89
-
90
- item = pystac .Item .from_dict (d )
91
- self .assertEqual (code , GridExtension .ext (item ).code )
92
-
93
- def test_clear_code (self ) -> None :
94
- GridExtension .ext (self .item ).apply (code )
95
-
96
- with self .assertRaises (ValueError ):
97
- # Ignore type errors because this test intentionally checks behavior
98
- # that does not conform to the type signature.
99
- # https://github.com/stac-utils/pystac/pull/878#discussion_r957352232
100
- GridExtension .ext (self .item ).code = None # type: ignore
101
- with self .assertRaises (ValueError ):
102
- # First segment has to be all caps
103
- # https://github.com/stac-utils/pystac/pull/878#discussion_r957354927
104
- GridExtension .ext (self .item ).code = "this-is-not-a-grid-code"
105
- with self .assertRaises (ValueError ):
106
- # Folks might try to put an epsg code in
107
- # https://github.com/stac-utils/pystac/pull/878#discussion_r957355415
108
- GridExtension .ext (self .item ).code = "4326"
109
- with self .assertRaises (ValueError ):
110
- # Folks might try to put an epsg code in
111
- # https://github.com/stac-utils/pystac/pull/878#discussion_r957355415
112
- GridExtension .ext (self .item ).code = "EPSG:4326"
113
-
114
- def test_extension_not_implemented (self ) -> None :
115
- # Should raise exception if Item does not include extension URI
116
- item = pystac .Item .from_file (self .sentinel_example_uri )
117
- item .stac_extensions .remove (GridExtension .get_schema_uri ())
118
-
119
- with self .assertRaises (pystac .ExtensionNotImplemented ):
120
- _ = GridExtension .ext (item )
121
-
122
- # Should raise exception if owning Item does not include extension URI
123
- item .properties ["grid:code" ] = None
124
-
125
- with self .assertRaises (pystac .ExtensionNotImplemented ):
126
- _ = GridExtension .ext (item )
127
-
128
- def test_item_ext_add_to (self ) -> None :
129
- item = pystac .Item .from_file (self .sentinel_example_uri )
130
- item .stac_extensions .remove (GridExtension .get_schema_uri ())
131
- self .assertNotIn (GridExtension .get_schema_uri (), item .stac_extensions )
132
-
133
- _ = GridExtension .ext (item , add_if_missing = True )
134
-
135
- self .assertIn (GridExtension .get_schema_uri (), item .stac_extensions )
136
-
137
- def test_should_raise_exception_when_passing_invalid_extension_object (
138
- self ,
139
- ) -> None :
140
- self .assertRaisesRegex (
141
- ExtensionTypeError ,
142
- r"^GridExtension does not apply to type 'object'$" ,
143
- GridExtension .ext ,
144
- object (),
145
- )
41
+ def test_stac_extensions (item : pystac .Item ) -> None :
42
+ assert GridExtension .has_extension (item )
43
+
44
+
45
+ def test_item_repr (item : pystac .Item ) -> None :
46
+ grid_item_ext = GridExtension .ext (item )
47
+ assert f"<ItemGridExtension Item id={ item .id } >" == grid_item_ext .__repr__ ()
48
+
49
+
50
+ @pytest .mark .vcr ()
51
+ def test_attributes (item : pystac .Item ) -> None :
52
+ GridExtension .ext (item ).apply (code )
53
+ assert code == GridExtension .ext (item ).code
54
+ item .validate ()
55
+
56
+
57
+ def test_invalid_code_value (item : pystac .Item ) -> None :
58
+ with pytest .raises (ValueError ):
59
+ GridExtension .ext (item ).apply ("not_a_valid_code" )
60
+
61
+
62
+ @pytest .mark .vcr ()
63
+ def test_modify (item : pystac .Item ) -> None :
64
+ GridExtension .ext (item ).apply (code )
65
+ GridExtension .ext (item ).apply (code + "a" )
66
+ assert code + "a" == GridExtension .ext (item ).code
67
+ item .validate ()
68
+
69
+
70
+ def test_from_dict () -> None :
71
+ d : dict [str , Any ] = {
72
+ "type" : "Feature" ,
73
+ "stac_version" : "1.1.0" ,
74
+ "id" : "an/asset" ,
75
+ "properties" : {
76
+ "grid:code" : code ,
77
+ "datetime" : "2018-01-02T00:00:00Z" ,
78
+ },
79
+ "geometry" : None ,
80
+ "links" : [],
81
+ "assets" : {},
82
+ "stac_extensions" : [GridExtension .get_schema_uri ()],
83
+ }
84
+ item = pystac .Item .from_dict (d )
85
+ assert code == GridExtension .ext (item ).code
86
+
87
+
88
+ def test_to_from_dict (item : pystac .Item ) -> None :
89
+ GridExtension .ext (item ).apply (code )
90
+ d = item .to_dict ()
91
+ assert code == d ["properties" ][grid .CODE_PROP ]
92
+
93
+ item = pystac .Item .from_dict (d )
94
+ assert code == GridExtension .ext (item ).code
95
+
96
+
97
+ def test_clear_code (item : pystac .Item ) -> None :
98
+ GridExtension .ext (item ).apply (code )
99
+
100
+ with pytest .raises (ValueError ):
101
+ # Ignore type errors because this test intentionally checks behavior
102
+ # that does not conform to the type signature.
103
+ # https://github.com/stac-utils/pystac/pull/878#discussion_r957352232
104
+ GridExtension .ext (item ).code = None # type: ignore
105
+ with pytest .raises (ValueError ):
106
+ # First segment has to be all caps
107
+ # https://github.com/stac-utils/pystac/pull/878#discussion_r957354927
108
+ GridExtension .ext (item ).code = "this-is-not-a-grid-code"
109
+ with pytest .raises (ValueError ):
110
+ # Folks might try to put an epsg code in
111
+ # https://github.com/stac-utils/pystac/pull/878#discussion_r957355415
112
+ GridExtension .ext (item ).code = "4326"
113
+ with pytest .raises (ValueError ):
114
+ # Folks might try to put an epsg code in
115
+ # https://github.com/stac-utils/pystac/pull/878#discussion_r957355415
116
+ GridExtension .ext (item ).code = "EPSG:4326"
117
+
118
+
119
+ def test_extension_not_implemented (sentinel2_example_item : pystac .Item ) -> None :
120
+ # Should raise exception if Item does not include extension URI
121
+ item = sentinel2_example_item
122
+ item .stac_extensions .remove (GridExtension .get_schema_uri ())
123
+
124
+ with pytest .raises (pystac .ExtensionNotImplemented ):
125
+ _ = GridExtension .ext (item )
126
+
127
+ # Should raise exception if owning Item does not include extension URI
128
+ item .properties ["grid:code" ] = None
129
+
130
+ with pytest .raises (pystac .ExtensionNotImplemented ):
131
+ _ = GridExtension .ext (item )
132
+
133
+
134
+ def test_item_ext_add_to (sentinel2_example_item : pystac .Item ) -> None :
135
+ item = sentinel2_example_item
136
+ item .stac_extensions .remove (GridExtension .get_schema_uri ())
137
+ assert GridExtension .get_schema_uri () not in item .stac_extensions
138
+
139
+ _ = GridExtension .ext (item , add_if_missing = True )
140
+
141
+ assert GridExtension .get_schema_uri () in item .stac_extensions
142
+
143
+
144
+ def test_should_raise_when_passing_invalid_extension_object () -> None :
145
+ with pytest .raises (
146
+ ExtensionTypeError , match = r"^GridExtension does not apply to type 'object'$"
147
+ ):
148
+ # intentionally calling it wrong so tell mypy to ignore this line:
149
+ GridExtension .ext (object ()) # type: ignore
146
150
147
151
148
152
@pytest .fixture
0 commit comments