2727import os
2828
2929
30+ netCDF4 = None
3031try :
31- import netCDF4 # noqa
32+ import netCDF4
3233except ImportError :
33- WITH_NC4 = False
34- else :
35- WITH_NC4 = True
34+ pass
35+
36+ geotiff = None
37+ try :
38+ import geotiff
39+ except ImportError :
40+ pass
41+
42+ fiona = None
43+ try :
44+ import fiona
45+ except (ImportError , ModuleNotFoundError ):
46+ pass
3647
3748
3849class ValidateTest (TestBase ):
@@ -70,9 +81,10 @@ class data_format(object):
7081 return fake_input
7182
7283 @pytest .mark .online
84+ @pytest .mark .requires_fiona
85+ @pytest .mark .skipif (fiona is None , reason = "fiona libraries are required for this test" )
7386 def test_gml_validator (self ):
74- """Test GML validator
75- """
87+ """Test GML validator"""
7688 gml_input = self .get_input ('gml/point.gml' , 'point.xsd' , FORMATS .GML .mime_type )
7789 self .assertTrue (validategml (gml_input , MODE .NONE ), 'NONE validation' )
7890 self .assertTrue (validategml (gml_input , MODE .SIMPLE ), 'SIMPLE validation' )
@@ -81,26 +93,39 @@ def test_gml_validator(self):
8193 gml_input .stream .close ()
8294
8395 @pytest .mark .online
96+ @pytest .mark .skipif (fiona is not None , reason = "fiona libraries must not be installed for this test" )
97+ def test_no_gml_validator (self ):
98+ """Test GML validator"""
99+ gml_input = self .get_input ('gml/point.gml' , 'point.xsd' , FORMATS .GML .mime_type )
100+ self .assertTrue (validategml (gml_input , MODE .NONE ), 'NONE validation' )
101+ self .assertTrue (validategml (gml_input , MODE .SIMPLE ), 'SIMPLE validation' )
102+ self .assertFalse (validategml (gml_input , MODE .STRICT ), 'STRICT validation' )
103+ # self.assertTrue(validategml(gml_input, MODE.VERYSTRICT), 'VERYSTRICT validation')
104+ gml_input .stream .close ()
105+
106+ @pytest .mark .online
107+ @pytest .mark .requires_fiona
84108 @pytest .mark .xfail (reason = "gml verystrict validation fails" )
109+ @pytest .mark .skipif (fiona is None , reason = "fiona libraries are required for this test" )
85110 def test_gml_validator_verystrict (self ):
86- """Test GML validator
87- """
111+ """Test GML validator"""
88112 gml_input = self .get_input ('gml/point.gml' , 'point.xsd' , FORMATS .GML .mime_type )
89113 self .assertTrue (validategml (gml_input , MODE .VERYSTRICT ), 'VERYSTRICT validation' )
90114 gml_input .stream .close ()
91115
116+
92117 def test_json_validator (self ):
93- """Test GeoJSON validator
94- """
118+ """Test GeoJSON validator"""
95119 json_input = self .get_input ('json/point.geojson' , None , FORMATS .JSON .mime_type )
96120 self .assertTrue (validatejson (json_input , MODE .NONE ), 'NONE validation' )
97121 self .assertTrue (validatejson (json_input , MODE .SIMPLE ), 'SIMPLE validation' )
98122 self .assertTrue (validatejson (json_input , MODE .STRICT ), 'STRICT validation' )
99123 json_input .stream .close ()
100124
125+ @pytest .mark .requires_fiona
126+ @pytest .mark .skipif (fiona is None , reason = "fiona libraries are required for this test" )
101127 def test_geojson_validator (self ):
102- """Test GeoJSON validator
103- """
128+ """Test GeoJSON validator"""
104129 geojson_input = self .get_input ('json/point.geojson' , 'json/schema/geojson.json' ,
105130 FORMATS .GEOJSON .mime_type )
106131 self .assertTrue (validategeojson (geojson_input , MODE .NONE ), 'NONE validation' )
@@ -109,61 +134,114 @@ def test_geojson_validator(self):
109134 self .assertTrue (validategeojson (geojson_input , MODE .VERYSTRICT ), 'VERYSTRICT validation' )
110135 geojson_input .stream .close ()
111136
137+
138+ @pytest .mark .skipif (fiona is not None , reason = "fiona libraries must not be installed for this test" )
139+ def test_no_geojson_validator (self ):
140+ """Test GeoJSON validator"""
141+ geojson_input = self .get_input ('json/point.geojson' , 'json/schema/geojson.json' ,
142+ FORMATS .GEOJSON .mime_type )
143+ self .assertTrue (validategeojson (geojson_input , MODE .NONE ), 'NONE validation' )
144+ self .assertTrue (validategeojson (geojson_input , MODE .SIMPLE ), 'SIMPLE validation' )
145+
146+ self .assertFalse (validategeojson (geojson_input , MODE .STRICT ), 'STRICT validation' )
147+
148+ # FIXME: MODE.VERYSTRICT should fail here
149+ self .assertTrue (validategeojson (geojson_input , MODE .VERYSTRICT ), 'VERYSTRICT validation' )
150+
151+ geojson_input .stream .close ()
152+
153+ @pytest .mark .requires_fiona
154+ @pytest .mark .skipif (fiona is None , reason = "fiona libraries are required for this test" )
112155 def test_shapefile_validator (self ):
113- """Test ESRI Shapefile validator
114- """
156+ """Test ESRI Shapefile validator"""
115157 shapefile_input = self .get_input ('shp/point.shp.zip' , None ,
116158 FORMATS .SHP .mime_type )
117159 self .assertTrue (validateshapefile (shapefile_input , MODE .NONE ), 'NONE validation' )
118160 self .assertTrue (validateshapefile (shapefile_input , MODE .SIMPLE ), 'SIMPLE validation' )
119161 self .assertTrue (validateshapefile (shapefile_input , MODE .STRICT ), 'STRICT validation' )
120162 shapefile_input .stream .close ()
121163
122- @pytest .mark .geotiff
164+ @pytest .mark .skipif (fiona is not None , reason = "fiona libraries must not be installed for this test" )
165+ def test_no_shapefile_validator (self ):
166+ """Test ESRI Shapefile validator"""
167+ shapefile_input = self .get_input ('shp/point.shp.zip' , None ,
168+ FORMATS .SHP .mime_type )
169+ self .assertTrue (validateshapefile (shapefile_input , MODE .NONE ), 'NONE validation' )
170+ self .assertTrue (validateshapefile (shapefile_input , MODE .SIMPLE ), 'SIMPLE validation' )
171+ self .assertFalse (validateshapefile (shapefile_input , MODE .STRICT ), 'STRICT validation' )
172+ shapefile_input .stream .close ()
173+
174+ @pytest .mark .skipif (geotiff is not None , reason = "geotiff libraries must not be installed for this test" )
175+ def test_no_geotiff_validator (self ):
176+ """Test GeoTIFF validator"""
177+ geotiff_input = self .get_input ('geotiff/dem.tiff' , None ,
178+ FORMATS .GEOTIFF .mime_type )
179+ self .assertTrue (validategeotiff (geotiff_input , MODE .NONE ), 'NONE validation' )
180+ self .assertTrue (validategeotiff (geotiff_input , MODE .SIMPLE ), 'SIMPLE validation' )
181+ self .assertFalse (validategeotiff (geotiff_input , MODE .STRICT ), 'STRICT validation' )
182+ geotiff_input .stream .close ()
183+
184+ @pytest .mark .requires_geotiff
185+ @pytest .mark .skipif (geotiff is None , reason = "geotiff libraries are required for this test" )
123186 def test_geotiff_validator (self ):
124- """Test GeoTIFF validator
125- """
187+ """Test GeoTIFF validator"""
126188 geotiff_input = self .get_input ('geotiff/dem.tiff' , None ,
127189 FORMATS .GEOTIFF .mime_type )
128190 self .assertTrue (validategeotiff (geotiff_input , MODE .NONE ), 'NONE validation' )
129191 self .assertTrue (validategeotiff (geotiff_input , MODE .SIMPLE ), 'SIMPLE validation' )
130192 self .assertTrue (validategeotiff (geotiff_input , MODE .STRICT ), 'STRICT validation' )
131193 geotiff_input .stream .close ()
132194
195+ @pytest .mark .requires_netcdf4
196+ @pytest .mark .skipif (netCDF4 is None , reason = "NetCDF4 libraries are required for this test" )
133197 def test_netcdf_validator (self ):
134- """Test netCDF validator
135- """
198+ """Test netCDF validator"""
136199 netcdf_input = self .get_input ('netcdf/time.nc' , None , FORMATS .NETCDF .mime_type )
137200 self .assertTrue (validatenetcdf (netcdf_input , MODE .NONE ), 'NONE validation' )
138201 self .assertTrue (validatenetcdf (netcdf_input , MODE .SIMPLE ), 'SIMPLE validation' )
139202 netcdf_input .stream .close ()
140- if WITH_NC4 :
141- self .assertTrue (validatenetcdf (netcdf_input , MODE .STRICT ), 'STRICT validation' )
142- netcdf_input .file = 'grub.nc'
143- self .assertFalse (validatenetcdf (netcdf_input , MODE .STRICT ))
144- else :
145- self .assertFalse (validatenetcdf (netcdf_input , MODE .STRICT ), 'STRICT validation' )
203+
204+ self .assertTrue (validatenetcdf (netcdf_input , MODE .STRICT ), 'STRICT validation' )
205+ netcdf_input .file = 'grub.nc'
206+ self .assertFalse (validatenetcdf (netcdf_input , MODE .STRICT ))
207+
208+ @pytest .mark .skipif (netCDF4 is not None , reason = "NetCDF4 libraries must not be installed for this test" )
209+ def test_no_netcdf_validator (self ):
210+ """Test netCDF validator"""
211+ netcdf_input = self .get_input ('netcdf/time.nc' , None , FORMATS .NETCDF .mime_type )
212+ self .assertTrue (validatenetcdf (netcdf_input , MODE .NONE ), 'NONE validation' )
213+ self .assertTrue (validatenetcdf (netcdf_input , MODE .SIMPLE ), 'SIMPLE validation' )
214+ netcdf_input .stream .close ()
215+
216+ self .assertFalse (validatenetcdf (netcdf_input , MODE .STRICT ), 'STRICT validation' )
146217
147218 @pytest .mark .online
148- @pytest .mark .xfail (reason = "test.opendap.org is offline" )
219+ @pytest .mark .requires_netcdf4
220+ @pytest .mark .skipif (netCDF4 is None , reason = "NetCDF4 libraries are required for this test" )
149221 def test_dods_validator (self ):
150222 opendap_input = ComplexInput ('dods' , 'opendap test' , [FORMATS .DODS ,])
151223 opendap_input .url = "http://test.opendap.org:80/opendap/netcdf/examples/sresa1b_ncar_ccsm3_0_run1_200001.nc"
152224 self .assertTrue (validatedods (opendap_input , MODE .NONE ), 'NONE validation' )
153225 self .assertTrue (validatedods (opendap_input , MODE .SIMPLE ), 'SIMPLE validation' )
154226
155- if WITH_NC4 :
156- self .assertTrue (validatedods (opendap_input , MODE .STRICT ), 'STRICT validation' )
157- opendap_input .url = 'Faulty url'
158- self .assertFalse (validatedods (opendap_input , MODE .STRICT ))
159- else :
160- self .assertFalse (validatedods (opendap_input , MODE .STRICT ), 'STRICT validation' )
227+ self .assertTrue (validatedods (opendap_input , MODE .STRICT ), 'STRICT validation' )
228+ opendap_input .url = 'Faulty url'
229+ self .assertFalse (validatedods (opendap_input , MODE .STRICT ))
161230
231+ @pytest .mark .online
232+ @pytest .mark .skipif (netCDF4 is not None , reason = "NetCDF4 libraries must not be installed for this test" )
162233 def test_dods_default (self ):
163234 opendap_input = ComplexInput ('dods' , 'opendap test' , [FORMATS .DODS ,],
164235 default = 'http://test.opendap.org' ,
165236 default_type = SOURCE_TYPE .URL ,
166237 mode = MODE .SIMPLE )
238+ opendap_input .url = "http://test.opendap.org:80/opendap/netcdf/examples/sresa1b_ncar_ccsm3_0_run1_200001.nc"
239+ self .assertTrue (validatedods (opendap_input , MODE .NONE ), 'NONE validation' )
240+ self .assertTrue (validatedods (opendap_input , MODE .SIMPLE ), 'SIMPLE validation' )
241+
242+ with pytest .warns (UserWarning ) as record :
243+ self .assertFalse (validatedods (opendap_input , MODE .STRICT ), 'STRICT validation' )
244+ assert "Complex validation requires netCDF4 support." in record [0 ].message .args [0 ]
167245
168246 def test_fail_validator (self ):
169247 fake_input = self .get_input ('point.xsd' , 'point.xsd' , FORMATS .SHP .mime_type )
0 commit comments