Skip to content

Commit 6c7c55e

Browse files
author
Georgi2704
committed
Add unit tests
1 parent 630eb77 commit 6c7c55e

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

tests/unit_tests/test_markdown.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from pydantic_forms.core import FormPage
2+
from pydantic_forms.validators.components.markdown import MarkdownColor, markdown
3+
from tests.unit_tests.helpers import PYDANTIC_VERSION
4+
5+
6+
def test_markdown_default():
7+
"""Ensure Markdown default is None and is unaffected by other field input."""
8+
MarkdownField = markdown(content="Hello world", color=MarkdownColor.PRIMARY)
9+
10+
class Form(FormPage):
11+
name: str = "default"
12+
markdown_field: MarkdownField
13+
14+
assert Form().model_dump() == {"name": "default", "markdown_field": None}
15+
assert Form(name="other").model_dump() == {"name": "other", "markdown_field": None}
16+
17+
18+
def test_markdown_schema():
19+
"""Ensure the markdown field generates a correct JSON schema with default data."""
20+
MarkdownField = markdown(content="Hello", color=MarkdownColor.PRIMARY)
21+
22+
class Form(FormPage):
23+
markdown_field: MarkdownField
24+
25+
if PYDANTIC_VERSION == "2.8":
26+
markdown_field_ref = {"allOf": [{"$ref": "#/$defs/MarkdownValue"}]}
27+
else:
28+
markdown_field_ref = {"$ref": "#/$defs/MarkdownValue"}
29+
30+
expected = {
31+
"$defs": {"MarkdownValue": {"properties": {}, "title": "MarkdownValue", "type": "object"}},
32+
"additionalProperties": False,
33+
"properties": {
34+
"markdown_field": {
35+
**markdown_field_ref,
36+
"default": {
37+
"content": "Hello",
38+
"color": "primary",
39+
},
40+
"format": "markdown",
41+
"type": "string",
42+
},
43+
},
44+
"title": "unknown",
45+
"type": "object",
46+
}
47+
48+
assert Form.model_json_schema() == expected
49+
50+
51+
def test_markdown_accepts_custom_color():
52+
"""Ensure custom (non-enum) color values are accepted and serialized correctly."""
53+
MarkdownField = markdown(content="Custom color", color="custom-blue")
54+
55+
class Form(FormPage):
56+
markdown_field: MarkdownField
57+
58+
schema = Form.model_json_schema()
59+
60+
default_data = schema["properties"]["markdown_field"]["default"]
61+
assert default_data["color"] == "custom-blue"
62+
assert default_data["content"] == "Custom color"
63+
64+
65+
def test_markdown_defaults_to_primary_color():
66+
"""Ensure the default color is primary when not specified."""
67+
MarkdownField = markdown(content="No color specified")
68+
69+
class Form(FormPage):
70+
markdown_field: MarkdownField
71+
72+
schema = Form.model_json_schema()
73+
assert schema["properties"]["markdown_field"]["default"]["color"] == MarkdownColor.PRIMARY

0 commit comments

Comments
 (0)