-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_xlsx_parser.py
50 lines (44 loc) · 2.02 KB
/
test_xlsx_parser.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
from mtd.tests.test_data import xlsx
from mtd.tests import SAMPLE_DATA_DF, SAMPLE_DATA_OBJ_BLANK
import os
from unittest import TestCase
from mtd.parsers import parse
from mtd.exceptions import MissingResourceError, UnsupportedFiletypeError
class XlsxParserTest(TestCase):
def setUp(self):
self.path = os.path.dirname(xlsx.__file__)
self.data = os.path.join(self.path, 'data.xlsx')
self.missing_sheet_data = os.path.join(self.path, 'missing_sheet_data.xlsx')
self.manifest = os.path.join(self.path, 'manifest.json')
self.manifest_alternate = os.path.join(self.path, 'manifest_alternate.json')
self.maxDiff = None
def test_data_df_matches_sample(self):
'''Check test XLSX data is parsed and looks like ground truth SAMPLE_DATA_DF
'''
parsed_data = parse(self.manifest, self.data)
self.assertTrue(parsed_data['data'].equals(SAMPLE_DATA_DF))
def test_data_obj_matches_sample(self):
'''Check test XLSX data is parsed and looks like ground truth SAMPLE_DATA_OBJ
'''
parsed_data = parse(self.manifest, self.data)
parsed_data_obj = parsed_data['data'].to_dict(orient='records')
self.assertEqual(SAMPLE_DATA_OBJ_BLANK, parsed_data_obj)
def test_missing_sheet(self):
'''Check error raised for missing sheet in 'location'
'''
with self.assertRaises(MissingResourceError):
parse(self.manifest, self.missing_sheet_data)
def test_no_file(self):
'''Check error raised for nonexistant file
'''
with self.assertRaises(MissingResourceError):
parse(self.manifest, 'foobar.xlsx')
with self.assertRaises(UnsupportedFiletypeError):
parse(self.manifest, os.path.join(self.path, 'manifest.xlsx'))
def test_alternate(self):
'''Check alternate manifest with no location or skipheader
'''
try:
parse(self.manifest_alternate, self.data)
except:
self.fail("Couldn't parse from alternate manifest.")