Skip to content

Commit 05677b1

Browse files
committed
Tests
1 parent 55d2b6b commit 05677b1

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

test/test_cli/data/unicode.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"_id": "5b45bd7bfdc0754510853734",
3+
"名称": "MaVol_screen",
4+
"路径": "https://gitlab.com/jinniu-stra/strawman/tree/master/src/screen/MaVol_screen",
5+
"标识符": "02180706mav001",
6+
"备注": "",
7+
"雷达图": {
8+
"盈利": "4.83",
9+
"抗风险": "4.28",
10+
"稳定": "4.83",
11+
"择股": "5.0"
12+
},
13+
"原理说明": "对均线和成交量进行评估选股。",
14+
"中文名称": "一阳穿多均",
15+
"套装配置": "",
16+
"入选理由": "该股近期在60日均线之上,当前同时上穿5、10、20和30日均线,成交量是前一日的2倍,短期强势",
17+
"策略话术": "",
18+
"策略优势": "",
19+
"状态": "稳定在线",
20+
"装备详情": "http://bbs.jinniuai.com/t/topic/69",
21+
"截止时间": "2019-06-11 00:00:00"
22+
}

test/test_cli/test_script.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,23 @@ def test_help():
7575

7676
pytest.param(f"""{executable} -l User - "{test_data_path / 'users.json'}" --strings-converters""",
7777
id="users_strings_converters"),
78+
pytest.param(f"""{executable} -m SomeUnicode "{test_data_path / 'unicode.json'}" """,
79+
id="convert_unicode"),
80+
pytest.param(f"""{executable} -m SomeUnicode "{test_data_path / 'unicode.json'}" --no-unidecode""",
81+
id="dont_convert_unicode"),
82+
pytest.param(f"""{executable} -m SomeUnicode "{test_data_path / 'unicode.json'}" --disable-unicode-conversion""",
83+
id="dont_convert_unicode_2"),
7884
]
7985

8086

81-
def _validate_result(proc: subprocess.Popen) -> Tuple[str, str]:
87+
def _validate_result(proc: subprocess.Popen, output=None, output_file: Path = None) -> Tuple[str, str]:
8288
stdout, stderr = map(bytes.decode, proc.communicate())
89+
if output_file:
90+
assert output is None
91+
with output_file.open(encoding='utf-8') as f:
92+
output = f.read()
93+
if output:
94+
stdout = output
8395
assert not stderr, stderr
8496
assert stdout, stdout
8597
assert proc.returncode == 0
@@ -153,3 +165,12 @@ def test_wrong_arguments(command):
153165
print("Command:", command)
154166
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
155167
_validate_result(proc)
168+
169+
170+
@pytest.mark.parametrize("command", test_commands)
171+
def test_script_output_file(command):
172+
file = tmp_path / 'out.py'
173+
command += f" -o {file}"
174+
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
175+
stdout, stderr = _validate_result(proc, output_file=file)
176+
print(stdout)

test/test_code_generation/test_models_code_generator.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,89 @@ def test_absolute_model_ref():
206206
assert wrapper.to_typing_code()[1] == "List[List['TestModel']]"
207207
with AbsoluteModelRef.inject({test_model: test_model}):
208208
assert wrapper.to_typing_code()[1] == "List[List['TestModel.TestModel']]"
209+
210+
211+
test_unicode_data = [
212+
pytest.param(
213+
model_factory("Test", {
214+
"foo": int,
215+
"bar": int,
216+
"baz": float
217+
}),
218+
{'convert_unicode': True},
219+
trim("""
220+
class Test:
221+
foo: int
222+
bar: int
223+
baz: float
224+
"""),
225+
id="test_pytest_setup"
226+
),
227+
pytest.param(
228+
model_factory("Test", {
229+
"поле1": int,
230+
"bar": int,
231+
"baz": float
232+
}),
233+
{'convert_unicode': True},
234+
trim("""
235+
class Test:
236+
pole1: int
237+
bar: int
238+
baz: float
239+
"""),
240+
id="test_field_on"
241+
),
242+
pytest.param(
243+
model_factory("Test", {
244+
"поле1": int,
245+
"bar": int,
246+
"baz": float
247+
}),
248+
{'convert_unicode': False},
249+
trim("""
250+
class Test:
251+
поле1: int
252+
bar: int
253+
baz: float
254+
"""),
255+
id="test_field_off"
256+
),
257+
pytest.param(
258+
model_factory("Тест", {
259+
"поле1": int,
260+
"bar": int,
261+
"baz": float
262+
}),
263+
{'convert_unicode': True},
264+
trim("""
265+
class Test:
266+
pole1: int
267+
bar: int
268+
baz: float
269+
"""),
270+
id="test_field_on"
271+
),
272+
pytest.param(
273+
model_factory("Тест", {
274+
"поле1": int,
275+
"bar": int,
276+
"baz": float
277+
}),
278+
{'convert_unicode': False},
279+
trim("""
280+
class Тест:
281+
поле1: int
282+
bar: int
283+
baz: float
284+
"""),
285+
id="test_classname_off"
286+
),
287+
]
288+
289+
290+
@pytest.mark.parametrize("value,kwargs,expected", test_unicode_data)
291+
def test_generated(value: ModelMeta, kwargs: dict, expected: str):
292+
generated = generate_code(([{"model": value, "nested": []}], {}),
293+
GenericModelCodeGenerator, class_generator_kwargs=kwargs)
294+
assert generated.rstrip() == expected, generated

test/test_code_generation/test_typing.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def model(data: dict, name: str):
5656
return ModelPtr(meta)
5757

5858

59-
class TestModel:
60-
pass
61-
62-
6359
# MetaData | Tuple[import_stmnt, type]
6460
test_data = [
6561
pytest.param(

0 commit comments

Comments
 (0)