Source

tw2.bootstrap / tests / test_widget.py

  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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
from tw2.core.testbase import (
    WidgetTest as _WidgetTest,
)
import tw2.core as twc
import tw2.forms as twf
import tw2.bootstrap as twb

import datetime


def test_every_widget_exposed():
    """ Is widget exposed by tw2.forms also in tw2.bootstrap? """

    is_widget = lambda obj: isinstance(obj, twc.widgets.WidgetMeta)

    twf_widgets = filter(is_widget, [getattr(twf, attr) for attr in dir(twf)])
    twb_widgets = filter(is_widget, [getattr(twb, attr) for attr in dir(twb)])
    twf_widget_names = [w.__name__ for w in twf_widgets]
    twb_widget_names = [w.__name__ for w in twb_widgets]

    def _is_widget_exposed(name):
        msg = "%r in tw2.forms but not tw2.bootstrap" % name
        assert name in twb_widget_names, msg

    for twf_widget in twf_widgets:
        yield _is_widget_exposed, twf_widget.__name__


def test_every_widget_covered():
    """ Is there a test for tw2.bootstrap widget's output? """

    # Check that there is a test for every widget except the following.
    # They are excluded for various reasons, mostly because they cannot be
    # rendered on their own.
    excluded = [
        twb.bootstrap_css,
        twb.bootstrap_responsive_css,
        twb.bootstrap_js,

        twb.Bootstrap,
        twb.InputField,
        twb.LinkField,
    ]

    is_widget = lambda obj: isinstance(obj, twc.widgets.WidgetMeta)
    is_test = lambda obj: isinstance(obj, type) and issubclass(obj, WidgetTest)

    twb_widgets = filter(is_widget, [getattr(twb, attr) for attr in dir(twb)])
    twb_widgets = filter(lambda w: w not in excluded, twb_widgets)
    twb_tests = filter(is_test, globals().values())
    tested_widgets = [test.widget for test in twb_tests]

    def _is_widget_covered(widget):
        msg = "tw2.bootstrap.%s is not tested explicitly." % widget.__name__
        assert widget in tested_widgets, msg

    for widget in twb_widgets:
        yield _is_widget_covered, widget


class WidgetTest(_WidgetTest):
    """ Only test against mako (we don't provide other templates). """
    engines = ['mako']

    # Universal initilization args. go here
    attrs = {'id': 'bootstrap-test'}
    params = {}


class TestTextField(WidgetTest):
    widget = twb.TextField
    expected = """
    <input name="bootstrap-test"
           id="bootstrap-test"
           class="input-medium"
           type="text">
    </input>
    """


class TestTextArea(WidgetTest):
    widget = twb.TextArea
    expected = """
    <textarea name="bootstrap-test"
              id="bootstrap-test"
              class="input-xlarge">
    </textarea>
    """


class TestCheckBox(WidgetTest):
    widget = twb.CheckBox
    expected = """
    <label class="checkbox">
        <input name="bootstrap-test" type="checkbox"
               id="bootstrap-test"/>
    </label>
    """


class TestRadioButton(WidgetTest):
    widget = twb.RadioButton
    expected = """
    <label class="radio">
        <input name="bootstrap-test" type="radio"
               id="bootstrap-test"/>
    </label>
    """


class TestPasswordField(WidgetTest):
    widget = twb.PasswordField
    expected = """
    <input name="bootstrap-test" type="password"
           class="input-medium" id="bootstrap-test"/>
    """


class TestFileField(WidgetTest):
    widget = twb.FileField
    expected = """
    <input name="bootstrap-test"
           type="file"
           class="input-file"
           id="bootstrap-test"/>
   """


class TestHiddenField(WidgetTest):
    widget = twb.HiddenField
    expected = """
    <input type="hidden" name="bootstrap-test" id="bootstrap-test"/>
    """


class TestIgnoredField(WidgetTest):
    widget = twb.IgnoredField
    expected = """
    <input type="hidden" name="bootstrap-test" id="bootstrap-test"/>
    """


class TestLabelField(WidgetTest):
    widget = twb.LabelField
    attrs = dict(id="bootstrap-test", value="foo")
    expected = """
    <span class="input-medium uneditable-input">foo<input
       type="hidden" id="bootstrap-test"
       value="foo" name="bootstrap-test"/>
    </span>
    """


class TestButton(WidgetTest):
    widget = twb.Button
    expected = """
    <input  class="btn"
            type="button"
            id="bootstrap-test"
            name="bootstrap-test" />
    """


class TestSubmitButton(WidgetTest):
    widget = twb.SubmitButton
    expected = """
    <input  class="btn btn-primary"
            type="submit"
            id="bootstrap-test" />
    """


class TestResetButton(WidgetTest):
    widget = twb.ResetButton
    expected = """
    <input  class="btn"
            type="reset"
            id="bootstrap-test"
            name="bootstrap-test" />
    """


class TestHorizontalLayout(WidgetTest):
    widget = twb.HorizontalLayout
    attrs = {
        'id': 'bootstrap-test',
        'children': [
            twb.ResetButton(id='foo'),
        ],
    }

    expected = """
    <div class="control-group ">
      <label class="control-label" for="bootstrap-test:foo">Foo</label>
      <div class="controls">
        <input id="bootstrap-test:foo"
               name="bootstrap-test:foo"
               type="reset"
               class="btn"/>
      </div>
    </div>
    """


class TestHorizontalForm(WidgetTest):
    widget = twb.HorizontalForm
    attrs = {
        'id': 'bootstrap-test',
        'children': [
            twb.ResetButton(id='foo'),
        ],
    }

    expected = """
    <form id="bootstrap-test:form"
          enctype="multipart/form-data"
          method="post"
          class="form-horizontal">
         <span class="error"></span>

      <fieldset>

        <div class="control-group ">
          <label class="control-label" for="bootstrap-test:foo">Foo</label>
          <div class="controls">
            <input type="reset" class="btn"
                   id="bootstrap-test:foo"
                   name="bootstrap-test:foo"/>
          </div>
        </div>

        <div class="form-actions">
          <input type="submit" class="btn btn-primary"
                 value="Save" id="submit"/>
        </div>
      </fieldset>
    </form>
    """


class TestCalendarDatePicker(WidgetTest):
    widget = twb.CalendarDatePicker
    expected = """
    <input name="bootstrap-test" type="text" id="bootstrap-test"
           class="input-medium" value="{today}" data-date="{today}"
           data-date-format="mm/dd/yyyy" />
    """.format(today=datetime.datetime.now().strftime("%m/%d/%Y"))


class TestCalendarTimePicker(WidgetTest):
    widget = twb.CalendarTimePicker
    expected = """
    <input name="bootstrap-test" type="text" id="bootstrap-test"
           class="input-medium"/>
    """


class TestCalendarDateTimePicker(WidgetTest):
    widget = twb.CalendarDateTimePicker
    expected = """
    <div id="bootstrap-test">
       <input name="bootstrap-test:date" type="text" id="bootstrap-test:date"
              class="input-medium" value="{today}" data-date="{today}"
              data-date-format="mm/dd/yyyy" />
       <input name="bootstrap-test:time" type="text"
              id="bootstrap-test:time" class="input-medium"/>
    </div>
    """.format(today=datetime.datetime.now().strftime("%m/%d/%Y"))


class TestCheckBoxList(WidgetTest):
    widget = twb.CheckBoxList
    expected = """<TODO>How should this actually work?</TODO>"""


class TestCheckBoxTable(WidgetTest):
    widget = twb.CheckBoxTable
    expected = """<TODO>How should this actually work?</TODO>"""


class TestDataGrid(WidgetTest):
    widget = twb.DataGrid
    expected = """<TODO>How should this actually work?</TODO>"""


class TestFieldSet(WidgetTest):
    widget = twb.FieldSet
    expected = """<TODO>How should this actually work?</TODO>"""


class TestForm(WidgetTest):
    widget = twb.Form
    expected = """<TODO>How should this actually work?</TODO>"""


class TestFormPage(WidgetTest):
    widget = twb.FormPage
    expected = """<TODO>How should this actually work?</TODO>"""


class TestGridLayout(WidgetTest):
    widget = twb.GridLayout
    expected = """<TODO>How should this actually work?</TODO>"""


class TestImageButton(WidgetTest):
    widget = twb.ImageButton
    expected = """<TODO>How should this actually work?</TODO>"""


class TestLabel(WidgetTest):
    widget = twb.Label
    expected = """<TODO>How should this actually work?</TODO>"""


class TestListFieldSet(WidgetTest):
    widget = twb.ListFieldSet
    expected = """<TODO>How should this actually work?</TODO>"""


class TestListForm(WidgetTest):
    widget = twb.ListForm
    expected = """<TODO>How should this actually work?</TODO>"""


class TestListLayout(WidgetTest):
    widget = twb.ListLayout
    expected = """<TODO>How should this actually work?</TODO>"""


class TestMultipleSelectField(WidgetTest):
    widget = twb.MultipleSelectField
    expected = """<TODO>How should this actually work?</TODO>"""


class TestMultipleSelectionField(WidgetTest):
    widget = twb.MultipleSelectionField
    expected = """<TODO>How should this actually work?</TODO>"""


class TestPostlabeledCheckBox(WidgetTest):
    widget = twb.PostlabeledCheckBox
    expected = """<TODO>How should this actually work?</TODO>"""


class TestPostlabeledPartialRadioButton(WidgetTest):
    widget = twb.PostlabeledPartialRadioButton
    expected = """<TODO>How should this actually work?</TODO>"""


class TestRadioButtonList(WidgetTest):
    widget = twb.RadioButtonList
    expected = """<TODO>How should this actually work?</TODO>"""


class TestRadioButtonTable(WidgetTest):
    widget = twb.RadioButtonTable
    expected = """<TODO>How should this actually work?</TODO>"""


class TestRowLayout(WidgetTest):
    widget = twb.RowLayout
    expected = """<TODO>How should this actually work?</TODO>"""


class TestSelectionField(WidgetTest):
    widget = twb.SelectionField
    expected = """<TODO>How should this actually work?</TODO>"""


class TestSeparatedCheckBoxTable(WidgetTest):
    widget = twb.SeparatedCheckBoxTable
    expected = """<TODO>How should this actually work?</TODO>"""


class TestSeparatedRadioButtonTable(WidgetTest):
    widget = twb.SeparatedRadioButtonTable
    expected = """<TODO>How should this actually work?</TODO>"""


class TestSingleSelectField(WidgetTest):
    widget = twb.SingleSelectField
    expected = """<TODO>How should this actually work?</TODO>"""


class TestSpacer(WidgetTest):
    widget = twb.Spacer
    expected = """<hr name="bootstrap-test" id="bootstrap-test"></hr>"""


class TestTableForm(WidgetTest):
    widget = twb.TableForm
    attrs = {'field1': twb.TextField(id='field1'),
             'field2': twb.TextField(id='field2'),
             'field3': twb.TextField(id='field3'),
             }
    expected = """<form method="post" enctype="multipart/form-data">
     <span class="error"></span>
    <table>
    <tr class="odd" id="field1:container">
        <th>Field1</th>
        <td>
            <input name="field1" id="field1" type="text" class="input-medium">
            <span id="field1:error"></span>
        </td>
    </tr><tr class="even" id="field2:container">
        <th>Field2</th>
        <td>
            <input name="field2" id="field2" type="text" class="input-medium">
            <span id="field2:error"></span>
        </td>
    </tr><tr class="odd" id="field3:container">
        <th>Field3</th>
        <td>
            <input name="field3" id="field3" type="text" class="input-medium">
            <span id="field3:error"></span>
        </td>
    </tr>
    <tr class="error"><td colspan="2">
        <span id=":error"></span>
    </td></tr>
</table>
    <input type="submit" id="submit" value="Save">
</form>"""


class TestTableFieldset(WidgetTest):
    widget = twb.TableFieldSet
    attrs = {'field1': twb.TextField(id='field1'),
             'field2': twb.TextField(id='field2'),
             'field3': twb.TextField(id='field3'),
             }
    expected = """<fieldset>
    <legend></legend>
    <table>
    <tr class="odd" id="field1:container">
        <th>Field1</th>
        <td>
            <input name="field1" id="field1" type="text" class="input-medium">
            <span id="field1:error"></span>
        </td>
    </tr><tr class="even" id="field2:container">
        <th>Field2</th>
        <td>
            <input name="field2" id="field2" type="text" class="input-medium">
            <span id="field2:error"></span>
        </td>
    </tr><tr class="odd" id="field3:container">
        <th>Field3</th>
        <td>
            <input name="field3" id="field3" type="text" class="input-medium">
            <span id="field3:error"></span>
        </td>
    </tr>
    <tr class="error"><td colspan="2">
        <span id=":error"></span>
    </td></tr>
</table>
</fieldset>"""


class TestTableLayout(WidgetTest):
    widget = twb.TableLayout
    attrs = {'children': [twb.TextField(id='field1'),
                          twb.TextField(id='field2'),
                          twb.TextField(id='field3')]}
    expected = """<table>
    <tr class="odd" id="field1:container">
        <th>Field1</th>
        <td>
            <input name="field1" id="field1" type="text" class="input-medium">
            <span id="field1:error"></span>
        </td>
    </tr><tr class="even" id="field2:container">
        <th>Field2</th>
        <td>
            <input name="field2" id="field2" type="text" class="input-medium">
            <span id="field2:error"></span>
        </td>
    </tr><tr class="odd" id="field3:container">
        <th>Field3</th>
        <td>
            <input name="field3" id="field3" type="text" class="input-medium">
            <span id="field3:error"></span>
        </td>
    </tr>
    <tr class="error"><td colspan="2">
        <span id=":error"></span>
    </td></tr>
</table>"""


class TestVerticalCheckBoxTable(WidgetTest):
    widget = twb.VerticalCheckBoxTable
    attrs = dict(id='bootstrap-test', options=['', 'Red', 'Blue'])
    expected = """
    <table class="table table-condensed"
           name="bootstrap-test"
           id="bootstrap-test">
        <tbody>
        <tr><td/></tr>
        <tr>
            <td>
              <label class="checkbox" for="bootstrap-test:1">
                <input type="checkbox" name="bootstrap-test"
                       value="Red" id="bootstrap-test:1" />Red</label>
            </td>
        </tr>
        <tr>
            <td>
              <label class="checkbox" for="bootstrap-test:2">
                <input type="checkbox" name="bootstrap-test"
                       value="Blue" id="bootstrap-test:2" />Blue</label>
            </td>
        </tr>
        </tbody>
    </table>
    """


class TestVerticalRadioButtonTable(WidgetTest):
    widget = twb.VerticalRadioButtonTable
    attrs = dict(id='bootstrap-test', options=['', 'Red', 'Blue'])
    expected = """
    <table class="table table-condensed"
           name="bootstrap-test"
           id="bootstrap-test">
        <tbody>
        <tr><td/></tr>
        <tr>
            <td>
              <label class="radio" for="bootstrap-test:1">
                <input type="radio" name="bootstrap-test"
                       value="Red" id="bootstrap-test:1" />Red</label>
            </td>
        </tr>
        <tr>
            <td>
              <label class="radio" for="bootstrap-test:2">
                <input type="radio" name="bootstrap-test"
                       value="Blue" id="bootstrap-test:2" />Blue</label>
            </td>
        </tr>
        </tbody>
    </table>
    """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.