Skip to content

Commit b668506

Browse files
committed
Add generator testcase for invalid product blocks config
Signed-off-by: Mark90 <[email protected]>
1 parent 93a439f commit b668506

File tree

6 files changed

+122
-41
lines changed

6 files changed

+122
-41
lines changed

test/unit_tests/cli/conftest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
from functools import partial
3+
4+
import pytest
5+
from typer.testing import CliRunner
6+
7+
from orchestrator.cli.database import app as db_app
8+
from test.unit_tests.cli.helpers import create_main
9+
10+
11+
@pytest.fixture(scope="module")
12+
def monkey_module():
13+
with pytest.MonkeyPatch.context() as mp:
14+
yield mp
15+
16+
17+
@pytest.fixture(scope="module")
18+
def tmp_generate_path(tmp_path_factory):
19+
yield tmp_path_factory.mktemp("generate")
20+
21+
22+
@pytest.fixture(scope="module")
23+
def cli_invoke(tmp_generate_path, monkey_module):
24+
monkey_module.chdir(tmp_generate_path)
25+
sys.path.append(str(tmp_generate_path))
26+
create_main()
27+
28+
runner = CliRunner()
29+
# Don't catch exceptions because this will cost you grey hair.
30+
invoke = partial(runner.invoke, catch_exceptions=False)
31+
invoke(db_app, ["init"])
32+
33+
yield invoke
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Testcase for invalid config: multiple root product blocks
2+
config:
3+
summary_forms: False
4+
name: invalidexample1
5+
type: InvalidExample4
6+
tag: INVALIDEXAMPLE1
7+
description: "Invalid Product example 1"
8+
product_blocks:
9+
- name: block1
10+
type: Block1
11+
tag: BLOCK1
12+
fields:
13+
- name: num_val
14+
type: int
15+
- name: block2
16+
type: Block2
17+
tag: BLOCK2
18+
fields:
19+
- name: str_val
20+
type: str
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Testcase for invalid config: cyclic product blocks
2+
config:
3+
summary_forms: False
4+
name: invalidexample1
5+
type: InvalidExample4
6+
tag: INVALIDEXAMPLE1
7+
description: "Invalid Product example 1"
8+
product_blocks:
9+
- name: block1
10+
type: Block1
11+
tag: BLOCK1
12+
fields:
13+
- name: num_val
14+
type: int
15+
- name: sub_block
16+
type: Block2
17+
- name: block2
18+
type: Block2
19+
tag: BLOCK2
20+
fields:
21+
- name: str_val
22+
type: str
23+
- name: sub_block
24+
type: Block1

test/unit_tests/cli/helpers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pathlib import Path
2+
3+
4+
def absolute_path(path: str) -> str:
5+
file = Path(__file__).resolve().parent / "data" / path
6+
return str(file)
7+
8+
9+
def create_main():
10+
with open("main.py", "w") as fp:
11+
fp.write(
12+
"from orchestrator import OrchestratorCore\n"
13+
"from orchestrator.cli.main import app as core_cli\n"
14+
"from orchestrator.settings import AppSettings\n"
15+
"\n"
16+
"app = OrchestratorCore(base_settings=AppSettings())\n"
17+
'if __name__ == "__main__":\n'
18+
" core_cli()\n"
19+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
import structlog
3+
4+
from orchestrator.cli.generate import app as generate_app
5+
from test.unit_tests.cli.helpers import absolute_path
6+
7+
logger = structlog.get_logger()
8+
9+
10+
@pytest.mark.parametrize(
11+
"config_file,expected_exception,expected_message",
12+
[
13+
("invalid_product_config1.yaml", ValueError, "found multiple"),
14+
("invalid_product_config2.yaml", ValueError, "Cycle detected"),
15+
],
16+
)
17+
def test_product_block_validation(config_file, expected_exception, expected_message, cli_invoke):
18+
config_file = absolute_path(config_file)
19+
with pytest.raises(expected_exception, match=expected_message):
20+
cli_invoke(generate_app, ["product-blocks", "--config-file", config_file])

test/unit_tests/cli/test_generate_code.py

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,31 @@
11
import re
2-
import sys
32
from difflib import context_diff
43
from filecmp import dircmp
5-
from functools import partial
64
from pathlib import Path
75

86
import pytest
97
import structlog
108
from more_itertools import one
11-
from typer.testing import CliRunner
129

13-
from orchestrator.cli.database import app as db_app
1410
from orchestrator.cli.generate import app as generate_app
11+
from test.unit_tests.cli.helpers import absolute_path
1512

1613
logger = structlog.get_logger()
1714

1815

19-
def absolute_path(path: str) -> str:
20-
file = Path(__file__).resolve().parent / "data" / path
21-
return str(file)
22-
23-
24-
def create_main():
25-
with open("main.py", "w") as fp:
26-
fp.write(
27-
"from orchestrator import OrchestratorCore\n"
28-
"from orchestrator.cli.main import app as core_cli\n"
29-
"from orchestrator.settings import AppSettings\n"
30-
"\n"
31-
"app = OrchestratorCore(base_settings=AppSettings())\n"
32-
'if __name__ == "__main__":\n'
33-
" core_cli()\n"
34-
)
35-
36-
3716
@pytest.fixture(scope="module")
38-
def monkey_module():
39-
with pytest.MonkeyPatch.context() as mp:
40-
yield mp
41-
42-
43-
@pytest.fixture(scope="module")
44-
def actual_folder(tmp_path_factory, monkey_module) -> Path:
45-
tmp_path = tmp_path_factory.mktemp("generate")
46-
monkey_module.chdir(tmp_path)
47-
sys.path.append(str(tmp_path))
48-
create_main()
49-
runner = CliRunner()
50-
51-
# Don't catch exceptions because this will cost you grey hair.
52-
invoke = partial(runner.invoke, catch_exceptions=False)
53-
invoke(db_app, ["init"])
17+
def actual_folder(cli_invoke, tmp_generate_path) -> Path:
5418
for config_file in (
5519
absolute_path("product_config2.yaml"),
5620
absolute_path("product_config1.yaml"),
5721
absolute_path("product_config4.yaml"),
5822
):
5923
for cmd in ("product-blocks", "product", "workflows", "unit-tests"):
60-
invoke(generate_app, [cmd, "--config-file", config_file, "--no-dryrun", "--force"])
24+
cli_invoke(generate_app, [cmd, "--config-file", config_file, "--no-dryrun", "--force"])
25+
26+
cli_invoke(generate_app, ["migration", "--config-file", config_file])
6127

62-
invoke(generate_app, ["migration", "--config-file", config_file])
63-
return tmp_path
28+
return tmp_generate_path
6429

6530

6631
@pytest.fixture(scope="module")

0 commit comments

Comments
 (0)