"""Test for utils hfa.py.""" import pandas as pd import pytest from src.utils import dataframes def test_dict_to_dataframe(): """Test dict_to_dataframe return expected success response.""" records = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'} ] dtypes = {'id': 'int64', 'name': 'string'} df = dataframes.dict_to_dataframe(records, dtypes=dtypes) assert isinstance(df, pd.DataFrame) assert list(df.columns) == ['id', 'name'] assert df.dtypes['id'] == 'int64' assert df.dtypes['name'] == 'string' assert df.shape == (2, 2) def test_dict_to_dataframe_empty(): """Test dict_to_dataframe return empty response.""" df = dataframes.dict_to_dataframe([]) assert isinstance(df, pd.DataFrame) assert df.empty @pytest.mark.parametrize( 'dfs, columns, drop_duplicates, ignore_index, expected_dict', [ pytest.param( [ pd.DataFrame({'a': [1, 2], 'b': [3, 4]}), pd.DataFrame({'a': [5], 'b': [6]}), ], None, False, True, [ {'a': 1, 'b': 3}, {'a': 2, 'b': 4}, {'a': 5, 'b': 6}, ], id='basic concat, no drop duplicates, no columns filter', ), pytest.param( [ pd.DataFrame({'a': [1, 2], 'b': [3, 4]}), pd.DataFrame({'a': [5], 'b': [6]}), ], ['a'], False, True, [ {'a': 1}, {'a': 2}, {'a': 5}, ], id='select columns after concat', ), pytest.param( [ pd.DataFrame({'a': [1, 2, 2], 'b': [3, 4, 4]}), pd.DataFrame({'a': [2, 3], 'b': [4, 5]}), ], None, True, True, [ {'a': 1, 'b': 3}, {'a': 2, 'b': 4}, {'a': 3, 'b': 5}, ], id='drop duplicates', ), pytest.param( [], ['a', 'b'], False, True, [], id='empty list of dataframes returns empty with columns', ), pytest.param( [ pd.DataFrame({'a': [1, 2]}, index=[10, 11]), pd.DataFrame({'a': [3]}, index=[20]), ], None, False, False, [ {'a': 1}, {'a': 2}, {'a': 3}, ], id='preserve index when ignore_index=False', ), ], ) def test_merge_dataframes(dfs, columns, drop_duplicates, ignore_index, expected_dict): """Test merge_dataframes returns dataframe.""" result = dataframes.merge_dataframes( dataframes=dfs, columns=columns, drop_duplicates=drop_duplicates, ignore_index=ignore_index ) assert result.to_dict(orient='records') == expected_dict