-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
166 additions
and
8 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...ests/integration_tests/components/validation/basic_component_invalid_value/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: .my_component | ||
|
||
params: | ||
a_string: "test" | ||
an_int: {} |
4 changes: 4 additions & 0 deletions
4
...ests/integration_tests/components/validation/basic_component_missing_value/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type: .my_component | ||
|
||
params: | ||
a_string: "test" |
5 changes: 5 additions & 0 deletions
5
...ents_tests/integration_tests/components/validation/basic_component_success/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: .my_component | ||
|
||
params: | ||
a_string: "a string" | ||
an_int: 5 |
13 changes: 13 additions & 0 deletions
13
...ts/integration_tests/components/validation/nested_component_invalid_values/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
type: .my_nested_component | ||
|
||
params: | ||
nested: | ||
foo: | ||
a_string: "test" | ||
an_int: {} | ||
bar: | ||
a_string: "test" | ||
an_int: 5 | ||
baz: | ||
a_string: {} | ||
an_int: 5 |
11 changes: 11 additions & 0 deletions
11
...ts/integration_tests/components/validation/nested_component_missing_values/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: .my_nested_component | ||
|
||
params: | ||
nested: | ||
foo: | ||
a_string: "test" | ||
bar: | ||
a_string: "test" | ||
an_int: 5 | ||
baz: | ||
an_int: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
49 changes: 49 additions & 0 deletions
49
...omponents/dagster_components_tests/integration_tests/validation_tests/basic_components.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Dict | ||
|
||
from dagster._core.definitions.definitions_class import Definitions | ||
from dagster_components import Component, component_type | ||
from dagster_components.core.component import ComponentLoadContext | ||
from pydantic import BaseModel | ||
from typing_extensions import Self | ||
|
||
|
||
class MyComponentSchema(BaseModel): | ||
a_string: str | ||
an_int: int | ||
|
||
|
||
@component_type | ||
class MyComponent(Component): | ||
name = "my_component" | ||
params_schema = MyComponentSchema | ||
|
||
@classmethod | ||
def load(cls, context: ComponentLoadContext) -> Self: | ||
context.load_params(cls.params_schema) | ||
return cls() | ||
|
||
def build_defs(self, context: ComponentLoadContext) -> Definitions: | ||
return Definitions() | ||
|
||
|
||
class MyNestedModel(BaseModel): | ||
a_string: str | ||
an_int: int | ||
|
||
|
||
class MyNestedComponentSchema(BaseModel): | ||
nested: Dict[str, MyNestedModel] | ||
|
||
|
||
@component_type | ||
class MyNestedComponent(Component): | ||
name = "my_nested_component" | ||
params_schema = MyNestedComponentSchema | ||
|
||
@classmethod | ||
def load(cls, context: ComponentLoadContext) -> Self: | ||
context.load_params(cls.params_schema) | ||
return cls() | ||
|
||
def build_defs(self, context: ComponentLoadContext) -> Definitions: | ||
return Definitions() |
85 changes: 78 additions & 7 deletions
85
.../dagster_components_tests/integration_tests/validation_tests/test_component_validation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,81 @@ | ||
from dagster_components import Component, component_type | ||
from dagster_components.core.component import get_component_type_name, is_registered_component_type | ||
import shutil | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import pytest | ||
from dagster._core.definitions.definitions_class import Definitions | ||
from pydantic import ValidationError | ||
|
||
def test_registered_component_with_default_name() -> None: | ||
@component_type | ||
class RegisteredComponent(Component): ... | ||
from dagster_components_tests.integration_tests.component_loader import ( | ||
build_defs_from_component_path, | ||
load_test_component_project_context, | ||
) | ||
|
||
assert is_registered_component_type(RegisteredComponent) | ||
assert get_component_type_name(RegisteredComponent) == "registered_component" | ||
|
||
def load_test_component_defs_inject_component(path: str, component_to_inject: Path) -> Definitions: | ||
origin_path = Path(__file__).parent.parent / "components" / path | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
shutil.copytree(origin_path, tmpdir, dirs_exist_ok=True) | ||
shutil.copy(component_to_inject, Path(tmpdir) / "__init__.py") | ||
|
||
context = load_test_component_project_context() | ||
return build_defs_from_component_path( | ||
path=Path(tmpdir), | ||
registry=context.component_registry, | ||
resources={}, | ||
) | ||
|
||
|
||
def test_basic_component_success() -> None: | ||
load_test_component_defs_inject_component( | ||
"validation/basic_component_success", Path(__file__).parent / "basic_components.py" | ||
) | ||
|
||
|
||
def test_basic_component_invalid_value() -> None: | ||
with pytest.raises(ValidationError) as e: | ||
load_test_component_defs_inject_component( | ||
"validation/basic_component_invalid_value", | ||
Path(__file__).parent / "basic_components.py", | ||
) | ||
|
||
assert "component.yaml:5" in str(e.value) | ||
assert "Input should be a valid integer" in str(e.value) | ||
|
||
|
||
def test_basic_component_missing_value() -> None: | ||
with pytest.raises(ValidationError) as e: | ||
load_test_component_defs_inject_component( | ||
"validation/basic_component_missing_value", | ||
Path(__file__).parent / "basic_components.py", | ||
) | ||
|
||
# Points to the lowest element in the hierarchy which is missing a required field | ||
assert "component.yaml:4" in str(e.value) | ||
assert "Field required" in str(e.value) | ||
|
||
|
||
def test_nested_component_invalid_values() -> None: | ||
with pytest.raises(ValidationError) as e: | ||
load_test_component_defs_inject_component( | ||
"validation/nested_component_invalid_values", | ||
Path(__file__).parent / "basic_components.py", | ||
) | ||
|
||
assert "component.yaml:7" in str(e.value) | ||
assert "Input should be a valid integer" in str(e.value) | ||
assert "component.yaml:12" in str(e.value) | ||
assert "Input should be a valid string" in str(e.value) | ||
|
||
|
||
def test_nested_component_missing_value() -> None: | ||
with pytest.raises(ValidationError) as e: | ||
load_test_component_defs_inject_component( | ||
"validation/nested_component_missing_values", | ||
Path(__file__).parent / "basic_components.py", | ||
) | ||
|
||
assert "component.yaml:6" in str(e.value) | ||
assert "Field required" in str(e.value) | ||
assert "component.yaml:11" in str(e.value) |