Skip to content

Commit

Permalink
fix: handle DESCRIBE SELECT (#61)
Browse files Browse the repository at this point in the history
* fix: handle DESCRIBE SELECT

* format

* remove special handling in conftest.query

* format
  • Loading branch information
xiaoyu-meng-mxy authored Sep 9, 2024
1 parent dff9b6e commit 0fdfe37
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mysql_mimic/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ async def _show(self, expression: exp.Show) -> AllowedResult:
async def _describe_middleware(self, q: Query) -> AllowedResult:
"""Intercept DESCRIBE statements"""
if isinstance(q.expression, exp.Describe):
if isinstance(q.expression.this, exp.Select):
# Mysql parse treats EXPLAIN SELECT as a DESCRIBE SELECT statement
return await q.next()
name = q.expression.this.name
show = self.dialect().parse(f"SHOW COLUMNS FROM {name}")[0]
return await self._show(show) if isinstance(show, exp.Show) else None
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def query(
self.waiting.set()
await self.pause.wait()
self.waiting.clear()
assert isinstance(expression, exp.Select)

self.last_query_attrs = attrs
if self.echo:
return [(sql,)], ["sql"]
Expand Down
17 changes: 15 additions & 2 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ async def q4(sql: str) -> Sequence[Dict[str, Any]]:
raise RuntimeError("Unexpected fixture param")


# # Uncomment to make tests only use mysql-connector, which can help during debugging
# Uncomment to make tests only use mysql-connector, which can help during debugging
# @pytest_asyncio.fixture
# async def query_fixture(
# mysql_connector_conn: MySQLConnection,
# mysql_connector_conn: MySQLConnectionAbstract,
# ) -> QueryFixture:
# async def q1(sql: str) -> Sequence[Dict[str, Any]]:
# return await query(mysql_connector_conn, sql)
Expand Down Expand Up @@ -293,6 +293,19 @@ async def test_query_attributes(
assert session.last_query_attrs == query_attrs


@pytest.mark.asyncio
async def test_describe_select(
session: MockSession,
server: MysqlServer,
query_fixture: QueryFixture,
) -> None:
session.echo = True
sql = "DESCRIBE SELECT b from a"
with freeze_time("2023-01-01"):
result = await query_fixture(sql)
assert [{"sql": "DESCRIBE SELECT b from a"}] == list(result)


# pylint: disable=trailing-whitespace
@pytest.mark.asyncio
@pytest.mark.parametrize(
Expand Down

0 comments on commit 0fdfe37

Please sign in to comment.