|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | + |
| 4 | +from data_tools import costs |
| 5 | + |
| 6 | +PID = costs.invoice.PROJECT_ID_FIELD |
| 7 | +CLUSTER = costs.invoice.CLUSTER_NAME_FIELD |
| 8 | +BALANCE = costs.invoice.BALANCE_FIELD |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def sample_invoice_dataframe() -> pd.DataFrame: |
| 13 | + return pd.DataFrame( |
| 14 | + { |
| 15 | + PID: ["vllm-test", "vllm-test", "webrca-1b021a"], |
| 16 | + CLUSTER: ["ocp-test", "ocp-test", "ocp-prod"], |
| 17 | + BALANCE: [1.234, 2.345, None], |
| 18 | + } |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def test_row_filter_empty_returns_none(): |
| 23 | + assert costs._row_filter() is None |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "filters", |
| 28 | + [ |
| 29 | + {PID: "vllm-test", CLUSTER: "ocp-test"}, |
| 30 | + {PID: "vllm-test", CLUSTER: "ocp-prod"}, |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_row_filter_builds_combined_and_expression(filters: dict[str, str]): |
| 34 | + expression = costs._row_filter(**filters) |
| 35 | + assert isinstance(expression, costs.And) |
| 36 | + assert isinstance(expression.left, costs.EqualTo) |
| 37 | + assert isinstance(expression.right, costs.EqualTo) |
| 38 | + |
| 39 | + |
| 40 | +def test_select_and_group_rounds_and_forwards_filters( |
| 41 | + monkeypatch: pytest.MonkeyPatch, sample_invoice_dataframe: pd.DataFrame |
| 42 | +): |
| 43 | + captured: dict[str, object] = {} |
| 44 | + |
| 45 | + def _fake_loader(cols=None, **filters): |
| 46 | + captured["cols"] = cols |
| 47 | + captured["filters"] = filters |
| 48 | + return sample_invoice_dataframe |
| 49 | + |
| 50 | + monkeypatch.setattr(costs, "get_invoice_dataframe", _fake_loader) |
| 51 | + |
| 52 | + result = costs.select_and_group( |
| 53 | + [BALANCE], |
| 54 | + [PID, CLUSTER], |
| 55 | + agg_col=BALANCE, |
| 56 | + agg_name="lifetime_allocation_balance", |
| 57 | + **{PID: "vllm-test"}, |
| 58 | + ) |
| 59 | + |
| 60 | + assert captured["filters"] == {PID: "vllm-test"} |
| 61 | + assert captured["cols"] == [BALANCE, PID, CLUSTER] |
| 62 | + |
| 63 | + values = sorted(result["lifetime_allocation_balance"].tolist()) |
| 64 | + assert values == [costs.Decimal("0.00"), costs.Decimal("3.58")] |
| 65 | + assert all(v.as_tuple().exponent == -2 for v in values) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "invalid_filters", |
| 70 | + [ |
| 71 | + {PID: "does-not-exist"}, |
| 72 | + {CLUSTER: "not-a-real-cluster"}, |
| 73 | + {PID: "does-not-exist", CLUSTER: "not-a-real-cluster"}, |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_calculate_lifetime_costs_invalid_queries_return_empty( |
| 77 | + monkeypatch: pytest.MonkeyPatch, invalid_filters: dict[str, str] |
| 78 | +): |
| 79 | + empty_df = pd.DataFrame(columns=[PID, CLUSTER, BALANCE]) |
| 80 | + monkeypatch.setattr(costs, "get_invoice_dataframe", lambda cols=None, **f: empty_df) |
| 81 | + |
| 82 | + result = costs.calculate_lifetime_costs(**invalid_filters) |
| 83 | + |
| 84 | + assert result.empty |
| 85 | + assert result.columns.tolist() == [PID, CLUSTER, "lifetime_allocation_balance"] |
| 86 | + |
| 87 | + |
| 88 | +class _FakeIcebergTable: |
| 89 | + """Responds to .scan().select().to_pandas() chains.""" |
| 90 | + |
| 91 | + def __init__(self, df: pd.DataFrame): |
| 92 | + self._df = df |
| 93 | + |
| 94 | + def scan(self, row_filter=None): |
| 95 | + return self |
| 96 | + |
| 97 | + def select(self, *cols): |
| 98 | + return self |
| 99 | + |
| 100 | + def to_pandas(self): |
| 101 | + return self._df |
| 102 | + |
| 103 | + |
| 104 | +def test_get_invoice_dataframe_warns_when_no_rows_match( |
| 105 | + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 106 | +): |
| 107 | + table = _FakeIcebergTable(pd.DataFrame(columns=[PID, BALANCE])) |
| 108 | + monkeypatch.setattr(costs, "_get_table", lambda: table) |
| 109 | + |
| 110 | + with caplog.at_level("WARNING", logger="data_tools.costs"): |
| 111 | + result = costs.get_invoice_dataframe([PID, BALANCE], **{PID: "does-not-exist"}) |
| 112 | + |
| 113 | + assert result.empty |
| 114 | + assert "No invoice rows matched filters" in caplog.text |
0 commit comments