-
Notifications
You must be signed in to change notification settings - Fork 259
[Refactor] Screenshot generator: Better state management via mocks #860
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
base: dev
Are you sure you want to change the base?
[Refactor] Screenshot generator: Better state management via mocks #860
Conversation
| # Set up mocks to provide whatever temporary data/state a particular screenshot | ||
| # might need. | ||
| @contextmanager | ||
| def mock_load_psbt(base64_psbt: str, seed: Seed = seed_12b): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is not the case, but if in the future a context manager needs to declare more variables, this could be a good alternative to avoid the withnesting:
from contextlib import ExitStack
with ExitStack() as stack:
stack.enter_context(patch.object(controller, 'psbt', decoder.get_psbt()))
stack.enter_context(patch.object(controller, 'psbt_seed', seed))
stack.enter_context(patch.object(controller, 'psbt_parser', PSBTParser(p=controller.psbt, seed=seed)))
yieldThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Diving further into python techniques I've never used!
For the moment I like the with patch... convention (even with the nesting), because it directly parallels the same technique used elsewhere in the test suite.
|
I understood the changes and I think it's a good QOL improvement. ACK tested. |
fc6fa4f to
b447edd
Compare
|
Rebased to current |
Motivation
The screenshot generator's
ScreenshotConfig.run_beforeparam currently provides a way to inject data into theControllerstate just before a given screenshot is rendered.The downside is that these state changes remain in place for all subsequent screenshots that follow in the
screenshot_list.Some screenshots depend on a prior state change and try to document this with
# Relies on callback abovecomments.A few other screenshots also depend on prior state changes but do NOT document the dependency.
This
run_beforemethod and its aftereffects is not ideal.And when more complex contexts are required for new screenshots (see #858) the weaknesses here look even worse.
Solution
This PR refactors
run_beforeto use temporaryunittest.mock.patchcalls instead. The patches are defined using the slightly less straightforwardcontextmanagerpython approach (I had not been terribly familiar withyield) and passed intoScreenshotConfigvia a newmock_context_managerparam. But the tradeoff win is that this approach brings familiar test suite mocking patterns into the screenshot generator.Simplified example:
The screenshots that were relying on prior state changes being present now just call whichever
mock_context_managerthey need. Those dependencies on a particular expected internal state are now explicit in all cases.Interestingly, there were also some DRY wins.
This pull request is categorized as a:
Checklist
pytestand made sure all unit tests pass before submitting the PRIf you modified or added functionality/workflow, did you add new unit tests?
I have tested this PR on the following platforms/os: