Skip to content

Commit

Permalink
fix: fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane Clark committed Nov 15, 2024
1 parent fb2f2a4 commit c07f97c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
47 changes: 22 additions & 25 deletions .github/workflows/master-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ jobs:
runs-on: ${{ matrix.os }}
if: ${{ github.event.label.name == 'ci-run-tests' || github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
env:
SNOWFLAKE_PASSWORD: ${{ secrets.SCHEMACHANGE_SNOWFLAKE_PASSWORD }}
SNOWFLAKE_USER: ${{ secrets.SCHEMACHANGE_SNOWFLAKE_USER }}
SNOWFLAKE_ACCOUNT: ${{ secrets.SCHEMACHANGE_SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_DATABASE: SCHEMACHANGE_DEMO
SNOWFLAKE_WAREHOUSE: SCHEMACHANGE_DEMO_WH
SNOWFLAKE_USER: ${{ secrets.SCHEMACHANGE_SNOWFLAKE_USER }}
SNOWFLAKE_ROLE: SCHEMACHANGE_DEMO-DEPLOY
SNOWFLAKE_WAREHOUSE: SCHEMACHANGE_DEMO_WH
SNOWFLAKE_DATABASE: SCHEMACHANGE_DEMO
MY_TARGET_SCHEMA: ${{ matrix.scenario-name }}_${{ github.run_number }}_${{ strategy.job-index }}
SNOWFLAKE_PASSWORD: ${{ secrets.SCHEMACHANGE_SNOWFLAKE_PASSWORD }}
SCENARIO_NAME: ${{ matrix.scenario-name }}
steps:
- uses: actions/checkout@v4
Expand All @@ -60,17 +60,16 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Create and populate connections.toml
run: |
touch connections.toml
echo [default] >> connections.toml
echo account = "$SNOWFLAKE_ACCOUNT" >> connections.toml
echo user = "$SNOWFLAKE_USER" >> connections.toml
echo role = "$SNOWFLAKE_ROLE" >> connections.toml
echo warehouse = "$SNOWFLAKE_WAREHOUSE" >> connections.toml
echo database = "$SNOWFLAKE_DATABASE" >> connections.toml
echo schema = "$MY_TARGET_SCHEMA" >> connections.toml
echo password = "$SCHEMACHANGE_SNOWFLAKE_PASSWORD" >> connections.toml
touch ./connections.toml
echo [default] >> ./connections.toml
echo account = \"${SNOWFLAKE_ACCOUNT}\" >> ./connections.toml
echo user = \"${SNOWFLAKE_USER}\" >> ./connections.toml
echo role = \"${SNOWFLAKE_ROLE}\" >> ./connections.toml
echo warehouse = \"${SNOWFLAKE_WAREHOUSE}\" >> ./connections.toml
echo database = \"${SNOWFLAKE_DATABASE}\" >> ./connections.toml
echo password = \"${SNOWFLAKE_PASSWORD}\" >> ./connections.toml
echo "cat connections.toml"
cat connections.toml
cat ./connections.toml
- name: Test with pytest
id: pytest
run: |
Expand All @@ -84,24 +83,20 @@ jobs:
--config-file-name schemachange-config-setup.yml \
--root-folder ./demo/${SCENARIO_NAME}/1_setup \
--connection-name default \
--connections-file-path connections.toml
echo "::endgroup::"'
--connections-file-path ./connections.toml \
--verbose
echo "::endgroup::"
echo "::group::Testing Rendering to ${MY_TARGET_SCHEMA}"
schemachange render \
--config-folder ./demo/${SCENARIO_NAME} \
--connection-name default \
--connections-file-path connections.toml \
./demo/${SCENARIO_NAME}/2_test/A__render.sql
schemachange render \
--config-folder ./demo/${SCENARIO_NAME} \
--connection-name default \
--connections-file-path connections.toml \
./demo/${SCENARIO_NAME}/2_test/R__render.sql
schemachange render \
--config-folder ./demo/${SCENARIO_NAME} \
--connection-name default \
--connections-file-path connections.toml
./demo/${SCENARIO_NAME}/2_test/V1.0.0__render.sql
echo "::endgroup::"
Expand All @@ -110,8 +105,9 @@ jobs:
schemachange deploy \
--config-folder ./demo/${SCENARIO_NAME} \
--connection-name default \
--connections-file-path connections.toml \
--root-folder ./demo/${SCENARIO_NAME}/2_test
--connections-file-path ./connections.toml \
--root-folder ./demo/${SCENARIO_NAME}/2_test \
--verbose
RESULT=$?
if [ $RESULT -eq 0 ]; then
echo "Deployment Completed!"
Expand All @@ -126,8 +122,9 @@ jobs:
--config-folder ./demo \
--config-file-name schemachange-config-teardown.yml \
--connection-name default \
--connections-file-path connections.toml \
--connections-file-path ./connections.toml \
--root-folder ./demo/${SCENARIO_NAME}/3_teardown \
--verbose
echo "::endgroup::"
if [ $RESULT -ne 0 ]; then
Expand Down
14 changes: 9 additions & 5 deletions schemachange/session/SnowflakeSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import structlog

from schemachange.config.ChangeHistoryTable import ChangeHistoryTable
from schemachange.config.utils import get_snowflake_identifier_string
from schemachange.session.Script import VersionedScript, RepeatableScript, AlwaysScript


Expand Down Expand Up @@ -79,15 +80,18 @@ def __init__(
"application": application,
"session_parameters": self.session_parameters,
}
connect_kwargs = {k: v for k, v in connect_kwargs.items() if v is not None}
self.logger.debug("snowflake.connector.connect kwargs", **connect_kwargs)
self.con = snowflake.connector.connect(**connect_kwargs)
print(f"Current session ID: {self.con.session_id}")
self.account = self.con.account
self.user = self.con.user
self.role = self.con.role
self.warehouse = self.con.warehouse
self.database = self.con.database
self.schema = self.con.schema
self.user = get_snowflake_identifier_string(self.con.user, "user")
self.role = get_snowflake_identifier_string(self.con.role, "role")
self.warehouse = get_snowflake_identifier_string(
self.con.warehouse, "warehouse"
)
self.database = get_snowflake_identifier_string(self.con.database, "database")
self.schema = get_snowflake_identifier_string(self.con.schema, "schema")

if not self.autocommit:
self.con.autocommit(False)
Expand Down
25 changes: 14 additions & 11 deletions tests/session/test_SnowflakeSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ def session() -> SnowflakeSession:
logger = structlog.testing.CapturingLogger()

with mock.patch("snowflake.connector.connect"):
# noinspection PyTypeChecker
return SnowflakeSession(
user="user",
account="account",
role="role",
warehouse="warehouse",
schemachange_version="3.6.1.dev",
application="schemachange",
change_history_table=change_history_table,
logger=logger,
)
with mock.patch(
"schemachange.session.SnowflakeSession.get_snowflake_identifier_string"
):
# noinspection PyTypeChecker
return SnowflakeSession(
user="user",
account="account",
role="role",
warehouse="warehouse",
schemachange_version="3.6.1.dev",
application="schemachange",
change_history_table=change_history_table,
logger=logger,
)


class TestSnowflakeSession:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
"snowflake_warehouse": "warehouse",
"snowflake_role": "role",
}
script_path = Path(__file__).parent.parent / "demo" / "basics_demo" / "A__basic001.sql"
script_path = (
Path(__file__).parent.parent / "demo" / "basics_demo" / "2_test" / "A__basic001.sql"
)

no_command = pytest.param(
"schemachange.cli.deploy",
Expand Down Expand Up @@ -418,9 +420,11 @@
)
@mock.patch("pathlib.Path.is_file", return_value=True)
@mock.patch("schemachange.session.SnowflakeSession.snowflake.connector.connect")
@mock.patch("schemachange.session.SnowflakeSession.get_snowflake_identifier_string")
def test_main_deploy_subcommand_given_arguments_make_sure_arguments_set_on_call(
_,
__,
___,
to_mock: str,
cli_args: list[str],
expected_config: dict,
Expand Down Expand Up @@ -476,8 +480,10 @@ def test_main_deploy_subcommand_given_arguments_make_sure_arguments_set_on_call(
],
)
@mock.patch("schemachange.session.SnowflakeSession.snowflake.connector.connect")
@mock.patch("schemachange.session.SnowflakeSession.get_snowflake_identifier_string")
def test_main_deploy_config_folder(
_,
__,
to_mock: str,
args: list[str],
expected_config: dict,
Expand Down Expand Up @@ -541,8 +547,10 @@ def test_main_deploy_config_folder(
],
)
@mock.patch("schemachange.session.SnowflakeSession.snowflake.connector.connect")
@mock.patch("schemachange.session.SnowflakeSession.get_snowflake_identifier_string")
def test_main_deploy_modules_folder(
_,
__,
to_mock: str,
args: list[str],
expected_config: dict,
Expand Down

0 comments on commit c07f97c

Please sign in to comment.