-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_tests.py
139 lines (115 loc) · 4.26 KB
/
generate_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import shutil
from collections import defaultdict
from collections.abc import Iterator
from importlib import import_module
from itertools import chain
from pathlib import Path
from pkgutil import walk_packages
from types import ModuleType
import kio.schema
from kio.static.protocol import ApiMessage
from .case import to_snake_case
def generate_modules(parent: ModuleType) -> Iterator[ModuleType]:
for package in walk_packages(parent.__path__):
module = import_module(f"{parent.__name__}.{package.name}")
if package.ispkg:
yield from generate_modules(module)
else:
yield module
def get_entities() -> Iterator[tuple[type, str]]:
modules = list(generate_modules(import_module("kio.schema")))
for module in modules:
items = module.__dict__.copy()
# Eliminate non-entity modules, situated directly under `kio.schema`.
if sum(1 for c in module.__name__ if c == ".") < 3:
continue
for key, value in items.items():
if key.startswith("__"):
continue
# Eliminate anything not defined in the module.
if getattr(value, "__module__", None) != module.__name__:
continue
# Eliminate anything that's not a class.
if type(value) is not type:
continue
assert isinstance(module.__file__, str)
yield value, module.__file__
imports = """\
from __future__ import annotations
import io
from hypothesis import given, settings
from hypothesis.strategies import from_type
from kio.serial import entity_writer
from tests.conftest import setup_buffer, JavaTester
from kio.serial import entity_reader
from typing import Final
import pytest
"""
import_code = """\
from {entity_module} import {entity_type}
"""
test_code = """\
read_{entity_snake_case}: Final = entity_reader({entity_type})
@pytest.mark.roundtrip
@given(from_type({entity_type}))
@settings(max_examples=1)
def test_{entity_snake_case}_roundtrip(instance: {entity_type}) -> None:
writer = entity_writer({entity_type})
with setup_buffer() as buffer:
writer(buffer, instance)
buffer.seek(0)
result = read_{entity_snake_case}(buffer)
assert instance == result
"""
test_code_java = """\
@pytest.mark.java
@given(instance=from_type({entity_type}))
def test_{entity_snake_case}_java(instance: {entity_type}, java_tester: JavaTester) -> None:
java_tester.test(instance)
"""
base_dir = Path(__file__).parent.parent.resolve()
generated_tests_module = base_dir / "tests" / "generated"
schema_src_dir = Path(kio.schema.__file__).parent.resolve()
def build_filename(source_path: str) -> str:
return "test_" + str(Path(source_path).relative_to(schema_src_dir)).replace(
"/", "_"
)
def main() -> None:
shutil.rmtree(generated_tests_module, ignore_errors=True)
os.mkdir(generated_tests_module)
(generated_tests_module / "__init__.py").touch()
module_imports = defaultdict(list)
module_code = defaultdict(list)
for entity_type, file in get_entities():
module_path = generated_tests_module / build_filename(file)
module_imports[module_path].append(
import_code.format(
entity_module=entity_type.__module__,
entity_type=entity_type.__name__,
)
)
module_code[module_path].append(
test_code.format(
entity_type=entity_type.__name__,
entity_snake_case=to_snake_case(entity_type.__name__),
)
)
if issubclass(entity_type, ApiMessage) and entity_type.__name__ not in {
"ProduceRequest", # Records
"FetchResponse", # Records
"FetchSnapshotResponse", # Records
}:
module_code[module_path].append(
test_code_java.format(
entity_type=entity_type.__name__,
entity_snake_case=to_snake_case(entity_type.__name__),
)
)
for module_path, entity_imports in module_imports.items():
with module_path.open("w") as fd:
print(imports, file=fd)
for code in chain(entity_imports, module_code[module_path]):
print(code, file=fd)
if __name__ == "__main__":
main()