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

better mock for sql executor to use in facade #1769

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sfc-gh-pjafari
Copy link
Contributor

Pre-review checklist

  • I've confirmed that instructions included in README.md are still correct after my changes in the codebase.
  • I've added or updated automated unit tests to verify correctness of my new code.
  • I've added or updated integration tests to verify correctness of my new code.
  • I've confirmed that my changes are working by executing CLI's commands manually on MacOS.
  • I've confirmed that my changes are working by executing CLI's commands manually on Windows.
  • I've confirmed that my changes are up-to-date with the target branch.
  • I've described my changes in the release notes.
  • I've described my changes in the section below.

Changes description

...

Comment on lines +48 to +49
def all_calls_made(self):
return len(self._mock_results) == 0
Copy link
Contributor

@sfc-gh-fcampbell sfc-gh-fcampbell Oct 22, 2024

Choose a reason for hiding this comment

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

idea, we can automate this:

Suggested change
def all_calls_made(self):
return len(self._mock_results) == 0
def __enter__():
return self
def __exit__(exc_type, exc_val, exc_tb):
if not (exc_type and exc_val and exc_tb):
num_remaining = len(self._mock_results)
assert num_remaining == 0, f"Some expected calls were not made, {num_remaining} remain"
return False # Don't suppress the exception

use it as

validating_executor = MockQueriesExecutor(
    expected_calls=[
        ("select current_role()", [mock_cursor([("old_role",)], [])]),
        ("use role mock_role", [None]),
        ("select current_warehouse()", [mock_cursor([("old_wh",)], [])]),
        ("use warehouse mock_wh", [None]),
        ("select current_database()", [mock_cursor([("old_db",)], [])]),
        ("use database mock_db", [None]),
        (mock_script, [None]),
        ("use database old_db", [None]),
        ("use warehouse old_wh", [None]),
        ("use role old_role", [None]),
    ]
)
sql_facade = SnowflakeSQLFacade(validating_executor)
with validating_executor:
    sql_facade.execute_user_script(
        queries=mock_script,
        script_name=mock_script_name,
        role=role,
        warehouse=wh,
        database=database,
    )

Comment on lines +41 to +44
if dedent(queries).strip() != dedent(query_expected).strip():
raise Exception(
f"Test failed, expected query {query_expected} but received query {queries}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if dedent(queries).strip() != dedent(query_expected).strip():
raise Exception(
f"Test failed, expected query {query_expected} but received query {queries}"
)
assert dedent(queries).strip() == dedent(query_expected).strip(), f"Expected query {query_expected} but received query {queries}"

that way we get pytest assertion rewriting prettiness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants