Skip to content

Commit 4dca74c

Browse files
committed
test: add more tests
1 parent 5e7823c commit 4dca74c

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/djc_ext_pydantic/validation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from django_components import Component
66
from pydantic import TypeAdapter, ValidationError
77

8-
98
ComponentTypes = Tuple[Any, Any, Any, Any, Any, Any]
109

1110

tests/test_validation.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Any, Tuple
2+
from typing import Any, Dict, List, Tuple, Type, Union
33

44
import pytest
55
from django_components import Component, SlotContent, types
@@ -427,3 +427,77 @@ def get_context_data(self, a, b, c, var1, var2, **attrs):
427427
"slot2": lambda ctx, data, ref: "abc",
428428
},
429429
)
430+
431+
@djc_test(
432+
components_settings={"extensions": [PydanticExtension]},
433+
)
434+
def test_handles_component_types(self):
435+
TestArgs = Tuple[Type[Component]]
436+
437+
class TestKwargs(TypedDict):
438+
component: Type[Component]
439+
440+
class TestComponent(Component[TestArgs, TestKwargs, Any, Any, Any, Any]):
441+
def get_context_data(self, a, component, **attrs):
442+
return {
443+
"component": component,
444+
}
445+
446+
template: types.django_html = """
447+
{% load component_tags %}
448+
Component: <strong>{{ component }}</strong>
449+
"""
450+
451+
with pytest.raises(
452+
ValidationError,
453+
match=re.escape("Positional arguments of component 'TestComponent' failed validation"),
454+
):
455+
TestComponent.render(
456+
args=[123], # type: ignore
457+
kwargs={"component": 1}, # type: ignore
458+
)
459+
460+
TestComponent.render(
461+
args=(TestComponent,),
462+
kwargs={"component": TestComponent},
463+
)
464+
465+
def test_handles_typing_module(self):
466+
TodoArgs = Tuple[
467+
Union[str, int],
468+
Dict[str, int],
469+
List[str],
470+
Tuple[int, Union[str, int]],
471+
]
472+
473+
class TodoKwargs(TypedDict):
474+
one: Union[str, int]
475+
two: Dict[str, int]
476+
three: List[str]
477+
four: Tuple[int, Union[str, int]]
478+
479+
class TodoData(TypedDict):
480+
one: Union[str, int]
481+
two: Dict[str, int]
482+
three: List[str]
483+
four: Tuple[int, Union[str, int]]
484+
485+
TodoComp = Component[TodoArgs, TodoKwargs, Any, TodoData, Any, Any]
486+
487+
class TestComponent(TodoComp):
488+
def get_context_data(self, *args, **kwargs):
489+
return {
490+
**kwargs,
491+
}
492+
493+
template = ""
494+
495+
TestComponent.render(
496+
args=("str", {"str": 123}, ["a", "b", "c"], (123, "123")),
497+
kwargs={
498+
"one": "str",
499+
"two": {"str": 123},
500+
"three": ["a", "b", "c"],
501+
"four": (123, "123"),
502+
},
503+
)

0 commit comments

Comments
 (0)