-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement length validation for text objects
We can use the native Pydantic max_length validation for the plain text and markdown text objects by implementing a __len__ dunder method on the text objects. To do this, create a base class for text objects.
- Loading branch information
1 parent
2d74e9d
commit 7466941
Showing
5 changed files
with
81 additions
and
79 deletions.
There are no files selected for viewing
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
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.
Empty file.
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,40 @@ | ||
"""Tests for Block Kit models.""" | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
from pydantic import BaseModel, Field, ValidationError | ||
|
||
from templatebot.storage.slack import blockkit | ||
|
||
|
||
def test_plain_text_object_length() -> None: | ||
"""Test that the length of a plain text object is correct.""" | ||
|
||
class Model(BaseModel): | ||
text: blockkit.SlackPlainTextObject = Field(..., max_length=5) | ||
|
||
data = Model.model_validate( | ||
{"text": {"type": "plain_text", "text": "Hello"}} | ||
) | ||
assert data.text.text == "Hello" | ||
assert data.text.type == "plain_text" | ||
|
||
with pytest.raises(ValidationError): | ||
Model.model_validate( | ||
{"text": {"type": "plain_text", "text": "Hello!"}} | ||
) | ||
|
||
|
||
def test_mrkdwn_text_object_length() -> None: | ||
"""Test that the length of a mrkdwn text object is correct.""" | ||
|
||
class Model(BaseModel): | ||
text: blockkit.SlackMrkdwnTextObject = Field(..., max_length=5) | ||
|
||
data = Model.model_validate({"text": {"type": "mrkdwn", "text": "Hello"}}) | ||
assert data.text.text == "Hello" | ||
assert data.text.type == "mrkdwn" | ||
|
||
with pytest.raises(ValidationError): | ||
Model.model_validate({"text": {"type": "mrkdwn", "text": "Hello!"}}) |