Passed
Pull Request — master (#8)
by
unknown
01:09
created

test.test_config.TestCheckURL.test_full_url_is_ok()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import logging
2
from unittest import TestCase
3
4
from hypothesis import example, given, strategies as st, assume
5
from hypothesis.provisional import domains, urls
6
7
import config
8
9
10
def _everything_except(excluded_types):
11
    return st.from_type(type).flatmap(st.from_type).filter(lambda x: not isinstance(x, excluded_types))
12
13
14
MIN_PORT = 0
15
MAX_PORT = 2 ** 16 - 1
16
PORTS_VALID = st.integers(min_value=MIN_PORT, max_value=MAX_PORT)
17
PORTS_INVALID = st.integers().filter(lambda x: x < MIN_PORT or x > MAX_PORT)
18
19
20
@st.composite
21
def _urls_with_out_of_bounds_port(draw):
22
    domain = draw(domains())
23
    port = draw(PORTS_INVALID)
24
    return "http://%s:%d" % (domain, port)
25
26
27
@st.composite
28
def _urls_with_bogus_port(draw):
29
    domain = draw(domains())
30
    port = draw(_everything_except(int))
31
    return "http://%s:%s" % (domain, port)
32
33
34
class TestCheckURL(TestCase):
35
    @given(urls())
36
    @example("http://example.com/some/path?x=2")
37
    @example("http://example.com/some/path")
38
    @example("http://example.com/")
39
    @example("http://example.com")
40
    def test_full_url_is_ok(self, url):
41
        self.assertEqual(config._check_url(url), url)
42
43
    @given(domains())
44
    @example(None)
45
    @example("example.com")
46
    @example("://example.com")
47
    def test_raises_for_missing_or_wrong_scheme(self, url):
48
        self.assertRaises(ValueError, config._check_url, url)
49
50
    @given(_urls_with_out_of_bounds_port())
51
    def test_raises_for_out_of_bounds_port_number(self, url):
52
        self.assertRaises(ValueError, config._check_url, url)
53
54
    @given(domain=domains(), port=_everything_except(int))
55
    def test_raises_for_bogus_port_number(self, domain, port):
56
        assume(str(port) not in ("[]", ""))  # urllib bug: https://bugs.python.org/issue36338
57
        url = "http://%s:%s" % (domain, port)
58
        self.assertRaises(ValueError, config._check_url, url)
59
60
61
class TestCheckPort(TestCase):
62
    @given(PORTS_VALID)
63
    def test_port_range_works(self, port):
64
        self.assertEqual(config._check_port(port), port)
65
66
    @given(PORTS_INVALID)
67
    def test_outside_port_range_fails(self, port):
68
        self.assertRaises(ValueError, config._check_port, port)
69
70
    @given(port=_everything_except(int))
71
    def test_raises_for_text(self, port):
72
        assume(not str(port).isnumeric())
73
        self.assertRaises(ValueError, config._check_port, port)
74
75
76
class TestCheckLogLevel(TestCase):
77
    def test_check_log_level_works_for_expected_values(self):
78
        for level in config.LOG_LEVELS:
79
            with self.subTest(level):
80
                self.assertEqual(config._check_log_level(level), getattr(logging, level.upper()))
81
82
    @given(st.text().filter(lambda x: x.upper() not in config.LOG_LEVELS))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable config does not seem to be defined.
Loading history...
83
    def test_check_log_level_raises_for_random_text(self, value):
84
        self.assertRaises(ValueError, config._check_log_level, value)
85
86
    @given(_everything_except(str))
87
    def test_check_log_level_raises_for_random_non_text_stuff(self, value):
88
        self.assertRaises(ValueError, config._check_log_level, value)
89