Skip to content

Failing unit tests with missing usernames #41

@f0lie

Description

@f0lie
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― LeaderboardViewTests.test_manage_leaderboard_participants ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

self = <QueryDict: {}>, key = 'username'

    def __getitem__(self, key):
        """
        Return the last data value for this key, or [] if it's an empty list;
        raise KeyError if not found.
        """
        try:
>           list_ = super().__getitem__(key)
E           KeyError: 'username'

/usr/local/lib/python3.8/site-packages/django/utils/datastructures.py:76: KeyError

During handling of the above exception, another exception occurred:

self = <test_views.LeaderboardViewTests testMethod=test_manage_leaderboard_participants>

    def test_manage_leaderboard_participants(self):
        """ Test GET and POST method to Manage leaderboard Participants view with valid id and authenticated user """
    
        self.c.login(username="testuser", password="testpassword")
    
        url = reverse(
            "manage_leaderboard_participants", kwargs={"id": self.leaderboard.id}
        )
    
        response = self.c.get(url)
        assert response.status_code == 200
    
>       response = self.c.post(url)

github_leaderboard/app/tests/test_views.py:108: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.8/site-packages/django/test/client.py:748: in post
    response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
/usr/local/lib/python3.8/site-packages/django/test/client.py:404: in post
    return self.generic('POST', path, post_data, content_type,
/usr/local/lib/python3.8/site-packages/django/test/client.py:470: in generic
    return self.request(**r)
/usr/local/lib/python3.8/site-packages/django/test/client.py:716: in request
    self.check_exception(response)
/usr/local/lib/python3.8/site-packages/django/test/client.py:577: in check_exception
    raise exc_value
/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py:47: in inner
    response = get_response(request)
/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py:181: in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
/usr/local/lib/python3.8/contextlib.py:75: in inner
    return func(*args, **kwds)
/usr/local/lib/python3.8/site-packages/django/views/generic/base.py:70: in view
    return self.dispatch(request, *args, **kwargs)
/usr/local/lib/python3.8/site-packages/django/utils/decorators.py:43: in _wrapper
    return bound_method(*args, **kwargs)
/usr/local/lib/python3.8/site-packages/django/contrib/auth/decorators.py:21: in _wrapped_view
    return view_func(request, *args, **kwargs)
/usr/local/lib/python3.8/site-packages/django/views/generic/base.py:98: in dispatch
    return handler(request, *args, **kwargs)
github_leaderboard/app/views.py:131: in post
    username = request.POST["username"]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <QueryDict: {}>, key = 'username'

    def __getitem__(self, key):
        """
        Return the last data value for this key, or [] if it's an empty list;
        raise KeyError if not found.
        """
        try:
            list_ = super().__getitem__(key)
        except KeyError:
>           raise MultiValueDictKeyError(key)
E           django.utils.datastructures.MultiValueDictKeyError: 'username'

/usr/local/lib/python3.8/site-packages/django/utils/datastructures.py:78: MultiValueDictKeyError
-------------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------------
False
False
-------------------------------------------------------------------------------------------- Captured stderr call --------------------------------------------------------------------------------------------
ERROR 2021-04-18 19:42:57,280 log 1 140032005809984 Internal Server Error: /app/leaderboard/1/manage_participants
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/utils/datastructures.py", line 76, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'username'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/app/github_leaderboard/app/views.py", line 131, in post
    username = request.POST["username"]
  File "/usr/local/lib/python3.8/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'username'
--------------------------------------------------------------------------------------------- Captured log call ----------------------------------------------------------------------------------------------
ERROR    django.request:log.py:224 Internal Server Error: /app/leaderboard/1/manage_participants
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/utils/datastructures.py", line 76, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'username'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/app/github_leaderboard/app/views.py", line 131, in post
    username = request.POST["username"]
  File "/usr/local/lib/python3.8/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'username'

I think the code is trying to test if someone is missing. But when the server responds with missing data, it removes the username filed and hence we have a key error. I am not familiar enough with the code to fix this but it doesn't seem that hard to do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions