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

Update use_role testing in test_run_processor #1477

Closed
wants to merge 4 commits into from

Conversation

sfc-gh-pjafari
Copy link
Contributor

@sfc-gh-pjafari sfc-gh-pjafari commented Aug 22, 2024

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

This is the first (small) step toward improvements to unit tests. Epic link

A WIP document of areas of improvement I've found so far [WIP] Current issues found in SnowCli tests

Example to show how we can update testing of use_role, so we can write less boilerplate. Using run_processor testing as an example.

  • Add tests for code paths that have use_role (2 in this case)
  • Mock use_role in all other tests
    ...

Comment on lines +94 to +98
# Test create_dev_app with no existing application AND create succeeds AND app role == package role
@mock.patch(RUN_PROCESSOR_GET_EXISTING_APP_INFO, return_value=None)
@mock.patch(NATIVEAPP_MANAGER_EXECUTE)
@mock_connection()
def test_create_dev_app_w_warehouse_access_exception(
mock_conn, mock_execute, temp_dir, mock_cursor
def test_use_role_in_create_dev_app_w_no_additional_privileges(
mock_execute, mock_get_existing_app_info, temp_dir, mock_cursor
Copy link
Contributor Author

@sfc-gh-pjafari sfc-gh-pjafari Aug 22, 2024

Choose a reason for hiding this comment

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

this test_use_role_in_create_dev_app_w_no_additional_privileges and the below test_use_role_in_create_dev_app_w_additional_privileges are the two that test use_role flows in create_or_upgrade_app.

Comment on lines +218 to +223
@mock.patch(NATIVEAPP_MANAGER_EXECUTE)
@mock.patch(NATIVEAPP_MANAGER_USE_ROLE)
@mock_connection()
def test_create_dev_app_w_warehouse_access_exception(
mock_conn, mock_use_role, mock_execute, temp_dir, mock_cursor
):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this test and the one below, we mock use_role with@mock.patch(NATIVEAPP_MANAGER_USE_ROLE). The mocking of _execute_query that was made as part of use_role are removed from mock_execute_helper.

@sfc-gh-pjafari sfc-gh-pjafari marked this pull request as ready for review August 22, 2024 19:00
@sfc-gh-pjafari sfc-gh-pjafari requested a review from a team as a code owner August 22, 2024 19:00
Comment on lines -257 to -261
(
mock_cursor([{"CURRENT_ROLE()": "old_role"}], []),
mock.call("select current_role()", cursor_class=DictCursor),
),
(None, mock.call("use role package_role")),
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, but not executing "use role" at the right time means that the behaviour could be incorrect. Removing boilerplate is good, but aren't we making the test weaker as a result?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the idea is we only need to test the behavior of use_role once for each occurrence of it. So if we are writing tests for one function (that uses use_role) we can write one (or as many needed) test to make sure that use_role works as expected. The rest of the tests we write for this function can mock use_role.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this example, use_role behavior is tested in test_get_app_pkg_distribution_in_snowflake. The rest of the tests that are written for get_app_pkg_distribution_in_snowflake in particular, can mock use_role.

Copy link
Contributor

Choose a reason for hiding this comment

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

For a happy path vs exception path pair of tests, that might make sense in some cases, but I'm not sure I'm generally aligned with this idea. And even then, it seems like a micro-optimization to me. As Cam was saying earlier today, DRY isn't necessary a target in tests, and often will end up with negative side effects. Happy to discuss more when I'm back if you want.

@sfc-gh-pjafari
Copy link
Contributor Author

sfc-gh-pjafari commented Aug 26, 2024

Closing this in favor of a proper longer-term solution.

@sfc-gh-pjafari sfc-gh-pjafari deleted the use_role-test branch October 23, 2024 14:00
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