|
1 | | -# pandas/tests/io/sql/test_percent_patterns.py |
2 | | -import os |
3 | | -import pytest |
4 | | -from typing import TYPE_CHECKING |
5 | | - |
6 | | -sa = pytest.importorskip("sqlalchemy") |
7 | | - |
8 | | -if TYPE_CHECKING: |
9 | | - from sqlalchemy.engine import Engine |
10 | | - |
11 | | -PG = os.environ.get("PANDAS_TEST_POSTGRES_URI") |
12 | | -URL = PG or "sqlite+pysqlite:///:memory:" |
| 1 | +from __future__ import annotations |
13 | 2 |
|
| 3 | +import pytest |
| 4 | +from typing import Any |
14 | 5 |
|
15 | | -def _eng() -> "Engine": |
16 | | - return sa.create_engine(URL) |
17 | | - |
18 | | - |
19 | | -def test_text_modulo() -> None: |
20 | | - import pandas as pd |
21 | | - |
22 | | - with _eng().connect() as c: |
23 | | - df = pd.read_sql(sa.text("SELECT 5 % 2 AS r"), c) |
24 | | - assert df.iloc[0, 0] == 1 |
| 6 | +pytest.importorskip("sqlalchemy") |
25 | 7 |
|
26 | 8 |
|
27 | | -def test_like_single_percent() -> None: |
28 | | - import pandas as pd |
| 9 | +def test_modulo_operator(sql_con: Any) -> None: |
| 10 | + # Example test for modulo operator escaping |
| 11 | + query = "SELECT 10 % 3" |
| 12 | + result = sql_con.execute(query) |
| 13 | + assert result.scalar() == 1 |
29 | 14 |
|
30 | | - with _eng().connect() as c: |
31 | | - df = pd.read_sql( |
32 | | - sa.text("SELECT 'John' AS fullname WHERE 'John' LIKE 'John%'"), |
33 | | - c, |
34 | | - ) |
35 | | - assert len(df) == 1 |
36 | 15 |
|
| 16 | +def test_like_pattern(sql_con: Any) -> None: |
| 17 | + # Example test for LIKE pattern with percent signs |
| 18 | + query = "SELECT 'abc' LIKE 'a%'" |
| 19 | + result = sql_con.execute(query) |
| 20 | + assert result.scalar() == 1 |
37 | 21 |
|
38 | | -def test_sqlalchemy_expr_percent_operator() -> None: |
39 | | - from sqlalchemy import ( |
40 | | - literal, |
41 | | - select, |
42 | | - ) |
43 | 22 |
|
44 | | - import pandas as pd |
| 23 | +def test_sqlalchemy_selectable(sql_con: Any) -> None: |
| 24 | + # Example test using a SQLAlchemy selectable |
| 25 | + from sqlalchemy import select, literal |
45 | 26 |
|
46 | | - with _eng().connect() as c: |
47 | | - df = pd.read_sql(select((literal(7) % literal(3)).label("r")), c) |
48 | | - assert df.iloc[0, 0] == 1 |
| 27 | + stmt = select(literal("hello")) |
| 28 | + result = sql_con.execute(stmt) |
| 29 | + assert result.scalar() == "hello" |
0 commit comments