|
1 | 1 | from unittest import TestCase |
2 | 2 | import textwrap |
3 | 3 |
|
| 4 | +import jsonpath_ng |
| 5 | + |
4 | 6 | from jsonschema import exceptions |
5 | 7 | from jsonschema.validators import _LATEST_VERSION |
6 | 8 |
|
@@ -703,119 +705,55 @@ def test_hashable(self): |
703 | 705 |
|
704 | 706 |
|
705 | 707 | class TestJsonPathRendering(TestCase): |
706 | | - def test_str(self): |
707 | | - e = exceptions.ValidationError( |
708 | | - path=["x"], |
| 708 | + def validate_json_path_rendering(self, property_name, expected_path): |
| 709 | + error = exceptions.ValidationError( |
| 710 | + path=[property_name], |
709 | 711 | message="1", |
710 | 712 | validator="foo", |
711 | 713 | instance="i1", |
712 | 714 | ) |
713 | | - self.assertEqual(e.json_path, "$.x") |
714 | 715 |
|
715 | | - def test_empty_str(self): |
716 | | - e = exceptions.ValidationError( |
717 | | - path=[""], |
718 | | - message="1", |
719 | | - validator="foo", |
720 | | - instance="i1", |
721 | | - ) |
722 | | - self.assertEqual(e.json_path, "$['']") |
| 716 | + rendered_json_path = error.json_path |
| 717 | + self.assertEqual(rendered_json_path, expected_path) |
723 | 718 |
|
724 | | - def test_numeric_str(self): |
725 | | - e = exceptions.ValidationError( |
726 | | - path=["1"], |
727 | | - message="1", |
728 | | - validator="foo", |
729 | | - instance="i1", |
730 | | - ) |
731 | | - self.assertEqual(e.json_path, "$['1']") |
| 719 | + re_parsed_name = jsonpath_ng.parse(rendered_json_path).right.fields[0] |
| 720 | + self.assertEqual(re_parsed_name, property_name) |
732 | 721 |
|
733 | | - def test_period_str(self): |
734 | | - e = exceptions.ValidationError( |
735 | | - path=["."], |
736 | | - message="1", |
737 | | - validator="foo", |
738 | | - instance="i1", |
739 | | - ) |
740 | | - self.assertEqual(e.json_path, "$['.']") |
| 722 | + def test_basic(self): |
| 723 | + self.validate_json_path_rendering("x", "$.x") |
741 | 724 |
|
742 | | - def test_single_quote_str(self): |
743 | | - e = exceptions.ValidationError( |
744 | | - path=["'"], |
745 | | - message="1", |
746 | | - validator="foo", |
747 | | - instance="i1", |
748 | | - ) |
749 | | - self.assertEqual(e.json_path, r"$['\'']") |
| 725 | + def test_empty(self): |
| 726 | + self.validate_json_path_rendering("", "$['']") |
750 | 727 |
|
751 | | - def test_space_str(self): |
752 | | - e = exceptions.ValidationError( |
753 | | - path=[" "], |
754 | | - message="1", |
755 | | - validator="foo", |
756 | | - instance="i1", |
757 | | - ) |
758 | | - self.assertEqual(e.json_path, "$[' ']") |
| 728 | + def test_number(self): |
| 729 | + self.validate_json_path_rendering("1", "$['1']") |
759 | 730 |
|
760 | | - def test_backslash_str(self): |
761 | | - e = exceptions.ValidationError( |
762 | | - path=["\\"], |
763 | | - message="1", |
764 | | - validator="foo", |
765 | | - instance="i1", |
766 | | - ) |
767 | | - self.assertEqual(e.json_path, r"$['\\']") |
| 731 | + def test_period(self): |
| 732 | + self.validate_json_path_rendering(".", "$['.']") |
| 733 | + |
| 734 | + def test_single_quote(self): |
| 735 | + self.validate_json_path_rendering("'", r"$['\'']") |
| 736 | + |
| 737 | + def test_space(self): |
| 738 | + self.validate_json_path_rendering(" ", "$[' ']") |
| 739 | + |
| 740 | + def test_backslash(self): |
| 741 | + self.validate_json_path_rendering("\\", r"$['\\']") |
768 | 742 |
|
769 | 743 | def test_backslash_single_quote(self): |
770 | | - e = exceptions.ValidationError( |
771 | | - path=[r"\'"], |
772 | | - message="1", |
773 | | - validator="foo", |
774 | | - instance="i1", |
775 | | - ) |
776 | | - self.assertEqual(e.json_path, r"$['\\\'']") |
| 744 | + self.validate_json_path_rendering(r"\'", r"$['\\\'']") |
777 | 745 |
|
778 | 746 | def test_underscore(self): |
779 | | - e = exceptions.ValidationError( |
780 | | - path=["_"], |
781 | | - message="1", |
782 | | - validator="foo", |
783 | | - instance="i1", |
784 | | - ) |
785 | | - self.assertEqual(e.json_path, r"$['_']") |
| 747 | + self.validate_json_path_rendering("_", r"$['_']") |
786 | 748 |
|
787 | 749 | def test_double_quote(self): |
788 | | - e = exceptions.ValidationError( |
789 | | - path=['"'], |
790 | | - message="1", |
791 | | - validator="foo", |
792 | | - instance="i1", |
793 | | - ) |
794 | | - self.assertEqual(e.json_path, """$['"']""") |
| 750 | + self.validate_json_path_rendering('"', """$['"']""") |
795 | 751 |
|
796 | 752 | def test_hyphen(self): |
797 | | - e = exceptions.ValidationError( |
798 | | - path=["-"], |
799 | | - message="1", |
800 | | - validator="foo", |
801 | | - instance="i1", |
802 | | - ) |
803 | | - self.assertEqual(e.json_path, "$['-']") |
| 753 | + self.validate_json_path_rendering("-", "$['-']") |
804 | 754 |
|
805 | 755 | def test_json_path_injection(self): |
806 | | - e = exceptions.ValidationError( |
807 | | - path=["a[0]"], |
808 | | - message="1", |
809 | | - validator="foo", |
810 | | - instance="i1", |
811 | | - ) |
812 | | - self.assertEqual(e.json_path, "$['a[0]']") |
| 756 | + self.validate_json_path_rendering("a[0]", "$['a[0]']") |
813 | 757 |
|
814 | 758 | def test_open_bracket(self): |
815 | | - e = exceptions.ValidationError( |
816 | | - path=["["], |
817 | | - message="1", |
818 | | - validator="foo", |
819 | | - instance="i1", |
820 | | - ) |
821 | | - self.assertEqual(e.json_path, "$['[']") |
| 759 | + self.validate_json_path_rendering("[", "$['[']") |
0 commit comments