|
| 1 | +import textwrap |
| 2 | + |
1 | 3 | from jsonschema import Draft4Validator, exceptions
|
| 4 | +from jsonschema.compat import PY3 |
2 | 5 | from jsonschema.tests.compat import mock, unittest
|
3 | 6 |
|
4 | 7 |
|
@@ -270,21 +273,110 @@ def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
|
270 | 273 | self.assertIsInstance(tree["foo"], exceptions.ErrorTree)
|
271 | 274 |
|
272 | 275 |
|
| 276 | +class TestErrorReprStr(unittest.TestCase): |
| 277 | + def make_error(self, **kwargs): |
| 278 | + defaults = dict( |
| 279 | + message=u"hello", |
| 280 | + validator=u"type", |
| 281 | + validator_value=u"string", |
| 282 | + instance=5, |
| 283 | + schema={u"type": u"string"}, |
| 284 | + ) |
| 285 | + defaults.update(kwargs) |
| 286 | + return exceptions.ValidationError(**defaults) |
| 287 | + |
| 288 | + def assertShows(self, expected, **kwargs): |
| 289 | + if PY3: |
| 290 | + expected = expected.replace("u'", "'") |
| 291 | + expected = textwrap.dedent(expected).rstrip("\n") |
| 292 | + |
| 293 | + error = self.make_error(**kwargs) |
| 294 | + message_line, _, rest = str(error).partition("\n") |
| 295 | + self.assertEqual(message_line, error.message) |
| 296 | + self.assertEqual(rest, expected) |
| 297 | + |
| 298 | + def test_repr(self): |
| 299 | + self.assertEqual( |
| 300 | + repr(exceptions.ValidationError(message="Hello!")), |
| 301 | + "<ValidationError: %r>" % "Hello!", |
| 302 | + ) |
| 303 | + |
| 304 | + def test_unset_error(self): |
| 305 | + error = exceptions.ValidationError("message") |
| 306 | + self.assertEqual(str(error), "message") |
| 307 | + |
| 308 | + kwargs = { |
| 309 | + "validator": "type", |
| 310 | + "validator_value": "string", |
| 311 | + "instance": 5, |
| 312 | + "schema": {"type": "string"} |
| 313 | + } |
| 314 | + # Just the message should show if any of the attributes are unset |
| 315 | + for attr in kwargs: |
| 316 | + k = dict(kwargs) |
| 317 | + del k[attr] |
| 318 | + error = exceptions.ValidationError("message", **k) |
| 319 | + self.assertEqual(str(error), "message") |
| 320 | + |
| 321 | + def test_empty_paths(self): |
| 322 | + self.assertShows( |
| 323 | + """ |
| 324 | + Failed validating u'type' in schema: |
| 325 | + {u'type': u'string'} |
| 326 | +
|
| 327 | + On instance: |
| 328 | + 5 |
| 329 | + """, |
| 330 | + path=[], |
| 331 | + schema_path=[], |
| 332 | + ) |
| 333 | + |
| 334 | + def test_one_item_paths(self): |
| 335 | + self.assertShows( |
| 336 | + """ |
| 337 | + Failed validating u'type' in schema: |
| 338 | + {u'type': u'string'} |
| 339 | +
|
| 340 | + On instance[0]: |
| 341 | + 5 |
| 342 | + """, |
| 343 | + path=[0], |
| 344 | + schema_path=["items"], |
| 345 | + ) |
| 346 | + |
| 347 | + def test_multiple_item_paths(self): |
| 348 | + self.assertShows( |
| 349 | + """ |
| 350 | + Failed validating u'type' in schema[u'items'][0]: |
| 351 | + {u'type': u'string'} |
| 352 | +
|
| 353 | + On instance[0][u'a']: |
| 354 | + 5 |
| 355 | + """, |
| 356 | + path=[0, u"a"], |
| 357 | + schema_path=[u"items", 0, 1], |
| 358 | + ) |
| 359 | + |
| 360 | + def test_uses_pprint(self): |
| 361 | + with mock.patch("pprint.pformat") as pformat: |
| 362 | + str(self.make_error()) |
| 363 | + self.assertEqual(pformat.call_count, 2) # schema + instance |
| 364 | + |
273 | 365 | def test_str_works_with_instances_having_overriden_eq_operator(self):
|
274 | 366 | """
|
275 |
| - Checks for https://github.com/Julian/jsonschema/issues/164 which |
276 |
| - rendered exceptions unusable when a `ValidationError` involved classes |
277 |
| - withthe `eq` operator overridden (such as pandas.DataFrame), |
278 |
| - caused by a `XX in YYY` check within `__unicode__`() method. |
| 367 | + Check for https://github.com/Julian/jsonschema/issues/164 which |
| 368 | + rendered exceptions unusable when a `ValidationError` involved |
| 369 | + instances with an `__eq__` method that returned truthy values. |
| 370 | +
|
279 | 371 | """
|
280 | 372 |
|
281 |
| - class InstanceWithOverridenEq(object): |
282 |
| - def __eq__(self, other): |
283 |
| - raise Exception("Instance's __eq__()hould not have been called!") |
284 |
| - inst = InstanceWithOverridenEq() |
| 373 | + instance = mock.MagicMock() |
285 | 374 | error = exceptions.ValidationError(
|
286 |
| - "a message", validator="foo", instance=inst, validator_value='some', schema='schema', |
| 375 | + "a message", |
| 376 | + validator="foo", |
| 377 | + instance=instance, |
| 378 | + validator_value="some", |
| 379 | + schema="schema", |
287 | 380 | )
|
288 |
| - |
289 |
| - ex_str = str(error) |
290 |
| - self.assertTrue(str(exceptions).find(type(inst).__name__), ex_str) |
| 381 | + str(error) |
| 382 | + self.assertFalse(instance.__eq__.called) |
0 commit comments