File: test_formdata.py

package info (click to toggle)
python-requests-toolbelt 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 876 kB
  • sloc: python: 3,653; makefile: 166; sh: 7
file content (76 lines) | stat: -rw-r--r-- 2,068 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Test module for requests_toolbelt.utils.formdata."""
try:
    from urllib.parse import parse_qs
except ImportError:
    from urlparse import parse_qs

from requests_toolbelt.utils.formdata import urlencode

import pytest

dict_query = {
    'first_nested': {
        'second_nested': {
            'third_nested': {
                'fourth0': 'fourth_value0',
                'fourth1': 'fourth_value1',
            },
            'third0': 'third_value0',
        },
        'second0': 'second_value0',
    },
    'outter': 'outter_value',
}

list_query = [
    ('first_nested', [
        ('second_nested', [
            ('third_nested', [
                ('fourth0', 'fourth_value0'),
                ('fourth1', 'fourth_value1'),
            ]),
            ('third0', 'third_value0'),
        ]),
        ('second0', 'second_value0'),
    ]),
    ('outter', 'outter_value'),
]

mixed_dict_query = {
    'first_nested': {
        'second_nested': [
            ('third_nested', {
                'fourth0': 'fourth_value0',
                'fourth1': 'fourth_value1',
            }),
            ('third0', 'third_value0'),
        ],
        'second0': 'second_value0',
    },
    'outter': 'outter_value',
}

expected_parsed_query = {
    'first_nested[second0]': ['second_value0'],
    'first_nested[second_nested][third0]': ['third_value0'],
    'first_nested[second_nested][third_nested][fourth0]': ['fourth_value0'],
    'first_nested[second_nested][third_nested][fourth1]': ['fourth_value1'],
    'outter': ['outter_value'],
}


@pytest.mark.parametrize("query", [dict_query, list_query, mixed_dict_query])
def test_urlencode_flattens_nested_structures(query):
    """Show that when parsed, the structure is conveniently flat."""
    parsed = parse_qs(urlencode(query))

    assert parsed == expected_parsed_query


def test_urlencode_catches_invalid_input():
    """Show that queries are loosely validated."""
    with pytest.raises(ValueError):
        urlencode(['fo'])

    with pytest.raises(ValueError):
        urlencode([('foo', 'bar', 'bogus')])