Skip to content

Commit

Permalink
fix: description for CREATE DATABASE
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Jan 1, 2024
1 parent 6963493 commit 6015ac8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
4 changes: 4 additions & 0 deletions fakesnow/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

SCHEMA_UNSET = "schema_unset"
SUCCESS_SQL = "SELECT 'Statement executed successfully.' as status"
DATABASE_CREATED_SQL = Template("SELECT 'Database ${db} successfully created.' as status")
TABLE_CREATED_SQL = Template("SELECT 'Table ${table} successfully created.' as status")
INSERTED_SQL = Template("SELECT ${count} as 'number of rows inserted'")

Expand Down Expand Up @@ -228,6 +229,9 @@ def _execute(
if create_db_name := transformed.args.get("create_db_name"):
# we created a new database, so create the info schema extensions
self._duck_conn.execute(info_schema.creation_sql(create_db_name))
table_created_sql = DATABASE_CREATED_SQL.substitute(db=create_db_name)
self._duck_conn.execute(table_created_sql)
self._last_sql = table_created_sql

if table_comment := cast(tuple[exp.Table, str], transformed.args.get("table_comment")):
# record table comment
Expand Down
39 changes: 23 additions & 16 deletions tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,6 @@ def test_connect_with_non_existent_db_or_schema(_fakesnow_no_auto_create: None):
assert conn.schema == "JAFFLES"


def test_create_table_result(dcur: snowflake.connector.cursor.DictCursor):
dcur.execute("create or replace table example (X int)")
assert dcur.fetchall() == [{"status": "Table EXAMPLE successfully created."}]
# description is expected by sql alchemy
assert dcur.description == [ResultMetadata(name='status', type_code=2, display_size=None, internal_size=16777216, precision=None, scale=None, is_nullable=True)] # fmt: skip


def test_insert_result(dcur: snowflake.connector.cursor.DictCursor):
dcur.execute("create or replace table example (X int)")
dcur.execute("insert into example values (1), (2)")
assert dcur.fetchall() == [{"number of rows inserted": 2}]
# description is expected by sql alchemy
# TODO: Snowflake is actually precision=19, is_nullable=False
assert dcur.description == [ResultMetadata(name='number of rows inserted', type_code=0, display_size=None, internal_size=None, precision=38, scale=0, is_nullable=True)] # fmt: skip


def test_current_database_schema(conn: snowflake.connector.SnowflakeConnection):
with conn.cursor(snowflake.connector.cursor.DictCursor) as cur:
cur.execute("select current_database(), current_schema()")
Expand Down Expand Up @@ -298,6 +282,29 @@ def test_describe_info_schema_columns(cur: snowflake.connector.cursor.SnowflakeC
assert cur.description == expected_metadata


## descriptions are needed for ipython-sql/jupysql which describes every statement


def test_description_create_database(dcur: snowflake.connector.cursor.DictCursor):
dcur.execute("create database example")
assert dcur.fetchall() == [{"status": "Database EXAMPLE successfully created."}]
assert dcur.description == [ResultMetadata(name='status', type_code=2, display_size=None, internal_size=16777216, precision=None, scale=None, is_nullable=True)] # fmt: skip


def test_description_create_table(dcur: snowflake.connector.cursor.DictCursor):
dcur.execute("create or replace table example (X int)")
assert dcur.fetchall() == [{"status": "Table EXAMPLE successfully created."}]
assert dcur.description == [ResultMetadata(name='status', type_code=2, display_size=None, internal_size=16777216, precision=None, scale=None, is_nullable=True)] # fmt: skip


def test_description_insert(dcur: snowflake.connector.cursor.DictCursor):
dcur.execute("create or replace table example (X int)")
dcur.execute("insert into example values (1), (2)")
assert dcur.fetchall() == [{"number of rows inserted": 2}]
# TODO: Snowflake is actually precision=19, is_nullable=False
assert dcur.description == [ResultMetadata(name='number of rows inserted', type_code=0, display_size=None, internal_size=None, precision=38, scale=0, is_nullable=True)] # fmt: skip


def test_executemany(cur: snowflake.connector.cursor.SnowflakeCursor):
cur.execute("create table customers (ID int, FIRST_NAME varchar, LAST_NAME varchar)")

Expand Down

0 comments on commit 6015ac8

Please sign in to comment.