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

Simplify tests for CLI connections commands #21983

Merged
merged 4 commits into from
Mar 4, 2022

Conversation

dstandish
Copy link
Contributor

@dstandish dstandish commented Mar 4, 2022

The tests (particularly with the export subcommand) were doing a fair bit of unnecessary mocking. In particular they mocked builtins.open. But there's no good reason that we can't actually write out the file and read the file in the assert (which is a much better way to test this). There were also a number of assertions re splittext which are not really relevant.

I suspect the reason that the author did this is because the file wasn't closed, so when you read the value from the exported file (if the process has not been exited) then the buffer may not be fully written to disk. I fix this by closing the file after writing the connections.

As part of this change I also update to use current pytest techniques for parameterization and temp files.

@dstandish dstandish requested review from ashb and uranusjr March 4, 2022 05:50
def tearDown(self):
clear_db_connections()
def setup_method(self):
clear_db_connections(add_default_connections_back=True)
Copy link
Member

Choose a reason for hiding this comment

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

I think we should do this after the test to clean up properly. Maybe a setup_class and a teardown_method so we ensure a clean slate both before and after each test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah i was thinking about that

maybe an autouse fixture in the module that clears after yield

they could all use same fixture except that only some of the classes require the example connectios

Copy link
Contributor Author

Choose a reason for hiding this comment

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

like

@pytest.fixture(scope='class', autouse=True)
def clear_connections():
    yield
    clear_db_connections(add_default_connections_back=False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

Isn’t that essentially the same as

def teardown_method(self):
    clear_db_connections(add_default_connections_back=False)

but more code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no because there are 4 (or more?) classes and this will apply to all of them... right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually 6 :)

The tests (particularly with the export subcommand) were doing a fair bit of unnecessary mocking.  In particular they mocked `builtins.open`.  But there's no good reason that we can't actually write out the file and read the file in the assert (which is a much better way to test this).

I suspect the reason that the author did this is because the file wasn't _closed_, so when you read the value from the exported file (if the process has not been exited) then the buffer may not be fully written to disk.  I fix this by closing the file after writing the connections.

As part of this change I also update to use current pytest techniques for parameterization and setup / teardown.
@dstandish dstandish force-pushed the simplify-connection-command-tests branch from 604aa35 to d72c9fc Compare March 4, 2022 15:02
@github-actions github-actions bot added the okay to merge It's ok to merge this PR as it does not require more tests label Mar 4, 2022
@github-actions
Copy link

github-actions bot commented Mar 4, 2022

The PR is likely OK to be merged with just subset of tests for default Python and Database versions without running the full matrix of tests, because it does not modify the core of Airflow. If the committers decide that the full tests matrix is needed, they will add the label 'full tests needed'. Then you should rebase to the latest main or amend the last commit of the PR, and push it with --force-with-lease.

@dstandish dstandish merged commit e81527c into apache:main Mar 4, 2022
@dstandish dstandish deleted the simplify-connection-command-tests branch March 4, 2022 18:26
@ephraimbuddy ephraimbuddy added the type:misc/internal Changelog: Misc changes that should appear in change log label Apr 8, 2022
@ephraimbuddy ephraimbuddy added changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) and removed type:misc/internal Changelog: Misc changes that should appear in change log labels Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:CLI changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) okay to merge It's ok to merge this PR as it does not require more tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants