From cf5ec787414c84c7e5d694d0a01ef74f9ca7753d Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 28 Nov 2023 14:06:26 +0100 Subject: [PATCH] add tests for swisstopo address source --- .coveragerc.contrib-data_sources-swisstopo | 3 + .gitignore | 1 + Makefile | 6 +- .../sources/test_address_geo_admin_source.py | 108 ++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 .coveragerc.contrib-data_sources-swisstopo create mode 100644 tests/contrib.data_sources.swisstopo/sources/test_address_geo_admin_source.py diff --git a/.coveragerc.contrib-data_sources-swisstopo b/.coveragerc.contrib-data_sources-swisstopo new file mode 100644 index 0000000000..4fba04f251 --- /dev/null +++ b/.coveragerc.contrib-data_sources-swisstopo @@ -0,0 +1,3 @@ +[run] +source = + pyramid_oereb/contrib/data_sources/swisstopo/*.py diff --git a/.gitignore b/.gitignore index ba92131023..803fb3fec7 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index e4cb5c0500..2fe14b907b 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 diff --git a/tests/contrib.data_sources.swisstopo/sources/test_address_geo_admin_source.py b/tests/contrib.data_sources.swisstopo/sources/test_address_geo_admin_source.py new file mode 100644 index 0000000000..4eb71d8d01 --- /dev/null +++ b/tests/contrib.data_sources.swisstopo/sources/test_address_geo_admin_source.py @@ -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 4410 Liestal", + "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)