You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
1.1 KiB

package sensor
import "testing"
func TestParsePower(t *testing.T) {
for _, tt := range []struct {
name string
payload string
power float64
wantErr bool
}{
{"tasmota", `{"Time":"2026-10-12T12:00:00","ENERGY":{"Total":12.5,"Voltage":230,"Power":139.60}}`, 139.6, false},
{"zero", `{"ENERGY":{"Power":0}}`, 0, false},
{"threshold", `{"ENERGY":{"Power":3.00}}`, 3, false},
{"missing energy", `{}`, 0, true},
{"missing power", `{"ENERGY":{"Voltage":230}}`, 0, true},
{"null energy", `{"ENERGY":null}`, 0, true},
{"null power", `{"ENERGY":{"Power":null}}`, 0, true},
{"string power", `{"ENERGY":{"Power":"139.60"}}`, 0, true},
{"overflow", `{"ENERGY":{"Power":1e1000}}`, 0, true},
{"invalid json", `{"ENERGY":`, 0, true},
{"trailing json", `{"ENERGY":{"Power":1}} {}`, 0, true},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := ParsePower([]byte(tt.payload))
if (err != nil) != tt.wantErr {
t.Fatalf("ParsePower() error = %v; want error %v", err, tt.wantErr)
}
if !tt.wantErr && got != tt.power {
t.Errorf("ParsePower() = %v; want %v", got, tt.power)
}
})
}
}