import os import feed_file_exporter as ffe from constants import database as db_consts def test_split_rows_to_files(tmp_path): output = str(tmp_path) fname = 'File.txt' rows = [ { db_consts.RECORD_TYPE: 'H', db_consts.BODY_CONTENT: 'HEADER', db_consts.FILE_NAME: fname, }, { db_consts.RECORD_TYPE: 'B', db_consts.BODY_CONTENT: 'BODY1', db_consts.FILE_NAME: fname, }, { db_consts.RECORD_TYPE: 'T', db_consts.BODY_CONTENT: 'TAIL', db_consts.FILE_NAME: fname, }, ] count, upload_list, gen_list = ffe.split_rows_to_files(rows, output) assert count == 3 assert upload_list == [fname] assert len(gen_list) == 1 assert os.path.exists(gen_list[0]) def test_split_rows_to_files_mismatch_raises(tmp_path): output = str(tmp_path) rows = [ { db_consts.RECORD_TYPE: 'H', db_consts.BODY_CONTENT: 'HEADER', db_consts.FILE_NAME: 'A.txt', }, { db_consts.RECORD_TYPE: 'T', db_consts.BODY_CONTENT: 'TAIL', db_consts.FILE_NAME: 'B.txt', }, ] try: ffe.split_rows_to_files(rows, output) except ValueError: pass else: raise AssertionError('Expected ValueError on header/tail mismatch') class _Row: def __init__(self, d): self._d = d def keys(self): return list(self._d.keys()) def __iter__(self): return iter(self._d.values()) class _RP: def __init__(self, rows): self._rows = rows def fetchone(self): return _Row(self._rows[0]) def __iter__(self): for r in self._rows[1:]: yield _Row(r) def test_split_res_to_files(tmp_path): output = str(tmp_path) fname = 'Res.txt' rows = [ { db_consts.RECORD_TYPE: 'H', db_consts.BODY_CONTENT: 'HEADER', db_consts.FILE_NAME: fname, }, { db_consts.RECORD_TYPE: 'B', db_consts.BODY_CONTENT: 'BODY1', db_consts.FILE_NAME: fname, }, { db_consts.RECORD_TYPE: 'T', db_consts.BODY_CONTENT: 'TAIL', db_consts.FILE_NAME: fname, }, ] rp = _RP(rows) count, gen_list = ffe.split_res_to_files(rp, output) assert count == 3 assert len(gen_list) == 1 assert os.path.exists(gen_list[0])