From 1b76fc9127efe7de3c13e386a930b20234002f6f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 28 Jun 2026 15:03:00 +0000 Subject: [PATCH] Add test for CDCSodaExtractor.save_to_csv Co-authored-by: Data-Science-Link <61164085+Data-Science-Link@users.noreply.github.com> --- .../cdc_api/test_soda_extractor.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 data_engineering/data_sources/cdc_api/test_soda_extractor.py diff --git a/data_engineering/data_sources/cdc_api/test_soda_extractor.py b/data_engineering/data_sources/cdc_api/test_soda_extractor.py new file mode 100644 index 0000000..55961a8 --- /dev/null +++ b/data_engineering/data_sources/cdc_api/test_soda_extractor.py @@ -0,0 +1,37 @@ +import pandas as pd +from pathlib import Path +from data_engineering.data_sources.cdc_api.soda_extractor import CDCSodaExtractor + +def test_save_to_csv(tmp_path: Path): + """Test saving a DataFrame to a CSV file.""" + extractor = CDCSodaExtractor() + + # Create a dummy DataFrame + df = pd.DataFrame({ + "indicator": ["Synthetic opioids, excl. methadone (T40.4)", "Synthetic opioids, excl. methadone (T40.4)"], + "year": [2021, 2022], + "deaths": [1000, 1200] + }) + + # Define a test output path inside a non-existent subdirectory + # to test parent directory creation + output_path = tmp_path / "test_dir" / "test_output.csv" + + # Save to CSV + extractor.save_to_csv(df, output_path) + + # Assert the file was created + assert output_path.exists() + assert output_path.is_file() + + # Read back the saved CSV and verify its contents + saved_df = pd.read_csv(output_path) + + # Verify shape and columns + assert len(saved_df) == 2 + assert list(saved_df.columns) == ["indicator", "year", "deaths"] + + # Verify data types and values + assert saved_df["year"].tolist() == [2021, 2022] + assert saved_df["deaths"].tolist() == [1000, 1200] + assert saved_df["indicator"].tolist() == ["Synthetic opioids, excl. methadone (T40.4)", "Synthetic opioids, excl. methadone (T40.4)"]