|
| 1 | +"""Tests for the SegTransactionStats class.""" |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pyretailscience.segmentation import SegTransactionStats |
| 7 | + |
| 8 | + |
| 9 | +class TestCalcSegStats: |
| 10 | + """Tests for the _calc_seg_stats method.""" |
| 11 | + |
| 12 | + @pytest.fixture() |
| 13 | + def base_df(self): |
| 14 | + """Return a base DataFrame for testing.""" |
| 15 | + return pd.DataFrame( |
| 16 | + { |
| 17 | + "customer_id": [1, 2, 3, 4, 5], |
| 18 | + "total_price": [100, 200, 150, 300, 250], |
| 19 | + "transaction_id": [101, 102, 103, 104, 105], |
| 20 | + "segment_id": ["A", "B", "A", "B", "A"], |
| 21 | + "quantity": [10, 20, 15, 30, 25], |
| 22 | + }, |
| 23 | + ) |
| 24 | + |
| 25 | + def test_correctly_calculates_revenue_transactions_customers_per_segment(self, base_df): |
| 26 | + """Test that the method correctly calculates at the transaction-item level.""" |
| 27 | + expected_output = pd.DataFrame( |
| 28 | + { |
| 29 | + "revenue": [500, 500, 1000], |
| 30 | + "transactions": [3, 2, 5], |
| 31 | + "customers": [3, 2, 5], |
| 32 | + "total_quantity": [50, 50, 100], |
| 33 | + "price_per_unit": [10.0, 10.0, 10.0], |
| 34 | + "quantity_per_transaction": [16.666667, 25.0, 20.0], |
| 35 | + }, |
| 36 | + index=["A", "B", "total"], |
| 37 | + ) |
| 38 | + |
| 39 | + segment_stats = SegTransactionStats._calc_seg_stats(base_df, "segment_id") |
| 40 | + pd.testing.assert_frame_equal(segment_stats, expected_output) |
| 41 | + |
| 42 | + def test_correctly_calculates_revenue_transactions_customers(self): |
| 43 | + """Test that the method correctly calculates at the transaction level.""" |
| 44 | + df = pd.DataFrame( |
| 45 | + { |
| 46 | + "customer_id": [1, 2, 3, 4, 5], |
| 47 | + "total_price": [100, 200, 150, 300, 250], |
| 48 | + "transaction_id": [101, 102, 103, 104, 105], |
| 49 | + "segment_id": ["A", "B", "A", "B", "A"], |
| 50 | + }, |
| 51 | + ) |
| 52 | + |
| 53 | + expected_output = pd.DataFrame( |
| 54 | + { |
| 55 | + "revenue": [500, 500, 1000], |
| 56 | + "transactions": [3, 2, 5], |
| 57 | + "customers": [3, 2, 5], |
| 58 | + }, |
| 59 | + index=["A", "B", "total"], |
| 60 | + ) |
| 61 | + |
| 62 | + segment_stats = SegTransactionStats._calc_seg_stats(df, "segment_id") |
| 63 | + pd.testing.assert_frame_equal(segment_stats, expected_output) |
| 64 | + |
| 65 | + def test_does_not_alter_original_dataframe(self, base_df): |
| 66 | + """Test that the method does not alter the original DataFrame.""" |
| 67 | + original_df = base_df.copy() |
| 68 | + _ = SegTransactionStats._calc_seg_stats(base_df, "segment_id") |
| 69 | + |
| 70 | + pd.testing.assert_frame_equal(base_df, original_df) |
| 71 | + |
| 72 | + def test_handles_dataframe_with_one_segment(self, base_df): |
| 73 | + """Test that the method correctly handles a DataFrame with only one segment.""" |
| 74 | + df = base_df.copy() |
| 75 | + df["segment_id"] = "A" |
| 76 | + |
| 77 | + expected_output = pd.DataFrame( |
| 78 | + { |
| 79 | + "revenue": [1000, 1000], |
| 80 | + "transactions": [5, 5], |
| 81 | + "customers": [5, 5], |
| 82 | + "total_quantity": [100, 100], |
| 83 | + "price_per_unit": [10.0, 10.0], |
| 84 | + "quantity_per_transaction": [20.0, 20.0], |
| 85 | + }, |
| 86 | + index=["A", "total"], |
| 87 | + ) |
| 88 | + |
| 89 | + segment_stats = SegTransactionStats._calc_seg_stats(df, "segment_id") |
| 90 | + pd.testing.assert_frame_equal(segment_stats, expected_output) |
| 91 | + |
| 92 | + |
| 93 | +class TestSegTransactionStats: |
| 94 | + """Tests for the SegTransactionStats class.""" |
| 95 | + |
| 96 | + def test_handles_empty_dataframe_with_errors(self): |
| 97 | + """Test that the method raises an error when the DataFrame is missing a required column.""" |
| 98 | + df = pd.DataFrame(columns=["total_price", "transaction_id", "segment_id", "quantity"]) |
| 99 | + |
| 100 | + with pytest.raises(ValueError): |
| 101 | + SegTransactionStats(df, "segment_id") |
0 commit comments