Skip to content

test: RFMTransformer.fit never exercises the ndarray (non-DataFrame) input branch #49

Description

@shivamlalakiya

What's wrong

RFMTransformer.fit branches on whether X has a .columns attribute. Every test in tests/test_rfm.py fits a pandas.DataFrame, so the bare-ndarray branch (the one that synthesizes feature_names_in_ as ["x0", "x1", ...]) never runs. pytest --cov-report=term-missing shows it uncovered today.

Because RFMTransformer requires the columns donor_id, gift_date, gift_amount by name, fitting it on an unlabelled ndarray can never supply those names — so the correct, already-implemented behaviour is that it raises a clear ValueError naming the missing columns instead of failing later with a confusing KeyError. That behaviour is currently unverified.

Where

File Locate with
philanthropy/preprocessing/_rfm.py grep -n "X_arr = np.asarray(X)" philanthropy/preprocessing/_rfm.py

What to change

No source change — this is test-only.

  1. Open tests/test_rfm.py and look at test_rfm_transformer_validation (locate with grep -n "def test_rfm_transformer_validation" tests/test_rfm.py) for the existing style of asserting on _validate_input's error.
  2. Add a new test after it that calls RFMTransformer().fit(np.ones((3, 3))) (a bare ndarray, no column labels) and asserts it raises ValueError naming the required columns. tests/test_rfm.py doesn't import numpy yet — add import numpy as np at the top alongside the existing pandas/pytest imports.

Tests to add or extend

  • File: tests/test_rfm.py
  • Add: test_rfm_transformer_ndarray_input_raises_missing_columns
def test_rfm_transformer_ndarray_input_raises_missing_columns():
    with pytest.raises(ValueError, match="donor_id"):
        RFMTransformer().fit(np.ones((3, 3)))

(Check the top of the file for the existing numpy import name before adding the test.)

Done when

python -m pytest tests/test_rfm.py --cov=philanthropy.preprocessing._rfm -q
python -m coverage report --include='philanthropy/preprocessing/_rfm.py' --fail-under=100
make ci
make riskcov

The coverage report --fail-under=100 line exits non-zero until _rfm.py reads fully covered; make ci and make riskcov must also stay green.

First time here?

Read CONTRIBUTING.md and AGENTS.md. Add a ## [Unreleased] CHANGELOG entry and yourself to CONTRIBUTORS.md in the same PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    beginnerNo fundraising domain knowledge neededeasySmall, well-scoped, single-file changegood first issueGood for newcomershelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions