from types import SimpleNamespace import feed_file_exporter as ffe from constants import database as db_consts def _rows_for_file(file_name='X.txt'): return [ { db_consts.RECORD_TYPE: 'H', db_consts.BODY_CONTENT: 'H', db_consts.FILE_NAME: file_name, }, { db_consts.RECORD_TYPE: 'N', db_consts.BODY_CONTENT: 'B', db_consts.FILE_NAME: file_name, }, { db_consts.RECORD_TYPE: 'T', db_consts.BODY_CONTENT: 'T', db_consts.FILE_NAME: file_name, }, ] def test_process_regular_feed_mocks_persist(tmp_path, mocker): table = 'tbl' # Mock persistence layer mocker.patch.object( ffe.feed_persist, 'get_all_periods', return_value=SimpleNamespace(message=[202501]), ) mocker.patch.object( ffe.feed_persist, 'get_group_names_for_period', return_value=SimpleNamespace(message=['G']), ) mocker.patch.object( ffe.feed_persist, 'get_by_period_group_name', return_value=SimpleNamespace(message=_rows_for_file('R.txt')), ) # Spy split_rows_to_files to confirm invocation split_spy = mocker.spy(ffe, 'split_rows_to_files') count, files = ffe.process_regular_feed(table, str(tmp_path)) assert count == 3 assert len(files) == 1 assert split_spy.call_count == 1 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_process_aggregate_feed_mocks_persist(tmp_path, mocker): table = 'tbl' # Force legacy fetch mode to test original split_rows_to_files path mocker.patch('config.FETCH_MODE', 'LEGACY') mocker.patch.object(ffe, 'get_group_name_list', return_value=['G1', 'G2']) mocker.patch.object( ffe.feed_persist, 'get_count_by_group_name', return_value=SimpleNamespace(message=3), ) mocker.patch.object( ffe.feed_persist, 'get_by_group_name', return_value=SimpleNamespace(message=_rows_for_file('A.txt')), ) split_spy = mocker.spy(ffe, 'split_rows_to_files') count, files = ffe.process_aggregate_feed(table, str(tmp_path)) # Two groups, each writes 3 rows assert count == 6 assert len(files) == 2 assert split_spy.call_count == 2