Commit 81c12c52 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: repurpose exporting test

parent 3b1a57f0
......@@ -101,93 +101,6 @@ def test_exportData_subjid():
assert np.all(got.index == [10, 11, 12])
@clear_plugins
def test_exportData_varformats():
custom.registerBuiltIns()
def check(gotfile, expected):
with open(gotfile, 'rt') as f:
got = f.read().strip()
return got == expected
datefmt = '%Y a %m b %d c'
timefmt = '%Y a %m b %d c %M d %H e %S'
floatfmt = '{} poo'
@custom.formatter('test_datefmt')
def datefmt_func(dtable, column, series):
def format(val):
return val.strftime(datefmt)
return series.apply(format)
@custom.formatter('test_timefmt')
def timefmt_func(dtable, column, series):
def format(val):
return val.strftime(timefmt)
return series.apply(format)
@custom.formatter('test_floatfmt')
def floatfmt_func(dtable, column, series):
def format(val):
return floatfmt.format(val)
return series.apply(format)
with tempdir():
gen_test_data(
3, 3, 'data.tsv', ctypes={1 : 'date', 2 : 'datetime'})
variables = list(range(1, 4))
vartable, proctable, cattable, _ = gen_tables(
variables, {1 : 'Date', 2 : 'Time'})
colnames = ['{}-0.0'.format(v) for v in variables]
colobjs = [datatable.Column(None, 'eid', 0, 0, 0, 0)] + \
[datatable.Column(None, c, i, v, 0, 0)
for i, (c, v) in enumerate(zip(colnames, variables), 1)]
data = pd.read_csv(
'data.tsv', delimiter='\t',
parse_dates=['1-0.0', '2-0.0'],
index_col=0,
infer_datetime_format=True)
dtable = datatable.DataTable(
data, colobjs, vartable, proctable, cattable)
expdates = [v.strftime(datefmt) for v in data['1-0.0']]
exptimes = [v.strftime(timefmt) for v in data['2-0.0']]
expfloats = [ floatfmt.format(v) for v in data['3-0.0']]
exporting.exportData(dtable,
'export.tsv',
'tsv',
dateFormat='test_datefmt',
timeFormat='test_timefmt',
formatters={ 3 : 'test_floatfmt'})
got = pd.read_csv('export.tsv', delimiter='\t', dtype=str, index_col=0)
assert np.all(got['1-0.0'] == expdates)
assert np.all(got['2-0.0'] == exptimes)
assert np.all(got['3-0.0'] == expfloats)
expdates = exporting.defaultDateTimeFormat(
dtable, colobjs[1], dtable[:, '1-0.0'])
exptimes = exporting.defaultDateTimeFormat(
dtable, colobjs[2], dtable[:, '2-0.0'])
exporting.exportData(dtable, 'export.tsv', 'tsv')
got = pd.read_csv('export.tsv', delimiter='\t', dtype=str, index_col=0)
assert np.all(got['1-0.0'] == expdates)
assert np.all(got['2-0.0'] == exptimes)
def test_exportData_TSV_subject_ordering():
......
......@@ -1433,3 +1433,73 @@ def test_main_output_format():
main.main('-ow -f hdf5 out.bin data.tsv'.split())
got = pd.read_hdf('out.bin')
assert_frame_equal(exp, got, check_dtype=False)
@patch_logging
@clear_plugins
def test_main_varformats():
def check(gotfile, expected):
with open(gotfile, 'rt') as f:
got = f.read().strip()
return got == expected
datefmt = '%Y a %m b %d c'
timefmt = '%Y a %m b %d c %M d %H e %S'
floatfmt = '{:.0f} poo'
@custom.formatter('test_datefmt')
def datefmt_func(dtable, column, series):
def format(val):
return val.strftime(datefmt)
return series.apply(format)
@custom.formatter('test_timefmt')
def timefmt_func(dtable, column, series):
def format(val):
return val.strftime(timefmt)
return series.apply(format)
@custom.formatter('test_floatfmt')
def floatfmt_func(dtable, column, series):
def format(val):
return floatfmt.format(val)
return series.apply(format)
vartable = tw.dedent("""
ID\tType
1\tdate
2\ttime
3\tcontinuous
""")
with tempdir():
gen_test_data(3, 3, 'data.tsv', ctypes={1 : 'date', 2 : 'datetime'})
with open('vartable.tsv', 'wt') as f:
f.write(vartable)
data = pd.read_csv(
'data.tsv', delimiter='\t',
parse_dates=['1-0.0', '2-0.0'],
index_col=0,
infer_datetime_format=True)
expdates = [v.strftime(datefmt) for v in data['1-0.0']]
exptimes = [v.strftime(timefmt) for v in data['2-0.0']]
expfloats = [ floatfmt.format(v) for v in data['3-0.0']]
main.main('-edf test_datefmt '
'-etf test_timefmt '
'-tvf 3 test_floatfmt '
'-vf vartable.tsv '
'export.tsv data.tsv'.split())
got = pd.read_csv('export.tsv', delimiter='\t', dtype=str, index_col=0)
assert np.all(got['1-0.0'] == expdates)
assert np.all(got['2-0.0'] == exptimes)
assert np.all(got['3-0.0'] == expfloats)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment