def test_from_single_value(self):
     option = Option(text=PlainTextObject(text="option_1"),
                     value="option_1")
     self.assertDictEqual(
         option.to_dict("text"),
         option.from_single_value("option_1").to_dict("text"),
     )
示例#2
0
class OptionTests(unittest.TestCase):
    def setUp(self) -> None:
        self.common = Option(label="an option", value="option_1")

    def test_block_style_json(self):
        expected = {
            "text": {"type": "plain_text", "text": "an option", "emoji": True},
            "value": "option_1",
        }
        self.assertDictEqual(expected, self.common.to_dict("block"))
        self.assertDictEqual(expected, self.common.to_dict())

    def test_dialog_style_json(self):
        expected = {"label": "an option", "value": "option_1"}
        self.assertDictEqual(expected, self.common.to_dict("dialog"))

    def test_action_style_json(self):
        expected = {"text": "an option", "value": "option_1"}
        self.assertDictEqual(expected, self.common.to_dict("action"))

    def test_from_single_value(self):
        option = Option(label="option_1", value="option_1")
        self.assertDictEqual(
            option.to_dict("text"),
            option.from_single_value("option_1").to_dict("text"),
        )

    def test_label_length(self):
        with self.assertRaises(SlackObjectFormationError):
            Option(label=STRING_301_CHARS, value="option_1").to_dict("text")

    def test_value_length(self):
        with self.assertRaises(SlackObjectFormationError):
            Option(label="option_1", value=STRING_301_CHARS).to_dict("text")
示例#3
0
    def setUp(self) -> None:
        self.common_options = [
            Option.from_single_value("one"),
            Option.from_single_value("two"),
            Option.from_single_value("three"),
        ]

        self.common = OptionGroup(label="an option", options=self.common_options)
示例#4
0
class SelectElementTests(unittest.TestCase):
    option_one = Option.from_single_value("one")
    option_two = Option.from_single_value("two")
    options = [option_one, option_two, Option.from_single_value("three")]

    def test_json(self):
        self.maxDiff = None
        self.assertDictEqual(
            SelectElement(
                placeholder="selectedValue",
                action_id="dropdown",
                options=self.options,
                initial_option=self.option_two,
            ).to_dict(),
            {
                "placeholder": {
                    "emoji": True,
                    "text": "selectedValue",
                    "type": "plain_text",
                },
                "action_id": "dropdown",
                "options": [o.to_dict("block") for o in self.options],
                "initial_option": self.option_two.to_dict(),
                "type": "static_select",
            },
        )

        self.assertDictEqual(
            SelectElement(
                placeholder="selectedValue",
                action_id="dropdown",
                options=self.options,
                confirm=ConfirmObject(title="title", text="text"),
            ).to_dict(),
            {
                "placeholder": {
                    "emoji": True,
                    "text": "selectedValue",
                    "type": "plain_text",
                },
                "action_id":
                "dropdown",
                "options": [o.to_dict("block") for o in self.options],
                "confirm":
                ConfirmObject(title="title", text="text").to_dict("block"),
                "type":
                "static_select",
            },
        )

    def test_options_length(self):
        with self.assertRaises(SlackObjectFormationError):
            SelectElement(
                placeholder="select",
                action_id="selector",
                options=[self.option_one] * 101,
            ).to_dict()
示例#5
0
    def setUp(self) -> None:
        self.options = [
            Option.from_single_value("one"),
            Option.from_single_value("two"),
            Option.from_single_value("three"),
        ]

        self.option_group = [
            OptionGroup(label="group_1", options=self.options)
        ]
示例#6
0
    def test_json(self):
        option = Option.from_single_value("one")

        self.assertDictEqual(
            ActionExternalSelector(name="select_1",
                                   text="selector_1",
                                   min_query_length=3).to_dict(),
            {
                "name": "select_1",
                "text": "selector_1",
                "min_query_length": 3,
                "type": "select",
                "data_source": "external",
            },
        )

        self.assertDictEqual(
            ActionExternalSelector(name="select_1",
                                   text="selector_1",
                                   selected_option=option).to_dict(),
            {
                "name": "select_1",
                "text": "selector_1",
                "selected_options": [option.to_dict("action")],
                "type": "select",
                "data_source": "external",
            },
        )
示例#7
0
 def test_basic_json_formation(self):
     options = [
         Option.from_single_value("one"),
         Option.from_single_value("two"),
         Option.from_single_value("three"),
     ]
     self.assertDictEqual(
         DialogStaticSelector(name="dialog",
                              label="Dialog",
                              options=options).to_dict(),
         {
             "optional":
             False,
             "label":
             "Dialog",
             "type":
             "select",
             "name":
             "dialog",
             "options": [
                 {
                     "label": "one",
                     "value": "one"
                 },
                 {
                     "label": "two",
                     "value": "two"
                 },
                 {
                     "label": "three",
                     "value": "three"
                 },
             ],
             "data_source":
             "static",
         },
     )
示例#8
0
 def test_basic_json_formation(self):
     o = Option.from_single_value("one")
     self.assertDictEqual(
         DialogExternalSelector(
             name="dialog",
             label="Dialog",
             value=o,
             min_query_length=3,
             optional=True,
             placeholder="something",
         ).to_dict(),
         {
             "optional": True,
             "label": "Dialog",
             "type": "select",
             "name": "dialog",
             "min_query_length": 3,
             "placeholder": "something",
             "selected_options": [o.to_dict("dialog")],
             "data_source": "external",
         },
     )
 def test_valid_construction(self):
     modal_view = View(
         type="modal",
         callback_id="modal-id",
         title=PlainTextObject(text="Awesome Modal"),
         submit=PlainTextObject(text="Submit"),
         close=PlainTextObject(text="Cancel"),
         blocks=[
             InputBlock(
                 block_id="b-id",
                 label=PlainTextObject(text="Input label"),
                 element=PlainTextInputElement(action_id="a-id"),
             ),
             InputBlock(
                 block_id="cb-id",
                 label=PlainTextObject(text="Label"),
                 element=CheckboxesElement(
                     action_id="a-cb-id",
                     options=[
                         Option(
                             text=PlainTextObject(
                                 text="*this is plain_text text*"),
                             value="v1",
                         ),
                         Option(
                             text=MarkdownTextObject(
                                 text="*this is mrkdwn text*"),
                             value="v2",
                         ),
                     ],
                 ),
             ),
             SectionBlock(
                 block_id="sb-id",
                 text=MarkdownTextObject(
                     text="This is a mrkdwn text section block."),
                 fields=[
                     PlainTextObject(text="*this is plain_text text*",
                                     emoji=True),
                     MarkdownTextObject(text="*this is mrkdwn text*"),
                     PlainTextObject(text="*this is plain_text text*",
                                     emoji=True),
                 ],
             ),
             DividerBlock(),
             SectionBlock(
                 block_id="rb-id",
                 text=MarkdownTextObject(
                     text=
                     "This is a section block with radio button accessory"),
                 accessory=RadioButtonsElement(
                     initial_option=Option(
                         text=PlainTextObject(text="Option 1"),
                         value="option 1",
                         description=PlainTextObject(
                             text="Description for option 1"),
                     ),
                     options=[
                         Option(
                             text=PlainTextObject(text="Option 1"),
                             value="option 1",
                             description=PlainTextObject(
                                 text="Description for option 1"),
                         ),
                         Option(
                             text=PlainTextObject(text="Option 2"),
                             value="option 2",
                             description=PlainTextObject(
                                 text="Description for option 2"),
                         ),
                     ],
                 ),
             ),
         ],
     )
     modal_view.validate_json()
示例#10
0
 def test_all_state_values(self):
     # Testing with
     # {"type":"modal","title":{"type":"plain_text","text":"My App","emoji":true},"submit":{"type":"plain_text","text":"Submit","emoji":true},"close":{"type":"plain_text","text":"Cancel","emoji":true},"blocks":[{"type":"input","element":{"type":"plain_text_input"},"label":{"type":"plain_text","text":"Label","emoji":true}},{"type":"input","element":{"type":"plain_text_input","multiline":true},"label":{"type":"plain_text","text":"Label","emoji":true}},{"type":"input","element":{"type":"datepicker","initial_date":"1990-04-28","placeholder":{"type":"plain_text","text":"Select a date","emoji":true}},"label":{"type":"plain_text","text":"Label","emoji":true}},{"type":"input","element":{"type":"users_select","placeholder":{"type":"plain_text","text":"Select a user","emoji":true}},"label":{"type":"plain_text","text":"Label","emoji":true}},{"type":"input","element":{"type":"multi_static_select","placeholder":{"type":"plain_text","text":"Select options","emoji":true},"options":[{"text":{"type":"plain_text","text":"*this is plain_text text*","emoji":true},"value":"value-0"},{"text":{"type":"plain_text","text":"*this is plain_text text*","emoji":true},"value":"value-1"},{"text":{"type":"plain_text","text":"*this is plain_text text*","emoji":true},"value":"value-2"}]},"label":{"type":"plain_text","text":"Label","emoji":true}},{"type":"input","element":{"type":"checkboxes","options":[{"text":{"type":"plain_text","text":"*this is plain_text text*","emoji":true},"value":"value-0"},{"text":{"type":"plain_text","text":"*this is plain_text text*","emoji":true},"value":"value-1"},{"text":{"type":"plain_text","text":"*this is plain_text text*","emoji":true},"value":"value-2"}]},"label":{"type":"plain_text","text":"Label","emoji":true}},{"type":"input","element":{"type":"radio_buttons","initial_option":{"text":{"type":"plain_text","text":"Option 1"},"value":"option 1","description":{"type":"plain_text","text":"Description for option 1"}},"options":[{"text":{"type":"plain_text","text":"Option 1"},"value":"option 1","description":{"type":"plain_text","text":"Description for option 1"}},{"text":{"type":"plain_text","text":"Option 2"},"value":"option 2","description":{"type":"plain_text","text":"Description for option 2"}},{"text":{"type":"plain_text","text":"Option 3"},"value":"option 3","description":{"type":"plain_text","text":"Description for option 3"}}]},"label":{"type":"plain_text","text":"Label","emoji":true}}]}
     expected = {
         "values": {
             "b1": {
                 "a1": {
                     "type": "datepicker",
                     "selected_date": "1990-04-12"
                 }
             },
             "b2": {
                 "a2": {
                     "type": "plain_text_input",
                     "value": "This is a test"
                 }
             },
             # multiline
             "b3": {
                 "a3": {
                     "type": "plain_text_input",
                     "value": "Something wrong\nPlease help me!",
                 }
             },
             "b4": {
                 "a4": {
                     "type": "users_select",
                     "selected_user": "******"
                 }
             },
             "b4-2": {
                 "a4-2": {
                     "type": "multi_users_select",
                     "selected_users": ["U123", "U234"],
                 }
             },
             "b5": {
                 "a5": {
                     "type": "conversations_select",
                     "selected_conversation": "C123",
                 }
             },
             "b5-2": {
                 "a5-2": {
                     "type": "multi_conversations_select",
                     "selected_conversations": ["C123", "C234"],
                 }
             },
             "b6": {
                 "a6": {
                     "type": "channels_select",
                     "selected_channel": "C123"
                 }
             },
             "b6-2": {
                 "a6-2": {
                     "type": "multi_channels_select",
                     "selected_channels": ["C123", "C234"],
                 }
             },
             "b7": {
                 "a7": {
                     "type":
                     "multi_static_select",
                     "selected_options": [
                         {
                             "text": {
                                 "type": "plain_text",
                                 "text": "*this is plain_text text*",
                                 "emoji": True,
                             },
                             "value": "value-0",
                         },
                         {
                             "text": {
                                 "type": "plain_text",
                                 "text": "*this is plain_text text*",
                                 "emoji": True,
                             },
                             "value": "value-1",
                         },
                     ],
                 }
             },
             "b8": {
                 "a8": {
                     "type":
                     "checkboxes",
                     "selected_options": [
                         {
                             "text": {
                                 "type": "plain_text",
                                 "text": "*this is plain_text text*",
                                 "emoji": True,
                             },
                             "value": "value-0",
                         },
                         {
                             "text": {
                                 "type": "plain_text",
                                 "text": "*this is plain_text text*",
                                 "emoji": True,
                             },
                             "value": "value-1",
                         },
                     ],
                 }
             },
             "b9": {
                 "a9": {
                     "type": "radio_buttons",
                     "selected_option": {
                         "text": {
                             "type": "plain_text",
                             "text": "Option 1",
                             "emoji": True,
                         },
                         "value": "option 1",
                         "description": {
                             "type": "plain_text",
                             "text": "Description for option 1",
                             "emoji": True,
                         },
                     },
                 }
             },
         }
     }
     state = ViewState(
         values={
             "b1": {
                 "a1":
                 ViewStateValue(type="datepicker",
                                selected_date="1990-04-12")
             },
             "b2": {
                 "a2":
                 ViewStateValue(type="plain_text_input",
                                value="This is a test")
             },
             "b3": {
                 "a3":
                 ViewStateValue(
                     type="plain_text_input",
                     value="Something wrong\nPlease help me!",
                 )
             },
             "b4": {
                 "a4": ViewStateValue(type="users_select",
                                      selected_user="******")
             },
             "b4-2": {
                 "a4-2":
                 ViewStateValue(type="multi_users_select",
                                selected_users=["U123", "U234"])
             },
             "b5": {
                 "a5":
                 ViewStateValue(type="conversations_select",
                                selected_conversation="C123")
             },
             "b5-2": {
                 "a5-2":
                 ViewStateValue(
                     type="multi_conversations_select",
                     selected_conversations=["C123", "C234"],
                 )
             },
             "b6": {
                 "a6":
                 ViewStateValue(type="channels_select",
                                selected_channel="C123")
             },
             "b6-2": {
                 "a6-2":
                 ViewStateValue(type="multi_channels_select",
                                selected_channels=["C123", "C234"])
             },
             "b7": {
                 "a7":
                 ViewStateValue(
                     type="multi_static_select",
                     selected_options=[
                         Option(
                             text=PlainTextObject(
                                 text="*this is plain_text text*",
                                 emoji=True),
                             value="value-0",
                         ),
                         Option(
                             text=PlainTextObject(
                                 text="*this is plain_text text*",
                                 emoji=True),
                             value="value-1",
                         ),
                     ],
                 )
             },
             "b8": {
                 "a8":
                 ViewStateValue(
                     type="checkboxes",
                     selected_options=[
                         Option(
                             text=PlainTextObject(
                                 text="*this is plain_text text*",
                                 emoji=True),
                             value="value-0",
                         ),
                         Option(
                             text=PlainTextObject(
                                 text="*this is plain_text text*",
                                 emoji=True),
                             value="value-1",
                         ),
                     ],
                 )
             },
             "b9": {
                 "a9":
                 ViewStateValue(
                     type="radio_buttons",
                     selected_option=Option(
                         text=PlainTextObject(text="Option 1", emoji=True),
                         value="option 1",
                         description=PlainTextObject(
                             text="Description for option 1", emoji=True),
                     ),
                 )
             },
         })
     self.assertDictEqual(expected, ViewState(**expected).to_dict())
     self.assertDictEqual(expected, state.to_dict())
示例#11
0
class StaticSelectElementTests(unittest.TestCase):
    maxDiff = None

    def test_document_options(self):
        input = {
            "action_id":
            "text1234",
            "type":
            "static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Select an item"
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*"
                    },
                    "value": "value-0",
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*"
                    },
                    "value": "value-1",
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*"
                    },
                    "value": "value-2",
                },
            ],
        }
        self.assertDictEqual(input, StaticSelectElement(**input).to_dict())

    def test_document_option_groups(self):
        input = {
            "action_id":
            "text1234",
            "type":
            "static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Select an item"
            },
            "option_groups": [
                {
                    "label": {
                        "type": "plain_text",
                        "text": "Group 1"
                    },
                    "options": [
                        {
                            "text": {
                                "type": "plain_text",
                                "text": "*this is plain_text text*",
                            },
                            "value": "value-0",
                        },
                        {
                            "text": {
                                "type": "plain_text",
                                "text": "*this is plain_text text*",
                            },
                            "value": "value-1",
                        },
                        {
                            "text": {
                                "type": "plain_text",
                                "text": "*this is plain_text text*",
                            },
                            "value": "value-2",
                        },
                    ],
                },
                {
                    "label": {
                        "type": "plain_text",
                        "text": "Group 2"
                    },
                    "options": [{
                        "text": {
                            "type": "plain_text",
                            "text": "*this is plain_text text*",
                        },
                        "value": "value-3",
                    }],
                },
            ],
        }
        self.assertDictEqual(input, StaticSelectElement(**input).to_dict())

    option_one = Option.from_single_value("one")
    option_two = Option.from_single_value("two")
    options = [option_one, option_two, Option.from_single_value("three")]

    def test_json(self):
        dict_options = []
        for o in self.options:
            dict_options.append(o.to_dict())

        self.assertDictEqual(
            {
                "placeholder": {
                    "emoji": True,
                    "text": "selectedValue",
                    "type": "plain_text",
                },
                "action_id": "dropdown",
                "options": dict_options,
                "initial_option": self.option_two.to_dict(),
                "type": "static_select",
            },
            StaticSelectElement(
                placeholder="selectedValue",
                action_id="dropdown",
                options=self.options,
                initial_option=self.option_two,
            ).to_dict(),
        )

        self.assertDictEqual(
            {
                "placeholder": {
                    "emoji": True,
                    "text": "selectedValue",
                    "type": "plain_text",
                },
                "action_id":
                "dropdown",
                "options":
                dict_options,
                "confirm":
                ConfirmObject(title="title", text="text").to_dict("block"),
                "type":
                "static_select",
            },
            StaticSelectElement(
                placeholder="selectedValue",
                action_id="dropdown",
                options=self.options,
                confirm=ConfirmObject(title="title", text="text"),
            ).to_dict(),
        )

    def test_options_length(self):
        with self.assertRaises(SlackObjectFormationError):
            StaticSelectElement(
                placeholder="select",
                action_id="selector",
                options=[self.option_one] * 101,
            ).to_dict()
 def test_value_length(self):
     with self.assertRaises(SlackObjectFormationError):
         Option(text=PlainTextObject(text="option_1"),
                value=STRING_301_CHARS).to_dict("text")
示例#13
0
            "country_name": "SOUTH AFRICA"
        }
    },
    {
        "country": {
            "country_id": "ZM",
            "country_name": "ZAMBIA"
        }
    },
    {
        "country": {
            "country_id": "ZW",
            "country_name": "ZIMBABWE"
        }
    },
    {
        "country": {
            "country_id": "IN",
            "country_name": "INDIA"
        }
    },
]

if __name__ == "__main__":
    print(len(COUNTRIES))

COUNTRY_OPTIONS = [
    Option(label=x["country"]["country_name"],
           value=x["country"]["country_id"]).to_dict() for x in COUNTRIES
][:100]
 def setUp(self) -> None:
     self.common = Option(text=PlainTextObject(text="an option"),
                          value="option_1")
示例#15
0
 def setUp(self) -> None:
     self.selected_opt = Option.from_single_value("U12345")
示例#16
0
 def setUp(self) -> None:
     self.common = Option(label="an option", value="option_1")
示例#17
0
 def test_label_length(self):
     with self.assertRaises(SlackObjectFormationError):
         Option(label=STRING_301_CHARS, value="option_1").to_dict("text")
示例#18
0
 def test_from_single_value(self):
     option = Option(label="option_1", value="option_1")
     self.assertDictEqual(
         option.to_dict("text"),
         option.from_single_value("option_1").to_dict("text"),
     )