Motorola's device telemetry team owns this function, which parses a raw signal report string coming off a handset (device id followed by comma-separated dBm samples, for example "MOTO-42,-80,-95,-70,-120") into a summary the dashboard displays.
def parse_signal_report(raw):
parts = raw.split(",")
device_id = parts[0]
samples = [int(x) for x in parts[1:]]
average = sum(samples) / len(samples)
peak = max(samples)
dropout_ratio = len([s for s in samples if s < -100]) / len(samples)
return {
"device_id": device_id,
"average_dbm": round(average),
"peak_dbm": peak,
"dropout_ratio": dropout_ratio,
}
How would you test this function in Python? Describe the specific test cases you would write, including edge cases, and explain what each one would catch.
Write focused unit tests that verify the complete returned dictionary, including exact dropout thresholds, rounding, device ID preservation, and expected exceptions for invalid reports.
def test_signal_report_case(raw, expected):