Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benpankow committed Jan 9, 2025
1 parent a4a521a commit 54d3e9d
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: .my_component

params:
a_string: "test"
an_int: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: .my_component

params:
a_string: "test"
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_definitions_component_validation_error() -> None:
with pytest.raises(ValidationError) as e:
load_test_component_defs("definitions/validation_error_file")

assert "component.yaml:4" in str(e)
assert "component.yaml:4" in str(e.value)
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()
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)

0 comments on commit 54d3e9d

Please sign in to comment.