|
| 1 | +"""Tests for SQLResult DataFrame conversion methods.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from sqlspec.core import SQL, SQLResult |
| 8 | +from sqlspec.typing import PYARROW_INSTALLED |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def sample_data() -> list[dict[str, Any]]: |
| 13 | + """Create sample dict data for testing.""" |
| 14 | + return [ |
| 15 | + {"id": 1, "name": "Alice", "age": 30}, |
| 16 | + {"id": 2, "name": "Bob", "age": 25}, |
| 17 | + {"id": 3, "name": "Charlie", "age": 35}, |
| 18 | + ] |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def sql_result(sample_data: list[dict[str, Any]]) -> SQLResult: |
| 23 | + """Create an SQLResult with sample data.""" |
| 24 | + stmt = SQL("SELECT * FROM users") |
| 25 | + return SQLResult(statement=stmt, data=sample_data, rows_affected=3) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.skipif(not PYARROW_INSTALLED, reason="pyarrow not installed") |
| 29 | +def test_sql_result_to_arrow(sql_result: SQLResult) -> None: |
| 30 | + """Test converting SQLResult to Arrow Table.""" |
| 31 | + import pyarrow as pa |
| 32 | + |
| 33 | + table = sql_result.to_arrow() |
| 34 | + |
| 35 | + assert isinstance(table, pa.Table) |
| 36 | + assert table.num_rows == 3 |
| 37 | + assert table.column_names == ["id", "name", "age"] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.skipif(not PYARROW_INSTALLED, reason="pyarrow not installed") |
| 41 | +def test_sql_result_to_arrow_empty_data() -> None: |
| 42 | + """Test to_arrow() with empty data list.""" |
| 43 | + import pyarrow as pa |
| 44 | + |
| 45 | + stmt = SQL("SELECT * FROM users WHERE 1=0") |
| 46 | + result = SQLResult(statement=stmt, data=[]) |
| 47 | + |
| 48 | + table = result.to_arrow() |
| 49 | + |
| 50 | + assert isinstance(table, pa.Table) |
| 51 | + assert table.num_rows == 0 |
| 52 | + |
| 53 | + |
| 54 | +def test_sql_result_to_pandas(sql_result: SQLResult) -> None: |
| 55 | + """Test converting SQLResult to pandas DataFrame.""" |
| 56 | + pandas = pytest.importorskip("pandas") |
| 57 | + |
| 58 | + df = sql_result.to_pandas() |
| 59 | + |
| 60 | + assert isinstance(df, pandas.DataFrame) |
| 61 | + assert len(df) == 3 |
| 62 | + assert list(df.columns) == ["id", "name", "age"] |
| 63 | + assert df["name"].tolist() == ["Alice", "Bob", "Charlie"] |
| 64 | + |
| 65 | + |
| 66 | +def test_sql_result_to_pandas_empty_data() -> None: |
| 67 | + """Test to_pandas() with empty data list.""" |
| 68 | + pandas = pytest.importorskip("pandas") |
| 69 | + |
| 70 | + stmt = SQL("SELECT * FROM users WHERE 1=0") |
| 71 | + result = SQLResult(statement=stmt, data=[]) |
| 72 | + |
| 73 | + df = result.to_pandas() |
| 74 | + |
| 75 | + assert isinstance(df, pandas.DataFrame) |
| 76 | + assert len(df) == 0 |
| 77 | + |
| 78 | + |
| 79 | +def test_sql_result_to_pandas_with_null_values() -> None: |
| 80 | + """Test to_pandas() correctly handles NULL values.""" |
| 81 | + pandas = pytest.importorskip("pandas") |
| 82 | + |
| 83 | + data: list[dict[str, Any]] = [ |
| 84 | + { "id": 1, "name": "Alice", "email": "[email protected]"}, |
| 85 | + {"id": 2, "name": "Bob", "email": None}, |
| 86 | + { "id": 3, "name": None, "email": "[email protected]"}, |
| 87 | + ] |
| 88 | + stmt = SQL("SELECT * FROM users") |
| 89 | + result = SQLResult(statement=stmt, data=data) |
| 90 | + |
| 91 | + df = result.to_pandas() |
| 92 | + |
| 93 | + assert pandas.isna(df.loc[1, "email"]) |
| 94 | + assert pandas.isna(df.loc[2, "name"]) |
| 95 | + |
| 96 | + |
| 97 | +def test_sql_result_to_polars(sql_result: SQLResult) -> None: |
| 98 | + """Test converting SQLResult to Polars DataFrame.""" |
| 99 | + polars = pytest.importorskip("polars") |
| 100 | + |
| 101 | + df = sql_result.to_polars() |
| 102 | + |
| 103 | + assert isinstance(df, polars.DataFrame) |
| 104 | + assert len(df) == 3 |
| 105 | + assert df.columns == ["id", "name", "age"] |
| 106 | + assert df["name"].to_list() == ["Alice", "Bob", "Charlie"] |
| 107 | + |
| 108 | + |
| 109 | +def test_sql_result_to_polars_empty_data() -> None: |
| 110 | + """Test to_polars() with empty data list.""" |
| 111 | + polars = pytest.importorskip("polars") |
| 112 | + |
| 113 | + stmt = SQL("SELECT * FROM users WHERE 1=0") |
| 114 | + result = SQLResult(statement=stmt, data=[]) |
| 115 | + |
| 116 | + df = result.to_polars() |
| 117 | + |
| 118 | + assert isinstance(df, polars.DataFrame) |
| 119 | + assert len(df) == 0 |
| 120 | + |
| 121 | + |
| 122 | +def test_sql_result_to_polars_with_null_values() -> None: |
| 123 | + """Test to_polars() correctly handles NULL values.""" |
| 124 | + pytest.importorskip("polars") |
| 125 | + |
| 126 | + data: list[dict[str, Any]] = [ |
| 127 | + { "id": 1, "name": "Alice", "email": "[email protected]"}, |
| 128 | + {"id": 2, "name": "Bob", "email": None}, |
| 129 | + { "id": 3, "name": None, "email": "[email protected]"}, |
| 130 | + ] |
| 131 | + stmt = SQL("SELECT * FROM users") |
| 132 | + result = SQLResult(statement=stmt, data=data) |
| 133 | + |
| 134 | + df = result.to_polars() |
| 135 | + |
| 136 | + assert df["email"][1] is None |
| 137 | + assert df["name"][2] is None |
| 138 | + |
| 139 | + |
| 140 | +def test_sql_result_methods_with_none_data_raise() -> None: |
| 141 | + """Test that methods raise ValueError when data is None.""" |
| 142 | + stmt = SQL("SELECT * FROM users") |
| 143 | + result = SQLResult(statement=stmt, data=None) |
| 144 | + |
| 145 | + with pytest.raises(ValueError, match="No data available"): |
| 146 | + result.to_pandas() |
| 147 | + |
| 148 | + with pytest.raises(ValueError, match="No data available"): |
| 149 | + result.to_polars() |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.skipif(not PYARROW_INSTALLED, reason="pyarrow not installed") |
| 153 | +def test_sql_result_to_arrow_with_none_data_raises() -> None: |
| 154 | + """Test that to_arrow() raises ValueError when data is None.""" |
| 155 | + stmt = SQL("SELECT * FROM users") |
| 156 | + result = SQLResult(statement=stmt, data=None) |
| 157 | + |
| 158 | + with pytest.raises(ValueError, match="No data available"): |
| 159 | + result.to_arrow() |
0 commit comments