@@ -187,31 +187,31 @@ def test_invalid_format_default_message(self):
187
187
188
188
189
189
class TestErrorReprStr (unittest .TestCase ):
190
-
191
- message = "hello"
192
-
193
- def setUp (self ):
194
- self .error = ValidationError (
195
- message = self .message ,
190
+ def make_error (self , ** kwargs ):
191
+ defaults = dict (
192
+ message = u"hello" ,
196
193
validator = u"type" ,
197
194
validator_value = u"string" ,
198
195
instance = 5 ,
199
- schema = {u"type" : u"string" },
196
+ schema = {u"type" : u"string" },
200
197
)
198
+ defaults .update (kwargs )
199
+ return ValidationError (** defaults )
201
200
202
- def assertShows (self , message ):
201
+ def assertShows (self , expected , ** kwargs ):
203
202
if PY3 :
204
- message = message .replace ("u'" , "'" )
205
- message = textwrap .dedent (message ).rstrip ("\n " )
203
+ expected = expected .replace ("u'" , "'" )
204
+ expected = textwrap .dedent (expected ).rstrip ("\n " )
206
205
207
- message_line , _ , rest = str (self .error ).partition ("\n " )
208
- self .assertEqual (message_line , self .message )
209
- self .assertEqual (rest , message )
206
+ error = self .make_error (** kwargs )
207
+ message_line , _ , rest = str (error ).partition ("\n " )
208
+ self .assertEqual (message_line , error .message )
209
+ self .assertEqual (rest , expected )
210
210
211
211
def test_repr (self ):
212
212
self .assertEqual (
213
- repr (self . error ),
214
- "<ValidationError: %r>" % self . message ,
213
+ repr (ValidationError ( message = "Hello!" ) ),
214
+ "<ValidationError: %r>" % "Hello!" ,
215
215
)
216
216
217
217
def test_unset_error (self ):
@@ -232,46 +232,47 @@ def test_unset_error(self):
232
232
self .assertEqual (str (error ), "message" )
233
233
234
234
def test_empty_paths (self ):
235
- self .error .path = self .error .schema_path = []
236
235
self .assertShows (
237
236
"""
238
237
Failed validating u'type' in schema:
239
238
{u'type': u'string'}
240
239
241
240
On instance:
242
241
5
243
- """
242
+ """ ,
243
+ path = [],
244
+ schema_path = [],
244
245
)
245
246
246
247
def test_one_item_paths (self ):
247
- self .error .path = [0 ]
248
- self .error .schema_path = ["items" ]
249
248
self .assertShows (
250
249
"""
251
250
Failed validating u'type' in schema:
252
251
{u'type': u'string'}
253
252
254
253
On instance[0]:
255
254
5
256
- """
255
+ """ ,
256
+ path = [0 ],
257
+ schema_path = ["items" ],
257
258
)
258
259
259
260
def test_multiple_item_paths (self ):
260
- self .error .path = [0 , u"a" ]
261
- self .error .schema_path = [u"items" , 0 , 1 ]
262
261
self .assertShows (
263
262
"""
264
263
Failed validating u'type' in schema[u'items'][0]:
265
264
{u'type': u'string'}
266
265
267
266
On instance[0][u'a']:
268
267
5
269
- """
268
+ """ ,
269
+ path = [0 , u"a" ],
270
+ schema_path = [u"items" , 0 , 1 ],
270
271
)
271
272
272
273
def test_uses_pprint (self ):
273
274
with mock .patch ("pprint.pformat" ) as pformat :
274
- str (self .error )
275
+ str (self .make_error () )
275
276
self .assertEqual (pformat .call_count , 2 ) # schema + instance
276
277
277
278
0 commit comments