"""Unit tests for the test utility functions.""" import json as json_tools import pytest from owsrequest import test_utils @pytest.fixture def call_spec(): """Return an ows call spec for testing.""" return { 'service': 'ows-tiger', 'path': '/rainbow/unicorn/123', 'status': 200, 'json': { 'lions': 'crown', 'tigers': 'woohoo', 'bears': 'honey'}} def test_mock_ows_requests_matched_spec(call_spec): """Test that the expected status and json payload are returned.""" mock_method = test_utils.mock_ows_requests([call_spec]) result = mock_method(call_spec['service'], call_spec['path']) assert result.status_code == call_spec['status'] assert result.json() == call_spec['json'] assert json_tools.loads(result.text) == call_spec['json'] def test_regex_call_spec(): """Test that regex path matcher works.""" spec = { 'service': 'ows-tiger', 'path_regex': r'/rainbow/[0-9]+\Z', 'status': 200, 'json': { 'lions': 'foo' }} mock_method = test_utils.mock_ows_requests([spec]) matching_result = mock_method(spec['service'], '/rainbow/123') assert matching_result.status_code == spec['status'] assert matching_result.json() == spec['json'] assert json_tools.loads(matching_result.text) == spec['json'] def test_mock_ows_requests_multiple_specs(call_spec): """Test that the expected status and json payload are returned.""" another_call_spec = { 'service': 'ows-jaguar', 'path': '/jaguar-path/12345', 'json': None, 'text': 'null', 'status': 404} call_specs = [call_spec, another_call_spec] mock_method = test_utils.mock_ows_requests(call_specs) result_one = mock_method(call_spec['service'], call_spec['path']) result_two = mock_method( another_call_spec['service'], another_call_spec['path']) assert result_one.status_code == call_spec['status'] assert result_one.json() == call_spec['json'] assert json_tools.loads(result_one.text) == call_spec['json'] assert result_two.status_code == another_call_spec['status'] assert result_two.json() == another_call_spec['json'] assert json_tools.loads(result_two.text) == another_call_spec['json'] def test_mock_ows_reest_spec_not_matched(call_spec): """Test that an exception is raised if an un-specced call is made.""" mock_method = test_utils.mock_ows_requests([call_spec]) with pytest.raises(Exception): mock_method('another-service', '/what/is/this/path/who/knows')