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

Pytest returns JSONDecodeError #63

Closed
Llibyddap opened this issue Sep 10, 2019 · 4 comments
Closed

Pytest returns JSONDecodeError #63

Llibyddap opened this issue Sep 10, 2019 · 4 comments

Comments

@Llibyddap
Copy link

Still working through exactly how everything fits together (great project that does everything) - but finally got around to testing the code after noticing that the Swagger UI would post data into the DB (verified in pgAdmin), but would throw a response error.

To start fresh - pulled a new version of everything and ran the test from a clean install - same error...

============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /app/app/tests
collected 23 items

app/app/tests/api/api_v1/test_celery.py .                                [  4%]
app/app/tests/api/api_v1/test_items.py FF                                [ 13%]
app/app/tests/api/api_v1/test_login.py .F                                [ 21%]
app/app/tests/api/api_v1/test_users.py FFF..F                            [ 47%]
app/app/tests/crud/test_item.py ....                                     [ 65%]
app/app/tests/crud/test_user.py ........                                 [100%]

=================================== FAILURES ===================================
_______________________________ test_create_item _______________________________

superuser_token_headers = {'Authorization': 'Bearer eyJ0eXAiOiJ...o7a_gBBlGdepjpY'}

    def test_create_item(superuser_token_headers):
        server_api = get_server_api()
        data = {"title": "Foo", "description": "Fighters"}
        response = requests.post(
            f"{server_api}{config.API_V1_STR}/items/",
            headers=superuser_token_headers,
            json=data,
        )
>       content = response.json()

app/app/tests/api/api_v1/test_items.py:16: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
usr/local/lib/python3.7/site-packages/requests/models.py:897: in json
    return complexjson.loads(self.text, **kwargs)
usr/local/lib/python3.7/json/__init__.py:348: in loads
    return _default_decoder.decode(s)
usr/local/lib/python3.7/json/decoder.py:337: in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <json.decoder.JSONDecoder object at 0x7f3f7d41d510>
s = 'Internal Server Error', idx = 0

    def raw_decode(self, s, idx=0):
        """Decode a JSON document from ``s`` (a ``str`` beginning with
        a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.
    
        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.
    
        """
        try:
            obj, end = self.scan_once(s, idx)
        except StopIteration as err:
>           raise JSONDecodeError("Expecting value", s, err.value) from None
E           json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

usr/local/lib/python3.7/json/decoder.py:355: JSONDecodeError

You can see that the tests all fail on same JSON decoding. I've tried each of the failed tests and noted the same behavior - the POSTs will update the db - but returning information from the bd has the decoder error...

Here is an example of a successful POST to the database of an item but the response fails.

backend_1        | INFO: ('172.21.0.5', 57326) - "POST /api/v1/items/ HTTP/1.1" 500
backend_1        | ERROR: Exception in ASGI application
backend_1        | Traceback (most recent call last):
backend_1        |   File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
backend_1        |     result = await app(self.scope, self.receive, self.send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 133, in __call__
backend_1        |     await self.error_middleware(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 177, in __call__
backend_1        |     raise exc from None
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 155, in __call__
backend_1        |     await self.app(scope, receive, _send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/base.py", line 25, in __call__
backend_1        |     response = await self.dispatch_func(request, self.call_next)
backend_1        |   File "./app/main.py", line 34, in db_session_middleware
backend_1        |     response = await call_next(request)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/base.py", line 45, in call_next
backend_1        |     task.result()
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/base.py", line 38, in coro
backend_1        |     await self.app(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 84, in __call__
backend_1        |     await self.simple_response(scope, receive, send, request_headers=headers)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 140, in simple_response
backend_1        |     await self.app(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
backend_1        |     raise exc from None
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
backend_1        |     await self.app(scope, receive, sender)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 590, in __call__
backend_1        |     await route(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 208, in __call__
backend_1        |     await self.app(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
backend_1        |     response = await func(request)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/fastapi/routing.py", line 124, in app
backend_1        |     skip_defaults=response_model_skip_defaults,
backend_1        |   File "/usr/local/lib/python3.7/site-packages/fastapi/routing.py", line 56, in serialize_response
backend_1        |     raise ValidationError(errors, field.type_)
backend_1        | pydantic.error_wrappers.ValidationError: 1 validation error for Item
backend_1        | response
backend_1        |   value is not a valid dict (type=type_error.dict)

There are no changes to the code... As I'm slightly better than noob status, it's possible that I've missed something in the install workflow - but, if so, can't find it. Any help or a point in the right direction would be greatly appreciated.

@ebreton
Copy link
Contributor

ebreton commented Sep 10, 2019

I would say you are hitting the same wall as others with the (not yet implemented) orm_mode.

You can check https://github.com/tiangolo/full-stack-fastapi-postgresql/pull/43/files and let us know if that does not resolve your issue 🤞

@Llibyddap
Copy link
Author

Yes - this was exactly the issue and it resolves to a Successful Response now... Thanks. My homework now is to understand why.

Thanks.

@ebreton
Copy link
Contributor

ebreton commented Sep 10, 2019

Well @Llibyddap , the explanation is a long story that involves a PR from tiangolo in pydantic itself... but the point is that this repo has not been properly updated (yet) to support the new version of fastapi

that's for @tiangolo to fix 😁

Happy digging 👨‍🏭

@tiangolo
Copy link
Owner

Thanks for the help here @ebreton ! 👏 🙇

Thanks for reporting back and closing the issue @Llibyddap 👍

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

3 participants