Skip to content

Commit

Permalink
Merge pull request wise-agents#375 from kabir/tests_use_existing_env_…
Browse files Browse the repository at this point in the history
…vars

wise-agents#251 Don't set env vars in tests.
  • Loading branch information
maeste authored Oct 1, 2024
2 parents 6272de2 + a31c9cf commit 99e167a
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 165 deletions.
5 changes: 4 additions & 1 deletion tests/wiseagents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# This is the __init__.py file for the wiseagents package

# Import any modules or subpackages here
from .testing_utils import assert_env_var_set, assert_env_vars_set, assert_standard_variables_set

# Define any necessary initialization code here

# Optionally, you can define __all__ to specify the public interface of the package
# __all__ = ['module1', 'module2', 'subpackage']
# __all__ = ['module1', 'module2', 'subpackage']

__all__ = ['assert_env_var_set', 'assert_env_vars_set', 'assert_standard_variables_set']
35 changes: 4 additions & 31 deletions tests/wiseagents/agents/test_graph_rag_challenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from wiseagents.graphdb import Entity, GraphDocument, Neo4jLangChainWiseAgentGraphDB, Relationship, Source
from wiseagents.llm import OpenaiAPIWiseAgentLLM
from wiseagents.transports import StompWiseAgentTransport
from tests.wiseagents import assert_standard_variables_set

cond = threading.Condition()
assertError : AssertionError = None
Expand All @@ -19,11 +20,10 @@
collection_name = "test-vector-db"
@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():

assert_standard_variables_set()

# Ensure that nothing exists the graph DB
original_neo4j_username = os.environ.get("NEO4J_USERNAME")
os.environ["NEO4J_USERNAME"] = "neo4j"
original_neo4j_password = os.environ.get("NEO4J_PASSWORD")
os.environ["NEO4J_PASSWORD"] = "neo4jpassword"
graph_db = Neo4jLangChainWiseAgentGraphDB(properties=["name", "type"], collection_name=collection_name,
url="bolt://localhost:7687", refresh_graph_schema=False)
graph_db.query("MATCH (n)-[r]-() DELETE r")
Expand Down Expand Up @@ -54,33 +54,6 @@ def run_after_all_tests():
graph_db.delete_vector_db()
graph_db.close()

# Clean up environment variables
if original_neo4j_username is None:
del os.environ["NEO4J_USERNAME"]
else:
os.environ["NEO4J_USERNAME"] = original_neo4j_username
if original_neo4j_password is None:
del os.environ["NEO4J_PASSWORD"]
else:
os.environ["NEO4J_PASSWORD"] = original_neo4j_password






def set_env_variable(env_variable: str, value: str) -> str:
original_value = os.environ.get(env_variable)
os.environ[env_variable] = value
return original_value


def reset_env_variable(env_variable: str, original_value: str):
if original_value is None:
del os.environ[env_variable]
else:
os.environ[env_variable] = original_value


def get_connection_string():
return f"postgresql+psycopg://{os.environ['POSTGRES_USER']}:{os.environ['POSTGRES_PASSWORD']}@localhost:6024/{os.environ['POSTGRES_DB']}"
Expand Down
3 changes: 3 additions & 0 deletions tests/wiseagents/agents/test_phased_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
PhasedCoordinatorWiseAgent
from wiseagents.llm import OpenaiAPIWiseAgentLLM
from wiseagents.transports import StompWiseAgentTransport
from tests.wiseagents import assert_standard_variables_set

cond = threading.Condition()
assertError : AssertionError = None

@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand All @@ -37,6 +39,7 @@ def test_phased_coordinator():
"""
try:
groq_api_key = os.getenv("GROQ_API_KEY")

global assertError

llm = OpenaiAPIWiseAgentLLM(system_message="You are a helpful assistant.",
Expand Down
31 changes: 2 additions & 29 deletions tests/wiseagents/agents/test_rag_challenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
from wiseagents.llm import OpenaiAPIWiseAgentLLM
from wiseagents.transports import StompWiseAgentTransport
from wiseagents.vectordb import Document, PGVectorLangChainWiseAgentVectorDB
from tests.wiseagents import assert_standard_variables_set

cond = threading.Condition()
assertError : AssertionError = None


@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
original_postgres_user = set_env_variable("POSTGRES_USER", "postgres")
original_postgres_password = set_env_variable("POSTGRES_PASSWORD", "postgres")
original_postgres_db = set_env_variable("POSTGRES_DB", "postgres")
original_stomp_user = set_env_variable("STOMP_USER", "artemis")
original_stomp_password = set_env_variable("STOMP_PASSWORD", "artemis")

assert_standard_variables_set()
# Vector DB set up
pg_vector_db = PGVectorLangChainWiseAgentVectorDB(get_connection_string())
pg_vector_db.delete_collection("wise-agents-collection")
Expand All @@ -40,29 +36,6 @@ def run_after_all_tests():
# Vector DB clean up
pg_vector_db.delete_collection("wise-agents-collection")

# Clean up environment variables
reset_env_variable("POSTGRES_USER", original_postgres_user)
reset_env_variable("POSTGRES_PASSWORD", original_postgres_password)
reset_env_variable("POSTGRES_DB", original_postgres_db)
reset_env_variable("STOMP_USER", original_stomp_user)
reset_env_variable("STOMP_PASSWORD", original_stomp_password)





def set_env_variable(env_variable: str, value: str) -> str:
original_value = os.environ.get(env_variable)
os.environ[env_variable] = value
return original_value


def reset_env_variable(env_variable: str, original_value: str):
if original_value is None:
del os.environ[env_variable]
else:
os.environ[env_variable] = original_value


def get_connection_string():
return f"postgresql+psycopg://{os.environ['POSTGRES_USER']}:{os.environ['POSTGRES_PASSWORD']}@localhost:6024/{os.environ['POSTGRES_DB']}"
Expand Down
2 changes: 2 additions & 0 deletions tests/wiseagents/agents/test_sequential_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SequentialMemoryCoordinatorWiseAgent
from wiseagents.llm import OpenaiAPIWiseAgentLLM, WiseAgentLLM
from wiseagents.transports import StompWiseAgentTransport
from tests.wiseagents import assert_standard_variables_set

cond1 = threading.Condition()
cond2 = threading.Condition()
Expand All @@ -20,6 +21,7 @@

@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand Down
3 changes: 2 additions & 1 deletion tests/wiseagents/agents/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
import os
import threading

import pytest
Expand All @@ -9,10 +8,12 @@
from wiseagents.agents import LLMWiseAgentWithTools, PassThroughClientAgent
from wiseagents.llm import OpenaiAPIWiseAgentLLM
from wiseagents.transports.stomp import StompWiseAgentTransport
from tests.wiseagents import assert_standard_variables_set


@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand Down
2 changes: 2 additions & 0 deletions tests/wiseagents/agents/test_tools_fakeLLM.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from wiseagents.agents import LLMWiseAgentWithTools, PassThroughClientAgent
from wiseagents.llm import WiseAgentRemoteLLM
from wiseagents.transports.stomp import StompWiseAgentTransport
from tests.wiseagents import assert_standard_variables_set


@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand Down
2 changes: 2 additions & 0 deletions tests/wiseagents/agents/test_tools_groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from wiseagents.agents import LLMWiseAgentWithTools, PassThroughClientAgent
from wiseagents.llm import OpenaiAPIWiseAgentLLM
from wiseagents.transports.stomp import StompWiseAgentTransport
from tests.wiseagents import assert_standard_variables_set


@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand Down
44 changes: 2 additions & 42 deletions tests/wiseagents/cli/test_WiseAgentCli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
from wiseagents.cli.wise_agent_cli import main
from wiseagents.graphdb import Entity, GraphDocument, Neo4jLangChainWiseAgentGraphDB, Relationship, Source
from wiseagents.vectordb import Document, PGVectorLangChainWiseAgentVectorDB

from tests.wiseagents import assert_standard_variables_set

@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
original_postgres_user = set_env_variable("POSTGRES_USER", "postgres")
original_postgres_password = set_env_variable("POSTGRES_PASSWORD", "postgres")
original_postgres_db = set_env_variable("POSTGRES_DB", "postgres")
original_stomp_user = set_env_variable("STOMP_USER", "artemis")
original_stomp_password = set_env_variable("STOMP_PASSWORD", "artemis")
original_neo4j_username = set_env_variable("NEO4J_USERNAME", "neo4j")
original_neo4j_password = set_env_variable("NEO4J_PASSWORD", "neo4jpassword")
assert_standard_variables_set()

# Vector DB set up
pg_vector_db = PGVectorLangChainWiseAgentVectorDB(get_connection_string())
Expand Down Expand Up @@ -60,45 +54,11 @@ def run_after_all_tests():
graph_db.delete_vector_db()
graph_db.close()

# Clean up environment variables
reset_env_variable("POSTGRES_USER", original_postgres_user)
reset_env_variable("POSTGRES_PASSWORD", original_postgres_password)
reset_env_variable("POSTGRES_DB", original_postgres_db)
reset_env_variable("STOMP_USER", original_stomp_user)
reset_env_variable("STOMP_PASSWORD", original_stomp_password)
reset_env_variable("NEO4J_USERNAME", original_neo4j_username)
reset_env_variable("NEO4J_PASSWORD", original_neo4j_password)




def set_env_variable(env_variable: str, value: str) -> str:
original_value = os.environ.get(env_variable)
os.environ[env_variable] = value
return original_value


def reset_env_variable(env_variable: str, original_value: str):
if original_value is None:
del os.environ[env_variable]
else:
os.environ[env_variable] = original_value


def get_connection_string():
return f"postgresql+psycopg://{os.environ['POSTGRES_USER']}:{os.environ['POSTGRES_PASSWORD']}@localhost:6024/{os.environ['POSTGRES_DB']}"


def set_env(monkeypatch):
"""
This test requires a running pgvector instance. The required
vector database can be started using the run_vectordb.sh script.
"""
monkeypatch.setenv("POSTGRES_USER", "postgres")
monkeypatch.setenv("POSTGRES_PASSWORD", "postgres")
monkeypatch.setenv("POSTGRES_DB", "postgres")


@pytest.mark.needsllm
def test_cli_with_rag_agent(monkeypatch, pytestconfig, capsys):
inputs = ['/load-agents', '', '/send', 'RAGWiseAgent1', 'Who won the NBA championship in 2024?', '/exit']
Expand Down
41 changes: 7 additions & 34 deletions tests/wiseagents/graphdb/test_Neo4jLangChainWiseAgentGraphDB.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import os

import pytest

from wiseagents.graphdb import Entity, GraphDocument, Neo4jLangChainWiseAgentGraphDB, Relationship, Source
from tests.wiseagents import assert_standard_variables_set

collection_name = "test-vector-db"
@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
# Ensure that nothing exists the graph DB
original_neo4j_username = os.environ.get("NEO4J_USERNAME")
os.environ["NEO4J_USERNAME"] = "neo4j"
original_neo4j_password = os.environ.get("NEO4J_PASSWORD")
os.environ["NEO4J_PASSWORD"] = "neo4jpassword"
graph_db = Neo4jLangChainWiseAgentGraphDB(properties=["name", "type"], collection_name=collection_name,
url="bolt://localhost:7687", refresh_graph_schema=False)
graph_db.query("MATCH (n)-[r]-() DELETE r")
Expand All @@ -27,30 +23,9 @@ def run_after_all_tests():
graph_db.delete_vector_db()
graph_db.close()

# Clean up environment variables
if original_neo4j_username is None:
del os.environ["NEO4J_USERNAME"]
else:
os.environ["NEO4J_USERNAME"] = original_neo4j_username
if original_neo4j_password is None:
del os.environ["NEO4J_PASSWORD"]
else:
os.environ["NEO4J_PASSWORD"] = original_neo4j_password


def set_env(monkeypatch):
"""
This test requires a running Neo4j instance on bolt://localhost:7687. The required
graph database can be started using the run_graphdb.sh script.
"""
monkeypatch.setenv("NEO4J_USERNAME", "neo4j")
assert os.environ.get("NEO4J_USERNAME") == "neo4j"
monkeypatch.setenv("NEO4J_PASSWORD", "neo4jpassword")
assert os.environ.get("NEO4J_PASSWORD") == "neo4jpassword"


def test_insert_graph_documents_and_query(monkeypatch):
set_env(monkeypatch)


def test_insert_graph_documents_and_query():
graph_db = Neo4jLangChainWiseAgentGraphDB(properties=["name", "type"], collection_name=collection_name,
url="bolt://localhost:7687", refresh_graph_schema=False)

Expand Down Expand Up @@ -96,8 +71,7 @@ def test_insert_graph_documents_and_query(monkeypatch):



def test_insert_entity_and_query(monkeypatch):
set_env(monkeypatch)
def test_insert_entity_and_query():
graph_db = Neo4jLangChainWiseAgentGraphDB(properties=["name", "type"], collection_name=collection_name,
url="bolt://localhost:7687", refresh_graph_schema=False)

Expand All @@ -115,8 +89,7 @@ def test_insert_entity_and_query(monkeypatch):
graph_db.close()


def test_insert_relationship_and_query(monkeypatch):
set_env(monkeypatch)
def test_insert_relationship_and_query():
graph_db = Neo4jLangChainWiseAgentGraphDB(properties=["name", "type"], collection_name=collection_name,
url="bolt://localhost:7687", refresh_graph_schema=False)

Expand Down
4 changes: 2 additions & 2 deletions tests/wiseagents/llm/test_WiseAgentRemoteLLM.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest

from wiseagents import WiseAgentRegistry
from wiseagents.llm import OpenaiAPIWiseAgentLLM

from tests.wiseagents import assert_standard_variables_set

@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand Down
3 changes: 2 additions & 1 deletion tests/wiseagents/test_WiseAgentRegistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

from wiseagents import WiseAgent, WiseAgentContext, WiseAgentMessage, WiseAgentMetaData, WiseAgentRegistry, WiseAgentTransport
from wiseagents.transports.stomp import StompWiseAgentTransport

from tests.wiseagents import assert_standard_variables_set

@pytest.fixture(scope="session", autouse=True)
def run_after_all_tests():
assert_standard_variables_set()
yield


Expand Down
Loading

0 comments on commit 99e167a

Please sign in to comment.