"""Unit tests for services.""" import json from typing import Tuple import pytest from _pytest.monkeypatch import MonkeyPatch from pytest_mock import MockerFixture from owsclient.services import ( discover_service_url, get_owsclient_service_map, ) @pytest.mark.parametrize( "environment, service_name, expected", [ ( "qa", "ows-permissions", ("qa", "https://qa-ows-permissions.theorchard.io"), ), ( "prod", "ows-permissions", ("prod", "https://prod-ows-permissions.theorchard.io"), ), ( "dev", "ows-dmp", ("qa", "https://qa-ows-dmp.theorchard.io"), ), ( "uat", "ows-abacus-event", ("uat", "https://uat-ows-abacus-event.theorchard.io"), ), ], ) def test_discover_service_url( environment: str, service_name: str, expected: Tuple[str, str] ) -> None: """Test discover_service_url.""" service_url = discover_service_url(environment, service_name) assert service_url == expected def test_get_owsclient_service_map( monkeypatch: MonkeyPatch, ) -> None: """Test get_owsclient_service_map.""" service_mapping = { "ows-dmp": "http://localhost:5001", } monkeypatch.setenv("OWSREQUEST_SERVICE_MAP", json.dumps(service_mapping)) assert get_owsclient_service_map() == service_mapping def test_get_owsclient_service_map_empty( monkeypatch: MonkeyPatch, ) -> None: """Test get_owsclient_service_map when service map is empty.""" monkeypatch.setenv("OWSREQUEST_SERVICE_MAP", "") assert get_owsclient_service_map() == {} def test_get_owsclient_service_map_invalid( monkeypatch: MonkeyPatch, ) -> None: """Test get_owsclient_service_map when service map is invalid..""" monkeypatch.setenv("OWSREQUEST_SERVICE_MAP", "invalid") assert get_owsclient_service_map() == {} def test_discover_service_url_dev(mocker: MockerFixture) -> None: """Test discover_service_url for dev environment.""" mocker.patch( "owsclient.services.owsclient_service_map", new={ "ows-dmp": "http://localhost:5001", }, ) environment, service_url = discover_service_url("dev", "ows-dmp") assert environment == "dev" assert service_url == "http://localhost:5001" def test_discover_service_url_uat(mocker: MockerFixture) -> None: """Test discover_service_url for uat environment.""" mocker.patch( "owsclient.services.owsclient_service_map", new={ "ows-dmp": "http://localhost:5001", "ows-abacus-event": "http://localhost:5002", }, ) environment, service_url = discover_service_url("uat", "ows-abacus-event") assert environment == "uat" assert service_url == "http://localhost:5002"