1+ import pandas as pd
2+ import pytest
3+ import matplotlib
4+ matplotlib .use ("Agg" ) # non-interactive backend for testing
5+ from utils import clean_mta_df , plot_ridership_recovery
6+
7+ def test_clean_mta_df_converts_date_and_sorts ():
8+ df = pd .DataFrame ({
9+ "date" : ["2020-01-02" , "2020-01-01" ],
10+ "x" : [2 , 1 ],
11+ })
12+
13+ out = clean_mta_df (df )
14+
15+ assert str (out ["date" ].dtype ).startswith ("datetime64" )
16+
17+ assert out ["date" ].is_monotonic_increasing
18+
19+ assert list (out ["x" ]) == [1 , 2 ]
20+
21+ def test_clean_mta_df_missing_date_raises ():
22+ df = pd .DataFrame ({"x" : [1 , 2 ]})
23+ with pytest .raises (KeyError ):
24+ clean_mta_df (df )
25+
26+ def test_clean_mta_df_does_not_modify_original ():
27+ """Test that the original DataFrame is not mutated."""
28+ df = pd .DataFrame ({
29+ "date" : ["2020-01-02" , "2020-01-01" ],
30+ "x" : [2 , 1 ],
31+ })
32+ original_dates = list (df ["date" ])
33+
34+ clean_mta_df (df )
35+
36+ # original df should remain unchanged
37+ assert list (df ["date" ]) == original_dates
38+
39+
40+ def test_clean_mta_df_already_sorted ():
41+ """Test that already-sorted data passes through correctly."""
42+ df = pd .DataFrame ({
43+ "date" : ["2020-01-01" , "2020-01-02" , "2020-01-03" ],
44+ "x" : [1 , 2 , 3 ],
45+ })
46+
47+ out = clean_mta_df (df )
48+
49+ assert out ["date" ].is_monotonic_increasing
50+ assert list (out ["x" ]) == [1 , 2 , 3 ]
51+
52+
53+ # ---------- Tests for plot_ridership_recovery ----------
54+
55+ def _make_ridership_df ():
56+ """Helper: create a small valid ridership DataFrame for testing."""
57+ return pd .DataFrame ({
58+ "date" : pd .to_datetime (["2020-03-01" , "2020-03-02" , "2020-03-03" ]),
59+ "subways_of_comparable_pre_pandemic_day" : [0.9 , 0.5 , 0.6 ],
60+ "buses_of_comparable_pre_pandemic_day" : [0.95 , 0.6 , 0.7 ],
61+ "lirr_of_comparable_pre_pandemic_day" : [0.85 , 0.4 , 0.5 ],
62+ "metro_north_of_comparable_pre_pandemic_day" : [0.88 , 0.45 , 0.55 ],
63+ })
64+
65+
66+ def test_plot_ridership_recovery_returns_figure ():
67+ """Test that the function returns a matplotlib Figure without error."""
68+ df = _make_ridership_df ()
69+ fig = plot_ridership_recovery (df )
70+ assert isinstance (fig , matplotlib .figure .Figure )
71+ matplotlib .pyplot .close (fig )
72+
73+
74+ def test_plot_ridership_recovery_missing_column_raises ():
75+ """Test that KeyError is raised when a required column is missing."""
76+ df = pd .DataFrame ({
77+ "date" : pd .to_datetime (["2020-03-01" ]),
78+ "subways_of_comparable_pre_pandemic_day" : [0.9 ],
79+ })
80+ with pytest .raises (KeyError ):
81+ plot_ridership_recovery (df )
0 commit comments