1from datetime import datetime
2import string
3import uuid
4
5import pytest
6
7from falcon.routing import converters
8
9
10_TEST_UUID = uuid.uuid4()
11_TEST_UUID_STR = str(_TEST_UUID)
12_TEST_UUID_STR_SANS_HYPHENS = _TEST_UUID_STR.replace('-', '')
13
14
15@pytest.mark.parametrize('value, num_digits, min, max, expected', [
16    ('123', None, None, None, 123),
17    ('01', None, None, None, 1),
18    ('001', None, None, None, 1),
19    ('0', None, None, None, 0),
20    ('00', None, None, None, 00),
21
22    ('1', 1, None, None, 1),
23    ('12', 1, None, None, None),
24    ('12', 2, None, None, 12),
25
26    ('1', 1, 1, 1, 1),
27    ('1', 1, 1, None, 1),
28    ('1', 1, 1, 2, 1),
29    ('1', 1, 2, None, None),
30    ('1', 1, 2, 1, None),
31    ('2', 1, 1, 2, 2),
32    ('2', 1, 2, 2, 2),
33    ('3', 1, 1, 2, None),
34
35    ('12', 1, None, None, None),
36    ('12', 1, 1, 12, None),
37    ('12', 2, None, None, 12),
38    ('12', 2, 1, 12, 12),
39    ('12', 2, 12, 12, 12),
40    ('12', 2, 13, 12, None),
41    ('12', 2, 13, 13, None),
42])
43def test_int_converter(value, num_digits, min, max, expected):
44    c = converters.IntConverter(num_digits, min, max)
45    assert c.convert(value) == expected
46
47
48@pytest.mark.parametrize('value', (
49    ['0x0F', 'something', '', ' '] +
50    ['123' + w for w in string.whitespace] +
51    [w + '123' for w in string.whitespace]
52))
53def test_int_converter_malformed(value):
54    c = converters.IntConverter()
55    assert c.convert(value) is None
56
57
58@pytest.mark.parametrize('num_digits', [0, -1, -10])
59def test_int_converter_invalid_config(num_digits):
60    with pytest.raises(ValueError):
61        converters.IntConverter(num_digits)
62
63
64@pytest.mark.parametrize('value, format_string, expected', [
65    ('07-03-17', '%m-%d-%y', datetime(2017, 7, 3)),
66    ('07-03-17 ', '%m-%d-%y ', datetime(2017, 7, 3)),
67    ('2017-07-03T14:30:01Z', '%Y-%m-%dT%H:%M:%SZ', datetime(2017, 7, 3, 14, 30, 1)),
68    ('2017-07-03T14:30:01', '%Y-%m-%dT%H:%M:%S', datetime(2017, 7, 3, 14, 30, 1)),
69    ('2017_19', '%Y_%H', datetime(2017, 1, 1, 19, 0)),
70
71    ('2017-07-03T14:30:01', '%Y-%m-%dT%H:%M:%SZ', None),
72    ('07-03-17 ', '%m-%d-%y', None),
73    (' 07-03-17', '%m-%d-%y', None),
74    ('07 -03-17', '%m-%d-%y', None),
75])
76def test_datetime_converter(value, format_string, expected):
77    c = converters.DateTimeConverter(format_string)
78    assert c.convert(value) == expected
79
80
81def test_datetime_converter_default_format():
82    c = converters.DateTimeConverter()
83    assert c.convert('2017-07-03T14:30:01Z') == datetime(2017, 7, 3, 14, 30, 1)
84
85
86@pytest.mark.parametrize('value, expected', [
87    (_TEST_UUID_STR, _TEST_UUID),
88    (_TEST_UUID_STR.replace('-', '', 1), _TEST_UUID),
89    (_TEST_UUID_STR_SANS_HYPHENS, _TEST_UUID),
90    ('urn:uuid:' + _TEST_UUID_STR, _TEST_UUID),
91    ('urn:uuid:' + _TEST_UUID_STR_SANS_HYPHENS, _TEST_UUID),
92
93    (' ', None),
94    (_TEST_UUID_STR + ' ', None),
95    (' ' + _TEST_UUID_STR, None),
96    (_TEST_UUID_STR[:-1], None),
97    (_TEST_UUID_STR[0], None),
98    (_TEST_UUID_STR[:-1] + 'g', None),
99    (_TEST_UUID_STR.replace('-', '_'), None),
100])
101def test_uuid_converter(value, expected):
102    c = converters.UUIDConverter()
103    assert c.convert(value) == expected
104