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

Cannot pass param, param_exists or params kwarg to Mock constructor #104

Closed
sarayourfriend opened this issue Dec 26, 2023 · 0 comments · Fixed by #111
Closed

Cannot pass param, param_exists or params kwarg to Mock constructor #104

sarayourfriend opened this issue Dec 26, 2023 · 0 comments · Fixed by #111

Comments

@sarayourfriend
Copy link
Collaborator

Passing any of the query parameter settings to the Mock constructor results in the following error:

> pook.get(url="https://example.com", params={"a": "b"})
src/pook/api.py:342: in get
    return mock(url, method="GET", **kw)
src/pook/api.py:326: in mock
    return _engine.mock(url, **kw)
src/pook/engine.py:150: in mock
    mock = Mock(url=url, **kw)
src/pook/mock.py:130: in __init__
    trigger_methods(self, kw)
src/pook/helpers.py:41: in trigger_methods
    member(value)
src/pook/mock.py:355: in params
    url = furl(self._request.rawurl)
src/pook/request.py:81: in rawurl
    return self._url if isregex(self._url) else urlunparse(self._url)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

components = None

    def urlunparse(components):
        """Put a parsed URL back together again.  This may result in a
        slightly different, but equivalent URL, if the URL that was parsed
        originally had redundant delimiters, e.g. a ? with an empty query
        (the draft states that these are equivalent)."""
        scheme, netloc, url, params, query, fragment, _coerce_result = (
>                                                     _coerce_args(*components))
E       TypeError: urllib.parse._coerce_args() argument after * must be an iterable, not NoneType

/usr/lib64/python3.12/urllib/parse.py:515: TypeError

This is because params is called on the Mock instance by trigger_methods without a url necessarily being present. The implementation of Mock::params is such that a url must be set on the request object before. This happens even if url is passed to the constructor because this line sorts the keys before iteration, causing params to get handled before url:

for name in sorted(args):

A better solution than sorting the list would be to keep a static list of the methods that should be executed in the order they're meant to be. For consistency, this should start with the alphabetical order to maintain the current behaviour, but include the fix to move url forward ahead of other methods that depend on it.

Ideally, the mock would be lazy in these cases and wait to process the param inputs until the URL is set. This would be more flexible overall anyway, as it would allow for the creation of a mock factory function in testing code that sets up params without the URL, and wouldn't behave in such an unexpected way.

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 a pull request may close this issue.

1 participant