-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,359 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Sarm copy tests.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.