Skip to content

Commit d797437

Browse files
Update test_tools.py
Defined `test_file_export_function` test in test_tools.py for testing the `export_to_file` function in `common/tools.py`. Defined additional constants to ensure proper working of the same.
1 parent 7171cba commit d797437

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/test_tools.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
TEST_DIRECTORY = Path(__file__).parent / "test_directory"
1212
FILE_DIR_TEST_DIRECTORY = TEST_DIRECTORY / "file_dir"
1313

14+
PANDAS_READ_METHODS_MAP = {
15+
".csv": "read_csv", ".xlsx": "read_excel",
16+
".html": "read_html", ".json": "read_json",
17+
}
18+
1419
# Test parameters for individual functions defined below
1520

1621
PARSE_QUERY_FUNC_PARAMS = [
@@ -32,6 +37,16 @@
3237
(3, FILE_DIR_TEST_DIRECTORY / "reports", True),
3338
]
3439

40+
EXPORT_FILE_TEST_PARAMS = [
41+
"export.csv", "output.xlsx", "records.html", "save.json"
42+
]
43+
44+
# Sample dataframe for testing `tools.export_to_file` function.
45+
46+
SAMPLE_EXPORT_FILE_DATA = pd.DataFrame(
47+
{2023: [87, 95, 98, 82, 84], 2024: [91, 93, 98, 87, 81]}
48+
)
49+
3550
# Test results for individual functions defined below
3651

3752
PARSE_QUERY_TEST_RESULTS = [
@@ -104,3 +119,17 @@ def test_get_directories_function(ctr: int, path: Path, recur: bool) -> None:
104119
tools.get_directories(path, recur),
105120
read_tests_hdf(f"/function/get_directories/test{ctr}"),
106121
)
122+
123+
124+
@pytest.mark.parametrize("file", EXPORT_FILE_TEST_PARAMS)
125+
def test_file_export_function(
126+
file: str, data: pd.DataFrame = SAMPLE_EXPORT_FILE_DATA
127+
) -> None:
128+
"""Tests the `tools.export_to_file` function with different file formats."""
129+
130+
path: Path = TEST_DIRECTORY / file
131+
132+
tools.export_to_file(data, path)
133+
assert path.is_file()
134+
135+
path.unlink()

0 commit comments

Comments
 (0)