File: test_doc.py

package info (click to toggle)
yattag 1.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 1,234; makefile: 4
file content (145 lines) | stat: -rw-r--r-- 4,906 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import unittest
from yattag import Doc
import xml.etree.ElementTree as ET

class TestDoc(unittest.TestCase):

    def test_select_option(self):
        doc, tag, text = Doc(
            defaults = {'ingredient': ['chocolate', 'coffee']}
        ).tagtext()
        with doc.select(name = 'ingredient', multiple = "multiple"):
            for value, description in (
                ("chocolate", "Dark Chocolate"),
                ("almonds", "Roasted almonds"),
                ("honey", "Acacia honey"),
                ("coffee", "Ethiopian coffee")
            ):
                with doc.option(value = value):
                    text(description)
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(root[3].attrib['selected'], 'selected')

    def test_input_text(self):
        doc, tag, text = Doc(
            defaults = {'color': 'yellow'},
            errors = {'color': 'yellow not available'}
        ).tagtext()
        with tag('body'):
            doc.input('color', ('data-stuff', 'stuff'), type = 'text')
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(
            root[1].attrib['value'],
            'yellow'
        )
        self.assertEqual(
            root[1].attrib['data-stuff'],
            'stuff'
        )
        self.assertTrue(
            'error' in root[1].attrib['class']
        )
        
    def test_input_no_slash(self):
        doc = Doc(stag_end = '>')
        doc.input('passw', type="password")
        self.assertTrue(
            doc.getvalue() in (
                '<input name="passw" type="password">',
                '<input type="password" name="passw">'
            )
        )
        
    def test_textarea(self):
        doc, tag, text = Doc(
            defaults = {
                'contact_message': 'You just won the lottery!'
            },
            errors = {
                'contact_message': 'Your message looks like spam.'
            }
        ).tagtext()
        with tag('p'):
            with doc.textarea(('data-my-data', '12345'), name = 'contact_message'):
                pass
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(
            root[1].attrib['class'], 'error'
        )
        self.assertEqual(
            root[1].attrib['data-my-data'], '12345'
        )
        self.assertEqual(
            root[1].text,
            'You just won the lottery!'
        )
        
    def test_input_radio(self):
        doc, tag, text = Doc(defaults = {'color': 'red'}).tagtext()
        with tag('body'):
            for color in ('blue', 'red', 'pink', 'yellow', 'ugly-yellow'):
                doc.input(('type', 'radio'), id = 'color-input', name = "color", value = color)
                text(color)
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(
            root[2].attrib['name'], 'color'
        )
        self.assertEqual(
            root[3].attrib['type'], 'radio'
        )
        self.assertEqual(
            root[1].attrib['checked'], 'checked'
        )
        self.assertRaises(
            KeyError, lambda: root[0].attrib['checked']
        )

    def test_file_and_tel_input_types(self):
        doc, tag, text = Doc().tagtext()
        with tag('body'):
            doc.input(name='test', type='file')
            doc.input(name='test2', type='tel')
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(
            root[0].attrib['type'],'file'
        )
        self.assertEqual(
            root[1].attrib['type'], 'tel'
        )
        
    def test_file_input_with_default_error(self):
        doc, tag, text = Doc(defaults= {"test":'notastreamhandler'}).tagtext()
        with self.assertRaises(Exception) as context:
             doc.input(name='test', type='file')

        self.assertTrue(
            'Default value for HTML form input of type "file" is not supported' in str(context.exception)
        )

    def test_input_checkbox(self):
        doc, tag, text = Doc(defaults = {'gift-wrap': 'yes'}).tagtext()
        with tag('body'):
            doc.input('gift-wrap', type='checkbox', value = "yes")
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(
            root[0].attrib['checked'], 'checked'
        )
        
        doc, tag, text = Doc(defaults = {'extras': ['fast-shipping', 'gift-wrap']}).tagtext()
        with tag('body'):
            for extra in ('fast-shipping', 'extension-of-warranty', 'gift-wrap'):
                doc.input('extras', type="checkbox", value = extra)
        root = ET.fromstring(doc.getvalue())
        self.assertEqual(
            root[0].attrib['checked'], 'checked'
        )
        self.assertRaises(
            KeyError, lambda: root[1].attrib['checked']
        )
        self.assertEqual(
            root[2].attrib['checked'], 'checked'
        )
       
if __name__ == '__main__':
    unittest.main()