File: test_dotnet_codegen.py

package info (click to toggle)
python-schema-salad 8.4.20230213094415-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,440 kB
  • sloc: python: 14,250; cs: 1,771; java: 1,243; makefile: 189; xml: 184; sh: 108; javascript: 46
file content (96 lines) | stat: -rw-r--r-- 3,366 bytes parent folder | download
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
import shutil
from pathlib import Path
from typing import Any, Dict, List, Optional, cast

from schema_salad import codegen, ref_resolver
from schema_salad.schema import load_schema

from .util import get_data


def test_cwl_gen(tmp_path: Path) -> None:
    topmed_example_path = get_data("tests/test_real_cwl/topmed/topmed_variant_calling_pipeline.cwl")
    assert topmed_example_path
    target_dir = tmp_path / "target"
    examples_dir = tmp_path / "examples"

    target_dir.mkdir()
    examples_dir.mkdir()
    shutil.copyfile(topmed_example_path, examples_dir / "valid_topmed.cwl")

    dotnet_codegen(cwl_file_uri, target_dir, examples=examples_dir)
    solution_path = target_dir / "Solution.sln"
    assert solution_path.exists()
    tests_dir = target_dir / "Test" / "src"
    assert tests_dir.exists()
    with open(tests_dir / "ExampleTests.cs") as f:
        assert "topmed" in f.read()


def test_meta_schema_gen(tmp_path: Path) -> None:
    target_dir = tmp_path / "target"
    target_dir.mkdir()
    dotnet_codegen(metaschema_file_uri, target_dir)
    solution_path = target_dir / "Solution.sln"
    assert solution_path.exists()
    src_dir = target_dir / "DotnetTest" / "src"
    assert src_dir.exists()
    record_schema_dir = src_dir / "RecordSchema.cs"
    assert record_schema_dir.exists()
    with open(record_schema_dir) as f:
        assert "public class RecordSchema : IRecordSchema, " "ISaveable\n{\n" in f.read()


def test_class_field(tmp_path: Path) -> None:
    schema_path = get_data_uri("tests/class_field_test.yml")
    assert schema_path
    target_dir = tmp_path / "target"

    target_dir.mkdir()
    dotnet_codegen(schema_path, target_dir)

    solution_path = target_dir / "Solution.sln"
    assert solution_path.exists()

    tests_dir = target_dir / "Test"
    assert tests_dir.exists()

    with open(target_dir / "DotnetTest" / "src" / "ClassFieldString.cs") as f:
        assert (
            'public ClassFieldString(string class_ = "ClassFieldString",'
            + " LoadingOptions? loadingOptions = null,"
            + " Dictionary<object, object>? extensionFields = null)\n    {\n"
            in f.read()
        )
    with open(target_dir / "DotnetTest" / "src" / "ClassFieldEnum.cs") as f:
        assert (
            "public ClassFieldEnum(ClassFieldEnum_class? class_ = null,"
            + " LoadingOptions? loadingOptions = null,"
            + " Dictionary<object, object>? extensionFields = null)\n    {\n"
            in f.read()
        )


def get_data_uri(resource_path: str) -> str:
    path = get_data(resource_path)
    assert path
    return ref_resolver.file_uri(path)


cwl_file_uri = get_data_uri("tests/test_schema/CommonWorkflowLanguage.yml")
metaschema_file_uri = get_data_uri("metaschema/metaschema.yml")


def dotnet_codegen(file_uri: str, target: Path, examples: Optional[Path] = None) -> None:
    document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(file_uri)
    schema_raw_doc = metaschema_loader.fetch(file_uri)
    schema_doc, schema_metadata = metaschema_loader.resolve_all(schema_raw_doc, file_uri)
    codegen.codegen(
        "dotnet",
        cast(List[Dict[str, Any]], schema_doc),
        schema_metadata,
        document_loader,
        package="DotnetTest",
        target=str(target),
        examples=str(examples) if examples else None,
    )