Skip to content

Commit

Permalink
fix(tests): more coverage, more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Avantol13 committed Oct 30, 2023
1 parent ddf1518 commit 098770a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
31 changes: 21 additions & 10 deletions gen3discoveryai/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import traceback
from contextlib import asynccontextmanager
from importlib.metadata import version
import traceback

import fastapi
import yaml
Expand Down Expand Up @@ -105,19 +105,14 @@ async def lifespan(fastapi_app: fastapi.FastAPI):
}
config.topics[topic].update(topic_raw_cfg["metadata"])

chain_instance = chain_factory.get(
topic_raw_cfg["topic_chain"],
_create_and_register_topic_chain(
topic=topic,
metadata=config.topics[topic],
)
config.topics[topic].update(
{
"topic_chain": chain_instance,
}
topic_raw_cfg=topic_raw_cfg,
chain_factory=chain_factory,
)

logging.info(f"Added topic `{topic}`")
logging.debug(f"`{topic}` configuration: `{topic_raw_cfg}`")

except Exception as exc:
logging.error(
f"Unable to load `{topic}` configuration with: {topic_raw_cfg}. "
Expand All @@ -135,4 +130,20 @@ async def lifespan(fastapi_app: fastapi.FastAPI):
config.topics.clear()


def _create_and_register_topic_chain(topic, topic_raw_cfg, chain_factory):
"""
Small helper function to create instance of the topic chain and add to the config
"""
chain_instance = chain_factory.get(
topic_raw_cfg["topic_chain"],
topic=topic,
metadata=config.topics[topic],
)
config.topics[topic].update(
{
"topic_chain": chain_instance,
}
)


app = get_app()
37 changes: 35 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import importlib
import os
from unittest.mock import patch

import pytest

from gen3discoveryai import config
from gen3discoveryai.main import _override_generated_openapi_spec
from gen3discoveryai import config, main
from gen3discoveryai.main import _override_generated_openapi_spec, lifespan
from gen3discoveryai.topic_chains.utils import get_from_cfg_metadata


Expand All @@ -21,6 +22,38 @@ def test_bad_config_metadata():
os.chdir(os.path.dirname(os.path.abspath(__file__)).rstrip("/") + "/..")


@pytest.mark.asyncio
@patch("gen3discoveryai.main._create_and_register_topic_chain")
async def test_bad_config_default_topic(create_and_register_topic_chain):
"""
Test when config loading raises an error, either it's reraised if it's the default topic
"""
create_and_register_topic_chain.side_effect = Exception("some expection")

with pytest.raises(Exception):
async with lifespan(main.app):
pass


@pytest.mark.asyncio
@patch("gen3discoveryai.main._create_and_register_topic_chain")
async def test_bad_config_non_default_topic(create_and_register_topic_chain):
"""
Test when config loading raises an error, either it's reraised if it's the default topic
"""

def _exception_if_default(*args, **kwargs):
# simulate default topic being okay, e.g. don't raise error here
if kwargs.get("topic") == "default":
pass

create_and_register_topic_chain.side_effect = _exception_if_default

async with lifespan(main.app):
# we don't expect an exception for non default topics
assert True


def test_metadata_cfg_util():
"""
If it exists, return it
Expand Down

0 comments on commit 098770a

Please sign in to comment.