|
10 | 10 | import tempfile
|
11 | 11 |
|
12 | 12 | from jsonschema import Draft4Validator, Draft7Validator, __version__, cli
|
13 |
| -from jsonschema.exceptions import SchemaError, ValidationError |
| 13 | +from jsonschema.exceptions import ( |
| 14 | + RefResolutionError, |
| 15 | + SchemaError, |
| 16 | + ValidationError, |
| 17 | +) |
14 | 18 | from jsonschema.tests._helpers import captured_output
|
15 | 19 | from jsonschema.validators import _LATEST_VERSION, validate
|
16 | 20 |
|
@@ -685,72 +689,90 @@ def test_successful_validation_of_just_the_schema_pretty_output(self):
|
685 | 689 | )
|
686 | 690 |
|
687 | 691 | def test_successful_validate_with_specifying_base_uri_absolute_path(self):
|
688 |
| - absolute_path = os.getcwd() |
689 | 692 | try:
|
690 | 693 | schema_file = tempfile.NamedTemporaryFile(
|
691 | 694 | mode='w+',
|
692 |
| - prefix='schema', |
693 |
| - suffix='.json', |
694 |
| - dir=absolute_path, |
695 | 695 | delete=False
|
696 | 696 | )
|
697 | 697 | self.addCleanup(os.remove, schema_file.name)
|
698 | 698 | schema = """
|
699 | 699 | {"type": "object", "properties": {"KEY1":
|
700 |
| - {"$ref": %s%s#definitions/schemas"}}, |
701 |
| - "definitions": {"schemas": {"type": "string"}}} |
702 |
| - """ % ("\"", os.path.basename(schema_file.name)) |
703 |
| - schema_file.write(schema) |
| 700 | + {"$ref": "%s#definitions/num"}}} |
| 701 | + """ % (os.path.basename(schema_file.name)) |
| 702 | + tmp_schema = """{"definitions": {"num": {"type": "integer"}}}""" |
| 703 | + schema_file.write(tmp_schema) |
704 | 704 | finally:
|
705 | 705 | schema_file.close()
|
706 | 706 |
|
707 | 707 | file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
|
708 |
| - absolute_path = file_prefix.format(absolute_path) |
| 708 | + absolute_dir_path = file_prefix.format( |
| 709 | + os.path.dirname(schema_file.name) |
| 710 | + ) |
709 | 711 | self.assertOutputs(
|
710 |
| - files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'), |
| 712 | + files=dict(some_schema=schema, some_instance='{"KEY1": 1}'), |
711 | 713 | argv=[
|
712 | 714 | "-i", "some_instance",
|
713 |
| - "--base-uri", absolute_path, |
| 715 | + "--base-uri", absolute_dir_path, |
714 | 716 | "some_schema",
|
715 | 717 | ],
|
716 | 718 | stdout="",
|
717 | 719 | stderr="",
|
718 | 720 | )
|
719 | 721 |
|
720 | 722 | def test_failure_validate_with_specifying_base_uri_absolute_path(self):
|
721 |
| - absolute_path = os.getcwd() |
722 | 723 | try:
|
723 | 724 | schema_file = tempfile.NamedTemporaryFile(
|
724 | 725 | mode='w+',
|
725 |
| - prefix='schema', |
726 |
| - suffix='.json', |
727 |
| - dir=absolute_path, |
728 | 726 | delete=False
|
729 | 727 | )
|
730 | 728 | self.addCleanup(os.remove, schema_file.name)
|
731 | 729 | schema = """
|
732 |
| - {"type": "object", "properties": {"KEY1": |
733 |
| - {"$ref": %s%s#definitions/schemas"}}, |
734 |
| - "definitions": {"schemas": {"type": "string"}}} |
735 |
| - """ % ("\"", os.path.basename(schema_file.name)) |
736 |
| - schema_file.write(schema) |
| 730 | + {"type": "object", "properties": {"KEY1": |
| 731 | + {"$ref": "%s#definitions/num"}}} |
| 732 | + """ % (os.path.basename(schema_file.name)) |
| 733 | + tmp_schema = """{"definitions": {"num": {"type": "integer"}}}""" |
| 734 | + schema_file.write(tmp_schema) |
737 | 735 | finally:
|
738 | 736 | schema_file.close()
|
739 | 737 |
|
740 | 738 | file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
|
741 |
| - absolute_path = file_prefix.format(absolute_path) |
| 739 | + absolute_dir_path = file_prefix.format( |
| 740 | + os.path.dirname(schema_file.name) |
| 741 | + ) |
742 | 742 | self.assertOutputs(
|
743 |
| - files=dict(some_schema=schema, some_instance='{"KEY1": 1}'), |
| 743 | + files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'), |
744 | 744 | argv=[
|
745 | 745 | "-i", "some_instance",
|
746 |
| - "--base-uri", absolute_path, |
| 746 | + "--base-uri", absolute_dir_path, |
747 | 747 | "some_schema",
|
748 | 748 | ],
|
749 | 749 | exit_code=1,
|
750 | 750 | stdout="",
|
751 |
| - stderr="1: 1 is not of type 'string'\n", |
| 751 | + stderr="1: '1' is not of type 'integer'\n", |
752 | 752 | )
|
753 | 753 |
|
| 754 | + def test_validate_with_specifying_invalid_base_uri(self): |
| 755 | + schema = """ |
| 756 | + {"type": "object", "properties": {"KEY1": |
| 757 | + {"$ref": "foo.json#definitions/num"}}} |
| 758 | + """ |
| 759 | + instance = '{"KEY1": 1}' |
| 760 | + |
| 761 | + with self.assertRaises(RefResolutionError) as e: |
| 762 | + self.assertOutputs( |
| 763 | + files=dict( |
| 764 | + some_schema=schema, |
| 765 | + some_instance=instance, |
| 766 | + ), |
| 767 | + argv=[ |
| 768 | + "-i", "some_instance", |
| 769 | + "--base-uri", ".", |
| 770 | + "some_schema", |
| 771 | + ], |
| 772 | + ) |
| 773 | + error = str(e.exception) |
| 774 | + self.assertEqual(error, "unknown url type: 'foo.json'") |
| 775 | + |
754 | 776 | def test_it_validates_using_the_latest_validator_when_unspecified(self):
|
755 | 777 | # There isn't a better way now I can think of to ensure that the
|
756 | 778 | # latest version was used, given that the call to validator_for
|
|
0 commit comments