|
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | from enum import Enum |
| 5 | +from typing import Literal, Union |
5 | 6 |
|
| 7 | +from pydantic import BaseModel |
6 | 8 | import pytest |
7 | 9 |
|
8 | 10 | from data_designer.config.utils.errors import InvalidEnumValueError |
9 | | -from data_designer.config.utils.type_helpers import SAMPLER_PARAMS, get_sampler_params, resolve_string_enum |
| 11 | +from data_designer.config.utils.type_helpers import ( |
| 12 | + SAMPLER_PARAMS, |
| 13 | + create_str_enum_from_discriminated_type_union, |
| 14 | + get_sampler_params, |
| 15 | + resolve_string_enum, |
| 16 | +) |
10 | 17 |
|
11 | 18 |
|
12 | 19 | class StubTestEnum(str, Enum): |
13 | 20 | TEST = "test" |
14 | 21 |
|
15 | 22 |
|
| 23 | +class StubModelA(BaseModel): |
| 24 | + column_type: Literal["type-a", "type-a-alt"] = "type-a" |
| 25 | + name: str |
| 26 | + |
| 27 | + |
| 28 | +class StubModelB(BaseModel): |
| 29 | + column_type: Literal["type-b"] = "type-b" |
| 30 | + value: int |
| 31 | + |
| 32 | + |
| 33 | +class StubModelC(BaseModel): |
| 34 | + column_type: Literal["type-c-with-dashes"] = "type-c-with-dashes" |
| 35 | + data: str |
| 36 | + |
| 37 | + |
| 38 | +class StubModelWithoutDiscriminator(BaseModel): |
| 39 | + name: str |
| 40 | + value: int |
| 41 | + |
| 42 | + |
| 43 | +class NotAModel: |
| 44 | + column_type: str = "not-a-model" |
| 45 | + |
| 46 | + |
| 47 | +def test_create_str_enum_from_type_union_basic() -> None: |
| 48 | + type_union = Union[StubModelA, StubModelB] |
| 49 | + result = create_str_enum_from_discriminated_type_union("TestEnum", type_union, "column_type") |
| 50 | + |
| 51 | + assert issubclass(result, Enum) |
| 52 | + assert issubclass(result, str) |
| 53 | + assert hasattr(result, "TYPE_A") |
| 54 | + assert hasattr(result, "TYPE_A_ALT") |
| 55 | + assert hasattr(result, "TYPE_B") |
| 56 | + assert result.TYPE_A.value == "type-a" |
| 57 | + assert result.TYPE_A_ALT.value == "type-a-alt" |
| 58 | + assert result.TYPE_B.value == "type-b" |
| 59 | + assert len(result) == 3 |
| 60 | + |
| 61 | + |
| 62 | +def test_create_str_enum_from_type_union_with_dashes() -> None: |
| 63 | + type_union = Union[StubModelC, StubModelA] |
| 64 | + result = create_str_enum_from_discriminated_type_union("TestEnum", type_union, "column_type") |
| 65 | + |
| 66 | + assert hasattr(result, "TYPE_C_WITH_DASHES") |
| 67 | + assert result.TYPE_C_WITH_DASHES.value == "type-c-with-dashes" |
| 68 | + |
| 69 | + |
| 70 | +def test_create_str_enum_from_type_union_multiple_models() -> None: |
| 71 | + type_union = Union[StubModelA, StubModelB, StubModelC] |
| 72 | + result = create_str_enum_from_discriminated_type_union("TestEnum", type_union, "column_type") |
| 73 | + |
| 74 | + assert len(result) == 4 |
| 75 | + assert hasattr(result, "TYPE_A") |
| 76 | + assert hasattr(result, "TYPE_A_ALT") |
| 77 | + assert hasattr(result, "TYPE_B") |
| 78 | + assert hasattr(result, "TYPE_C_WITH_DASHES") |
| 79 | + |
| 80 | + |
| 81 | +def test_create_str_enum_from_type_union_duplicate_values() -> None: |
| 82 | + class StubModelD(BaseModel): |
| 83 | + column_type: Literal["type-a"] = "type-a" |
| 84 | + extra: str |
| 85 | + |
| 86 | + type_union = Union[StubModelA, StubModelD] |
| 87 | + result = create_str_enum_from_discriminated_type_union("TestEnum", type_union, "column_type") |
| 88 | + |
| 89 | + assert len(result) == 2 |
| 90 | + assert hasattr(result, "TYPE_A") |
| 91 | + assert hasattr(result, "TYPE_A_ALT") |
| 92 | + |
| 93 | + |
| 94 | +def test_create_str_enum_from_type_union_not_pydantic_model() -> None: |
| 95 | + type_union = Union[StubModelA, NotAModel] |
| 96 | + |
| 97 | + with pytest.raises(ValueError, match="is not a Pydantic model"): |
| 98 | + create_str_enum_from_discriminated_type_union("TestEnum", type_union, "column_type") |
| 99 | + |
| 100 | + |
| 101 | +def test_create_str_enum_from_type_union_missing_discriminator_field() -> None: |
| 102 | + type_union = Union[StubModelA, StubModelWithoutDiscriminator] |
| 103 | + |
| 104 | + with pytest.raises(ValueError, match="column_type is not a field of"): |
| 105 | + create_str_enum_from_discriminated_type_union("TestEnum", type_union, "column_type") |
| 106 | + |
| 107 | + |
| 108 | +def test_create_str_enum_from_type_union_custom_discriminator_name() -> None: |
| 109 | + class StubModelE(BaseModel): |
| 110 | + type_field: Literal["custom-type"] = "custom-type" |
| 111 | + name: str |
| 112 | + |
| 113 | + class StubModelF(BaseModel): |
| 114 | + type_field: Literal["another-type"] = "another-type" |
| 115 | + value: int |
| 116 | + |
| 117 | + type_union = Union[StubModelE, StubModelF] |
| 118 | + result = create_str_enum_from_discriminated_type_union("TestEnum", type_union, "type_field") |
| 119 | + |
| 120 | + assert hasattr(result, "CUSTOM_TYPE") |
| 121 | + assert result.CUSTOM_TYPE.value == "custom-type" |
| 122 | + assert hasattr(result, "ANOTHER_TYPE") |
| 123 | + assert result.ANOTHER_TYPE.value == "another-type" |
| 124 | + |
| 125 | + |
16 | 126 | def test_get_sampler_params(): |
17 | 127 | expected_sampler_keys = { |
18 | 128 | "bernoulli", |
|
0 commit comments