From 45a5f1c37c66740e6973958472da4281460d544d Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 29 May 2020 11:16:43 +0800 Subject: [PATCH] change: convert lst.0.name to lst[0].name --- httprunner/compat.py | 12 +++++++++--- httprunner/exceptions.py | 4 ++-- tests/compat_test.py | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/httprunner/compat.py b/httprunner/compat.py index 3c8112c6..99b6c7f9 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -24,12 +24,18 @@ def convert_jmespath(raw: Text) -> Text: raw_list.append(f'"{item}"') elif item.isdigit(): # convert lst.0.name to lst[0].name - raw_list.append(f"[{item}]") + if len(raw_list) == 0: + raise exceptions.FileFormatError( + f"Invalid jmespath: {raw}, jmespath should startswith headers/body/status_code/cookies" + ) + + last_item = raw_list.pop() + item = f"{last_item}[{item}]" + raw_list.append(item) else: raw_list.append(item) - # lst.[0].name => lst[0].name - return ".".join(raw_list).replace(".[", "[") + return ".".join(raw_list) def convert_extractors(extractors: Union[List, Dict]) -> Dict: diff --git a/httprunner/exceptions.py b/httprunner/exceptions.py index 39e45f7e..6559fd87 100644 --- a/httprunner/exceptions.py +++ b/httprunner/exceptions.py @@ -40,11 +40,11 @@ class FileFormatError(MyBaseError): pass -class TestCaseFormatError(MyBaseError): +class TestCaseFormatError(FileFormatError): pass -class TestSuiteFormatError(MyBaseError): +class TestSuiteFormatError(FileFormatError): pass diff --git a/tests/compat_test.py b/tests/compat_test.py index 9722013d..17341ff2 100644 --- a/tests/compat_test.py +++ b/tests/compat_test.py @@ -1,6 +1,6 @@ import unittest -from httprunner import compat +from httprunner import compat, exceptions class TestCompat(unittest.TestCase): @@ -18,6 +18,8 @@ class TestCompat(unittest.TestCase): compat.convert_jmespath("body.data.buildings.0.building_id"), "body.data.buildings[0].building_id", ) + with self.assertRaises(exceptions.FileFormatError): + compat.convert_jmespath("2.buildings.0.building_id") def test_convert_extractors(self): self.assertEqual(