Skip to content

Commit

Permalink
almost done with coverage for io.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kovner committed Dec 26, 2018
1 parent 77c2a01 commit 78a188b
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 18 deletions.
27 changes: 20 additions & 7 deletions jsv/io.py
Expand Up @@ -300,6 +300,10 @@ def exit(self):
def has_tmpl_file(self):
return self._has_tmpl_file

@property
def manage_rec_fp(self):
return self._manage_rec_fp

@property
def manage_tmpl_fp(self):
return self._manage_tmpl_fp
Expand Down Expand Up @@ -356,10 +360,16 @@ def __init__(self, record_file, record_mode='at', template_dict=None, template_f

def __enter__(self):
self.files.enter()
if self.files.manage_tmpl_fp:
for line in self.template_lines():
if line != '#_ {}':
print(line, file=self.files.tmpl_fp)
if self.files.has_tmpl_file:
if self.files.manage_tmpl_fp:
for line in self.template_lines():
if line != '#_ {}':
print(line, file=self.files.tmpl_fp)
else:
if self.files.manage_rec_fp:
for line in self.template_lines():
if line != '#_ {}':
print(line, file=self.files.rec_fp)
return self

def __exit__(self, t, v, tr):
Expand All @@ -368,7 +378,10 @@ def __exit__(self, t, v, tr):
def __setitem__(self, key, value):
super().__setitem__(key, value)
try:
fp = self.files.tmpl_fp
if self.files.has_tmpl_file:
fp = self.files.tmpl_fp
else:
fp = self.files.rec_fp
except RuntimeError:
fp = None
if fp:
Expand Down Expand Up @@ -408,8 +421,8 @@ class JSVReader(JSVCollection):
records should use the file extension ``.jsvr`` and templates should use file extension ``.jsvt``.
"""
def __init__(self, record_file, template_dict=None, template_file=None):
super().__init__(template_dict)
def __init__(self, record_file, template_file=None):
super().__init__()
self._fm = FileManager(record_file, 'rt', template_file, 'rt')
if self._fm.has_tmpl_file and not self._fm.manage_tmpl_fp:
populate_from_tmpl_file(self._fm.tmpl_fp, self)
Expand Down
109 changes: 98 additions & 11 deletions test/test_io.py
Expand Up @@ -217,14 +217,11 @@ def test_jsv_reader():
assert rec == exp


# Categories:
# 'record_input_type': ['file_name', 'file_pointer'],
# 'template_input_type': ['file_name', 'file_pointer', 'record_file']
reader_data = [
{
'record_input_type': ['file_name', 'file_pointer'],
'template_input_type': ['file_name', 'file_pointer', 'record_file']
},
{
'record_input_type': 'file_name',
'template_input_type': 'record_file',
'expected': [
{'key_1': 'record_1'},
{'key_1': 'record_2'}
Expand All @@ -238,16 +235,97 @@ def test_jsv_reader():
]


@mark.parametrize('test_data', [x for x in reader_data if x['record_input_type'] == 'file_name'
and x['template_input_type'] == 'record_file'])
def test_reader_with_record_filename(test_data):
with patch('builtins.open', MagicMock(return_value=StringIO('\n'.join(test_data['input'])))):
def get_reader_input(input_arr, ret_type=None):
if ret_type == 'record':
return '\n'.join([x for x in input_arr if x[0] != '#'])
elif ret_type == 'template':
return '\n'.join([x for x in input_arr if x[0] == '#'])
else:
return '\n'.join([x for x in input_arr])


def test_bad_template_file():
rec_fp = StringIO()
tmpl_fp = StringIO('\n'.join([
'#_ {"key_1"}',
'{"record_1"}',
'{"record_2"}'
]))
try:
JSVReader(rec_fp, tmpl_fp)
assert False
except RuntimeError as ex:
assert str(ex) == 'Expecting only template definitions in a template file'


@mark.parametrize('record_str, template_str, expected_arr',
[(get_reader_input(x['input'], 'record'),
get_reader_input(x['input'], 'template'),
x['expected']) for x in reader_data])
def test_reader_with_record_file_template_file(record_str, template_str, expected_arr):
rec_fp = StringIO(record_str)
tmpl_fp = StringIO(template_str)
with JSVReader(rec_fp, tmpl_fp) as r:
for (tid, rec), exp in zip(r.items(), expected_arr):
assert tid == '_'
assert rec == exp


@mark.parametrize('record_str, template_str, expected_arr',
[(get_reader_input(x['input'], 'record'),
get_reader_input(x['input'], 'template'),
x['expected']) for x in reader_data])
def test_reader_with_record_file_template_filename(record_str, template_str, expected_arr):
rec_fp = StringIO(record_str)
with patch('builtins.open', MagicMock(return_value=StringIO(template_str))):
with JSVReader(rec_fp, '/some/template_file') as r:
for (tid, rec), exp in zip(r.items(), expected_arr):
assert tid == '_'
assert rec == exp


@mark.parametrize('record_str, template_str, expected_arr',
[(get_reader_input(x['input'], 'record'),
get_reader_input(x['input'], 'template'),
x['expected']) for x in reader_data])
def test_reader_with_record_filename_template_file(record_str, template_str, expected_arr):
tmpl_fp = StringIO(template_str)
with patch('builtins.open', MagicMock(return_value=StringIO(record_str))):
with JSVReader('/some/record/file', tmpl_fp) as r:
for (tid, rec), exp in zip(r.items(), expected_arr):
assert tid == '_'
assert rec == exp


@mark.parametrize('record_str, template_str, expected_arr',
[(get_reader_input(x['input'], 'record'),
get_reader_input(x['input'], 'template'),
x['expected']) for x in reader_data])
def test_reader_with_record_filename_template_filename(record_str, template_str, expected_arr):
with patch('builtins.open', MagicMock(side_effect=[StringIO(record_str), StringIO(template_str)])):
with JSVReader('/some/record/file', '/some/template_file') as r:
for (tid, rec), exp in zip(r.items(), expected_arr):
assert tid == '_'
assert rec == exp


@mark.parametrize('input_str, expected_arr', [(get_reader_input(x['input']), x['expected']) for x in reader_data])
def test_reader_with_record_filename(input_str, expected_arr):
with patch('builtins.open', MagicMock(return_value=StringIO(input_str))):
with JSVReader('/some/file') as r:
for (tid, rec), exp in zip(r.items(), test_data['expected']):
for (tid, rec), exp in zip(r.items(), expected_arr):
assert tid == '_'
assert rec == exp


@mark.parametrize('input_str, expected_arr', [(get_reader_input(x['input']), x['expected']) for x in reader_data])
def test_reader_with_record_file(input_str, expected_arr):
rec_fp = StringIO(input_str)
with JSVReader(rec_fp) as r:
for (tid, rec), exp in zip(r.items(), expected_arr):
assert tid == '_'
assert rec == exp


writer_tmpl = JSVTemplate('{"key_1"}')
writer_recs = [
Expand All @@ -272,6 +350,15 @@ def __iter__(self):

@patch('builtins.open', mock_file)
def test_jsv_writer():
w = JSVWriter('/some/file')
print(w.files.manage_rec_fp)
w['_'] = writer_tmpl
with w:
for obj in writer_recs:
w.write(obj)
w.files.rec_fp.seek(0)
out = w.files.rec_fp.read()
assert out == write_expected['combined']
with JSVWriter('some file') as w:
w['_'] = writer_tmpl
for obj in writer_recs:
Expand Down

0 comments on commit 78a188b

Please sign in to comment.