forked from helloflask/bootstrap-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bootstrap_flask.py
788 lines (666 loc) · 31.8 KB
/
test_bootstrap_flask.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
import unittest
from flask import Flask, render_template_string, current_app, request, flash
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, BooleanField, PasswordField, FileField,\
MultipleFileField, RadioField, HiddenField
from wtforms.validators import DataRequired, Length
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
class HelloForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)])
remember = BooleanField('Remember me')
submit = SubmitField()
class BootstrapTestCase(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.testing = True
self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.app.secret_key = 'for test'
self.bootstrap = Bootstrap(self.app) # noqa
self.bootswatch_themes = [
'cerulean', 'cosmo', 'cyborg', 'darkly', 'default', 'flatly', 'journal', 'litera',
'lumen', 'lux', 'materia', 'minty', 'pulse', 'sandstone', 'simplex', 'sketchy', 'slate',
'solar', 'spacelab', 'superhero', 'united', 'yeti'
]
@self.app.route('/')
def index():
return render_template_string('{{ bootstrap.load_css() }}{{ bootstrap.load_js() }}')
self.context = self.app.test_request_context()
self.context.push()
self.client = self.app.test_client()
def tearDown(self):
self.context.pop()
def test_extension_init(self):
self.assertIn('bootstrap', current_app.extensions)
def test_load_css(self):
rv = self.bootstrap.load_css()
self.assertIn('bootstrap.min.css', rv)
def test_load_js(self):
rv = self.bootstrap.load_js()
self.assertIn('bootstrap.min.js', rv)
def test_local_resources(self):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertNotIn('https://cdn.jsdelivr.net/npm/bootstrap', data)
self.assertIn('bootstrap.min.js', data)
self.assertIn('bootstrap.min.css', data)
self.assertIn('jquery.min.js', data)
with self.client.get('/bootstrap/static/css/bootstrap.min.css') as css_response:
self.assertNotEqual(css_response.status_code, 404)
with self.client.get('/bootstrap/static/js/bootstrap.min.js') as js_response:
self.assertNotEqual(js_response.status_code, 404)
with self.client.get('/bootstrap/static/jquery.min.js') as jquery_response:
self.assertNotEqual(jquery_response.status_code, 404)
css_rv = self.bootstrap.load_css()
js_rv = self.bootstrap.load_js()
self.assertIn('/bootstrap/static/css/bootstrap.min.css', css_rv)
self.assertIn('/bootstrap/static/js/bootstrap.min.js', js_rv)
self.assertNotIn('https://cdn.jsdelivr.net/npm/bootstrap', css_rv)
self.assertNotIn('https://cdn.jsdelivr.net/npm/bootstrap', js_rv)
def test_cdn_resources(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('https://cdn.jsdelivr.net/npm/bootstrap', data)
self.assertIn('bootstrap.min.js', data)
self.assertIn('bootstrap.min.css', data)
css_rv = self.bootstrap.load_css()
js_rv = self.bootstrap.load_js()
self.assertNotIn('/bootstrap/static/css/bootstrap.min.css', css_rv)
self.assertNotIn('/bootstrap/static/js/bootstrap.min.js', js_rv)
self.assertIn('https://cdn.jsdelivr.net/npm/bootstrap', css_rv)
self.assertIn('https://cdn.jsdelivr.net/npm/bootstrap', js_rv)
def test_bootswatch_local(self):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True
for theme in self.bootswatch_themes:
current_app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = theme
data = self.client.get('/').get_data(as_text=True)
self.assertNotIn('https://cdn.jsdelivr.net/npm/bootswatch', data)
self.assertIn('swatch/%s/bootstrap.min.css' % theme, data)
with self.client.get('/bootstrap/static/css/swatch/%s/bootstrap.min.css' % theme) as css_response:
self.assertNotEqual(css_response.status_code, 404)
def test_bootswatch_cdn(self):
current_app.config['BOOTSTRAP_SERVE_LOCAL'] = False
for theme in self.bootswatch_themes:
current_app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = theme
data = self.client.get('/').get_data(as_text=True)
self.assertIn('https://cdn.jsdelivr.net/npm/bootswatch', data)
self.assertIn('dist/%s/bootstrap.min.css' % theme, data)
css_rv = self.bootstrap.load_css()
self.assertNotIn('/bootstrap/static/css/swatch/%s/bootstrap.min.css' % theme, data)
self.assertIn('https://cdn.jsdelivr.net/npm/bootswatch', css_rv)
def test_render_field(self):
@self.app.route('/field')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_field %}
{{ render_field(form.username) }}
{{ render_field(form.password) }}
''', form=form)
response = self.client.get('/field')
data = response.get_data(as_text=True)
self.assertIn('<input class="form-control" id="username" name="username"', data)
self.assertIn('<input class="form-control" id="password" name="password"', data)
def test_render_form(self):
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('<input class="form-control" id="username" name="username"', data)
self.assertIn('<input class="form-control" id="password" name="password"', data)
def test_render_form_row(self):
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form_row %}
{{ render_form_row([form.username, form.password]) }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('<div class="form-row">', data)
self.assertIn('<div class="col">', data)
def test_render_form_row_row_class(self):
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form_row %}
{{ render_form_row([form.username, form.password], row_class='row') }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('<div class="row">', data)
def test_render_form_row_col_class_default(self):
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form_row %}
{{ render_form_row([form.username, form.password], col_class_default='col-md-6') }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('<div class="col-md-6">', data)
def test_render_form_row_col_map(self):
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form_row %}
{{ render_form_row([form.username, form.password], col_map={'username': 'col-md-6'}) }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('<div class="col">', data)
self.assertIn('<div class="col-md-6">', data)
def test_render_pager(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
@self.app.route('/pager')
def test():
db.drop_all()
db.create_all()
for i in range(100):
m = Message()
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
return render_template_string('''
{% from 'bootstrap/pagination.html' import render_pager %}
{{ render_pager(pagination) }}
''', pagination=pagination, messages=messages)
response = self.client.get('/pager')
data = response.get_data(as_text=True)
self.assertIn('<nav aria-label="Page navigation">', data)
self.assertIn('Previous', data)
self.assertIn('Next', data)
self.assertIn('<li class="page-item disabled">', data)
response = self.client.get('/pager?page=2')
data = response.get_data(as_text=True)
self.assertIn('<nav aria-label="Page navigation">', data)
self.assertIn('Previous', data)
self.assertIn('Next', data)
self.assertNotIn('<li class="page-item disabled">', data)
def test_render_pagination(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
@self.app.route('/pagination')
def test():
db.drop_all()
db.create_all()
for i in range(100):
m = Message()
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
return render_template_string('''
{% from 'bootstrap/pagination.html' import render_pagination %}
{{ render_pagination(pagination) }}
''', pagination=pagination, messages=messages)
response = self.client.get('/pagination')
data = response.get_data(as_text=True)
self.assertIn('<nav aria-label="Page navigation">', data)
self.assertIn('<a class="page-link" href="#">1 <span class="sr-only">(current)</span></a>', data)
self.assertIn('10</a>', data)
response = self.client.get('/pagination?page=2')
data = response.get_data(as_text=True)
self.assertIn('<nav aria-label="Page navigation">', data)
self.assertIn('1</a>', data)
self.assertIn('<a class="page-link" href="#">2 <span class="sr-only">(current)</span></a>', data)
self.assertIn('10</a>', data)
def test_render_nav_item(self):
@self.app.route('/nav_item')
def test():
return render_template_string('''
{% from 'bootstrap/nav.html' import render_nav_item %}
{{ render_nav_item('test', 'Home') }}
''')
response = self.client.get('/nav_item')
data = response.get_data(as_text=True)
self.assertIn('<a class="nav-item nav-link active"', data)
def test_render_breadcrumb_item(self):
@self.app.route('/breadcrumb_item')
def test():
return render_template_string('''
{% from 'bootstrap/nav.html' import render_breadcrumb_item %}
{{ render_breadcrumb_item('test', 'Home') }}
''')
response = self.client.get('/breadcrumb_item')
data = response.get_data(as_text=True)
self.assertIn('<li class="breadcrumb-item active" aria-current="page">', data)
def test_render_static(self):
@self.app.route('/test_static')
def test():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_static %}
{{ render_static('css', 'test.css') }}
{{ render_static('js', 'test.js') }}
{{ render_static('icon', 'test.ico') }}
''')
response = self.client.get('/test_static')
data = response.get_data(as_text=True)
self.assertIn('<link rel="stylesheet" href="/static/test.css" type="text/css">', data)
self.assertIn('<script type="text/javascript" src="/static/test.js"></script>', data)
self.assertIn('<link rel="icon" href="/static/test.ico">', data)
def test_render_messages(self):
@self.app.route('/messages')
def test_messages():
flash('test message', 'danger')
return render_template_string('''
{% from 'bootstrap/utils.html' import render_messages %}
{{ render_messages() }}
''')
@self.app.route('/container')
def test_container():
flash('test message', 'danger')
return render_template_string('''
{% from 'bootstrap/utils.html' import render_messages %}
{{ render_messages(container=True) }}
''')
@self.app.route('/dismissible')
def test_dismissible():
flash('test message', 'danger')
return render_template_string('''
{% from 'bootstrap/utils.html' import render_messages %}
{{ render_messages(dismissible=True) }}
''')
@self.app.route('/dismiss_animate')
def test_dismiss_animate():
flash('test message', 'danger')
return render_template_string('''
{% from 'bootstrap/utils.html' import render_messages %}
{{ render_messages(dismissible=True, dismiss_animate=True) }}
''')
response = self.client.get('/messages')
data = response.get_data(as_text=True)
self.assertIn('<div class="alert alert-danger"', data)
response = self.client.get('/container')
data = response.get_data(as_text=True)
self.assertIn('<div class="container flashed-messages">', data)
response = self.client.get('/dismissible')
data = response.get_data(as_text=True)
self.assertIn('alert-dismissible', data)
self.assertIn('<button type="button" class="close" data-dismiss="alert"', data)
self.assertNotIn('fade show', data)
response = self.client.get('/dismiss_animate')
data = response.get_data(as_text=True)
self.assertIn('alert-dismissible', data)
self.assertIn('<button type="button" class="close" data-dismiss="alert"', data)
self.assertIn('fade show', data)
# test WTForm fields for render_form and render_field
def test_render_form_enctype(self):
class SingleUploadForm(FlaskForm):
avatar = FileField('Avatar')
class MultiUploadForm(FlaskForm):
photos = MultipleFileField('Multiple photos')
@self.app.route('/single')
def single():
form = SingleUploadForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
@self.app.route('/multi')
def multi():
form = SingleUploadForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.get('/single')
data = response.get_data(as_text=True)
self.assertIn('multipart/form-data', data)
response = self.client.get('/multi')
data = response.get_data(as_text=True)
self.assertIn('multipart/form-data', data)
# test render_kw class for WTForms field
def test_form_render_kw_class(self):
class TestForm(FlaskForm):
username = StringField('Username')
password = PasswordField('Password', render_kw={'class': 'my-password-class'})
submit = SubmitField(render_kw={'class': 'my-awesome-class'})
@self.app.route('/render_kw')
def render_kw():
form = TestForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.get('/render_kw')
data = response.get_data(as_text=True)
self.assertIn('class="form-control"', data)
self.assertNotIn('class="form-control "', data)
self.assertIn('class="form-control my-password-class"', data)
self.assertIn('my-awesome-class', data)
self.assertIn('btn', data)
# test WTForm field description for BooleanField
def test_form_description_for_booleanfield(self):
class TestForm(FlaskForm):
remember = BooleanField('Remember me', description='Just check this')
@self.app.route('/description')
def description():
form = TestForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.get('/description')
data = response.get_data(as_text=True)
self.assertIn('Remember me', data)
self.assertIn('<small class="form-text text-muted">Just check this</small>', data)
def test_button_size(self):
self.assertEqual(current_app.config['BOOTSTRAP_BTN_SIZE'], 'md')
current_app.config['BOOTSTRAP_BTN_SIZE'] = 'lg'
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('btn-lg', data)
@self.app.route('/form2')
def test_overwrite():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form, button_size='sm') }}
''', form=form)
response = self.client.get('/form2')
data = response.get_data(as_text=True)
self.assertNotIn('btn-lg', data)
self.assertIn('btn-sm', data)
def test_button_style(self):
self.assertEqual(current_app.config['BOOTSTRAP_BTN_STYLE'], 'primary')
current_app.config['BOOTSTRAP_BTN_STYLE'] = 'secondary'
@self.app.route('/form')
def test():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('btn-secondary', data)
@self.app.route('/form2')
def test_overwrite():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form, button_style='success') }}
''', form=form)
response = self.client.get('/form2')
data = response.get_data(as_text=True)
self.assertNotIn('btn-primary', data)
self.assertIn('btn-success', data)
@self.app.route('/form3')
def test_button_map():
form = HelloForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form, button_map={'submit': 'warning'}) }}
''', form=form)
response = self.client.get('/form3')
data = response.get_data(as_text=True)
self.assertNotIn('btn-primary', data)
self.assertIn('btn-warning', data)
def test_error_message_for_radiofield_and_booleanfield(self):
class TestForm(FlaskForm):
remember = BooleanField('Remember me', validators=[DataRequired()])
option = RadioField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')],
validators=[DataRequired()])
@self.app.route('/error', methods=['GET', 'POST'])
def error():
form = TestForm()
if form.validate_on_submit():
pass
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)
response = self.client.post('/error', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('This field is required', data)
def test_render_simple_table(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text)
@self.app.route('/table')
def test():
db.drop_all()
db.create_all()
for i in range(10):
m = Message(text='Test message {}'.format(i+1))
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
titles = [('id', '#'), ('text', 'Message')]
return render_template_string('''
{% from 'bootstrap/table.html' import render_table %}
{{ render_table(messages, titles) }}
''', titles=titles, messages=messages)
response = self.client.get('/table')
data = response.get_data(as_text=True)
self.assertIn('<table class="table">', data)
self.assertIn('<th scope="col">#</th>', data)
self.assertIn('<th scope="col">Message</th>', data)
self.assertIn('<th scope="col">Message</th>', data)
self.assertIn('<th scope="row">1</th>', data)
self.assertIn('<td>Test message 1</td>', data)
def test_render_customized_table(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text)
@self.app.route('/table')
def test():
db.drop_all()
db.create_all()
for i in range(10):
m = Message(text='Test message {}'.format(i+1))
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
titles = [('id', '#'), ('text', 'Message')]
return render_template_string('''
{% from 'bootstrap/table.html' import render_table %}
{{ render_table(messages, titles, table_classes='table-striped',
header_classes='thead-dark', caption='Messages') }}
''', titles=titles, messages=messages)
response = self.client.get('/table')
data = response.get_data(as_text=True)
self.assertIn('<table class="table table-striped">', data)
self.assertIn('<thead class="thead-dark">', data)
self.assertIn('<caption>Messages</caption>', data)
def test_render_responsive_table(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text)
@self.app.route('/table')
def test():
db.drop_all()
db.create_all()
for i in range(10):
m = Message(text='Test message {}'.format(i+1))
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
titles = [('id', '#'), ('text', 'Message')]
return render_template_string('''
{% from 'bootstrap/table.html' import render_table %}
{{ render_table(messages, titles, responsive=True,
responsive_class='table-responsive-sm') }}
''', titles=titles, messages=messages)
response = self.client.get('/table')
data = response.get_data(as_text=True)
self.assertIn('<div class="table-responsive-sm">', data)
def test_build_table_titles(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text)
@self.app.route('/table')
def test():
db.drop_all()
db.create_all()
for i in range(10):
m = Message(text='Test message {}'.format(i+1))
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
return render_template_string('''
{% from 'bootstrap/table.html' import render_table %}
{{ render_table(messages) }}
''', messages=messages)
response = self.client.get('/table')
data = response.get_data(as_text=True)
self.assertIn('<table class="table">', data)
self.assertIn('<th scope="col">#</th>', data)
self.assertIn('<th scope="col">Text</th>', data)
self.assertIn('<th scope="col">Text</th>', data)
self.assertIn('<th scope="row">1</th>', data)
self.assertIn('<td>Test message 1</td>', data)
def test_build_table_titles_with_empty_data(self):
@self.app.route('/table')
def test():
messages = []
return render_template_string('''
{% from 'bootstrap/table.html' import render_table %}
{{ render_table(messages) }}
''', messages=messages)
response = self.client.get('/table')
self.assertEqual(response.status_code, 200)
def test_render_table_with_actions(self):
db = SQLAlchemy(self.app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text)
@self.app.route('/table/<message_id>/view')
def test_view_message(message_id):
return 'Viewing {}'.format(message_id)
@self.app.route('/table')
def test():
db.drop_all()
db.create_all()
for i in range(10):
m = Message(text='Test message {}'.format(i+1))
db.session.add(m)
db.session.commit()
page = request.args.get('page', 1, type=int)
pagination = Message.query.paginate(page, per_page=10)
messages = pagination.items
titles = [('id', '#'), ('text', 'Message')]
return render_template_string('''
{% from 'bootstrap/table.html' import render_table %}
{{ render_table(messages, titles, show_actions=True,
view_url=url_for('test_view_message', message_id=':primary_key')) }}
''', titles=titles, messages=messages)
response = self.client.get('/table')
data = response.get_data(as_text=True)
self.assertIn('<a href="/table/1/view">', data)
self.assertIn('<img src="/bootstrap/static/img/view.svg" alt="View">', data)
def test_render_hidden_errors(self):
class TestForm(FlaskForm):
hide = HiddenField('Hide', validators=[DataRequired('Hide field is empty.')])
submit = SubmitField()
@self.app.route('/error', methods=['GET', 'POST'])
def error():
form = TestForm()
if form.validate_on_submit():
pass
return render_template_string('''
{% from 'bootstrap/form.html' import render_field, render_hidden_errors %}
<form method="post">
{{ form.hidden_tag() }}
{{ render_hidden_errors(form) }}
{{ render_field(form.submit) }}
</form>
''', form=form)
response = self.client.post('/error', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Hide field is empty.', data)
def test_render_icon(self):
@self.app.route('/icon')
def icon():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_icon %}
{{ render_icon('heart') }}
''')
@self.app.route('/icon-size')
def icon_size():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_icon %}
{{ render_icon('heart', 32) }}
''')
@self.app.route('/icon-style')
def icon_style():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_icon %}
{{ render_icon('heart', color='primary') }}
''')
@self.app.route('/icon-color')
def icon_color():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_icon %}
{{ render_icon('heart', color='green') }}
''')
response = self.client.get('/icon')
data = response.get_data(as_text=True)
self.assertIn('bootstrap-icons.svg#heart', data)
self.assertIn('width="1em"', data)
self.assertIn('height="1em"', data)
response = self.client.get('/icon-size')
data = response.get_data(as_text=True)
self.assertIn('bootstrap-icons.svg#heart', data)
self.assertIn('width="32"', data)
self.assertIn('height="32"', data)
response = self.client.get('/icon-style')
data = response.get_data(as_text=True)
self.assertIn('bootstrap-icons.svg#heart', data)
self.assertIn('text-primary', data)
response = self.client.get('/icon-color')
data = response.get_data(as_text=True)
self.assertIn('bootstrap-icons.svg#heart', data)
self.assertIn('style="color: green"', data)
def test_render_icon_config(self):
self.app.config['BOOTSTRAP_ICON_SIZE'] = 100
self.app.config['BOOTSTRAP_ICON_COLOR'] = 'success'
@self.app.route('/icon')
def icon():
return render_template_string('''
{% from 'bootstrap/utils.html' import render_icon %}
{{ render_icon('heart') }}
''')
response = self.client.get('/icon')
data = response.get_data(as_text=True)
self.assertIn('width="100"', data)
self.assertIn('height="100"', data)
self.assertIn('text-success', data)