File: test_subtypes.py

package info (click to toggle)
python-schema-salad 8.9.20250408123006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,056 kB
  • sloc: python: 19,177; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (153 lines) | stat: -rw-r--r-- 5,481 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""Confirm subtypes."""

import pytest

from schema_salad.avro import schema
from schema_salad.avro.schema import Names, SchemaParseException
from schema_salad.schema import load_schema

from .util import get_data

types = [
    (["int", "float", "double"], "int", True),
    (["int", "float", "double"], ["int"], True),
    (["int", "float", "double"], ["int", "float"], True),
    (["int", "float", "double"], ["int", "float", "File"], False),
    ({"type": "array", "items": ["int", "float", "double"]}, ["int", "float"], False),
    (
        {"type": "array", "items": ["int", "float", "double"]},
        {"type": "array", "items": ["int", "float"]},
        True,
    ),
    ("Any", "int", True),
    ("Any", ["int", "null"], False),
    ("Any", ["int"], True),
    ("Any", None, False),
    ("Any", ["null"], False),
    ("Any", "null", False),
    (
        "Any",
        {"type": "record", "fields": [{"name": "species", "type": "string"}]},
        True,
    ),
    ("Any", {"type": "enum", "symbols": ["homo_sapiens"]}, True),
    (
        {"type": "enum", "symbols": ["homo_sapiens", "mus_musculus"]},
        {"type": "enum", "symbols": ["homo_sapiens"]},
        True,
    ),
    (
        {"type": "enum", "symbols": ["homo_sapiens", "mus_musculus"]},
        {"type": "enum", "symbols": ["homo_sapiens", "drosophila_melanogaster"]},
        False,
    ),
    (
        {"type": "record", "fields": [{"name": "species", "type": "string"}]},
        {"type": "enum", "symbols": ["homo_sapiens"]},
        False,
    ),
    (
        {
            "type": "record",
            "fields": [
                {"name": "species", "type": "string"},
                {"name": "id", "type": "int"},
            ],
        },
        {"type": "record", "fields": [{"name": "species", "type": "string"}]},
        True,
    ),
    (
        {
            "type": "record",
            "fields": [
                {"name": "species", "type": "string"},
                {"name": "id", "type": "int"},
            ],
        },
        {"type": "record", "fields": [{"name": "species", "type": "int"}]},
        False,
    ),
    (
        {"type": "record", "fields": [{"name": "species", "type": "string"}]},
        {
            "type": "record",
            "fields": [
                {"name": "species", "type": "string"},
                {"name": "id", "type": "int"},
            ],
        },
        False,
    ),
]


@pytest.mark.parametrize("old,new,result", types)
def test_subtypes(old: schema.PropType, new: schema.PropType, result: bool) -> None:
    """Test is_subtype() function."""
    assert schema.is_subtype({}, old, new) == result


def test_avro_loading_subtype() -> None:
    """Confirm conversion of SALAD style names to avro when overriding."""
    path = get_data("tests/test_schema/avro_subtype.yml")
    document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(path)
    assert isinstance(avsc_names, Names)
    assert avsc_names.get_name("com.example.derived_schema.ExtendedThing", None)


def test_avro_loading_subtype_bad() -> None:
    """Confirm subtype error when overriding incorrectly."""
    path = get_data("tests/test_schema/avro_subtype_bad.yml")
    target_error = (
        r"Field name .*\/override_me already in use with incompatible type. "
        r"Any vs \['string', 'int'\]\."
    )
    with pytest.raises(SchemaParseException, match=target_error):
        _ = load_schema(path)


def test_subtypes_nested() -> None:
    """Confirm correct subtype handling on a nested type definition."""
    path = get_data("tests/test_schema/avro_subtype_nested.yml")
    document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(path)
    assert isinstance(avsc_names, Names)
    assert avsc_names.get_name("com.example.nested_schema.ExtendedContainer", None)


def test_subtypes_nested_bad() -> None:
    """Confirm subtype error when overriding incorrectly in nested types."""
    path = get_data("tests/test_schema/avro_subtype_nested_bad.yml")
    target_error = (
        r"Field name .*\/override_me already in use with incompatible type. "
        r"Any vs \['string', 'int'\]\."
    )
    with pytest.raises(SchemaParseException, match=target_error):
        _ = load_schema(path)


def test_subtypes_recursive() -> None:
    """Confirm correct subtype handling on a recursive type definition."""
    path = get_data("tests/test_schema/avro_subtype_recursive.yml")
    document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(path)
    assert isinstance(avsc_names, Names)
    assert avsc_names.get_name("com.example.recursive_schema.RecursiveThing", None)


def test_subtypes_union() -> None:
    """Confirm correct subtype handling on an union type definition."""
    path = get_data("tests/test_schema/avro_subtype_union.yml")
    document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(path)
    assert isinstance(avsc_names, Names)
    assert avsc_names.get_name("com.example.union_schema.ExtendedContainer", None)


def test_subtypes_union_bad() -> None:
    """Confirm subtype error when overriding incorrectly in array types."""
    path = get_data("tests/test_schema/avro_subtype_union_bad.yml")
    target_error = (
        r"Field name .*\/override_me already in use with incompatible type. "
        r"Any vs \['string', 'int'\]\."
    )
    with pytest.raises(SchemaParseException, match=target_error):
        _ = load_schema(path)