Skip to content

Commit

Permalink
feat: support commit and rollback on connection
Browse files Browse the repository at this point in the history
resolves #6
  • Loading branch information
tekumara committed Jun 24, 2023
1 parent 1adaad0 commit c5520b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fakesnow/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ def __exit__(
) -> bool:
return False

def commit(self) -> None:
self.cursor().execute("COMMIT")

def cursor(self, cursor_class: Type[SnowflakeCursor] = SnowflakeCursor) -> FakeSnowflakeCursor:
return FakeSnowflakeCursor(conn=self, duck_conn=self._duck_conn, use_dict_result=cursor_class == DictCursor)

Expand All @@ -414,6 +417,9 @@ def execute_string(
]
return cursors if return_cursors else []

def rollback(self) -> None:
self.cursor().execute("ROLLBACK")

def _insert_df(
self, df: pd.DataFrame, table_name: str, database: str | None = None, schema: str | None = None
) -> int:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,23 @@ def test_timestamp_to_date(cur: snowflake.connector.cursor.SnowflakeCursor):
assert cur.fetchall() == [(datetime.date(1970, 1, 1), datetime.date(1970, 1, 1))]


def test_transactions(conn: snowflake.connector.SnowflakeConnection):
conn.execute_string(
"""CREATE TABLE table1 (i int);
BEGIN TRANSACTION;
INSERT INTO table1 (i) VALUES (1);"""
)
conn.rollback()
conn.execute_string(
"""BEGIN TRANSACTION;
INSERT INTO table1 (i) VALUES (2);"""
)
conn.commit()
with conn.cursor() as cur:
cur.execute("SELECT * FROM table1")
assert cur.fetchall() == [(2,)]


def test_unquoted_identifiers_are_upper_cased(conn: snowflake.connector.SnowflakeConnection):
with conn.cursor(snowflake.connector.cursor.DictCursor) as cur:
cur.execute("create table customers (id int, first_name varchar, last_name varchar)")
Expand Down

0 comments on commit c5520b0

Please sign in to comment.