1212import iris .tests as tests # isort:skip
1313
1414import pathlib
15+ from unittest import mock
16+
17+ import netCDF4
1518
1619import iris
1720import iris .io
@@ -148,19 +151,20 @@ def test_path_object(self):
148151 self .assertEqual (len (cubes ), 1 )
149152
150153
151- class TestOpenDAP (tests .IrisTest ):
152- def test_load (self ):
153- # Check that calling iris.load_* with a http URI triggers a call to
154- # ``iris.io.load_http``
154+ class TestOPeNDAP (tests .IrisTest ):
155+ def setUp (self ):
156+ self .url = "http://geoport.whoi.edu:80/thredds/dodsC/bathy/gom15"
155157
156- url = "http://geoport.whoi.edu:80/thredds/dodsC/bathy/gom15"
158+ def test_load_http_called (self ):
159+ # Check that calling iris.load_* with an http URI triggers a call to
160+ # ``iris.io.load_http``
157161
158162 class LoadHTTPCalled (Exception ):
159163 pass
160164
161165 def new_load_http (passed_urls , * args , ** kwargs ):
162166 self .assertEqual (len (passed_urls ), 1 )
163- self .assertEqual (url , passed_urls [0 ])
167+ self .assertEqual (self . url , passed_urls [0 ])
164168 raise LoadHTTPCalled ()
165169
166170 try :
@@ -174,11 +178,28 @@ def new_load_http(passed_urls, *args, **kwargs):
174178 iris .load_cubes ,
175179 ]:
176180 with self .assertRaises (LoadHTTPCalled ):
177- fn (url )
181+ fn (self . url )
178182
179183 finally :
180184 iris .io .load_http = orig
181185
186+ def test_netCDF_Dataset_call (self ):
187+ # Check that load_http calls netCDF4.Dataset and supplies the expected URL.
188+
189+ # To avoid making a request to an OPeNDAP server in a test, instead
190+ # mock the call to netCDF.Dataset so that it returns a dataset for a
191+ # local file.
192+ filename = tests .get_data_path (
193+ ("NetCDF" , "global" , "xyt" , "SMALL_total_column_co2.nc" )
194+ )
195+ fake_dataset = netCDF4 .Dataset (filename )
196+
197+ with mock .patch (
198+ "netCDF4.Dataset" , return_value = fake_dataset
199+ ) as dataset_loader :
200+ next (iris .io .load_http ([self .url ], callback = None ))
201+ dataset_loader .assert_called_with (self .url , mode = "r" )
202+
182203
183204if __name__ == "__main__" :
184205 tests .main ()
0 commit comments