5
5
from fastjsonschema import JsonSchemaValueException
6
6
from fastjsonschema import compile_to_code , compile as compile_spec
7
7
8
- @pytest .yield_fixture (autouse = True )
9
- def run_around_tests ():
10
- temp_dir = 'temp'
11
- # Code that will run before your test, for example:
12
- if not os .path .isdir (temp_dir ):
13
- os .makedirs (temp_dir )
14
- # A test function will be run at this point
15
- yield
16
- # Code that will run after your test, for example:
17
- shutil .rmtree (temp_dir )
18
8
19
-
20
- def test_compile_to_code ():
9
+ def test_compile_to_code (tmp_path , monkeypatch ):
21
10
code = compile_to_code ({
22
11
'properties' : {
23
12
'a' : {'type' : 'string' },
24
13
'b' : {'type' : 'integer' },
25
14
'c' : {'format' : 'hostname' }, # Test generation of regex patterns to the file.
26
15
}
27
16
})
28
- with open ('temp/ schema_1.py' , 'w' ) as f :
17
+ with open (tmp_path / ' schema_1.py' , 'w' ) as f :
29
18
f .write (code )
30
- from temp .schema_1 import validate
19
+ with monkeypatch .context () as m :
20
+ m .syspath_prepend (tmp_path )
21
+ from schema_1 import validate
31
22
assert validate ({
32
23
'a' : 'a' ,
33
24
'b' : 1 ,
@@ -38,15 +29,17 @@ def test_compile_to_code():
38
29
'c' : 'example.com' ,
39
30
}
40
31
41
- def test_compile_to_code_ipv6_regex ():
32
+ def test_compile_to_code_ipv6_regex (tmp_path , monkeypatch ):
42
33
code = compile_to_code ({
43
34
'properties' : {
44
35
'ip' : {'format' : 'ipv6' },
45
36
}
46
37
})
47
- with open ('temp/ schema_2.py' , 'w' ) as f :
38
+ with open (tmp_path / ' schema_2.py' , 'w' ) as f :
48
39
f .write (code )
49
- from temp .schema_2 import validate
40
+ with monkeypatch .context () as m :
41
+ m .syspath_prepend (tmp_path )
42
+ from schema_2 import validate
50
43
assert validate ({
51
44
'ip' : '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
52
45
}) == {
@@ -87,12 +80,14 @@ def test_compile_complex_one_of_all_of():
87
80
})
88
81
89
82
90
- def test_compile_to_code_custom_format ():
83
+ def test_compile_to_code_custom_format (tmp_path , monkeypatch ):
91
84
formats = {'my-format' : str .isidentifier }
92
85
code = compile_to_code ({'type' : 'string' , 'format' : 'my-format' }, formats = formats )
93
- with open ('temp/ schema_3.py' , 'w' ) as f :
86
+ with open (tmp_path / ' schema_3.py' , 'w' ) as f :
94
87
f .write (code )
95
- from temp .schema_3 import validate
88
+ with monkeypatch .context () as m :
89
+ m .syspath_prepend (tmp_path )
90
+ from schema_3 import validate
96
91
assert validate ("valid" , formats ) == "valid"
97
92
with pytest .raises (JsonSchemaValueException ) as exc :
98
93
validate ("not-valid" , formats )
0 commit comments