PyJaipur / PyJudge

Simple Programming Contest hosting software
MIT License
17 stars 29 forks source link

added Test_server.py with error in 3 #97

Closed shivankgtm closed 5 years ago

shivankgtm commented 5 years ago

I am facing trouble in creating a third test function.

shivankgtm commented 5 years ago
 test_file_upload _______________________________

    def test_file_upload():
        files= '/question/1/output.txt'
        global url
        url += '/check/1/output.txt'
>       assert requests.post(url, files = files).status_code == 200   # givin a error

test_server.py:19:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _
../../../anaconda3/lib/python3.7/site-packages/requests/api.py:116: in post
    return request('post', url, data=data, json=json, **kwargs)
../../../anaconda3/lib/python3.7/site-packages/requests/api.py:60: in request
    return session.request(method=method, url=url, **kwargs)
../../../anaconda3/lib/python3.7/site-packages/requests/sessions.py:510: inrequest
    prep = self.prepare_request(req)
../../../anaconda3/lib/python3.7/site-packages/requests/sessions.py:453: inprepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
../../../anaconda3/lib/python3.7/site-packages/requests/models.py:316: in prepare
    self.prepare_body(data, files, json)
../../../anaconda3/lib/python3.7/site-packages/requests/models.py:504: in prepare_body
    (body, content_type) = self._encode_files(files, data)
../../../anaconda3/lib/python3.7/site-packages/requests/models.py:126: in _encode_files
    files = to_key_val_list(files or {})
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _

value = '/question/1/output.txt'

    def to_key_val_list(value):
        """Take an object and test tosee if it can be represented as a
        dictionary. If it can be, return a list of tuples, e.g.,

        ::

            >>> to_key_val_list([('key', 'val')])
            [('key', 'val')]
            >>> to_key_val_list({'key': 'val'})
            [('key', 'val')]
            >>> to_key_val_list('string')
            ValueError: cannot encodeobjects that are not 2-tuples.

        :rtype: list
        """
        if value is None:
            return None

        if isinstance(value, (str, bytes, bool, int)):
>           raise ValueError('cannot encode objects that are not 2-tuples')
E           ValueError: cannot encodeobjects that are not 2-tuples

../../../anaconda3/lib/python3.7/site-packages/requests/utils.py:301: ValueError
====================== 1 failed, 2 passed in 0.90 seconds ======================
theSage21 commented 5 years ago

I think you have to open an actual file. not pass a string

shivankgtm commented 5 years ago

I think you have to open an actual file. not pass a string oh, ok

shivankgtm commented 5 years ago

now a new error. i have no idea what's going on. i hope it's not irritating.

platform linux -- Python 3.7.2, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: /home/shivank98/Desktop/PyJudge/PyJudge, inifile:
plugins: remotedata-0.3.0, openfiles-0.3.0, doctestplus-0.1.3, arraydiff-0.2, celery-4.2.1
collected 3 items

test_server.py ..F                                  [100%]

======================== FAILURES =========================
____________________ test_file_upload _____________________

    def test_file_upload():
        files= open('files/questions/1/output.txt')
        global url
        url += '/check/1/output.txt'

>       assert requests.post(url, files = files).status_code == 200   # givin a error

test_server.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../anaconda3/lib/python3.7/site-packages/requests/api.py:116: in post
    return request('post', url, data=data, json=json, **kwargs)
../../../anaconda3/lib/python3.7/site-packages/requests/api.py:60: in request
    return session.request(method=method, url=url, **kwargs)
../../../anaconda3/lib/python3.7/site-packages/requests/sessions.py:510: in request
    prep = self.prepare_request(req)
../../../anaconda3/lib/python3.7/site-packages/requests/sessions.py:453: in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
../../../anaconda3/lib/python3.7/site-packages/requests/models.py:316: in prepare
    self.prepare_body(data, files, json)
../../../anaconda3/lib/python3.7/site-packages/requests/models.py:504: in prepare_body
    (body, content_type) = self._encode_files(files, data)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

files = ['-1103\n', '32848\n', '-70788\n', '49859\n', '70551\n', '-43971\n', ...]
data = {}

    @staticmethod
    def _encode_files(files, data):
        """Build the body for a multipart/form-data request.

            Will successfully encode files when passed as a dict or a list of
            tuples. Order is retained if data is a list oftuples but arbitrary
            if parameters are supplied as a dict.
            The tuples may be 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype)
            or 4-tuples (filename, fileobj, contentype, custom_headers).
            """
        if (not files):
            raise ValueError("Files must be provided.")
        elif isinstance(data, basestring):
            raise ValueError("Data must not be a string.")

        new_fields = []
        fields = to_key_val_list(data or {})
        files = to_key_val_list(files or {})

        for field, val in fields:
            if isinstance(val, basestring) or not hasattr(val, '__iter__'):
                val = [val]
            for v in val:
                if v is not None:
                    # Don't call str() on bytestrings: in Py3 it all goes wrong.
                    if not isinstance(v, bytes):
                        v = str(v)

                    new_fields.append(
                        (field.decode('utf-8') if isinstance(field, bytes) else field,
                         v.encode('utf-8') if isinstance(v, str) else v))

>       for (k, v) in files:
E       ValueError: too many values to unpack (expected 2)

../../../anaconda3/lib/python3.7/site-packages/requests/models.py:141: ValueError
=========== 1 failed, 2 passed in 0.74 seconds ============
shivankgtm commented 5 years ago

Yaar jo document padhne ko bola tha woh padha hai?

yes, i have read that.

shivankgtm commented 5 years ago

@theSage21 @rishabhKalakoti finally it happened. I could not see what I was missing, though it was an easy thing. just because I didn't try to go back and see the function code it took a lot of time. Though leaned a lesson.

shivankgtm commented 5 years ago

@theSage21 is it fine. time to merge?

shivankgtm commented 5 years ago

working fine.