Skip to content

Commit

Permalink
add tests for swisstopo address source
Browse files Browse the repository at this point in the history
  • Loading branch information
michmuel committed Nov 28, 2023
1 parent aad8382 commit cf5ec78
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .coveragerc.contrib-data_sources-swisstopo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
source =
pyramid_oereb/contrib/data_sources/swisstopo/*.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ DataExtract.json
/coverage.contrib-data_sources-standard.xml
/coverage.contrib-data_sources-interlis.xml
/coverage.contrib-print_proxy-mapfish_print.xml
/coverage.contrib-data_sources-swisstopo.xml
/coverage.core.xml
/coverage.contrib-stats.xml
/coverage.xml
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ compare_files_%:

COMPARE_ALL_JSONS = $(foreach prefix, $(JSON_PREFIXES), compare_files_$(prefix).json)

.PHONY: check_fed_data
.PHONY: check_fed_data
check_fed_data: clean_fed_data prepare_fed_data $(COMPARE_ALL_JSONS)


Expand Down Expand Up @@ -318,6 +318,10 @@ test-contrib-data_sources-standard: ${VENV_ROOT}/requirements-timestamp
test-contrib-data_sources-interlis: ${VENV_ROOT}/requirements-timestamp
$(VENV_BIN)/py.test -vv $(PYTEST_OPTS) --cov-config .coveragerc.contrib-data_sources-interlis --cov $(PACKAGE)/contrib/data_sources/interlis_2_3 --cov-report=term-missing:skip-covered --cov-report=xml:coverage.contrib-data_sources-interlis.xml tests/contrib.data_sources.interlis_2_3

.PHONY: test-contrib-data_sources-swisstopo
test-contrib-data_sources-swisstopo: ${VENV_ROOT}/requirements-timestamp
$(VENV_BIN)/py.test -vv $(PYTEST_OPTS) --cov-config .coveragerc.contrib-data_sources-swisstopo --cov $(PACKAGE)/contrib/data_sources/swisstopo --cov-report=term-missing:skip-covered --cov-report=xml:coverage.contrib-data_sources-swisstopo.xml tests/contrib.data_sources.swisstopo

.PHONY: test-contrib-stats
test-contrib-stats: ${VENV_ROOT}/requirements-timestamp
$(VENV_BIN)/py.test -vv $(PYTEST_OPTS) --cov-config .coveragerc.contrib-stats --cov $(PACKAGE)/contrib/stats --cov-report=xml:coverage.contrib-stats.xml tests/contrib.stats
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import pytest
from unittest.mock import patch
from requests import HTTPError
from pyramid_oereb.contrib.data_sources.swisstopo.address import AddressGeoAdminSource


@pytest.fixture
def requests_get():
with patch('requests.get') as mocked_function:

class Response():
def __init__(self):
self.status_code = 200

# Response from:
# https://api3.geo.admin.ch/rest/services/api/SearchServer
# ?type=locations&searchText=4410%20Mühlemattstrasse%2036

def json(self):
return {
"results": [
{
"attrs":
{
"detail": "muehlemattstrasse 36 4410 liestal 2829 liestal ch bl",
"featureId": "2355731_0",
"geom_quadindex": "021101213123030021322",
"geom_st_box2d":
(
"BOX(621861.1194661561 259852.35739866708,"
"621861.1194661561 259852.35739866708)"
),
"label": "M\u00fchlemattstrasse 36 <b>4410 Liestal</b>",
"lat": 47.48907470703125,
"lon": 7.728708267211914,
"num": 36,
"objectclass": "",
"origin": "address",
"rank": 7,
"x": 259852.359375,
"y": 621861.125,
"zoomlevel": 10
},
"id": 1047846,
"weight": 4
}
]
}

mocked_function.return_value = Response()

yield Response


@pytest.fixture
def requests_get_bad_request():
with patch('requests.get') as mocked_function:

class Response():
def __init__(self):
self.status_code = 400

def raise_for_status(self):
raise HTTPError()

mocked_function.return_value = Response()

yield Response


def test_address_geo_admin_source_origin_in_kwarg():
A = AddressGeoAdminSource(**{"origins": "address2"})
assert A._origins == "address2"


def test_address_geo_admin_source_origin_not_in_kwarg():
A = AddressGeoAdminSource(**{})
assert A._origins == "address"


def test_address_geo_admin_source_response(requests_get):

with patch('pyramid_oereb.core.config.Config._config', new={"srid": 2056}):
street_name = 'Mühlemattstrasse'
zip_code = 4410
street_number = 36

agas = AddressGeoAdminSource()
agas.read(None, street_name, zip_code, street_number)

assert len(agas.records) == 1
assert agas.records[0].street_name == street_name
assert agas.records[0].zip_code == zip_code
assert agas.records[0].street_number == street_number
assert abs(agas.records[0].geom.x - 2621861.6883699098) < 0.01
assert abs(agas.records[0].geom.y - 1259852.8367522908) < 0.01


def test_address_geo_admin_source_response_bad_request(requests_get_bad_request):

with pytest.raises(HTTPError):

street_name = 'Mühlemattstrasse'
zip_code = 4410
street_number = 36

agas = AddressGeoAdminSource()
agas.read(None, street_name, zip_code, street_number)

0 comments on commit cf5ec78

Please sign in to comment.