Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle DESCRIBE SELECT #61

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer use MySQLConnection

# ) -> 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
Loading