Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9087723

Browse files
committed
Better handling of None.
Start testing StringValidator
1 parentb5c4947 commit9087723

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

‎github3/validation.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class DictValidator(BaseValidator):
126126
defis_valid(self,dictionary):
127127
try:
128128
d=dict(dictionary)
129+
exceptTypeError:
130+
returnself.none(dictionary)
129131
exceptValueError:
130132
returnFalse
131133

@@ -135,6 +137,9 @@ def is_valid(self, dictionary):
135137
)
136138

137139
defconvert(self,dictionary):
140+
ifself.none(dictionary):
141+
returnNone
142+
138143
dictionary=dict(dictionary)
139144
fork,vinself.sub_schema.items():
140145
ifdictionary.get(k):
@@ -153,6 +158,9 @@ def is_valid(self, lyst):
153158
returnall([is_valid(i)foriinlyst])
154159

155160
defconvert(self,lyst):
161+
ifself.none(lyst):
162+
returnNone
163+
156164
convert=self.sub_schema.convert
157165
return [convert(i)foriinlyst]
158166

@@ -164,8 +172,9 @@ def is_valid(self, integer):
164172
exceptValueError:
165173
returnFalse
166174
exceptTypeError:
167-
ifnotself.allow_none:
175+
ifintegerisnotNone:
168176
raise
177+
returnself.none(integer)
169178
returnTrue
170179

171180
defconvert(self,integer):

‎tests/test_validation.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ def setUp(self):
77
self.v=validation.IntegerValidator()
88

99
deftest_is_valid_raises_TypeError(self):
10-
self.assertRaises(TypeError,self.v.is_valid,None)
10+
self.assertRaises(TypeError,self.v.is_valid, {})
11+
12+
deftest_handles_None(self):
13+
self.assertFalse(self.v.is_valid(None))
1114

1215
deftest_handles_ValueError(self):
1316
self.assertFalse(self.v.is_valid('abc'))
@@ -43,6 +46,10 @@ def test_None_is_valid(self):
4346
self.v.allow_none=True
4447
self.assertTrue(self.v.is_valid(None))
4548

49+
deftest_converts_None(self):
50+
self.v.allow_none=True
51+
self.assertEqual(self.v.convert(None),None)
52+
4653
deftest_empty_list_is_valid(self):
4754
self.assertTrue(self.v.is_valid([]))
4855

@@ -97,6 +104,11 @@ class TestDictValidator(TestCase):
97104
defsetUp(self):
98105
self.v=validation.DictValidator(sub_schema=self.sub_schema)
99106

107+
deftest_handles_None(self):
108+
self.assertFalse(self.v.is_valid(None))
109+
self.v.allow_none=True
110+
self.assertTrue(self.v.is_valid(None))
111+
100112
deftest_skips_missing_optional_keys(self):
101113
data= {'committer':'Ian','message':'Foo'}
102114
self.assertTrue(self.v.is_valid(data))
@@ -109,6 +121,27 @@ def test_accepts_list_of_tuples(self):
109121
data= {'committer':'Ian','message':'Foo'}.items()
110122
self.assertTrue(self.v.is_valid(data))
111123

124+
deftest_converts_list_of_tuples(self):
125+
data= {'committer':'Ian','message':'Foo'}
126+
self.assertEqual(
127+
self.v.convert(data.items()),
128+
data
129+
)
130+
131+
deftest_converts_dicts(self):
132+
data= {'committer':'Ian','message':'Foo'}
133+
self.assertEqual(
134+
self.v.convert(data),
135+
data
136+
)
137+
138+
139+
classTestStringValidator(TestCase):
140+
defsetUp(self):
141+
self.v=validation.StringValidator()
142+
143+
deftest_fails_with_None(self):
144+
self.assertFalse(self.v.is_valid(None))
112145

113146
if__name__=='__main__':
114147
importunittest

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp