Skip to content

Commit 9ad5d2a

Browse files
Generated schemas test that functionally addresses NREL#379
1 parent ca13f6a commit 9ad5d2a

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/geophires_x_schema_generator/main.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55
from geophires_x_schema_generator import GeophiresXSchemaGenerator
66
from geophires_x_schema_generator import HipRaXSchemaGenerator
77

8-
if __name__ == '__main__':
9-
parser = argparse.ArgumentParser()
10-
parser.add_argument('--build-in-src', required=False, choices=[True, False], default=True)
11-
parser.add_argument('--build-path', required=False)
12-
args = parser.parse_args()
13-
build_in_src = args.build_in_src
148

9+
def generate_schemas(build_in_src: bool, build_path: str | Path) -> None:
1510
build_dir = Path(Path(__file__).parent)
16-
if not args.build_in_src:
11+
if not build_in_src:
1712
build_dir = Path(Path(__file__).parent.parent.parent, 'build')
1813

19-
if args.build_path:
20-
build_dir = Path(args.build_path)
14+
if build_path:
15+
build_dir = Path(build_path)
2116

2217
build_dir.mkdir(exist_ok=True)
2318

@@ -44,3 +39,13 @@ def build(json_file_name_prefix: str, generator: GeophiresXSchemaGenerator, rst_
4439

4540
build('geophires-', GeophiresXSchemaGenerator(), 'parameters.rst')
4641
build('hip-ra-x-', HipRaXSchemaGenerator(), 'hip_ra_x_parameters.rst')
42+
43+
44+
if __name__ == '__main__':
45+
parser = argparse.ArgumentParser()
46+
parser.add_argument('--build-in-src', required=False, choices=[True, False], default=True)
47+
parser.add_argument('--build-path', required=False)
48+
args = parser.parse_args()
49+
build_in_src_ = args.build_in_src
50+
build_path_ = args.build_path if args.build_path else None
51+
generate_schemas(build_in_src_, build_path_)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
import tempfile
3+
from pathlib import Path
4+
5+
from base_test_case import BaseTestCase
6+
from geophires_x_schema_generator.main import generate_schemas
7+
8+
9+
class GeneratedSchemasTestCase(BaseTestCase):
10+
11+
def test_generated_schemas_up_to_date(self) -> None:
12+
try:
13+
build_dir: Path = Path(tempfile.gettempdir())
14+
generate_schemas(False, build_dir)
15+
16+
def assert_schema(schema_file_name: str) -> None:
17+
with open(Path(build_dir, schema_file_name), encoding='utf-8') as f:
18+
generated_geophires_request_schema = json.loads(f.read())
19+
20+
src_geophires_request_path = Path(
21+
self._get_test_file_path(f'../../src/geophires_x_schema_generator/{schema_file_name}')
22+
)
23+
with open(src_geophires_request_path, encoding='utf-8') as f:
24+
src_geophires_request_schema = json.loads(f.read())
25+
26+
self.assertDictEqual(generated_geophires_request_schema, src_geophires_request_schema)
27+
28+
assert_schema('geophires-request.json')
29+
assert_schema('geophires-result.json')
30+
assert_schema('hip-ra-x-request.json')
31+
except AssertionError as ae:
32+
raise AssertionError(
33+
'Generated schemas in source are not up-to-date. '
34+
'Run src/geophires_x_schema_generator/main.py to update them.'
35+
) from ae

0 commit comments

Comments
 (0)