forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_validation.py
123 lines (89 loc) · 3.47 KB
/
test_validation.py
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
import os
from mock import patch
from pytest import raises
from readthedocs.config.validation import (
INVALID_BOOL,
INVALID_CHOICE,
INVALID_LIST,
INVALID_STRING,
ValidationError,
validate_bool,
validate_choice,
validate_list,
validate_path,
validate_string,
)
class TestValidateBool:
def test_it_accepts_true(self):
assert validate_bool(True) is True
def test_it_accepts_false(self):
assert validate_bool(False) is False
def test_it_accepts_0(self):
assert validate_bool(0) is False
def test_it_accepts_1(self):
assert validate_bool(1) is True
def test_it_fails_on_string(self):
with raises(ValidationError) as excinfo:
validate_bool('random string')
assert excinfo.value.code == INVALID_BOOL
class TestValidateChoice:
def test_it_accepts_valid_choice(self):
result = validate_choice('choice', ('choice', 'another_choice'))
assert result == 'choice'
with raises(ValidationError) as excinfo:
validate_choice('c', 'abc')
assert excinfo.value.code == INVALID_LIST
def test_it_rejects_invalid_choice(self):
with raises(ValidationError) as excinfo:
validate_choice('not-a-choice', ('choice', 'another_choice'))
assert excinfo.value.code == INVALID_CHOICE
class TestValidateList:
def test_it_accepts_list_types(self):
result = validate_list(['choice', 'another_choice'])
assert result == ['choice', 'another_choice']
result = validate_list(('choice', 'another_choice'))
assert result == ['choice', 'another_choice']
def iterator():
yield 'choice'
result = validate_list(iterator())
assert result == ['choice']
with raises(ValidationError) as excinfo:
validate_choice('c', 'abc')
assert excinfo.value.code == INVALID_LIST
def test_it_rejects_string_types(self):
with raises(ValidationError) as excinfo:
validate_list('choice')
assert excinfo.value.code == INVALID_LIST
class TestValidatePath:
def test_it_accepts_relative_path(self, tmpdir):
tmpdir.mkdir('a directory')
validate_path('a directory', str(tmpdir))
def test_it_accepts_files(self, tmpdir):
tmpdir.join('file').write('content')
validate_path('file', str(tmpdir))
def test_it_accepts_absolute_path(self, tmpdir):
path = str(tmpdir.mkdir('a directory'))
validate_path(path, 'does not matter')
def test_it_returns_relative_path(self, tmpdir):
tmpdir.mkdir('a directory')
path = validate_path('a directory', str(tmpdir))
assert path == 'a directory'
def test_it_only_accepts_strings(self):
with raises(ValidationError) as excinfo:
validate_path(None, '')
assert excinfo.value.code == INVALID_STRING
class TestValidateString:
def test_it_accepts_unicode(self):
result = validate_string('Unicöde')
assert isinstance(result, str)
def test_it_accepts_nonunicode(self):
result = validate_string('Unicode')
assert isinstance(result, str)
def test_it_rejects_float(self):
with raises(ValidationError) as excinfo:
validate_string(123.456)
assert excinfo.value.code == INVALID_STRING
def test_it_rejects_none(self):
with raises(ValidationError) as excinfo:
validate_string(None)
assert excinfo.value.code == INVALID_STRING