alchemy-mock icon indicating copy to clipboard operation
alchemy-mock copied to clipboard

clearing a session's history for testing calls

Open roth-mh opened this issue 4 years ago • 5 comments

Hi, I am using pytest and assert_has_calls to determine if my session makes certain queries, but I am running into the issue of having multiple tests on the same session. When this happens, the session still has the calls from the previous tests and so tests fail.

what is the best way to get around this? creating a new session every time? wiping the current session's call history?

the other aspect is that i am loading my mocked DB with data via the UnifiedAlchemyMagicMock(data=[ ...]) functionality but i don't want to have to do this for every test as it seems expensive and kind of clunky.

thanks in advance!

roth-mh avatar Nov 25 '19 18:11 roth-mh

perhaps another important piece of this puzzle is that I was imagining essentially creating a single, large UnifiedAlchemyMagicMock() instance that would hold the data for many tests, but I'm curious if this was the intended use. Or if the vision was that each suite of tests would have their own instance, or something more modular.

roth-mh avatar Nov 25 '19 18:11 roth-mh

dont think there is any specific vision. lib attempts to provide some tooling but how its used is up to the user. as for the session being shared, try using something like reset_mock which should reset all of the mock state but leave the unified mock data intact

miki725 avatar Nov 25 '19 20:11 miki725

Thank you! That is exactly what I was looking for. Follow up question:

reset_mock does not seem to clear objects that I've added to a session. is there a way to do this, or do I have to call a delete?

roth-mh avatar Nov 25 '19 20:11 roth-mh

currently there is no equivalent for reset_mock_data but that sounds like a good feature to have. PR is welcome or at some point Ill try to add one

miki725 avatar Nov 26 '19 00:11 miki725

Cool. I will keep you updated and, likewise, let me know if you make any progress.

Thanks!

roth-mh avatar Nov 27 '19 06:11 roth-mh