|
| 1 | +import os |
| 2 | +from collections import OrderedDict |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mfr.extensions.tabular.libs import stdlib_tools |
| 7 | +from mfr.extensions.tabular.exceptions import EmptyTableError |
| 8 | + |
| 9 | + |
| 10 | +BASE = os.path.dirname(os.path.abspath(__file__)) |
| 11 | + |
| 12 | + |
| 13 | +class TestTabularStdlibTools: |
| 14 | + |
| 15 | + def test_csv_stdlib(self): |
| 16 | + with open(os.path.join(BASE, 'files', 'test.csv')) as fp: |
| 17 | + sheets = stdlib_tools.csv_stdlib(fp) |
| 18 | + |
| 19 | + sheet = sheets.popitem()[1] |
| 20 | + assert sheet[0] == [ |
| 21 | + {'id': 'one', 'field': 'one', 'name': 'one', 'sortable': True}, |
| 22 | + {'id': 'two', 'field': 'two', 'name': 'two', 'sortable': True}, |
| 23 | + {'id': 'three', 'field': 'three', 'name': 'three', 'sortable': True} |
| 24 | + ] |
| 25 | + assert sheet[1][0] == OrderedDict([('one', 'à'), ('two', 'b'), ('three', 'c')]) |
| 26 | + assert sheet[1][1] == OrderedDict([('one', '1'), ('two', '2'), ('three', '3')]) |
| 27 | + |
| 28 | + def test_tsv_stdlib(self): |
| 29 | + with open(os.path.join(BASE, 'files', 'test.tsv')) as fp: |
| 30 | + sheets = stdlib_tools.tsv_stdlib(fp) |
| 31 | + |
| 32 | + sheet = sheets.popitem()[1] |
| 33 | + assert sheet[0] == [ |
| 34 | + {'id': 'one', 'field': 'one', 'name': 'one', 'sortable': True}, |
| 35 | + {'id': 'two', 'field': 'two', 'name': 'two', 'sortable': True}, |
| 36 | + {'id': 'three', 'field': 'three', 'name': 'three', 'sortable': True} |
| 37 | + ] |
| 38 | + assert sheet[1][0] == OrderedDict([('one', 'a'), ('two', 'b'), ('three', 'c')]) |
| 39 | + assert sheet[1][1] == OrderedDict([('one', '1'), ('two', '2'), ('three', '3')]) |
| 40 | + |
| 41 | + def test_tsv_stdlib_exception_raises(self): |
| 42 | + with open(os.path.join(BASE, 'files', 'invalid.tsv')) as fp: |
| 43 | + with pytest.raises(EmptyTableError) as e: |
| 44 | + stdlib_tools.tsv_stdlib(fp) |
| 45 | + assert e.value.code == 400 |
| 46 | + |
| 47 | + def test_csv_stdlib_exception_raises(self): |
| 48 | + with open(os.path.join(BASE, 'files', 'invalid.csv')) as fp: |
| 49 | + with pytest.raises(EmptyTableError) as e: |
| 50 | + stdlib_tools.tsv_stdlib(fp) |
| 51 | + assert e.value.code == 400 |
0 commit comments