1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
import pytest
import owslib
from owslib.etree import etree
from owslib.wfs import WebFeatureService
from tests.utils import service_ok
WFS_SERVICE_URL = 'https://www.dov.vlaanderen.be/geoserver/wfs?request=GetCapabilities'
@pytest.fixture
def mp_wfs_100(monkeypatch):
"""Monkeypatch the call to the remote GetCapabilities request of WFS
version 1.0.0.
Parameters
----------
monkeypatch : pytest.fixture
PyTest monkeypatch fixture.
"""
def read(*args, **kwargs):
with open('tests/resources/wfs_mapserver_demo_getcapabilities_100.xml', 'r') as f:
data = f.read()
if type(data) is not bytes:
data = data.encode('utf-8')
data = etree.fromstring(data)
return data
monkeypatch.setattr(
owslib.feature.common.WFSCapabilitiesReader, 'read', read)
@pytest.fixture
def mp_wfs_110(monkeypatch):
"""Monkeypatch the call to the remote GetCapabilities request of WFS
version 1.1.0.
Parameters
----------
monkeypatch : pytest.fixture
PyTest monkeypatch fixture.
"""
def read(*args, **kwargs):
with open('tests/resources/wfs_dov_getcapabilities_110.xml', 'r') as f:
data = f.read()
if type(data) is not bytes:
data = data.encode('utf-8')
data = etree.fromstring(data)
return data
monkeypatch.setattr(
owslib.feature.common.WFSCapabilitiesReader, 'read', read)
@pytest.fixture()
def mp_remote_describefeaturetype(monkeypatch):
"""Monkeypatch the call to the remote DescribeFeatureType request.
Returns a standard DescribeFeatureType response.
Parameters
----------
monkeypatch : pytest.fixture
PyTest monkeypatch fixture.
"""
def __remote_describefeaturetype(*args, **kwargs):
with open('tests/resources/wfs_schema_dov_boringen.xml', 'r') as f:
data = f.read()
if type(data) is not bytes:
data = data.encode('utf-8')
data = etree.fromstring(data)
return data
monkeypatch.setattr(owslib.feature.schema,
'_get_remote_describefeaturetype',
__remote_describefeaturetype)
@pytest.fixture()
def mp_remote_describefeaturetype_typename_eq_attribute(monkeypatch):
"""Monkeypatch the call to the remote DescribeFeatureType request.
Returns a DescribeFeatureType response where the typeName equals one of
the attributes.
Parameters
----------
monkeypatch : pytest.fixture
PyTest monkeypatch fixture.
"""
def __remote_describefeaturetype(*args, **kwargs):
with open('tests/resources/wfs_schema_dov_hhz.xml', 'r') as f:
data = f.read()
if type(data) is not bytes:
data = data.encode('utf-8')
data = etree.fromstring(data)
return data
monkeypatch.setattr(owslib.feature.schema,
'_get_remote_describefeaturetype',
__remote_describefeaturetype)
class TestOnline(object):
"""Class grouping online tests for the WFS get_schema method."""
@pytest.mark.xfail
@pytest.mark.online
@pytest.mark.skipif(not service_ok(WFS_SERVICE_URL),
reason="WFS service is unreachable")
@pytest.mark.parametrize("wfs_version", ["1.1.0", "2.0.0"])
def test_get_schema(self, wfs_version):
"""Test the get_schema method for a standard schema."""
wfs = WebFeatureService(WFS_SERVICE_URL, version=wfs_version)
schema = wfs.get_schema('dov-pub:Boringen')
@pytest.mark.xfail
@pytest.mark.online
@pytest.mark.skipif(not service_ok(WFS_SERVICE_URL),
reason="WFS service is unreachable")
@pytest.mark.parametrize("wfs_version", ["1.1.0", "2.0.0"])
def test_schema_result(self, wfs_version):
"""Test whether the output from get_schema is a wellformed dictionary."""
wfs = WebFeatureService(WFS_SERVICE_URL, version=wfs_version)
schema = wfs.get_schema('dov-pub:Boringen')
assert isinstance(schema, dict)
assert 'properties' in schema or 'geometry' in schema
if 'geometry' in schema:
assert 'geometry_column' in schema
if 'properties' in schema:
assert isinstance(schema['properties'], dict)
assert 'required' in schema
assert isinstance(schema['required'], list)
class TestOffline(object):
"""Class grouping offline tests for the WFS get_schema method."""
def test_get_schema_100(self, mp_wfs_100):
"""Test the get_schema method for a standard schema.
Parameters
----------
mp_wfs_100 : pytest.fixture
Monkeypatch the call to the remote GetCapabilities request.
"""
wfs100 = WebFeatureService(WFS_SERVICE_URL, version='1.0.0')
assert wfs100.identification.title == 'WFS Demo Server for MapServer'
assert wfs100.identification.keywords == []
assert list(wfs100.contents) == ['continents', 'cities']
def test_get_schema(self, mp_wfs_100, mp_remote_describefeaturetype):
"""Test the get_schema method for a standard schema.
Parameters
----------
mp_wfs_110 : pytest.fixture
Monkeypatch the call to the remote GetCapabilities request.
mp_remote_describefeaturetype : pytest.fixture
Monkeypatch the call to the remote DescribeFeatureType request.
"""
wfs110 = WebFeatureService(WFS_SERVICE_URL, version='1.1.0')
schema = wfs110.get_schema('dov-pub:Boringen')
def test_schema_result(self, mp_wfs_110, mp_remote_describefeaturetype):
"""Test whether the output from get_schema is a wellformed dictionary.
Parameters
----------
mp_wfs_110 : pytest.fixture
Monkeypatch the call to the remote GetCapabilities request.
mp_remote_describefeaturetype : pytest.fixture
Monkeypatch the call to the remote DescribeFeatureType request.
"""
wfs110 = WebFeatureService(WFS_SERVICE_URL, version='1.1.0')
schema = wfs110.get_schema('dov-pub:Boringen')
assert isinstance(schema, dict)
assert 'properties' in schema or 'geometry' in schema
if 'geometry' in schema:
assert 'geometry_column' in schema
if 'properties' in schema:
assert isinstance(schema['properties'], dict)
assert 'required' in schema
assert isinstance(schema['required'], list)
def test_get_schema_typename_eq_attribute(
self, mp_wfs_110,
mp_remote_describefeaturetype_typename_eq_attribute):
"""Test the get_schema method for a schema where the typeName equals
one of the attributes.
Parameters
----------
mp_wfs_110 : pytest.fixture
Monkeypatch the call to the remote GetCapabilities request.
mp_remote_describefeaturetype : pytest.fixture
Monkeypatch the call to the remote DescribeFeatureType
request.
"""
wfs110 = WebFeatureService(WFS_SERVICE_URL, version='1.1.0')
schema = wfs110.get_schema('gw_varia:hhz')
def test_get_datatype_geometry(self):
"""Test the _get_datatype helper function with geometry types."""
from owslib.feature.schema import _get_datatype, XS_NAMESPACE
from owslib.etree import etree
ns = {
'xs': XS_NAMESPACE,
'gml': 'http://www.opengis.net/gml',
'xsd': 'http://www.w3.org/2001/XMLSchema'
}
# Test geometry type
# XML: <element name="field1" type="gml:PointPropertyType"/>
element = etree.Element('{%s}element' % XS_NAMESPACE,
attrib={'name': 'field1', 'type': 'gml:PointPropertyType'},
nsmap=ns)
assert _get_datatype(element, "xsd", "gml") == "PointPropertyType"
def test_get_datatype_reference(self):
"""Test the _get_datatype helper function with element references."""
from owslib.feature.schema import _get_datatype, XS_NAMESPACE
from owslib.etree import etree
ns = {
'xs': XS_NAMESPACE,
'gml': 'http://www.opengis.net/gml',
'xsd': 'http://www.w3.org/2001/XMLSchema'
}
# Test element reference
# XML: <element name="field2" ref="gml:polygonProperty"/>
element = etree.Element('{%s}element' % XS_NAMESPACE,
attrib={'name': 'field2', 'ref': 'gml:polygonProperty'},
nsmap=ns)
assert _get_datatype(element, "xsd", "gml") == "polygonProperty"
@pytest.mark.parametrize("data_type", [
"xsd:boolean",
"xsd:date",
"xsd:dateTime",
"xsd:double",
"xsd:float",
"xsd:integer",
"xsd:int",
"xsd:string",
])
def test_get_datatype_simple_types(self, data_type):
"""Test the _get_datatype helper function with different simple types.
Parameters
----------
data_type : str
The XML Schema data type to test
"""
from owslib.feature.schema import _get_datatype, XS_NAMESPACE
from owslib.etree import etree
ns = {
'xs': XS_NAMESPACE,
'gml': 'http://www.opengis.net/gml',
'xsd': 'http://www.w3.org/2001/XMLSchema'
}
# Test simple type with restriction
# XML:
# <element name="field3">
# <simpleType>
# <restriction base="[data_type]"/>
# </simpleType>
# </element>
element = etree.Element('{%s}element' % XS_NAMESPACE, attrib={'name': 'field3'}, nsmap=ns)
simple_type = etree.SubElement(element, '{%s}simpleType' % XS_NAMESPACE)
restriction = etree.SubElement(simple_type, '{%s}restriction' % XS_NAMESPACE,
attrib={'base': data_type})
expected_type = data_type.split(":")[-1]
assert _get_datatype(element, "xsd", "gml") == expected_type
@pytest.mark.parametrize("data_type", [
"xsd:boolean",
"xsd:date",
"xsd:dateTime",
"xsd:double",
"xsd:float",
"xsd:integer",
"xsd:int",
"xsd:string",
])
def test_get_datatype_direct_types(self, data_type):
"""Test the _get_datatype helper function with direct type attributes.
Parameters
----------
data_type : str
The XML Schema data type to test
"""
from owslib.feature.schema import _get_datatype, XS_NAMESPACE
from owslib.etree import etree
ns = {
'xs': XS_NAMESPACE,
'gml': 'http://www.opengis.net/gml',
'xsd': 'http://www.w3.org/2001/XMLSchema'
}
# Test direct type attribute
# XML: <element name="field1" type="xsd:string"/>
element = etree.Element('{%s}element' % XS_NAMESPACE,
attrib={'name': 'field1', 'type': data_type},
nsmap=ns)
expected_type = data_type.split(":")[-1]
assert _get_datatype(element, "xsd", "gml") == expected_type
@pytest.mark.parametrize("data_type", [
"xsd:boolean",
"xsd:date",
"xsd:dateTime",
"xsd:double",
"xsd:float",
"xsd:integer",
"xsd:int",
"xsd:string",
])
def test_get_datatype_complex_types(self, data_type):
"""Test the _get_datatype helper function with complex type definitions.
Parameters
----------
data_type : str
The XML Schema data type to test
"""
from owslib.feature.schema import _get_datatype, XS_NAMESPACE
from owslib.etree import etree
ns = {
'xs': XS_NAMESPACE,
'gml': 'http://www.opengis.net/gml',
'xsd': 'http://www.w3.org/2001/XMLSchema'
}
# Test complex type
# XML:
# <element name="field4">
# <complexType>
# <sequence>
# <element type="xsd:type"/>
# </sequence>
# </complexType>
# </element>
element = etree.Element('{%s}element' % XS_NAMESPACE, attrib={'name': 'field4'}, nsmap=ns)
complex_type = etree.SubElement(element, '{%s}complexType' % XS_NAMESPACE)
sequence = etree.SubElement(complex_type, '{%s}sequence' % XS_NAMESPACE)
sub_element = etree.SubElement(sequence, '{%s}element' % XS_NAMESPACE,
attrib={'type': data_type})
expected_type = data_type.split(":")[-1]
assert _get_datatype(element, "xsd", "gml") == expected_type
def test_get_datatype_unknown(self):
"""Test the _get_datatype helper function with unknown structure."""
from owslib.feature.schema import _get_datatype, XS_NAMESPACE
from owslib.etree import etree
ns = {
'xs': XS_NAMESPACE,
'gml': 'http://www.opengis.net/gml',
'xsd': 'http://www.w3.org/2001/XMLSchema'
}
# Test unknown structure
# XML: <element name="field5"/>
element = etree.Element('{%s}element' % XS_NAMESPACE, attrib={'name': 'field5'}, nsmap=ns)
assert _get_datatype(element, "xsd", "gml") is None
|