|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import Any, Literal |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from data_designer.config.base import ConfigBase, ProcessorConfig, SingleColumnConfig |
| 11 | +from data_designer.engine.configurable_task import ConfigurableTask |
| 12 | +from data_designer.engine.processing.processors.base import Processor |
| 13 | +from data_designer.engine.resources.seed_reader import SeedReader |
| 14 | +from data_designer.engine.testing.utils import assert_valid_plugin |
| 15 | +from data_designer.plugins.plugin import Plugin, PluginType |
| 16 | + |
| 17 | +MODULE_NAME = __name__ |
| 18 | + |
| 19 | + |
| 20 | +class ValidColumnGeneratorConfig(SingleColumnConfig): |
| 21 | + name: str = "valid-column-generator" |
| 22 | + column_type: Literal["valid-column-generator"] = "valid-column-generator" |
| 23 | + |
| 24 | + @property |
| 25 | + def required_columns(self) -> list[str]: |
| 26 | + return [] |
| 27 | + |
| 28 | + @property |
| 29 | + def side_effect_columns(self) -> list[str]: |
| 30 | + return [] |
| 31 | + |
| 32 | + |
| 33 | +class ValidColumnGenerator(ConfigurableTask[ValidColumnGeneratorConfig]): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +class ValidSeedReaderConfig(ConfigBase): |
| 38 | + seed_type: Literal["valid-seed-reader"] = "valid-seed-reader" |
| 39 | + |
| 40 | + |
| 41 | +class ValidSeedReader(SeedReader): |
| 42 | + def get_dataset_uri(self) -> str: |
| 43 | + return "unused" |
| 44 | + |
| 45 | + def create_duckdb_connection(self) -> Any: |
| 46 | + raise NotImplementedError |
| 47 | + |
| 48 | + |
| 49 | +class ValidProcessorConfig(ProcessorConfig): |
| 50 | + name: str = "valid-processor" |
| 51 | + processor_type: Literal["valid-processor"] = "valid-processor" |
| 52 | + |
| 53 | + |
| 54 | +class ValidProcessor(Processor[ValidProcessorConfig]): |
| 55 | + def process_before_batch(self, data: dict[str, Any]) -> dict[str, Any]: |
| 56 | + return data |
| 57 | + |
| 58 | + |
| 59 | +class NonProcessor: |
| 60 | + pass |
| 61 | + |
| 62 | + |
| 63 | +class TaskButNotProcessor(ConfigurableTask[ValidProcessorConfig]): |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + ("plugin_type", "config_class_name", "implementation_class_name"), |
| 69 | + [ |
| 70 | + (PluginType.COLUMN_GENERATOR, "ValidColumnGeneratorConfig", "ValidColumnGenerator"), |
| 71 | + (PluginType.SEED_READER, "ValidSeedReaderConfig", "ValidSeedReader"), |
| 72 | + (PluginType.PROCESSOR, "ValidProcessorConfig", "ValidProcessor"), |
| 73 | + ], |
| 74 | +) |
| 75 | +def test_assert_valid_plugin_accepts_supported_plugin_types( |
| 76 | + plugin_type: PluginType, |
| 77 | + config_class_name: str, |
| 78 | + implementation_class_name: str, |
| 79 | +) -> None: |
| 80 | + plugin = Plugin( |
| 81 | + config_qualified_name=f"{MODULE_NAME}.{config_class_name}", |
| 82 | + impl_qualified_name=f"{MODULE_NAME}.{implementation_class_name}", |
| 83 | + plugin_type=plugin_type, |
| 84 | + ) |
| 85 | + |
| 86 | + assert_valid_plugin(plugin) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize( |
| 90 | + ("plugin_type", "config_class_name", "expected_message"), |
| 91 | + [ |
| 92 | + ( |
| 93 | + PluginType.COLUMN_GENERATOR, |
| 94 | + "ValidColumnGeneratorConfig", |
| 95 | + "Column generator plugin impl class must be a subclass of ConfigurableTask", |
| 96 | + ), |
| 97 | + ( |
| 98 | + PluginType.SEED_READER, |
| 99 | + "ValidSeedReaderConfig", |
| 100 | + "Seed reader plugin impl class must be a subclass of SeedReader", |
| 101 | + ), |
| 102 | + ( |
| 103 | + PluginType.PROCESSOR, |
| 104 | + "ValidProcessorConfig", |
| 105 | + "Processor plugin impl class must be a subclass of Processor", |
| 106 | + ), |
| 107 | + ], |
| 108 | +) |
| 109 | +def test_assert_valid_plugin_rejects_invalid_impl_for_supported_plugin_types( |
| 110 | + plugin_type: PluginType, |
| 111 | + config_class_name: str, |
| 112 | + expected_message: str, |
| 113 | +) -> None: |
| 114 | + plugin = Plugin( |
| 115 | + config_qualified_name=f"{MODULE_NAME}.{config_class_name}", |
| 116 | + impl_qualified_name=f"{MODULE_NAME}.NonProcessor", |
| 117 | + plugin_type=plugin_type, |
| 118 | + ) |
| 119 | + |
| 120 | + with pytest.raises(AssertionError, match=expected_message): |
| 121 | + assert_valid_plugin(plugin) |
| 122 | + |
| 123 | + |
| 124 | +def test_assert_valid_plugin_rejects_processor_plugin_with_configurable_task_impl() -> None: |
| 125 | + plugin = Plugin( |
| 126 | + config_qualified_name=f"{MODULE_NAME}.ValidProcessorConfig", |
| 127 | + impl_qualified_name=f"{MODULE_NAME}.TaskButNotProcessor", |
| 128 | + plugin_type=PluginType.PROCESSOR, |
| 129 | + ) |
| 130 | + |
| 131 | + with pytest.raises(AssertionError, match="Processor plugin impl class must be a subclass of Processor"): |
| 132 | + assert_valid_plugin(plugin) |
0 commit comments