class Image(Element): type = StringField() image_url = UrlField() alt_text = StringField() def __init__(self, image_url, alt_text): super().__init__("image", image_url, alt_text)
class PlainTextInput(ActionableElement): placeholder = TextField(plain=True, max_length=150) initial_value = StringField() multiline = BooleanField() min_length = IntegerField(max_value=3000) max_length = IntegerField() def __init__( self, action_id, placeholder=None, initial_value=None, multiline=None, min_length=None, max_length=None, ): super().__init__( "plain_text_input", action_id, placeholder, initial_value, multiline, min_length, max_length, )
class Button(ActionableElement): primary = "primary" danger = "danger" text = TextField(plain=True, max_length=75) url = UrlField(max_length=3000) value = StringField(max_length=2000) style = StringField(options=[primary, danger]) confirm = ObjectField(Confirm) def __init__( self, text, action_id=None, url=None, value=None, style=None, confirm=None ): if not any((action_id, url)): raise ValidationError("You should provide either action_id or url") super().__init__("button", action_id, text, url, value, style, confirm)
class ChannelsSelect(Select): initial_channel = StringField() def __init__( self, placeholder, action_id, initial_channel=None, confirm=None, ): super().__init__( "channels_select", action_id, placeholder, confirm, initial_channel, )
class UsersSelect(Select): initial_user = StringField() def __init__( self, placeholder, action_id, initial_user=None, confirm=None, ): super().__init__( "users_select", action_id, placeholder, confirm, initial_user, )
class ConversationsSelect(Select): initial_conversation = StringField() filter = ObjectField(Filter) def __init__( self, placeholder, action_id, initial_conversation=None, confirm=None, filter=None, ): super().__init__( "conversations_select", action_id, placeholder, confirm, initial_conversation, filter, )
class ActionableElement(Element): action_id = StringField(max_length=255)
class Element(Component): type = StringField()
class FakeComponent(Component): title = StringField()
class ParentFakeComponent(Component): title = StringField() text = TextField()
class FakeComponent(Component): title = StringField() text = StringField() not_field = NotField()
def test_string_field_with_incorrect_option_raises_exception(): with pytest.raises(ValidationError): StringField(options=["foo", "bar"]).validate("bad_option")
def test_string_field_with_exeeding_length_raises_exception(): with pytest.raises(ValidationError): StringField(max_length=5).validate("foobar")
def test_string_field_with_incorrect_type_raises_exception(): with pytest.raises(ValidationError): StringField().validate(5)
def test_string_field_validates_input(): assert StringField().validate("input") == "input"