Skip to content

Commit

Permalink
Added app_utils tests (#55)
Browse files Browse the repository at this point in the history
* Added app_utils tests

* Added test_dependencies

* Update test_dependencies.py

* Conflict resolution

* Update test_dependencies.py

* run swarm copy tests

* Added test_dependencies

* Fixed breaking changes

* Fixed settings test

* lint

* Remove unnecessary dependencies

* Added get_vlab_and_project tests

* Added test for get starting agent

* Added test for get_kg_token

* Added test lifespan

* lint

* unit tests

* Revert conftest.py

* Review comments

* Fixed lifespan test

* Fixed fixture

* Fixed test

---------

Co-authored-by: kanesoban <kanesoban@gmail.com>
  • Loading branch information
cszsol and kanesoban authored Dec 17, 2024
1 parent 2149c2a commit 8f54950
Show file tree
Hide file tree
Showing 7 changed files with 631 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Tool implementations without langchain or langgraph dependencies
- CRUDs.
- BlueNaas CRUD tools
- app unit tests
- Tests of AgentsRoutine.
- Unit tests for database

Expand Down
4 changes: 1 addition & 3 deletions swarm_copy/app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ async def get_vlab_and_project(
if not thread:
raise HTTPException(
status_code=404,
detail={
"detail": "Thread not found.",
},
detail="Thread not found.",
)
if thread and thread.vlab_id and thread.project_id:
vlab_and_project = {
Expand Down
75 changes: 75 additions & 0 deletions swarm_copy_tests/app/test_app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Test app utils."""

from unittest.mock import AsyncMock, patch

import pytest
from fastapi.exceptions import HTTPException
from httpx import AsyncClient

from swarm_copy.app.app_utils import setup_engine, validate_project
from swarm_copy.app.config import Settings


@pytest.mark.asyncio
async def test_validate_project(patch_required_env, httpx_mock, monkeypatch):
monkeypatch.setenv("NEUROAGENT_KEYCLOAK__VALIDATE_TOKEN", "true")
httpx_client = AsyncClient()
token = "fake_token"
test_vp = {"vlab_id": "test_vlab_DB", "project_id": "project_id_DB"}
vlab_url = "https://openbluebrain.com/api/virtual-lab-manager/virtual-labs"

# test with bad config
httpx_mock.add_response(
url=f'{vlab_url}/{test_vp["vlab_id"]}/projects/{test_vp["project_id"]}',
status_code=404,
)
with pytest.raises(HTTPException) as error:
await validate_project(
httpx_client=httpx_client,
vlab_id=test_vp["vlab_id"],
project_id=test_vp["project_id"],
token=token,
vlab_project_url=vlab_url,
)
assert error.value.status_code == 401

# test with good config
httpx_mock.add_response(
url=f'{vlab_url}/{test_vp["vlab_id"]}/projects/{test_vp["project_id"]}',
json="test_project_ID",
)
await validate_project(
httpx_client=httpx_client,
vlab_id=test_vp["vlab_id"],
project_id=test_vp["project_id"],
token=token,
vlab_project_url=vlab_url,
)
# we jsut want to assert that the httpx_mock was called.


@patch("neuroagent.app.app_utils.create_async_engine")
def test_setup_engine(create_engine_mock, monkeypatch, patch_required_env):
create_engine_mock.return_value = AsyncMock()

monkeypatch.setenv("NEUROAGENT_DB__PREFIX", "prefix")

settings = Settings()

connection_string = "postgresql+asyncpg://user:password@localhost/dbname"
retval = setup_engine(settings=settings, connection_string=connection_string)
assert retval is not None


@patch("neuroagent.app.app_utils.create_async_engine")
def test_setup_engine_no_connection_string(
create_engine_mock, monkeypatch, patch_required_env
):
create_engine_mock.return_value = AsyncMock()

monkeypatch.setenv("NEUROAGENT_DB__PREFIX", "prefix")

settings = Settings()

retval = setup_engine(settings=settings, connection_string=None)
assert retval is None
71 changes: 71 additions & 0 deletions swarm_copy_tests/app/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Test config"""

import pytest
from pydantic import ValidationError

from swarm_copy.app.config import Settings


def test_required(monkeypatch, patch_required_env):
settings = Settings()

assert settings.tools.literature.url == "https://fake_url"
assert settings.knowledge_graph.base_url == "https://fake_url/api/nexus/v1"
assert settings.openai.token.get_secret_value() == "dummy"

# make sure not case sensitive
monkeypatch.delenv("NEUROAGENT_TOOLS__LITERATURE__URL")
monkeypatch.setenv("neuroagent_tools__literature__URL", "https://new_fake_url")

settings = Settings()
assert settings.tools.literature.url == "https://new_fake_url"


def test_no_settings():
# We get an error when no custom variables provided
with pytest.raises(ValidationError):
Settings()


def test_setup_tools(monkeypatch, patch_required_env):
monkeypatch.setenv("NEUROAGENT_TOOLS__TRACE__SEARCH_SIZE", "20")
monkeypatch.setenv("NEUROAGENT_TOOLS__MORPHO__SEARCH_SIZE", "20")
monkeypatch.setenv("NEUROAGENT_TOOLS__KG_MORPHO_FEATURES__SEARCH_SIZE", "20")

monkeypatch.setenv("NEUROAGENT_KEYCLOAK__USERNAME", "user")
monkeypatch.setenv("NEUROAGENT_KEYCLOAK__PASSWORD", "pass")

settings = Settings()

assert settings.tools.morpho.search_size == 20
assert settings.tools.trace.search_size == 20
assert settings.tools.kg_morpho_features.search_size == 20
assert settings.keycloak.username == "user"
assert settings.keycloak.password.get_secret_value() == "pass"


def test_check_consistency(monkeypatch):
# We get an error when no custom variables provided
url = "https://fake_url"
monkeypatch.setenv("NEUROAGENT_TOOLS__LITERATURE__URL", url)
monkeypatch.setenv("NEUROAGENT_KNOWLEDGE_GRAPH__URL", url)

with pytest.raises(ValueError):
Settings()

monkeypatch.setenv("NEUROAGENT_GENERATIVE__OPENAI__TOKEN", "dummy")
monkeypatch.setenv("NEUROAGENT_KEYCLOAK__VALIDATE_TOKEN", "true")

with pytest.raises(ValueError):
Settings()

monkeypatch.setenv("NEUROAGENT_KEYCLOAK__VALIDATE_TOKEN", "false")

with pytest.raises(ValueError):
Settings()

monkeypatch.setenv("NEUROAGENT_KNOWLEDGE_GRAPH__BASE_URL", "http://fake_nexus.com")
monkeypatch.setenv("NEUROAGENT_KEYCLOAK__VALIDATE_TOKEN", "true")
monkeypatch.setenv("NEUROAGENT_KEYCLOAK__PASSWORD", "Hello")

Settings()
Loading

0 comments on commit 8f54950

Please sign in to comment.