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

refactor test data generation #237

Merged
merged 3 commits into from
Sep 14, 2023

Conversation

wpbonelli
Copy link
Contributor

@wpbonelli wpbonelli commented Sep 14, 2023

Use session-scoped pytest yield fixtures, custom parametrization, and filelock to generate files in the proper order, serially or in parallel via pytest-xdist.

The basic problem this solves is: can we use pytest to generate files, where some files depend on other files being created first, while parallelizing non-dependent file creation as much as possible?

How it works

Use pytest_generate_tests to enumerate "source" data files. Define fixtures, parametrized with the source data files, to convert to intermediate data files (kind of like rules in GNU make). Define test functions which consume the fixtures and make intermediate files. Define session-scoped yield fixtures to generate final data files, but only after yielding (at which point intermediate files already exist). Use filelock as suggested in pytest-xdist docs to protect the critical section (containing the yield statement and code to generate the final file) in the session-scoped fixtures. Define test functions consuming the session-scoped fixtures. We're done!

All the real work is done in test fixtures. Test functions are only used to invoke the fixtures. Test functions can be thought of like the top-level rule defining a makefile's targets.

If we didn't need to support xdist/parallel, yield fixtures would suffice. The filelock just makes sure only one worker process creates the final file(s).

ds = ds.rename({var: f"{var}_vol"})
ds = ds * params.metadata["hru_in_to_cf"]
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whoops! thanks

@codecov-commenter
Copy link

codecov-commenter commented Sep 14, 2023

Codecov Report

Merging #237 (579093b) into develop (a17937a) will not change coverage.
The diff coverage is n/a.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@           Coverage Diff            @@
##           develop     #237   +/-   ##
========================================
  Coverage    79.52%   79.52%           
========================================
  Files           51       51           
  Lines         6506     6506           
========================================
  Hits          5174     5174           
  Misses        1332     1332           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jmccreight
Copy link
Member

@wpbonelli thanks, very refreshing! 🍸

@jmccreight jmccreight merged commit 2f57332 into EC-USGS:develop Sep 14, 2023
9 checks passed
@wpbonelli wpbonelli deleted the test-data-generation branch September 14, 2023 21:21
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.

3 participants