test_compliance.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import pytest
  3. from pprint import pformat
  4. from . import OrderedDict
  5. from . import json
  6. from jmespath.visitor import Options
  7. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  8. COMPLIANCE_DIR = os.path.join(TEST_DIR, 'compliance')
  9. LEGACY_DIR = os.path.join(TEST_DIR, 'legacy')
  10. NOT_SPECIFIED = object()
  11. OPTIONS = Options(dict_cls=OrderedDict)
  12. def _load_all_cases():
  13. for full_path in _walk_files():
  14. if full_path.endswith('.json'):
  15. for given, test_type, test_data in load_cases(full_path):
  16. t = test_data
  17. # Benchmark tests aren't run as part of the normal
  18. # test suite, so we only care about 'result' and
  19. # 'error' test_types.
  20. if test_type == 'result':
  21. yield (given, t['expression'], t['result'], os.path.basename(full_path))
  22. elif test_type == 'error':
  23. yield (given, t['expression'], t['error'], os.path.basename(full_path))
  24. def _walk_files():
  25. # Check for a shortcut when running the tests interactively.
  26. # If a JMESPATH_TEST is defined, that file is used as the
  27. # only test to run. Useful when doing feature development.
  28. single_file = os.environ.get('JMESPATH_TEST')
  29. if single_file is not None:
  30. yield os.path.abspath(single_file)
  31. else:
  32. for root, dirnames, filenames in os.walk(TEST_DIR):
  33. for filename in filenames:
  34. yield os.path.join(root, filename)
  35. for root, dirnames, filenames in os.walk(LEGACY_DIR):
  36. for filename in filenames:
  37. yield os.path.join(root, filename)
  38. def load_cases(full_path):
  39. all_test_data = json.load(open(full_path), object_pairs_hook=OrderedDict)
  40. for test_data in all_test_data:
  41. given = test_data['given']
  42. for case in test_data['cases']:
  43. if 'result' in case:
  44. test_type = 'result'
  45. elif 'error' in case:
  46. test_type = 'error'
  47. elif 'bench' in case:
  48. test_type = 'bench'
  49. else:
  50. raise RuntimeError("Unknown test type: %s" % json.dumps(case))
  51. yield (given, test_type, case)
  52. @pytest.mark.parametrize(
  53. 'given,expression,expected,filename',
  54. list(_load_all_cases())
  55. )
  56. def test_compliance(given, expression, expected, filename):
  57. _test_expression(given, expression, expected, filename)
  58. def _test_expression(given, expression, expected, filename):
  59. import jmespath.parser
  60. try:
  61. parsed = jmespath.compile(expression)
  62. except ValueError as e:
  63. raise AssertionError(
  64. 'jmespath expression failed to compile: "%s", error: %s"' %
  65. (expression, e))
  66. actual = parsed.search(given, options=OPTIONS)
  67. expected_repr = json.dumps(expected, indent=4)
  68. actual_repr = json.dumps(actual, indent=4)
  69. error_msg = ("\n\n (%s) The expression '%s' was suppose to give:\n%s\n"
  70. "Instead it matched:\n%s\nparsed as:\n%s\ngiven:\n%s" % (
  71. filename, expression, expected_repr,
  72. actual_repr, pformat(parsed.parsed),
  73. json.dumps(given, indent=4)))
  74. error_msg = error_msg.replace(r'\n', '\n')
  75. assert actua == expected, error_msg
  76. def _test_error_expression(given, expression, error, filename):
  77. import jmespath.parser
  78. if error not in ('syntax', 'invalid-type',
  79. 'unknown-function', 'invalid-arity', 'invalid-value'):
  80. raise RuntimeError("Unknown error type '%s'" % error)
  81. try:
  82. parsed = jmespath.compile(expression)
  83. parsed.search(given)
  84. except ValueError:
  85. # Test passes, it raised a parse error as expected.
  86. pass
  87. except Exception as e:
  88. # Failure because an unexpected exception was raised.
  89. error_msg = ("\n\n (%s) The expression '%s' was suppose to be a "
  90. "syntax error, but it raised an unexpected error:\n\n%s" % (
  91. filename, expression, e))
  92. error_msg = error_msg.replace(r'\n', '\n')
  93. raise AssertionError(error_msg)
  94. else:
  95. error_msg = ("\n\n (%s) The expression '%s' was suppose to be a "
  96. "syntax error, but it successfully parsed as:\n\n%s" % (
  97. filename, expression, pformat(parsed.parsed)))
  98. error_msg = error_msg.replace(r'\n', '\n')
  99. raise AssertionError(error_msg)