def test_dropdown_with_just_keys(): dd = pglet.Dropdown( id="list1", label="Your favorite color:", options=[Option(key="key1"), Option(key="key2")], ) assert dd.get_cmd_str() == [ Command( indent=0, name=None, values=["dropdown"], attrs={ "label": "Your favorite color:", "id": ("list1", True) }, lines=[], commands=[], ), Command(indent=2, name=None, values=["option"], attrs={"key": "key1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["option"], attrs={"key": "key2"}, lines=[], commands=[]), ], "Test failed" do = Option("key1") assert isinstance(do, Option)
def test_tabs_add(): t = Tabs(tabs=[Tab(text="Tab1"), Tab("Tab2"), Tab("Tab3")]) assert isinstance(t, pglet.Control) assert isinstance(t, pglet.Tabs) assert t.get_cmd_str() == [ Command(indent=0, name=None, values=["tabs"], attrs={"value": "Tab1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab2"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab3"}, lines=[], commands=[]), ], "Test failed"
def test_nav_with_just_keys(): n = pglet.Nav(id="list1", value="list1", items=[Item(key="key1"), Item(key="key2")]) assert n.get_cmd_str() == [ Command(indent=0, name=None, values=["nav"], attrs={ "value": "list1", "id": ("list1", True) }, lines=[], commands=[]), Command(indent=2, name=None, values=["item"], attrs={"key": "key1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["item"], attrs={"key": "key2"}, lines=[], commands=[]), ], "Test failed" ni = Item("key1") assert isinstance(ni, Item)
def test_add_text_inside_stack(): text = Text(id="txt1", value='Hello, "world!"') button = Button(text="Super button") stack = Stack(id="header", controls=[text, button]) assert stack.get_cmd_str() == [ Command(indent=0, name=None, values=["stack"], attrs={"id": ("header", True)}, lines=[], commands=[]), Command( indent=2, name=None, values=["text"], attrs={ "value": 'Hello, "world!"', "id": ("txt1", True) }, lines=[], commands=[], ), Command(indent=2, name=None, values=["button"], attrs={"text": "Super button"}, lines=[], commands=[]), ], "Test failed"
def test_link_with_controls(): l = Link( value="Visit google", url="https://google.com", pre=True, align="right", width="100", size="large1", title="Link title", controls=[Text(value="LinkText1"), Text(value="LinkText2")], ) assert isinstance(l, pglet.Control) assert isinstance(l, pglet.Link) assert l.get_cmd_str() == [ Command( indent=0, name=None, values=["link"], attrs={ "align": "right", "pre": "true", "size": "large1", "title": "Link title", "url": "https://google.com", "value": "Visit google", "width": "100", }, lines=[], commands=[], ), Command(indent=2, name=None, values=["text"], attrs={"value": "LinkText1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["text"], attrs={"value": "LinkText2"}, lines=[], commands=[]), ], "Test failed"
def test_choicegroup(): cg = pglet.ChoiceGroup( id="list1", value="list1", label="Your favorite color:", options=[ Option(key="key1", text="value1", icon="Shop", icon_color="Green"), Option(key="key2", text="value2"), ], ) assert isinstance(cg, pglet.Control) assert isinstance(cg, pglet.ChoiceGroup) assert cg.get_cmd_str() == [ Command( indent=0, name=None, values=["choicegroup"], attrs={"label": "Your favorite color:", "value": "list1", "id": ("list1", True)}, lines=[], commands=[], ), Command( indent=2, name=None, values=["option"], attrs={"icon": "Shop", "iconcolor": "Green", "key": "key1", "text": "value1"}, lines=[], commands=[], ), Command(indent=2, name=None, values=["option"], attrs={"key": "key2", "text": "value2"}, lines=[], commands=[]), ], "Test failed" cgo = Option("key1") assert isinstance(cgo, Option)
def _get_cmd_attrs(self, update=False): command = Command(0, None, [], {}, [], []) if update and not self.__uid: return command for attrName in sorted(self.__attrs): attrName = attrName.lower() dirty = self.__attrs[attrName][1] if (update and not dirty) or attrName == "id": continue val = self.__attrs[attrName][0] sval = "" if val == None: continue elif isinstance(val, bool): sval = str(val).lower() elif isinstance(val, dt.datetime) or isinstance(val, dt.date): sval = val.isoformat() else: sval = str(val) command.attrs[attrName] = sval self.__attrs[attrName] = (val, False) id = self.__attrs.get("id") if not update and id != None: command.attrs["id"] = id elif update and len(command.attrs) > 0: command.values.append(self.__uid) return command
def test_nav(): n = pglet.Nav( id="list1", value="list1", items=[ Item( key="key1", text="item1", icon="mail", icon_color="green", url="https://google.com", new_window=True, expanded=True, ), Item(key="key2", text="item2"), ], ) assert isinstance(n, pglet.Control) assert isinstance(n, pglet.Nav) assert n.get_cmd_str() == [ Command(indent=0, name=None, values=["nav"], attrs={ "value": "list1", "id": ("list1", True) }, lines=[], commands=[]), Command( indent=2, name=None, values=["item"], attrs={ "expanded": "true", "icon": "mail", "iconcolor": "green", "key": "key1", "newwindow": "true", "text": "item1", "url": "https://google.com", }, lines=[], commands=[], ), Command(indent=2, name=None, values=["item"], attrs={ "key": "key2", "text": "item2" }, lines=[], commands=[]), ], "Test failed" ni = Item("key1") assert isinstance(ni, Item)
def test_panel_add(): p = Panel( open=True, title="Hello", type="small", auto_dismiss=True, light_dismiss=False, width=100, blocking=False, data="data1", controls=[Text(value="Are you sure?")], footer=[Button(text="OK"), Button(text="Cancel")], ) assert isinstance(p, pglet.Control) assert isinstance(p, pglet.Panel) assert p.get_cmd_str() == [ Command( indent=0, name=None, values=["panel"], attrs={ "autodismiss": "true", "blocking": "false", "data": "data1", "lightdismiss": "false", "open": "true", "title": "Hello", "type": "small", "width": "100", }, lines=[], commands=[], ), Command(indent=2, name=None, values=["text"], attrs={"value": "Are you sure?"}, lines=[], commands=[]), Command(indent=2, name=None, values=["footer"], attrs={}, lines=[], commands=[]), Command(indent=4, name=None, values=["button"], attrs={"text": "OK"}, lines=[], commands=[]), Command(indent=4, name=None, values=["button"], attrs={"text": "Cancel"}, lines=[], commands=[]), ], "Test failed"
def _fetch_page_details(self): values = self._conn.send_commands( self._conn.page_name, self._session_id, [ Command(0, "get", ["page", "hash"], None, None, None), Command(0, "get", ["page", "winwidth"], None, None, None), Command(0, "get", ["page", "winheight"], None, None, None), Command(0, "get", ["page", "userauthprovider"], None, None, None), Command(0, "get", ["page", "userid"], None, None, None), Command(0, "get", ["page", "userlogin"], None, None, None), Command(0, "get", ["page", "username"], None, None, None), Command(0, "get", ["page", "useremail"], None, None, None), Command(0, "get", ["page", "userclientip"], None, None, None), ], ).results self._set_attr("hash", values[0], False) self._set_attr("winwidth", values[1], False) self._set_attr("winheight", values[2], False) self._set_attr("userauthprovider", values[3], False) self._set_attr("userid", values[4], False) self._set_attr("userlogin", values[5], False) self._set_attr("username", values[6], False) self._set_attr("useremail", values[7], False) self._set_attr("userclientip", values[8], False)
def test_slider_add(): s = Slider( value=1, label="To what extend you agree", min=0, max=10, step=1, show_value=True, value_format="current_value is {value}", vertical=True, height=200, ) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.Slider) assert s.get_cmd_str() == [ Command( indent=0, name=None, values=["slider"], attrs={ "height": "200", "label": "To what extend you agree", "max": "10", "min": "0", "showvalue": "true", "step": "1", "value": "1", "valueformat": "current_value is {value}", "vertical": "true", }, lines=[], commands=[], ) ], "Test failed"
def test_searchbox_add(): sb = SearchBox( value="", placeholder="search for something", underlined=True, icon="icon1", icon_color="color1", data="data1", ) assert isinstance(sb, pglet.Control) assert isinstance(sb, pglet.SearchBox) assert sb.get_cmd_str() == [ Command( indent=0, name=None, values=["searchbox"], attrs={ "data": "data1", "icon": "icon1", "iconcolor": "color1", "onchange": "false", "placeholder": "search for something", "underlined": "true", "value": "", }, lines=[], commands=[], ) ], "Test failed"
def test_spinbutton_add(): s = SpinButton( value=1, label="To what extent you agree", min=0, max=10, step=1, icon="icon_name", width=200, data="data1", ) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.SpinButton) assert s.get_cmd_str() == [ Command( indent=0, name=None, values=["spinbutton"], attrs={ "data": "data1", "icon": "icon_name", "label": "To what extent you agree", "max": "10", "min": "0", "step": "1", "value": "1", "width": "200", }, lines=[], commands=[], ) ], "Test failed"
def test_toggle_add(): t = Toggle( value=True, label="This is toggle", inline=True, on_text="on text", off_text="off text", ) assert isinstance(t, pglet.Control) assert isinstance(t, pglet.Toggle) assert t.get_cmd_str() == [ Command( indent=0, name=None, values=["toggle"], attrs={ "inline": "true", "label": "This is toggle", "offtext": "off text", "ontext": "on text", "value": "true", }, lines=[], commands=[], ) ], "Test failed"
def test_message(): m = pglet.Message( value="This is message", dismiss=True, buttons=[ MessageButton(text="Yes, I agree", action="Yes"), MessageButton(text="No, I disagree", action="No"), ], ) assert isinstance(m, pglet.Control) assert isinstance(m, pglet.Message) assert m.get_cmd_str() == [ Command( indent=0, name=None, values=["message"], attrs={ "dismiss": "true", "value": "This is message" }, lines=[], commands=[], ), Command( indent=2, name=None, values=["button"], attrs={ "action": "Yes", "text": "Yes, I agree" }, lines=[], commands=[], ), Command( indent=2, name=None, values=["button"], attrs={ "action": "No", "text": "No, I disagree" }, lines=[], commands=[], ), ], "Test failed"
def test_splitstack_add(): s = SplitStack( horizontal=True, gutter_size=10, gutter_color="yellow", gutter_hover_color="orange", gutter_drag_color="blue", controls=[Stack(id="left"), Stack(id="center")], ) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.SplitStack) assert s.get_cmd_str() == [ Command( indent=0, name=None, values=["splitstack"], attrs={ "guttercolor": "yellow", "gutterdragcolor": "blue", "gutterhovercolor": "orange", "guttersize": "10", "horizontal": "true", }, lines=[], commands=[], ), Command( indent=2, name=None, values=["stack"], attrs={"id": ("left", True)}, lines=[], commands=[], ), Command( indent=2, name=None, values=["stack"], attrs={"id": ("center", True)}, lines=[], commands=[], ), ], "Test failed"
def test_piechart_add(): pc = PieChart( legend=True, tooltips=True, inner_value=40, inner_radius=42, width="100%", points=[ Point(value=20, color="yellow", legend="Yellow color", tooltip="20%"), Point(value=30, color="green", legend="Green color", tooltip="30%"), ], ) assert isinstance(pc, pglet.Control) assert isinstance(pc, pglet.PieChart) assert pc.get_cmd_str() == [ Command( indent=0, name=None, values=["piechart"], attrs={"innerradius": "42", "innervalue": "40", "legend": "true", "tooltips": "true", "width": "100%"}, lines=[], commands=[], ), Command(indent=2, name=None, values=["data"], attrs={}, lines=[], commands=[]), Command( indent=4, name=None, values=["p"], attrs={"color": "yellow", "legend": "Yellow color", "tooltip": "20%", "value": "20"}, lines=[], commands=[], ), Command( indent=4, name=None, values=["p"], attrs={"color": "green", "legend": "Green color", "tooltip": "30%", "value": "30"}, lines=[], commands=[], ), ], "Test failed"
def test_callout_add(): c = Callout( target="button1", position="leftBottom", gap=100, beak=True, beak_width=10, page_padding=10, focus=False, cover=True, visible=True, controls=[Text(value="This is callout")], ) assert isinstance(c, pglet.Control) assert isinstance(c, pglet.Callout) assert c.get_cmd_str() == [ Command( indent=0, name=None, values=["callout"], attrs={ "beak": "true", "beakwidth": "10", "cover": "true", "focus": "false", "gap": "100", "pagepadding": "10", "position": "leftBottom", "target": "button1", "visible": "true", }, lines=[], commands=[], ), Command(indent=2, name=None, values=["text"], attrs={"value": "This is callout"}, lines=[], commands=[]), ], "Test failed"
def test_text_double_quotes(): c = Text(value='Hello, "world!"') # raise Exception(c.get_cmd_str()) assert c.get_cmd_str() == [ Command(indent=0, name=None, values=["text"], attrs={"value": 'Hello, "world!"'}, lines=[], commands=[]) ], "Test failed"
def test_tabs_with_controls_add(): t = Tabs(tabs=[ Tab(text="Tab1", controls=[Button( text="OK"), Button(text="Cancel")]), Tab( "Tab2", controls=[Textbox(label="Textbox 1"), Textbox(label="Textbox 2")], ), ]) assert isinstance(t, pglet.Control) assert isinstance(t, pglet.Tabs) assert t.get_cmd_str() == [ Command(indent=0, name=None, values=["tabs"], attrs={"value": "Tab1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab1"}, lines=[], commands=[]), Command(indent=4, name=None, values=["button"], attrs={"text": "OK"}, lines=[], commands=[]), Command(indent=4, name=None, values=["button"], attrs={"text": "Cancel"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab2"}, lines=[], commands=[]), Command(indent=4, name=None, values=["textbox"], attrs={"label": "Textbox 1"}, lines=[], commands=[]), Command(indent=4, name=None, values=["textbox"], attrs={"label": "Textbox 2"}, lines=[], commands=[]), ], "Test failed"
def test_choicegroup_with_just_keys(): cg = pglet.ChoiceGroup( id="list1", label="Your favorite color:", options=[Option(key="key1"), Option(key="key2")], ) assert cg.get_cmd_str() == [ Command( indent=0, name=None, values=["choicegroup"], attrs={"label": "Your favorite color:", "id": ("list1", True)}, lines=[], commands=[], ), Command(indent=2, name=None, values=["option"], attrs={"key": "key1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["option"], attrs={"key": "key2"}, lines=[], commands=[]), ], "Test failed" cgo = Option("key1") assert isinstance(cgo, Option)
def test_link_add(): l = Link(value="search", url="http://google.com", align="left", new_window=True) assert isinstance(l, pglet.Control) assert isinstance(l, pglet.Link) assert l.get_cmd_str() == [ Command( indent=0, name=None, values=["link"], attrs={"align": "left", "newwindow": "true", "url": "http://google.com", "value": "search"}, lines=[], commands=[], ) ], "Test failed"
def test_textbox_add(): tb = pglet.Textbox(id="txt1", label="Your name:") assert isinstance(tb, pglet.Control) assert isinstance(tb, pglet.Textbox) assert [ Command( indent=" ", name=None, values=["textbox"], attrs={"label": "Your name:", "id": ("txt1", True)}, lines=[], commands=[], ) ], "Test failed"
def test_checkbox_add(): c = Checkbox(label="Do you agree?", value=True, visible=True, box_side="start", data="data1") assert isinstance(c, pglet.Control) assert isinstance(c, pglet.Checkbox) assert c.get_cmd_str() == [ Command( indent=0, name=None, values=["checkbox"], attrs={"boxside": "start", "data": "data1", "label": "Do you agree?", "value": "true", "visible": "true"}, lines=[], commands=[], ) ], "Test failed"
def test_text_add(): c = Text( value="Hello,\nworld!", markdown=True, align="left", vertical_align="top", size="tiny", bold=True, italic=False, pre=False, nowrap=True, block=False, color="#9FE2BF", bgcolor="#FF7F50", border_style="dotted", border_width="1", border_color="yellow", border_radius="4px", ) assert isinstance(c, pglet.Control) assert isinstance(c, pglet.Text) # raise Exception(s.get_cmd_str()) # assert c.get_cmd_str() == ('text align="left" block="false" bold='true italic="false" nowrap="true" pre="false" size="tiny" value="Hello,\\nworld!" verticalAlign="left"'), "Test failed" assert c.get_cmd_str() == [ Command( indent=0, name=None, values=["text"], attrs={ "align": "left", "bgcolor": "#FF7F50", "block": "false", "bold": "true", "bordercolor": "yellow", "borderradius": "4px", "borderstyle": "dotted", "borderwidth": "1", "color": "#9FE2BF", "italic": "false", "markdown": "true", "nowrap": "true", "pre": "false", "size": "tiny", "value": "Hello,\nworld!", "verticalalign": "top", }, lines=[], commands=[], ) ], "Test failed"
def test_stack_add(): s = Stack( horizontal=True, vertical_fill=True, horizontal_align="center", vertical_align="baseline", gap="large", wrap=True, scroll_x=True, scroll_y=True, controls=[Textbox(id="firstName"), Textbox(id="lastName")], ) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.Stack) # raise Exception(s.get_cmd_str()) assert s.get_cmd_str() == [ Command( indent=0, name=None, values=["stack"], attrs={ "gap": "large", "horizontal": "true", "horizontalalign": "center", "scrollx": "true", "scrolly": "true", "verticalalign": "baseline", "verticalfill": "true", "wrap": "true", }, lines=[], commands=[], ), Command(indent=2, name=None, values=["textbox"], attrs={"id": ("firstName", True)}, lines=[], commands=[]), Command(indent=2, name=None, values=["textbox"], attrs={"id": ("lastName", True)}, lines=[], commands=[]), ], "Test failed"
def test_iframe_add(): c = IFrame(src="https://google.com", border_style="solid") assert isinstance(c, pglet.Control) assert isinstance(c, pglet.IFrame) assert c.get_cmd_str() == [ Command( indent=0, name=None, values=["iframe"], attrs={ "borderstyle": "solid", "src": "https://google.com" }, lines=[], commands=[], ) ], "Test failed"
def test_icon_add(): c = Icon(name="Mail", color="#FF7F50", size="tiny") assert isinstance(c, pglet.Control) assert isinstance(c, pglet.Icon) # raise Exception(s.get_cmd_str()) assert c.get_cmd_str() == [ Command( indent=0, name=None, values=["icon"], attrs={ "color": "#FF7F50", "name": "Mail", "size": "tiny" }, lines=[], commands=[], ) ], "Test failed"
def test_button_add(): b = Button(id="button1", text="My button", primary=True, data="this is data") assert isinstance(b, pglet.Control) assert isinstance(b, pglet.Button) assert b.get_cmd_str() == [ Command( indent=0, name=None, values=["button"], attrs={ "data": "this is data", "primary": "true", "text": "My button", "id": ("button1", True) }, lines=[], commands=[], ) ], "Test failed"
def test_image_add(): i = Image( src="https://www.w3schools.com/css/img_5terre.jpg", alt="This is image", title="This is title", maximize_frame=False, ) assert isinstance(i, pglet.Control) assert isinstance(i, pglet.Image) assert i.get_cmd_str() == [ Command( indent=0, name=None, values=["image"], attrs={ "alt": "This is image", "maximizeframe": "false", "src": "https://www.w3schools.com/css/img_5terre.jpg", "title": "This is title", }, lines=[], commands=[], ) ], "Test failed"