Skip to content

Commit 33a6b8e

Browse files
bkalashnikovbogdandm
bkalashnikov
authored andcommitted
Fix code generation test
1 parent ac567dc commit 33a6b8e

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

test/test_cli/test_script.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ def test_help():
9595
]
9696

9797

98-
def load_model(code):
99-
module = types.ModuleType(uuid.uuid4().hex)
98+
def load_model(code, module_name=''):
99+
module_name = module_name or uuid.uuid4().hex
100+
module = types.ModuleType(module_name)
100101
exec(code, module.__dict__)
102+
sys.modules[module_name] = module
101103
return module
102104

103105

test/test_cli/test_self_validate_pydantic.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import imp
21
import json
32
import sys
43
from inspect import isclass
@@ -11,7 +10,7 @@
1110
from json_to_models.models.pydantic import PydanticModelCodeGenerator
1211
from json_to_models.models.structure import compose_models_flat
1312
from json_to_models.registry import ModelRegistry
14-
from .test_script import test_data_path
13+
from .test_script import test_data_path, load_model
1514

1615
test_self_validate_pydantic_data = [
1716
pytest.param(test_data_path / "gists.json", list, id="gists.json"),
@@ -39,14 +38,9 @@ def test_self_validate_pydantic(data, data_type):
3938

4039
structure = compose_models_flat(reg.models_map)
4140
code = generate_code(structure, PydanticModelCodeGenerator)
42-
module = imp.new_module("test_models")
43-
sys.modules["test_models"] = module
44-
try:
45-
exec(compile(code, "test_models.py", "exec"), module.__dict__)
46-
except Exception as e:
47-
assert not e, code
48-
49-
import test_models
41+
42+
test_models = load_model(code, 'test_models')
43+
5044
for name in dir(test_models):
5145
cls = getattr(test_models, name)
5246
if isclass(cls) and issubclass(cls, pydantic.BaseModel):

0 commit comments

Comments
 (0)