diff --git a/neo4j-app/neo4j_app/core/config.py b/neo4j-app/neo4j_app/core/config.py index 07b71d43..3d657dc7 100644 --- a/neo4j-app/neo4j_app/core/config.py +++ b/neo4j-app/neo4j_app/core/config.py @@ -3,7 +3,6 @@ import configparser import functools import logging -import re import sys from configparser import ConfigParser from logging.handlers import SysLogHandler @@ -66,8 +65,11 @@ class AppConfig(LowerCamelCaseModel, IgnoreExtraModel): _global: Optional[AppConfig] = None @validator("neo4j_import_batch_size") - def neo4j_import_batch_size_must_be_less_than_max_records_in_memory( # pylint: disable=no-self-argument - cls, v, values + def neo4j_import_batch_size_must_be_less_than_max_records_in_memory( + # pylint: disable=no-self-argument + cls, + v, + values, ): max_records = values["neo4j_app_max_records_in_memory"] if v > max_records: @@ -126,34 +128,6 @@ def to_uvicorn(self) -> UviCornModel: def neo4j_uri(self) -> str: return f"{self.neo4j_uri_scheme}://{self.neo4j_host}:{self.neo4j_port}" - @functools.cached_property - def es_host(self) -> str: - try: - host = self._es_address_match.group("host") - except IndexError as e: - msg = f"Couldn't find host name in {self.elasticsearch_address}" - raise ValueError(msg) from e - if isinstance(host, tuple): - msg = f"Found several potential hosts in {self.elasticsearch_address}" - raise ValueError(msg) - return host - - @functools.cached_property - def es_port(self) -> int: - try: - port = self._es_address_match.group("port") - except IndexError as e: - msg = f"Couldn't find port name in {self.elasticsearch_address}" - raise ValueError(msg) from e - if isinstance(port, tuple): - msg = f"Found several potential ports in {self.elasticsearch_address}" - raise ValueError(msg) - return int(port) - - @functools.cached_property - def es_hosts(self) -> List[Dict]: - return [{"host": self.es_host, "port": self.es_port}] - def to_neo4j_driver(self) -> neo4j.AsyncDriver: auth = None if self.neo4j_password: @@ -173,8 +147,7 @@ def to_es_client(self) -> ESClientABC: client_cls = OSClient if self.neo4j_app_uses_opensearch else ESClient # TODO: read the index name in a secure manner... client = client_cls( - hosts=[self.es_host], - port=self.es_port, + hosts=[self.elasticsearch_address], pagination=self.es_default_page_size, max_concurrency=self.es_max_concurrency, ) @@ -230,18 +203,6 @@ def _neo4j_app_syslog_facility_int(self) -> int: msg = f"Invalid syslog facility {self.neo4j_app_syslog_facility}" raise ValueError(msg) from e - @functools.cached_property - def _es_address_match(self) -> re.Match: - # It's acceptable not to pre-compile the regex here, it will only be called once - match = re.match( - r"^.*://(?P.*):(?P\d{4})$", self.elasticsearch_address - ) - if match is None: - raise ValueError( - f"Ill formatted elasticsearch address: {self.elasticsearch_address}" - ) - return match - class UviCornModel(BaseICIJModel): host: str = Field(default="127.0.0.1", const=True) diff --git a/neo4j-app/neo4j_app/tests/conftest.py b/neo4j-app/neo4j_app/tests/conftest.py index 02cea209..ca2d72c9 100644 --- a/neo4j-app/neo4j_app/tests/conftest.py +++ b/neo4j-app/neo4j_app/tests/conftest.py @@ -3,8 +3,9 @@ import contextlib import os import random +import traceback from pathlib import Path -from typing import Any, AsyncGenerator, Dict, Generator, Tuple, Union +from typing import Any, AsyncGenerator, Dict, Generator, Optional, Tuple, Union import neo4j import pytest @@ -422,3 +423,14 @@ def xml_elements_equal(actual, expected) -> bool: if len(actual) != len(expected): return False return all(xml_elements_equal(c1, c2) for c1, c2 in zip(actual, expected)) + + +@contextlib.contextmanager +def fail_if_exception(msg: Optional[str] = None): + try: + yield + except Exception as e: # pylint: disable=W0703 + trace = "".join(traceback.format_exception(None, e, e.__traceback__)) + if msg is None: + msg = "Test failed due to the following error" + pytest.fail(f"{msg}\n{trace}") diff --git a/neo4j-app/neo4j_app/tests/core/test_config.py b/neo4j-app/neo4j_app/tests/core/test_config.py index 396f322b..ac2dd361 100644 --- a/neo4j-app/neo4j_app/tests/core/test_config.py +++ b/neo4j-app/neo4j_app/tests/core/test_config.py @@ -5,6 +5,7 @@ from pydantic import ValidationError from neo4j_app.core import AppConfig, UviCornModel +from neo4j_app.tests.conftest import fail_if_exception def test_should_support_alias(): @@ -89,16 +90,16 @@ def test_to_uvicorn(config: AppConfig, expected_uvicorn_config: UviCornModel): assert uvicorn_config == expected_uvicorn_config -def test_should_parse_elasticsearch_address(): +@pytest.mark.pull(id="") +def test_should_support_address_without_port(): # Given config = AppConfig( - elasticsearch_address="http://elasticsearch:9222", + elasticsearch_address="http://elasticsearch", neo4j_project="test-project", ) # Then - assert config.es_host == "elasticsearch" - assert config.es_port == 9222 - assert config.es_hosts == [{"host": "elasticsearch", "port": 9222}] + with fail_if_exception("Failed to initialize ES client"): + config.to_es_client() @pytest.mark.parametrize("user,password", [(None, "somepass"), ("someuser", None)])