def test_warning_in_weighted_average_results_preprocessors(caplog):
    import logging
    from copy import deepcopy

    caplog.at_level(logging.WARNING)

    results1 = [{"a": 1}, {"a": 2}, {"a": 3}, {"a": 4}]
    results2 = [{"b": 1}, {"b": 2}, {"b": 3}, {"b": 4}]
    results3 = [
        {"a": 1, "c": 3},
        {"a": 2, "c": "invalid"},
        {"a": "invalid", "c": 1},
        {"a": 4, "c": "invalid"},
    ]
    results4 = [
        {"a": 1, "c": "invalid"},
        {"a": 2, "c": "invalid"},
        {"a": 3, "c": "invalid"},
        {"a": 4, "c": "invalid"},
    ]

    # test case 1: weight key `b` is not reported from all workers
    results_preprocessor1 = WeightedAverageResultsPreprocessor(["a"], "b")
    expected1 = deepcopy(results1)
    for res in expected1:
        res.update({"weight_avg_b(a)": 2.5})
    assert results_preprocessor1.preprocess(results1) == expected1
    assert (
        "Averaging weight `b` is not reported by all workers in `train.report()`."
        in caplog.text
    )
    assert "Use equal weight instead." in caplog.text

    # test case 2: metric key `a` (to be averaged) is not reported from all workers
    results_preprocessor1.preprocess(results2)
    assert "`a` is not reported from workers, so it is ignored." in caplog.text

    # test case 3: both metric and weight keys have invalid data type
    results_preprocessor2 = WeightedAverageResultsPreprocessor(["a"], "c")
    expected3 = deepcopy(results3)
    for res in expected3:
        res.update({"weight_avg_c(a)": 1.0})
    assert results_preprocessor2.preprocess(results3) == expected3

    # test case 4: all weight values are invalid
    expected4 = deepcopy(results4)
    for res in expected4:
        res.update({"weight_avg_c(a)": 2.5})
    assert results_preprocessor2.preprocess(results4) == expected4
    assert "Averaging weight `c` value type is not valid." in caplog.text

    for record in caplog.records:
        assert record.levelname == "WARNING"