Skip to content

Commit

Permalink
Merge pull request #2399 from cjmayo/test_tablib
Browse files Browse the repository at this point in the history
Test tsv model export using tablib
  • Loading branch information
samuelhwilliams authored Jul 23, 2024
2 parents d929d53 + aacc43a commit dd1426d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flask_admin/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ class MyModelView(BaseModelView):
A list of available export filetypes. `csv` only is default, but any
filetypes supported by tablib can be used.
Check tablib for https://github.com/kennethreitz/tablib/blob/master/README.rst
Check tablib for https://tablib.readthedocs.io/en/stable/formats.html
for supported types.
"""

Expand Down
25 changes: 25 additions & 0 deletions flask_admin/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,31 @@ def export_formatter(v, c, m, p):
assert rv.status_code == 500


def test_export_tablib(app, admin):
client = app.test_client()

# basic test of tsv export with a few records using tablib
view_data = {
1: Model(1, "col1_1", "col2_1"),
2: Model(2, "col1_2", "col2_2"),
3: Model(3, "col1_3", "col2_3"),
}

view = MockModelView(Model, view_data, can_export=True,
column_list=['col1', 'col2'],
export_types=['tsv'])
admin.add_view(view)

rv = client.get('/admin/model/export/tsv/')
data = rv.data.decode('utf-8')
assert rv.mimetype == 'text/tab-separated-values'
assert rv.status_code == 200
assert "Col1\tCol2\r\n" + \
"col1_1\tcol2_1\r\n" + \
"col1_2\tcol2_2\r\n" + \
"col1_3\tcol2_3\r\n" == data


def test_list_row_actions(app, admin):
client = app.test_client()

Expand Down
1 change: 1 addition & 0 deletions requirements/tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ arrow<0.14.0
colour
email-validator
wtforms
tablib

0 comments on commit dd1426d

Please sign in to comment.