File: test_jsonitem.py

package info (click to toggle)
python-dcos 0.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,440 kB
  • sloc: python: 8,196; sh: 194; makefile: 36
file content (231 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download | duplicates (4)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from dcos import jsonitem
from dcos.errors import DCOSException

import pytest


@pytest.fixture(params=range(6))
def bad_object(request):
    return [
        '{"key":value}',
        'this is a string',
        '4.5',
        '4',
        'true',
        '[1,2,3]',
    ][request.param]


@pytest.fixture(params=range(4))
def bad_number(request):
    return [
        'this is a string',
        'true',
        '{"key":"value"}',
        '[1,2,3]',
    ][request.param]


@pytest.fixture(params=range(5))
def bad_integer(request):
    return [
        'this is a string',
        'true',
        '{"key":"value"}',
        '45.0',
        '[1,2,3]',
    ][request.param]


@pytest.fixture(params=range(5))
def bad_boolean(request):
    return [
        'this is a string',
        '45',
        '{"key":"value"}',
        '45.0',
        '[1,2,3]',
    ][request.param]


@pytest.fixture(params=range(6))
def bad_array(request):
    return [
        'this is a string',
        '45',
        '{"key":"value"}',
        '45.0',
        'true',
        '[1,2,3',
    ][request.param]


@pytest.fixture(params=[
    'www.test.com',
    'http:/hi.com',
    'https//www.hi.com',
    'http://bad.port:here'
    ])
def bad_url(request):
    return request.param


@pytest.fixture(params=[
    ('string', 'this is a string', 'this is a string'),
    ('string', 'null', None),
    ('object', '{"key":"value"}', {'key': 'value'}),
    ('object', 'null', None),
    ('number', '4.2', 4.2),
    ('number', 'null', None),
    ('integer', '42', 42),
    ('integer', 'null', None),
    ('boolean', 'true', True),
    ('boolean', 'null', None),
    ('array', '[1,2,3]', [1, 2, 3]),
    ('array', 'null', None),
    ('url', 'http://test.com', 'http://test.com')
    ])
def jsonitem_tuple(request):
    return request.param


@pytest.fixture(params=range(13))
def parse_tuple(request):
    return [
        ('string=null', ('"string"', None)),
        ('string="this is a string with ="',
         ('"string"', 'this is a string with =')),
        ("string='this is a string with ='",
         ('"string"', 'this is a string with =')),
        ('object=null', ('"object"', None)),
        ("""object='{"key":"value"}'""", ('"object"', {'key': 'value'})),
        ('number=null', ('"number"', None)),
        ('number=4.2', ('"number"', 4.2)),
        ('integer=null', ('"integer"', None)),
        ('integer=42', ('"integer"', 42)),
        ('boolean=null', ('"boolean"', None)),
        ('boolean=true', ('"boolean"', True)),
        ('array=null', ('"array"', None)),
        ("array='[1,2,3]'", ('"array"', [1, 2, 3])),
    ][request.param]


@pytest.fixture(params=range(6))
def bad_parse(request):
    return [
        "====",
        "no equals",
        "object=[]",
        "something=cool",
        "integer=",
        "integer=45.0",
    ][request.param]


@pytest.fixture
def schema():
    return {
        'type': 'object',
        'properties': {
            'integer': {
                'type': 'integer'
            },
            'number': {
                'type': 'number'
            },
            'string': {
                'type': 'string',
            },
            'object': {
                'type': 'object'
            },
            'array': {
                'type': 'array'

            },
            'boolean': {
                'type': 'boolean',
            },
            'url': {
                'type': 'string',
                'format': 'url',

            }
        }
    }


def test_parse_string():
    string = 'this is a string "'
    assert jsonitem._parse_string(string) == string


def test_parse_object():
    assert jsonitem._parse_object('{"key": "value"}') == {'key': 'value'}


def test_parse_invalid_objects(bad_object):
    with pytest.raises(DCOSException):
        jsonitem._parse_object(bad_object)


def test_parse_number():
    assert jsonitem._parse_number('45') == 45
    assert jsonitem._parse_number('45.0') == 45.0


def test_parse_invalid_numbers(bad_number):
    with pytest.raises(DCOSException):
        jsonitem._parse_number(bad_number)


def test_parse_integer():
    assert jsonitem._parse_integer('45') == 45


def test_parse_invalid_integers(bad_integer):
    with pytest.raises(DCOSException):
        jsonitem._parse_integer(bad_integer)


def test_parse_boolean():
    assert jsonitem._parse_boolean('true') is True
    assert jsonitem._parse_boolean('false') is False


def test_parse_invalid_booleans(bad_boolean):
    with pytest.raises(DCOSException):
        jsonitem._parse_boolean(bad_boolean)


def test_parse_array():
    assert jsonitem._parse_array('[1,2,3]') == [1, 2, 3]


def test_parse_invalid_arrays(bad_array):
    with pytest.raises(DCOSException):
        jsonitem._parse_array(bad_array)


def test_parse_url():
    assert jsonitem._parse_url('http://test.com:12') == 'http://test.com:12'


def test_parse_invalid_url(bad_url):
    with pytest.raises(DCOSException):
        jsonitem._parse_url(bad_url)


def test_find_parser(schema, jsonitem_tuple):
    key, string_value, value = jsonitem_tuple
    assert jsonitem.find_parser(key, schema)(string_value) == value


def test_parse_json_item(schema, parse_tuple):
    arg, result = parse_tuple
    assert jsonitem.parse_json_item(arg, schema) == result


def test_parse_bad_json_item(schema, bad_parse):
    with pytest.raises(DCOSException):
        jsonitem.parse_json_item(bad_parse, schema)