|
| 1 | +import sqlite3 |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +from sqlalchemy import create_engine |
| 8 | +from src.querychat.datasource import DataFrameSource, SQLAlchemySource |
| 9 | +from src.querychat.querychat import df_to_html |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def sample_dataframe(): |
| 14 | + """Create a sample pandas DataFrame for testing.""" |
| 15 | + return pd.DataFrame( |
| 16 | + { |
| 17 | + "id": [1, 2, 3, 4, 5], |
| 18 | + "name": ["Alice", "Bob", "Charlie", "Diana", "Eve"], |
| 19 | + "age": [25, 30, 35, 28, 32], |
| 20 | + "salary": [50000, 60000, 70000, 55000, 65000], |
| 21 | + }, |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def sample_sqlite(): |
| 27 | + """Create a temporary SQLite database with test data.""" |
| 28 | + temp_db = tempfile.NamedTemporaryFile(delete=False, suffix=".db") # noqa: SIM115 |
| 29 | + temp_db.close() |
| 30 | + |
| 31 | + conn = sqlite3.connect(temp_db.name) |
| 32 | + cursor = conn.cursor() |
| 33 | + |
| 34 | + cursor.execute(""" |
| 35 | + CREATE TABLE employees ( |
| 36 | + id INTEGER PRIMARY KEY, |
| 37 | + name TEXT, |
| 38 | + age INTEGER, |
| 39 | + salary REAL |
| 40 | + ) |
| 41 | + """) |
| 42 | + |
| 43 | + test_data = [ |
| 44 | + (1, "Alice", 25, 50000), |
| 45 | + (2, "Bob", 30, 60000), |
| 46 | + (3, "Charlie", 35, 70000), |
| 47 | + (4, "Diana", 28, 55000), |
| 48 | + (5, "Eve", 32, 65000), |
| 49 | + ] |
| 50 | + |
| 51 | + cursor.executemany( |
| 52 | + "INSERT INTO employees (id, name, age, salary) VALUES (?, ?, ?, ?)", |
| 53 | + test_data, |
| 54 | + ) |
| 55 | + |
| 56 | + conn.commit() |
| 57 | + conn.close() |
| 58 | + |
| 59 | + engine = create_engine(f"sqlite:///{temp_db.name}") |
| 60 | + yield engine |
| 61 | + |
| 62 | + # Cleanup |
| 63 | + Path(temp_db.name).unlink() |
| 64 | + |
| 65 | + |
| 66 | +def test_df_to_html_with_dataframe_source_result(sample_dataframe): |
| 67 | + """Test that df_to_html() works with results from DataFrameSource.execute_query().""" |
| 68 | + source = DataFrameSource(sample_dataframe, "employees") |
| 69 | + |
| 70 | + # Execute query to get pandas DataFrame |
| 71 | + result_df = source.execute_query("SELECT * FROM employees WHERE age > 25") |
| 72 | + |
| 73 | + # This should succeed after the fix |
| 74 | + html_output = df_to_html(result_df) |
| 75 | + |
| 76 | + # Verify the HTML contains expected content |
| 77 | + assert isinstance(html_output, str) |
| 78 | + assert "<table" in html_output |
| 79 | + assert "Bob" in html_output |
| 80 | + assert "Charlie" in html_output |
| 81 | + assert "Diana" in html_output |
| 82 | + assert "Eve" in html_output |
| 83 | + |
| 84 | + |
| 85 | +def test_df_to_html_with_sqlalchemy_source_result(sample_sqlite): |
| 86 | + """Test that df_to_html() works with results from SQLAlchemySource.execute_query().""" |
| 87 | + source = SQLAlchemySource(sample_sqlite, "employees") |
| 88 | + |
| 89 | + # Execute query to get pandas DataFrame |
| 90 | + result_df = source.execute_query("SELECT * FROM employees WHERE age > 25") |
| 91 | + |
| 92 | + # This should succeed after the fix |
| 93 | + html_output = df_to_html(result_df) |
| 94 | + |
| 95 | + # Verify the HTML contains expected content |
| 96 | + assert isinstance(html_output, str) |
| 97 | + assert "<table" in html_output |
| 98 | + assert "Bob" in html_output |
| 99 | + assert "Charlie" in html_output |
| 100 | + assert "Diana" in html_output |
| 101 | + assert "Eve" in html_output |
| 102 | + |
| 103 | + |
| 104 | +def test_df_to_html_with_truncation(sample_dataframe): |
| 105 | + """Test that df_to_html() properly truncates large datasets.""" |
| 106 | + source = DataFrameSource(sample_dataframe, "employees") |
| 107 | + |
| 108 | + # Execute query to get all rows |
| 109 | + result_df = source.execute_query("SELECT * FROM employees") |
| 110 | + |
| 111 | + # Test with maxrows=3 to trigger truncation |
| 112 | + html_output = df_to_html(result_df, maxrows=3) |
| 113 | + |
| 114 | + # Should show truncation message |
| 115 | + assert "Showing only the first 3 rows out of 5" in html_output |
| 116 | + assert "<table" in html_output |
0 commit comments