1
1
from io import StringIO
2
2
from json import JSONDecodeError
3
+ from pathlib import Path
3
4
from textwrap import dedent
4
5
from unittest import TestCase
5
6
import errno
@@ -688,72 +689,69 @@ def test_successful_validation_of_just_the_schema_pretty_output(self):
688
689
stderr = "" ,
689
690
)
690
691
691
- def test_successful_validate_with_specifying_base_uri_absolute_path (self ):
692
- try :
693
- schema_file = tempfile .NamedTemporaryFile (
694
- mode = 'w+' ,
695
- delete = False
696
- )
697
- self .addCleanup (os .remove , schema_file .name )
698
- schema = """
699
- {"$ref": "%s#definitions/num"}
700
- """ % (os .path .basename (schema_file .name ))
701
- tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
702
- schema_file .write (tmp_schema )
703
- finally :
704
- schema_file .close ()
692
+ def test_successful_validation_via_explicit_base_uri (self ):
693
+ ref_schema_file = tempfile .NamedTemporaryFile (delete = False )
694
+ self .addCleanup (os .remove , ref_schema_file .name )
695
+
696
+ ref_path = Path (ref_schema_file .name )
697
+ ref_path .write_text ('{"definitions": {"num": {"type": "integer"}}}' )
698
+
699
+ schema = f'{{"$ref": "{ ref_path .name } #definitions/num"}}'
705
700
706
- file_prefix = "file:///{}/" if "nt" == os .name else "file://{}/"
707
- absolute_dir_path = file_prefix .format (
708
- os .path .dirname (schema_file .name )
709
- )
710
701
self .assertOutputs (
711
702
files = dict (some_schema = schema , some_instance = '1' ),
712
703
argv = [
713
704
"-i" , "some_instance" ,
714
- "--base-uri" , absolute_dir_path ,
705
+ "--base-uri" , ref_path . parent . as_uri () + "/" ,
715
706
"some_schema" ,
716
707
],
717
708
stdout = "" ,
718
709
stderr = "" ,
719
710
)
720
711
721
- def test_failure_validate_with_specifying_base_uri_absolute_path (self ):
722
- try :
723
- schema_file = tempfile .NamedTemporaryFile (
724
- mode = 'w+' ,
725
- delete = False
726
- )
727
- self .addCleanup (os .remove , schema_file .name )
728
- schema = """
729
- {"$ref": "%s#definitions/num"}
730
- """ % (os .path .basename (schema_file .name ))
731
- tmp_schema = """{"definitions": {"num": {"type": "integer"}}}"""
732
- schema_file .write (tmp_schema )
733
- finally :
734
- schema_file .close ()
712
+ def test_unsuccessful_validation_via_explicit_base_uri (self ):
713
+ ref_schema_file = tempfile .NamedTemporaryFile (delete = False )
714
+ self .addCleanup (os .remove , ref_schema_file .name )
715
+
716
+ ref_path = Path (ref_schema_file .name )
717
+ ref_path .write_text ('{"definitions": {"num": {"type": "integer"}}}' )
718
+
719
+ schema = f'{{"$ref": "{ ref_path .name } #definitions/num"}}'
735
720
736
- file_prefix = "file:///{}/" if "nt" == os .name else "file://{}/"
737
- absolute_dir_path = file_prefix .format (
738
- os .path .dirname (schema_file .name )
739
- )
740
721
self .assertOutputs (
741
722
files = dict (some_schema = schema , some_instance = '"1"' ),
742
723
argv = [
743
724
"-i" , "some_instance" ,
744
- "--base-uri" , absolute_dir_path ,
725
+ "--base-uri" , ref_path . parent . as_uri () + "/" ,
745
726
"some_schema" ,
746
727
],
747
728
exit_code = 1 ,
748
729
stdout = "" ,
749
730
stderr = "1: '1' is not of type 'integer'\n " ,
750
731
)
751
732
752
- def test_validate_with_specifying_invalid_base_uri (self ):
753
- schema = """
754
- {"$ref": "foo.json#definitions/num"}
755
- """
756
- instance = '1'
733
+ def test_nonexistent_file_with_explicit_base_uri (self ):
734
+ schema = '{"$ref": "someNonexistentFile.json#definitions/num"}'
735
+ instance = "1"
736
+
737
+ with self .assertRaises (RefResolutionError ) as e :
738
+ self .assertOutputs (
739
+ files = dict (
740
+ some_schema = schema ,
741
+ some_instance = instance ,
742
+ ),
743
+ argv = [
744
+ "-i" , "some_instance" ,
745
+ "--base-uri" , Path .cwd ().as_uri (),
746
+ "some_schema" ,
747
+ ],
748
+ )
749
+ error = str (e .exception )
750
+ self .assertIn ("/someNonexistentFile.json'" , error )
751
+
752
+ def test_invalid_exlicit_base_uri (self ):
753
+ schema = '{"$ref": "foo.json#definitions/num"}'
754
+ instance = "1"
757
755
758
756
with self .assertRaises (RefResolutionError ) as e :
759
757
self .assertOutputs (
@@ -763,12 +761,14 @@ def test_validate_with_specifying_invalid_base_uri(self):
763
761
),
764
762
argv = [
765
763
"-i" , "some_instance" ,
766
- "--base-uri" , ". " ,
764
+ "--base-uri" , "not@UR1 " ,
767
765
"some_schema" ,
768
766
],
769
767
)
770
768
error = str (e .exception )
771
- self .assertEqual (error , "unknown url type: 'foo.json'" )
769
+ self .assertEqual (
770
+ error , "unknown url type: 'foo.json'" ,
771
+ )
772
772
773
773
def test_it_validates_using_the_latest_validator_when_unspecified (self ):
774
774
# There isn't a better way now I can think of to ensure that the
0 commit comments