"""Test OWS territories functions.""" from unittest.mock import Mock from owsrequest import test_utils from pytest import raises from api.exceptions import OWSError from api.utils import ows_territories def test_get_territories_success(monkeypatch): """Test get_territories success.""" mock_json_response = {'foo': 'bar'} call_specs = [{ 'service': ows_territories.TERRITORIES_SERVICE, 'path': ows_territories.TERRITORIES_COMPLEMENT_ENDPOINT.format( standard='ISO_3166_1_2016'), 'status': 200, 'json': mock_json_response}] monkeypatch.setattr( ows_territories.request, 'process', test_utils.mock_ows_requests(call_specs), raising=False) result = ows_territories.get_territories() assert result == mock_json_response def test_get_territories_error(monkeypatch): """Test get_territories with error.""" ows_response_mock = Mock() monkeypatch.setattr( ows_territories.request, 'process', ows_response_mock, raising=False) with raises(OWSError): ows_territories.get_territories() assert ows_response_mock.json.called def test_get_country_mapping(): """Test _get_country_mapping successful mapping.""" api_response = { 'items': [ { 'territory_name': 'Afghanistan', 'territory_code_numeric': 4, 'orch_id': 17, 'territory_code_a2': 'AF', 'territory_code_a3': 'AFG', 'continent': 'Asia', 'standard': 'ISO_3166_1_2016' }, { 'territory_name': 'United States of America', 'territory_code_numeric': 840, 'orch_id': 1, 'territory_code_a2': 'US', 'territory_code_a3': 'USA', 'continent': 'North America', 'standard': 'ISO_3166_1_2016' }]} ows_territories.get_territories = Mock(return_value=api_response) actual = ows_territories.get_country_mapping() assert actual == {'AF': 17, 'US': 1} def test_get_country_mapping_empty(): """Test _get_country_mapping when empty response from api.""" api_response = {'foo': 'bar'} ows_territories.get_territories = Mock(return_value=api_response) with raises(KeyError): ows_territories.get_country_mapping()