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
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
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'
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
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
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(), expected_arr): assert tid == '_' assert rec == exp
def test_reader_iter(): fp = StringIO('\n'.join(['#_ {"key_1"}', '{"record_1"}', '{"record_2"}'])) with JSVReader(fp) as r: exp_arr = [{'key_1': 'record_1'}, {'key_1': 'record_2'}] for rec, exp in zip(r, exp_arr): assert rec == exp
def test_jsv_reader(): with JSVReader('some file') as r: for (tid, rec), exp in zip(r.items(), read_expected): assert tid == '_' assert rec == exp