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(test): fixing tests for when PANDABI_API_KEY is present in .env #1527

Merged
merged 2 commits into from
Jan 17, 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
2 changes: 1 addition & 1 deletion pandasai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def clear_cache(filename: str = None):
cache.clear()


def chat(query: str, *dataframes: List[DataFrame]):
def chat(query: str, *dataframes: DataFrame):
"""
Start a new chat interaction with the assistant on Dataframe(s).

Expand Down
41 changes: 38 additions & 3 deletions pandasai/agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import warnings
from typing import Any, List, Optional, Union

import duckdb
import pandas as pd

from pandasai.core.cache import Cache
from pandasai.core.code_execution.code_executor import CodeExecutor
from pandasai.core.code_generation.base import CodeGenerator
Expand All @@ -23,6 +26,7 @@
from pandasai.vectorstores.vectorstore import VectorStore

from ..config import Config
from ..constants import LOCAL_SOURCE_TYPES
from .state import AgentState


Expand Down Expand Up @@ -102,12 +106,43 @@
"""Execute the generated code."""
self._state.logger.log(f"Executing code: {code}")
code_executor = CodeExecutor(self._state.config)
code_executor.add_to_env(
"execute_sql_query", self._state.dfs[0].execute_sql_query
)
code_executor.add_to_env("execute_sql_query", self.execute_sql_query)

return code_executor.execute_and_return_result(code)

def _execute_local_sql_query(self, query: str) -> pd.DataFrame:
try:

Check warning on line 114 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L114

Added line #L114 was not covered by tests
# Use a context manager to ensure the connection is closed
with duckdb.connect() as con:

Check warning on line 116 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L116

Added line #L116 was not covered by tests
# Register all DataFrames in the state
for df in self._state.dfs:
con.register(df.name, df)

Check warning on line 119 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L118-L119

Added lines #L118 - L119 were not covered by tests

# Execute the query and fetch the result as a pandas DataFrame
result = con.sql(query).df()

Check warning on line 122 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L122

Added line #L122 was not covered by tests

return result
except duckdb.Error as e:
raise RuntimeError(f"SQL execution failed: {e}") from e

Check warning on line 126 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L124-L126

Added lines #L124 - L126 were not covered by tests

def execute_sql_query(self, query: str) -> pd.DataFrame:
"""
Executes an SQL query on registered DataFrames.

Args:
query (str): The SQL query to execute.

Returns:
pd.DataFrame: The result of the SQL query as a pandas DataFrame.
"""
if not self._state.dfs:
raise ValueError("No DataFrames available to register for query execution.")

Check warning on line 139 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L138-L139

Added lines #L138 - L139 were not covered by tests

if self._state.dfs[0].schema.source.type in LOCAL_SOURCE_TYPES:
return self._execute_local_sql_query(query)

Check warning on line 142 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L141-L142

Added lines #L141 - L142 were not covered by tests
else:
return self._state.dfs[0].execute_sql_query(query)

Check warning on line 144 in pandasai/agent/base.py

View check run for this annotation

Codecov / codecov/patch

pandasai/agent/base.py#L144

Added line #L144 was not covered by tests

def execute_with_retries(self, code: str) -> Any:
"""Execute the code with retry logic."""
max_retries = self._state.config.max_retries
Expand Down
2 changes: 1 addition & 1 deletion pandasai/agent/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pandasai.config import Config, ConfigManager
from pandasai.constants import DEFAULT_CACHE_DIRECTORY, DEFAULT_CHART_DIRECTORY
from pandasai.core.cache import Cache
from pandasai.data_loader.schema_validator import is_schema_source_same
from pandasai.data_loader.semantic_layer_schema import is_schema_source_same
from pandasai.exceptions import InvalidConfigError
from pandasai.helpers.folder import Folder
from pandasai.helpers.logger import Logger
Expand Down
9 changes: 0 additions & 9 deletions pandasai/data_loader/schema_validator.py

This file was deleted.

7 changes: 0 additions & 7 deletions pandasai/dataframe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,6 @@ def pull(self):

print(f"Dataset pulled successfully from path: {self.path}")

def execute_sql_query(self, query: str) -> pd.DataFrame:
import duckdb

db = duckdb.connect(":memory:")
db.register(self.name, self)
return db.query(query).df()

@staticmethod
def get_column_type(column_dtype) -> Optional[str]:
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/unit_tests/helpers/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandasai.helpers.session import Session, get_pandaai_session


@patch("pandasai.os.environ", {})
def test_session_init_without_api_key():
"""Test that Session initialization raises PandaAIApiKeyError when no API key is provided"""
with pytest.raises(PandaAIApiKeyError) as exc_info:
Expand All @@ -18,6 +19,7 @@ def test_session_init_without_api_key():
)


@patch("pandasai.os.environ", {})
def test_session_init_with_none_api_key():
"""Test that Session initialization raises PandaAIApiKeyError when API key is None"""
with pytest.raises(PandaAIApiKeyError) as exc_info:
Expand All @@ -28,18 +30,21 @@ def test_session_init_with_none_api_key():
)


@patch("pandasai.os.environ", {})
def test_session_init_with_api_key():
"""Test that Session initialization works with a valid API key"""
session = Session(api_key="test-key")
assert session._api_key == "test-key"


@patch("pandasai.os.environ", {})
def test_session_init_with_default_api_url():
"""Test that Session initialization uses DEFAULT_API_URL when no URL is provided"""
session = Session(api_key="test-key")
assert session._endpoint_url == DEFAULT_API_URL


@patch("pandasai.os.environ", {})
def test_session_init_with_custom_api_url():
"""Test that Session initialization uses provided URL"""
custom_url = "https://custom.api.url"
Expand All @@ -64,6 +69,7 @@ def test_session_init_with_env_api_url():
assert session._endpoint_url == "https://env.api.url"


@patch("pandasai.os.environ", {})
def test_get_pandaai_session_without_credentials():
"""Test that get_pandaai_session raises PandaAIApiKeyError when no credentials are provided"""
with pytest.raises(PandaAIApiKeyError) as exc_info:
Expand Down
5 changes: 4 additions & 1 deletion tests/unit_tests/test_pandasai_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def test_load_successful_zip_extraction(
mock_zip_file.return_value.__enter__.return_value.extractall.assert_called_once()
assert isinstance(result, MagicMock)

def test_load_without_api_credentials(self):
@patch("pandasai.os.environ", {})
def test_load_without_api_credentials(
self,
):
"""Test that load raises PandaAIApiKeyError when no API credentials are provided"""
with pytest.raises(PandaAIApiKeyError) as exc_info:
pandasai.load("test/dataset")
Expand Down
Loading