From 3ebc44a85e369fed71f206e769c5872272108e19 Mon Sep 17 00:00:00 2001 From: Sam Firke Date: Mon, 26 Aug 2024 20:14:19 +0000 Subject: [PATCH] add tests for scaling function --- pyproject.toml | 13 ++++++++++++- tests/__init__.py | 1 + tests/test_scaling.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_scaling.py diff --git a/pyproject.toml b/pyproject.toml index fbac55c..1cb4f4e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,12 @@ classifiers = [ ] dependencies = [ "requests>=2.0.0", + "pytest>=6.0.0", +] + +[tool.pytest.ini_options] +pythonpath = [ + ".", "src", ] [project.urls] @@ -22,4 +28,9 @@ Issues = "https://github.com/a2gov/clarityio/issues" [build-system] requires = ["hatchling"] -build-backend = "hatchling.build" \ No newline at end of file +build-backend = "hatchling.build" + +[tool.hatch.envs.test] +dependencies = [ + "pytest>=6.0.0", +] \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..4f54c83 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# tests/__init__.py \ No newline at end of file diff --git a/tests/test_scaling.py b/tests/test_scaling.py new file mode 100644 index 0000000..a2163e7 --- /dev/null +++ b/tests/test_scaling.py @@ -0,0 +1,30 @@ +import pytest +import pandas as pd +import numpy as np +from clarityio import scale_raw_to_aqi + +def test_scale_raw_values(): + assert scale_raw_to_aqi('pm2.5_24hr', 18.84) == 69.14676806083651 + assert scale_raw_to_aqi('nitrogen_dioxide_1hr', 300) == 138.64864864864865 + +def test_scale_pandas_series(): + data = pd.Series([5, 9, 30, 60]) + expected = pd.Series([27.777778, 50.0, 89.939163, 154.154506], dtype='float64') + result = scale_raw_to_aqi('pm2.5_24hr', data) + pd.testing.assert_series_equal(result, expected) + +def test_missing_inputs(): + assert np.isnan(scale_raw_to_aqi('sulfur_dioxide_1hr', None)) + assert np.isnan(scale_raw_to_aqi('ozone_8hr', np.nan)) + +def test_invalid_pollutant_name(): + with pytest.raises(ValueError, match="Unknown pollutant: invalid_pollutant"): + scale_raw_to_aqi('invalid_pollutant', 999) + +def test_negative_raw_value(): + with pytest.raises(ValueError, match="Negative pollutant values are invalid."): + assert scale_raw_to_aqi('carbon_monoxide_8hr', -0.010) == 0 + +def test_raw_value_exceeding_scale(): + with pytest.warns(UserWarning, match="Pollutant value 99999 is off the scale for pm10_24hr, returning maximum scaled value of 500."): + assert scale_raw_to_aqi('pm10_24hr', 99999) == 500 \ No newline at end of file