Skip to content

Commit ecd9c19

Browse files
committed
CCM-12896: Add a unit test for generate_models
1 parent 50304a2 commit ecd9c19

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

scripts/config/sonar-scanner.properties

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# Please DO NOT set the following properties `sonar.organization` and `sonar.projectKey` in this file. They must be stored as `SONAR_ORGANISATION_KEY` and `SONAR_PROJECT_KEY` GitHub secrets.
22

33
sonar.host.url=https://sonarcloud.io
4-
# Temporary change to allow testing of build before fixing Sonar coverage issues
5-
sonar.qualitygate.wait=false
6-
sonar.log.level=TRACE
4+
sonar.qualitygate.wait=true
75
sonar.sourceEncoding=UTF-8
86
sonar.sources=.
97
sonar.tests=tests/, src/asyncapigenerator/tests, src/cloudeventjekylldocs/tests, src/eventcatalogasyncapiimporter/tests, src/python-schema-generator/tests, src/cloudevents/tools/builder/__tests__, src/cloudevents/tools/cache/__tests__, src/cloudevents/tools/generator/__tests__, lambdas/mesh-poll/src/__tests__, lambdas/ttl-create-lambda/src/__tests__, lambdas/ttl-poll-lambda/src/__tests__, utils/utils/src/__tests__, utils/sender-management/src/__tests__

src/python-schema-generator/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ generate: install
3434
# Run tests
3535
test: install-dev
3636
@echo "Running tests..."
37-
@pytest tests/
37+
@cd ../.. && PYTHONPATH=src/python-schema-generator/src:$$PYTHONPATH pytest src/python-schema-generator/tests/
3838

3939
# Generate coverage report
4040
coverage: install-dev
4141
@echo "Generating coverage report..."
42-
@cd ../.. && pytest src/python-schema-generator/tests/ \
42+
@cd ../.. && PYTHONPATH=src/python-schema-generator/src:$$PYTHONPATH pytest src/python-schema-generator/tests/ \
4343
--cov=src/python-schema-generator \
4444
--cov-config=src/python-schema-generator/pytest.ini \
4545
--cov-report=html:src/python-schema-generator/htmlcov \

src/python-schema-generator/src/generate_models.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ def parse_args() -> argparse.Namespace:
4646
return parser.parse_args()
4747

4848

49-
def main() -> int:
49+
def main(args) -> int:
5050
"""Main entry point for the generator.
5151
5252
Returns:
5353
Exit code (0 for success, 1 for failure)
5454
"""
55-
args = parse_args()
56-
5755
try:
5856
print(f"Generating models in: {args.output_dir}")
5957

@@ -89,4 +87,5 @@ def main() -> int:
8987

9088

9189
if __name__ == "__main__":
92-
sys.exit(main())
90+
args = parse_args()
91+
sys.exit(main(args))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from src.generate_models import main
2+
from argparse import Namespace
3+
4+
class TestGenerateModels:
5+
"""Tests for generate_models module."""
6+
7+
def test_main_returns_zero_on_success(self, tmp_path):
8+
"""Test that main returns 0 on successful execution."""
9+
input_dir = tmp_path / "input"
10+
output_dir = tmp_path / "output"
11+
input_dir.mkdir()
12+
output_dir.mkdir()
13+
14+
(input_dir / "test.flattened.schema.json").write_text(
15+
'{"title": "TestSchema", "type": "object"}'
16+
)
17+
args = Namespace(input_dir=str(input_dir), output_dir=str(output_dir))
18+
19+
exit_code = main(args)
20+
21+
assert exit_code == 0
22+
23+
def test_main_returns_one_on_failure(self):
24+
"""Test that main returns 1 on failure."""
25+
input_dir = "/nonexistent/input/dir"
26+
output_dir = "/nonexistent/output/dir"
27+
args = Namespace(input_dir=str(input_dir), output_dir=str(output_dir))
28+
29+
exit_code = main(args)
30+
assert exit_code == 1
31+
32+
def test_main_generates_a_model_for_each_schema_and_an_init_file(self, tmp_path):
33+
"""Test that main generates Pydantic models from JSON schemas."""
34+
input_dir = tmp_path / "input"
35+
output_dir = tmp_path / "output"
36+
input_dir.mkdir()
37+
output_dir.mkdir()
38+
39+
(input_dir / "one.flattened.schema.json").write_text(
40+
'{"title": "One", "type": "object"}'
41+
)
42+
(input_dir / "two.flattened.schema.json").write_text(
43+
'{"title": "Two", "type": "object"}'
44+
)
45+
46+
args = Namespace(input_dir=str(input_dir), output_dir=str(output_dir))
47+
48+
main(args)
49+
50+
generated_files = list(output_dir.glob("*.py"))
51+
assert len(generated_files) == 3
52+
assert (output_dir / "__init__.py").exists()
53+
assert (output_dir / "one.py").exists()
54+
assert (output_dir / "two.py").exists()

0 commit comments

Comments
 (0)