Skip to content

Commit 587ffd5

Browse files
committed
Fix compile to code tests
1 parent 98399bb commit 587ffd5

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

tests/test_compile_to_code.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,20 @@
55
from fastjsonschema import JsonSchemaValueException
66
from fastjsonschema import compile_to_code, compile as compile_spec
77

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)
188

19-
20-
def test_compile_to_code():
9+
def test_compile_to_code(tmp_path, monkeypatch):
2110
code = compile_to_code({
2211
'properties': {
2312
'a': {'type': 'string'},
2413
'b': {'type': 'integer'},
2514
'c': {'format': 'hostname'}, # Test generation of regex patterns to the file.
2615
}
2716
})
28-
with open('temp/schema_1.py', 'w') as f:
17+
with open(tmp_path / 'schema_1.py', 'w') as f:
2918
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
3122
assert validate({
3223
'a': 'a',
3324
'b': 1,
@@ -38,15 +29,17 @@ def test_compile_to_code():
3829
'c': 'example.com',
3930
}
4031

41-
def test_compile_to_code_ipv6_regex():
32+
def test_compile_to_code_ipv6_regex(tmp_path, monkeypatch):
4233
code = compile_to_code({
4334
'properties': {
4435
'ip': {'format': 'ipv6'},
4536
}
4637
})
47-
with open('temp/schema_2.py', 'w') as f:
38+
with open(tmp_path / 'schema_2.py', 'w') as f:
4839
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
5043
assert validate({
5144
'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
5245
}) == {
@@ -87,12 +80,14 @@ def test_compile_complex_one_of_all_of():
8780
})
8881

8982

90-
def test_compile_to_code_custom_format():
83+
def test_compile_to_code_custom_format(tmp_path, monkeypatch):
9184
formats = {'my-format': str.isidentifier}
9285
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:
9487
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
9691
assert validate("valid", formats) == "valid"
9792
with pytest.raises(JsonSchemaValueException) as exc:
9893
validate("not-valid", formats)

0 commit comments

Comments
 (0)