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 add a test mocking a JSON list response #1456

Open
IncandescentChrysalis opened this issue Sep 16, 2024 · 3 comments
Open

Cannot add a test mocking a JSON list response #1456

IncandescentChrysalis opened this issue Sep 16, 2024 · 3 comments

Comments

@IncandescentChrysalis
Copy link
Contributor

IncandescentChrysalis commented Sep 16, 2024

There is no test for the get_issue_remote_links method, calling the REST API listing remote links of an issue.

Trying to add one, it seems the test framework does not handle a JSON response containing a list:

atlassian/jira.py:1731: in get_issue_remote_links
    return self.get(url, params=params)
atlassian/rest_client.py:350: in get
    advanced_mode=advanced_mode,
atlassian/rest_client.py:309: in request
    log.debug("HTTP: Response text -> %s", response.text)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Response [200]>

    @property
    def text(self):
        """Content of the response, in unicode.
    
        If Response.encoding is None, encoding will be guessed using
        ``charset_normalizer`` or ``chardet``.
    
        The encoding of the response content is determined based solely on HTTP
        headers, following RFC 2616 to the letter. If you can take advantage of
        non-HTTP knowledge to make a better guess at the encoding, you should
        set ``r.encoding`` appropriately before accessing this property.
        """
    
        # Try charset from content-type
        content = None
        encoding = self.encoding
    
        if not self.content:
            return ""
    
        # Fallback to auto-detected encoding.
        if self.encoding is None:
            encoding = self.apparent_encoding
    
        # Decode unicode from given encoding.
        try:
            content = str(self.content, encoding, errors="replace")
        except (LookupError, TypeError):
            # A LookupError is raised if the encoding was not found which could
            # indicate a misspelling or similar mistake.
            #
            # A TypeError can be raised if encoding is None
            #
            # So we try blindly encoding.
>           content = str(self.content, errors="replace")
E           TypeError: decoding to str: need a bytes-like object, list found
@IncandescentChrysalis IncandescentChrysalis changed the title Adding test mocking a JSON list response Caant add a test mocking a JSON list response Sep 16, 2024
@IncandescentChrysalis IncandescentChrysalis changed the title Caant add a test mocking a JSON list response Cannot add a test mocking a JSON list response Sep 16, 2024
@Spacetown
Copy link
Contributor

Can you post the working branch? Here no mock is visible.

@IncandescentChrysalis
Copy link
Contributor Author

IncandescentChrysalis commented Sep 20, 2024

I was merely suggesting an API route which response has a list type, but you can test this behaviour with whichever you prefer, even modifying one which already exists if you so prefer.

The response payload would look like this:

responses[None] = [
  …
]

@Spacetown
Copy link
Contributor

Now I get you. Long time since I added the request mockup. It expects a dict or uses the content as it is. You can update the code in mockup.py to handle also other types. I suggest to change the current else to check if data is a byte string, in all other cases the element should be dumped as JSON string and convert it to a bytes string.

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

No branches or pull requests

2 participants