8000 More validation testing. · Web5design/github3.py@da066a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit da066a0

Browse files
committed
More validation testing.
Actually found a bug before it hit any real code thanks to the tests. I love when that happens. :)
1 parent a2cdde2 commit da066a0

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

github3/validation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ def convert(self, string):
125125
class DictValidator(BaseValidator):
126126
def is_valid(self, dictionary):
127127
try:
128-
dictionary = dict(dictionary)
128+
d = dict(dictionary)
129129
except ValueError:
130130
return False
131131

132132
schema_items = self.sub_schema.items()
133-
return any(
134-
[v.is_valid(dictionary[k]) for (k, v) in schema_items]
133+
return all(
134+
[v.is_valid(d.get(k)) for (k, v) in schema_items]
135135
)
136136

137137
def convert(self, dictionary):

tests/test_validation.py

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,78 @@ def setUp(self):
3838
sub_schema=validation.IntegerValidator()
3939
)
4040

41-
def test_is_valid_None(self):
41+
def test_None_is_valid(self):
4242
self.assertFalse(self.v.is_valid(None))
4343
self.v.allow_none = True
4444
self.assertTrue(self.v.is_valid(None))
4545

46-
def test_is_valid_empty_list(self):
46+
def test_empty_list_is_valid(self):
4747
self.assertTrue(self.v.is_valid([]))
4848

49-
def test_is_valid_list_of_strings(self):
50-
self.assertFalse(
51-
self.v.is_valid(
52-
["abc", "def"]
53-
)
49+
def test_list_of_strings_is_not_valid(self):
50+
self.assertFalse(self.v.is_valid(["abc", "def"]))
51+
52+
def test_tuple_of_strings_is_not_valid(self):
53+
self.assertFalse(self.v.is_valid(("abc", "def")))
54+
55+
def test_list_of_integers_is_valid(self):
56+
self.assertTrue(self.v.is_valid([123, 456]))
57+
58+
def test_tuple_of_integers_is_valid(self):
59+
self.assertTrue(self.v.is_valid((123, 456)))
60+
61+
def test_list_of_strings_is_valid(self):
62+
self.assertTrue(self.v.is_valid(['123', '456']))
63+
64+
def test_tuple_of_strings_is_valid(self):
65+
self.assertTrue(self.v.is_valid(('123', '456')))
66+
67+
def test_list_of_strings_converts(self):
68+
self.assertEqual(
69+
self.v.convert(['123', '456']),
70+
[123, 456]
5471
)
72+
73+
def test_tuple_of_strings_converts(self):
74+
self.assertEqual(
75+
self.v.convert(('123', '456')),
76+
[123, 456]
77+
)
78+
79+
def test_list_of_integers_converts(self):
80+
self.assertEqual(
81+
self.v.convert([123, 456]),
82+
[123, 456]
83+
)
84+
85+
def test_tuple_of_integers_converts(self):
86+
self.assertEqual(
87+
self.v.convert((123, 456)),
88+
[123, 456]
89+
)
90+
91+
92+
class TestDictValidator(TestCase):
93+
sub_schema = {'author': validation.StringValidator(True),
94+
'committer': validation.StringValidator(),
95+
'message': validation.StringValidator()}
96+
97+
def setUp(self):
98+
self.v = validation.DictValidator(sub_schema=self.sub_schema)
99+
100+
def test_skips_missing_optional_keys(self):
101+
data = {'committer': 'Ian', 'message': 'Foo'}
102+
self.assertTrue(self.v.is_valid(data))
103+
104+
def test_fails_missing_required_keys(self):
105+
data = {'author': 'Ian', 'message': 'Foo'}
106+
self.assertFalse(self.v.is_valid(data))
107+
108+
def test_accepts_list_of_tuples(self):
109+
data = {'committer': 'Ian', 'message': 'Foo'}.items()
110+
self.assertTrue(self.v.is_valid(data))
111+
112+
113+
if __name__ == '__main__':
114+
import unittest
115+
unittest.main()

0 commit comments

Comments
 (0)
0