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)
Example #2
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)
        ]
Example #3
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",
            },
        )
 def to_dict(self) -> dict:  # skipcq: PYL-W0221
     json = super().to_dict()
     if self.data_source == "external":
         if isinstance(self.value, Option):
             json["selected_options"] = extract_json([self.value], "dialog")
         elif self.value is not None:
             json["selected_options"] = Option.from_single_value(self.value)
     else:
         if isinstance(self.value, Option):
             json["value"] = self.value.value
         elif self.value is not None:
             json["value"] = self.value
     return json
Example #5
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",
         },
     )
Example #6
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",
         },
     )
Example #7
0
 def setUp(self) -> None:
     self.selected_opt = Option.from_single_value("U12345")
Example #8
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_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"),
     )