2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222
23+ from requests .exceptions import HTTPError
24+ from requests .models import Response
2325import unittest
2426from unittest .mock import MagicMock
2527
@@ -58,11 +60,12 @@ def test_open_data_spatial_res(self):
5860 self .assertEqual ([10 ], [ds .sizes ["time" ]])
5961
6062 def test_open_data_splits (self ):
63+
6164 # Fail first call, succeed sequentially on split halves
6265 def side_effect (* args , ** kwargs ):
6366 time_range = kwargs .get ("time_range" )
6467 if time_range == ("2025-01-01" , "2025-01-10" ):
65- raise Exception ( "Too large request " )
68+ raise make_403_error ( "Cost limits exceeded " )
6669 if time_range == ("2025-01-01" , "2025-01-05" ):
6770 return self .ds_3d .isel (time = slice (0 , 5 ))
6871 if time_range == ("2025-01-06" , "2025-01-10" ):
@@ -76,10 +79,20 @@ def side_effect(*args, **kwargs):
7679 xr .testing .assert_equal (self .ds_3d , ds )
7780
7881 def test_open_with_split_base_case_error (self ):
79- self .accessor .store .open_data .side_effect = Exception ("fail always" )
82+ # Always raise 403 error
83+ self .accessor .store .open_data .side_effect = make_403_error ()
8084
8185 with self .assertRaises (DataStoreError ) as cm :
8286 _ = self .accessor .open_data (
83- "era5-land" , time_range = ("2025-01-01" , "2025-01-10" )
87+ "era5-land" ,
88+ time_range = ("2025-01-01" , "2025-01-10" ),
8489 )
90+
8591 self .assertIn ("Cannot further split time range" , str (cm .exception ))
92+
93+
94+ def make_403_error (message = "Cost limits exceeded" ):
95+ response = Response ()
96+ response .status_code = 403
97+ response ._content = message .encode () # optional
98+ return HTTPError (message , response = response )
0 commit comments