Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update-tests #48

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
run: pip3 install -r requirements.txt # Adjust this according to your project

- name: Run tests with coverage
run: coverage run -m pytest --cache-clear aiagents4pharma
run: coverage run -m pytest --cache-clear aiagents4pharma/talk2biomodels/tests/

- name: Check coverage
run: |
Expand Down Expand Up @@ -105,7 +105,7 @@ jobs:
run: pip3 install -r requirements.txt # Adjust this according to your project

- name: Run tests with coverage
run: coverage run -m pytest --cache-clear
run: coverage run -m pytest --cache-clear aiagents4pharma/talk2biomodels/tests/

- name: Check coverage
run: |
Expand Down
101 changes: 46 additions & 55 deletions aiagents4pharma/talk2biomodels/tests/test_ask_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import pytest
import streamlit as st
from langchain_core.callbacks import CallbackManagerForToolRun
from ..tools.ask_question import AskQuestionTool, AskQuestionInput, ModelData
from ..models.basico_model import BasicoModel

Expand All @@ -13,101 +12,93 @@ def ask_question_tool_fixture():
'''
Fixture for creating an instance of AskQuestionTool.
'''
return AskQuestionTool()
return AskQuestionTool(st_session_key="test_key",
sys_bio_model=ModelData(
sbml_file_path="aiagents4pharma/talk2biomodels/tests//BIOMD0000000064_url.xml"
)
)

@pytest.fixture(name="ask_question_tool_with_model_id")
def ask_question_tool__with_model_id_fixture():
'''
Fixture for creating an instance of AskQuestionTool.
'''
return AskQuestionTool(st_session_key="test_key",
sys_bio_model=ModelData(modelid=64))

@pytest.fixture(name="input_data", scope="module")
def input_data_fixture():
'''
Fixture for creating an instance of AskQuestionInput.
'''
return AskQuestionInput(question="What is the concentration of Pyruvate at time 5?",
sys_bio_model=ModelData(modelid=64),
st_session_key="test_key"
)
return AskQuestionInput(question="What is the concentration of Pyruvate at time 5?")

def test_run_with_sbml_file(input_data, ask_question_tool):
'''
Test the _run method of the AskQuestionTool class with a valid session key and model data.
Test the _run method of the AskQuestionTool class
with a valid session key and model data.
'''
input_data.sys_bio_model = ModelData(sbml_file_path="./BIOMD0000000064_url.xml")
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key)
result = ask_question_tool.invoke(input={'question':input_data.question})
assert isinstance(result, str)

def test_run_manager(input_data, ask_question_tool):
def test_run_manager(input_data, ask_question_tool_with_model_id):
'''
Test the run manager of the AskQuestionTool class.
'''
run_manager = CallbackManagerForToolRun(run_id=1, handlers=[], inheritable_handlers=False)
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key,
run_manager=run_manager)
assert isinstance(result, str)
run_manager = CallbackManagerForToolRun(run_id=1,
handlers=[],
inheritable_handlers=False,
metadata={"prompt": "Answer the question carefully."})
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key,
run_manager=run_manager)
ask_question_tool_with_model_id.metadata = {
"prompt": "Answer the question carefully."
}
result = ask_question_tool_with_model_id.invoke(input={'question':input_data.question})
assert isinstance(result, str)

def test_run_with_no_model_data_at_all(input_data, ask_question_tool):
'''
Test the _run method of the AskQuestionTool class with a valid session key and model data.
Test the _run method of the AskQuestionTool class
with a valid session key and NO model data.
'''
result = ask_question_tool.call_run(question=input_data.question,
st_session_key=input_data.st_session_key)
ask_question_tool.sys_bio_model = ModelData()
result = ask_question_tool.invoke(input={'question':input_data.question})
assert isinstance(result, str)

def test_run_with_session_key(input_data, ask_question_tool):
'''
Test the _run method of the AskQuestionTool class with a missing session key.
Test the _run method of the AskQuestionTool class
with a missing session key.
'''
input_data.sys_bio_model = ModelData(modelid=64)
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key)
del st.session_state["test_key"]
result = ask_question_tool.invoke(input={'question':input_data.question})
assert isinstance(result, str)

def test_run_with_none_key(input_data, ask_question_tool):
'''
Test the _run method of the AskQuestionTool class with a None session key.
Test the _run method of the AskQuestionTool class
with a None session key.
'''
input_data.st_session_key = None
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key)
ask_question_tool.st_session_key = None
result = ask_question_tool.invoke(input={'question':input_data.question})
assert isinstance(result, str)
input_data.sys_bio_model = ModelData()
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key)
ask_question_tool.sys_bio_model = ModelData()
result = ask_question_tool.invoke(input={'question':input_data.question})
# No model data or object in the streeamlit key
assert result == "Please provide a valid model object or \
Streamlit session key that contains the model object."
input_data.st_session_key = "test_key"
# delete the session key form the session state
st.session_state.pop(input_data.st_session_key, None)
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key)
assert result == f"Session key {input_data.st_session_key} " \
"not found in Streamlit session state."
del st.session_state["test_key"]
ask_question_tool.st_session_key = "test_key"
result = ask_question_tool.invoke(input={'question':input_data.question})
expected_result = f"Session key {ask_question_tool.st_session_key} "
expected_result += "not found in Streamlit session state."
assert result == expected_result

def test_run_with_a_simulated_model(input_data, ask_question_tool):
'''
Test the _run method of the AskQuestionTool class with a valid session key and model data.
Test the _run method of the AskQuestionTool class
with a valid session key and model data.
'''
model = BasicoModel(model_id=64)
model.simulate(duration=2, interval=2)
input_data.sys_bio_model = ModelData(model_object=model)
result = ask_question_tool.call_run(question=input_data.question,
sys_bio_model=input_data.sys_bio_model,
st_session_key=input_data.st_session_key)
ask_question_tool.sys_bio_model = ModelData(model_object=model)
result = ask_question_tool.invoke(input={'question':input_data.question})
assert isinstance(result, str)

def test_get_metadata(ask_question_tool):
Expand Down
6 changes: 4 additions & 2 deletions aiagents4pharma/talk2biomodels/tests/test_basico_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ def test_with_sbml_file():
"""
Test initialization of BasicoModel with sbml_file_path.
"""
model_object = BasicoModel(sbml_file_path="./BIOMD0000000064_url.xml")
assert model_object.sbml_file_path == "./BIOMD0000000064_url.xml"
model_object = BasicoModel(
sbml_file_path="aiagents4pharma/talk2biomodels/tests/BIOMD0000000064_url.xml")
assert model_object.sbml_file_path == \
"aiagents4pharma/talk2biomodels/tests/BIOMD0000000064_url.xml"
assert isinstance(model_object.simulate(duration=2, interval=2), pd.DataFrame)
assert isinstance(model_object.simulate(parameters={'NADH': 0.5}, duration=2, interval=2),
pd.DataFrame)
Expand Down
42 changes: 42 additions & 0 deletions aiagents4pharma/talk2biomodels/tests/test_custom_plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'''
Test cases for plot_figure.py
'''

import pytest
import streamlit as st
from ..tools.custom_plotter import CustomPlotterTool
from ..models.basico_model import BasicoModel

ST_SESSION_KEY = "test_key"

@pytest.fixture(name="custom_plotter_tool")
def custom_plotter_tool_fixture():
'''
Fixture for creating an instance of custom_plotter_tool.
'''
return CustomPlotterTool(st_session_key=ST_SESSION_KEY)

def test_tool(custom_plotter_tool):
'''
Test the tool custom_plotter_tool.
'''
custom_plotter = custom_plotter_tool
st.session_state[ST_SESSION_KEY] = None
response = custom_plotter.invoke(input={
'question': 'Plot only Th cells related species'
})
assert response == "Please run the simulation first before plotting the figure."
st.session_state[ST_SESSION_KEY] = BasicoModel(model_id=537)
response = custom_plotter.invoke(input={
'question': 'Plot only Th cells related species'
})
assert response == "Please run the simulation first before plotting the figure."
st.session_state[ST_SESSION_KEY].simulate()
response = custom_plotter.invoke(input={
'question': 'Plot only T helper cells related species'
})
assert response.startswith("No species found in the simulation")
response = custom_plotter.invoke(input={
'question': 'Plot only antibodies'
})
assert response.startswith("Plotted the figure")
26 changes: 26 additions & 0 deletions aiagents4pharma/talk2biomodels/tests/test_fetch_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
Test cases for plot_figure.py
'''

import streamlit as st
from ..models.basico_model import BasicoModel
from ..tools.fetch_parameters import FetchParametersTool

ST_SESSION_KEY = "test_key"
MODEL_OBJ = BasicoModel(model_id=537)

def test_tool_fetch_params():
'''
Test the tool fetch_params.
'''
st.session_state[ST_SESSION_KEY] = MODEL_OBJ
fetch_params = FetchParametersTool(st_session_key=ST_SESSION_KEY)
response = fetch_params.invoke(input={
'fetch_species': True,
'fetch_parameters': True
})
# Check if response is a dictionary
# with keys 'Species' and 'Parameters'
assert isinstance(response, dict)
assert 'Species' in response
assert 'Parameters' in response
Loading
Loading