Open flaeppe opened 2 years ago
This is a great suggestion and enhancement of the router settings.
I think this could be solved by adding a .configure(...)
method to the router and include the assertions and base_url settings in the snapshot/rollback functionality. That would allow the user to alter these settings within a specific test case and automatically be rolled back to previous configuration when exiting mocked context.
Something like this:
my_mock_api = respx.mock(assert_all_called=False)
...
@my_mock_api
def test_somthing_else():
...
# won't assert that all routes have been called when exiting this test case
@my_mock_api
def test_something():
my_mock_api.configure(assert_all_called=True)
...
# will assert that all routes have been called when exiting this test case
I gave this a try a few days ago @flaeppe, and got stuck on a "problem" with the base_url
.
How it works is that the base URL is turned into a bases pattern by the router, and in turn merged to each added route.
We need a way to alter/clone any existing routes' patterns if they have previous bases. This is also mentioned in #186.
About the configure
api, I'm now leaning towards your first idea of allowing call on the router instance to re-configure all settings. Actually, it already works like this, except that any existing routes are not cloned to the new router instance.
About the
configure
api, I'm now leaning towards your first idea of allowing call on the router instance to re-configure all settings. Actually, it already works like this, except that any existing routes are not cloned to the new router instance.
Cool, yeah. Some additional Config
object instance that could be attached as a "head" for patterns, together with having the bases as some sort of "swappable component" might work? I'm really just babbling now, haven't dug in to how that would actually pan out.
How it works is that the base URL is turned into a bases pattern by the router, and in turn merged to each added route.
One initial idea could be to "postpone" merging until matching/comparison happens, though that will come with a performance hit, not sure how severe that would be though. Or if it could be mitigated with some @cached_property
or similar.
But if I'm interpreting you right here, the problematic part is that bases are merged with each route and we have no way of "rewinding" that merge? So that we can only edit the bases patterns.
But if I'm interpreting you right here, the problematic part is that bases are merged with each route and we have no way of "rewinding" that merge? So that we can only edit the bases patterns.
Correct @flaeppe.
At the moment, I'm trying to actually implement a rewind feature, or more correctly adjust the merge_patterns
function to smartly override any existing bases with new ones in a pattern tree.
Also to get that working, I had to implement .clone()
on RouteList
, Route
and Pattern
(nested) to not mutate parent router state 😅 .
Side note...this would be of interest even for #190 in the case a route is added to the default global respx
router, then all subsequent routers would inherit that/those added routes and a test case would only need to be decorated with the sub-router.
I'm wondering if it could be possible to support reusing some settings of a
MockRouter
instance, when invoking__call__
. Basically that'll allow me to specify aMockRouter
with defaults, and for specific test cases flip theassert_all_called
-flag.Consider the code below. I want
base_url
andassert_all_called
to have its default, unless they're overriden by a new__call__
invocationCurrently, this gives me an error whenever I pass in new kwargs to
default
(test_1
andwith default
). As__call__
is just creating a newMockRouter
instance from scratch.I'm realising as I write this that support for this might appear better through some additional
MockRouter
method (e.g.MockRouter.replace()
) instead of changing__call__
.