Refactor setup and teardown of connection and test table into a fixture.

This commit is contained in:
Pablo Martin 2023-04-24 13:41:15 +02:00
parent 47a3e8b1e7
commit 2090ec7c91

View file

@ -1,3 +1,5 @@
import pytest
from lolafect.lolaconfig import build_lolaconfig from lolafect.lolaconfig import build_lolaconfig
from lolafect.connections import ( from lolafect.connections import (
open_ssh_tunnel_with_s3_pkey, open_ssh_tunnel_with_s3_pkey,
@ -25,7 +27,8 @@ from lolafect.utils import begin_sql_transaction, end_sql_transaction
TEST_LOLACONFIG = build_lolaconfig(flow_name="testing-suite") TEST_LOLACONFIG = build_lolaconfig(flow_name="testing-suite")
def test_sql_transaction_persists_changes_properly(): @pytest.fixture
def connection_with_test_table():
test_local_bind_host = "127.0.0.1" test_local_bind_host = "127.0.0.1"
test_local_bind_port = 12345 test_local_bind_port = 12345
@ -44,26 +47,41 @@ def test_sql_transaction_persists_changes_properly():
tunnel=tunnel tunnel=tunnel
), ),
) )
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(""" cursor.execute("""
CREATE TABLE sandbox.lolafect_transaction_test_table CREATE TABLE sandbox.lolafect_transaction_test_table
( (
a_test_column INT a_test_column INT
) )
""") """)
# Connection and table ready for tests
yield connection # Test happens now
# Test finished, time to remove stuff and close connection
cursor.execute("""
DROP TABLE sandbox.lolafect_transaction_test_table
"""
)
close_mysql_connection.run(connection=connection)
close_ssh_tunnel.run(tunnel=tunnel)
def test_sql_transaction_persists_changes_properly(connection_with_test_table):
cursor = connection_with_test_table.cursor()
cursor.execute(""" cursor.execute("""
SELECT a_test_column SELECT a_test_column
FROM sandbox.lolafect_transaction_test_table FROM sandbox.lolafect_transaction_test_table
""") """)
table_is_empty_at_first = not bool(cursor.fetchall()) # An empty tuple yields False table_is_empty_at_first = not bool(cursor.fetchall()) # An empty tuple yields False
begin_sql_transaction.run(connection=connection) begin_sql_transaction.run(connection=connection_with_test_table)
cursor.execute(""" cursor.execute("""
INSERT INTO sandbox.lolafect_transaction_test_table (a_test_column) INSERT INTO sandbox.lolafect_transaction_test_table (a_test_column)
VALUES (1) VALUES (1)
""") """)
end_sql_transaction.run(connection=connection, dry_run=False) end_sql_transaction.run(connection=connection_with_test_table, dry_run=False)
cursor.execute(""" cursor.execute("""
SELECT a_test_column SELECT a_test_column
@ -71,69 +89,29 @@ def test_sql_transaction_persists_changes_properly():
""") """)
table_has_a_record_after_transaction = bool(cursor.fetchall()) # A non-empty tuple yields True table_has_a_record_after_transaction = bool(cursor.fetchall()) # A non-empty tuple yields True
cursor.execute("""
DROP TABLE sandbox.lolafect_transaction_test_table
"""
)
close_mysql_connection.run(connection=connection)
close_ssh_tunnel.run(tunnel=tunnel)
assert table_is_empty_at_first and table_has_a_record_after_transaction assert table_is_empty_at_first and table_has_a_record_after_transaction
def test_sql_transaction_rollbacks_changes_properly(): def test_sql_transaction_rollbacks_changes_properly(connection_with_test_table):
test_local_bind_host = "127.0.0.1" cursor = connection_with_test_table.cursor()
test_local_bind_port = 12345
tunnel = open_ssh_tunnel_with_s3_pkey.run(
s3_bucket_name=TEST_LOLACONFIG.S3_BUCKET_NAME,
ssh_tunnel_credentials=TEST_LOLACONFIG.SSH_TUNNEL_CREDENTIALS,
remote_target_host=TEST_LOLACONFIG.DW_CREDENTIALS["host"],
remote_target_port=TEST_LOLACONFIG.DW_CREDENTIALS["port"],
local_bind_host=test_local_bind_host,
local_bind_port=test_local_bind_port,
)
connection = connect_to_mysql.run(
mysql_credentials=TEST_LOLACONFIG.DW_CREDENTIALS,
overriding_host_and_port=get_local_bind_address_from_ssh_tunnel.run(
tunnel=tunnel
),
)
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE sandbox.lolafect_transaction_test_table
(
a_test_column INT
)
""")
cursor.execute(""" cursor.execute("""
SELECT a_test_column SELECT a_test_column
FROM sandbox.lolafect_transaction_test_table FROM sandbox.lolafect_transaction_test_table
""") """)
table_is_empty_at_first = not bool(cursor.fetchall()) # An empty tuple yields False table_is_empty_at_first = not bool(cursor.fetchall()) # An empty tuple yields False
begin_sql_transaction.run(connection=connection) begin_sql_transaction.run(connection=connection_with_test_table)
cursor.execute(""" cursor.execute("""
INSERT INTO sandbox.lolafect_transaction_test_table (a_test_column) INSERT INTO sandbox.lolafect_transaction_test_table (a_test_column)
VALUES (1) VALUES (1)
""") """)
end_sql_transaction.run(connection=connection, dry_run=True) end_sql_transaction.run(connection=connection_with_test_table, dry_run=True)
cursor.execute(""" cursor.execute("""
SELECT a_test_column SELECT a_test_column
FROM sandbox.lolafect_transaction_test_table FROM sandbox.lolafect_transaction_test_table
""") """)
table_is_empty_after_rollback = not bool(cursor.fetchall()) # An tuple yields False table_is_empty_after_rollback = not bool(cursor.fetchall()) # A tuple yields False
cursor.execute("""
DROP TABLE sandbox.lolafect_transaction_test_table
"""
)
close_mysql_connection.run(connection=connection)
close_ssh_tunnel.run(tunnel=tunnel)
assert table_is_empty_at_first and table_is_empty_after_rollback assert table_is_empty_at_first and table_is_empty_after_rollback