Skip to content

Commit f8e3e91

Browse files
Refactor SQL tests for modulo and LIKE patterns
Refactor SQL tests to use SQLAlchemy select statements and remove unused imports.
1 parent 31b50e8 commit f8e3e91

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed
Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
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
132

3+
import pytest
4+
from typing import Any
145

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")
257

268

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
2914

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
3615

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
3721

38-
def test_sqlalchemy_expr_percent_operator() -> None:
39-
from sqlalchemy import (
40-
literal,
41-
select,
42-
)
4322

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
4526

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

Comments
 (0)