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.
 
 
 

43 lines
1.5 KiB

package main
import (
"testing"
"time"
)
func TestLoadConfigDefaults(t *testing.T) {
for _, key := range []string{"MQTT_BROKER", "MQTT_TOPIC", "MQTT_USERNAME", "MQTT_PASSWORD", "HTTP_ADDR", "SAMPLE_INTERVAL"} {
t.Setenv(key, "")
}
got, err := loadConfig()
if err != nil {
t.Fatal(err)
}
if got.Broker != "tcp://192.168.1.11:1883" || got.Topic != "tele/tasmota_washingmashine_C21183/SENSOR" || got.HTTPAddr != ":8080" || got.SampleInterval != time.Minute || got.Username != "" || got.Password != "" {
t.Fatalf("defaults: %+v", got)
}
}
func TestLoadConfigOverrides(t *testing.T) {
for key, value := range map[string]string{"MQTT_BROKER": "tcp://localhost:1884", "MQTT_TOPIC": "test/power", "MQTT_USERNAME": "tester", "MQTT_PASSWORD": "test-password", "HTTP_ADDR": "127.0.0.1:9090", "SAMPLE_INTERVAL": "250ms"} {
t.Setenv(key, value)
}
got, err := loadConfig()
if err != nil {
t.Fatal(err)
}
if got.Broker != "tcp://localhost:1884" || got.Topic != "test/power" || got.Username != "tester" || got.Password != "test-password" || got.HTTPAddr != "127.0.0.1:9090" || got.SampleInterval != 250*time.Millisecond {
t.Fatal("environment overrides not applied")
}
}
func TestLoadConfigRejectsInvalidInterval(t *testing.T) {
for _, interval := range []string{"nonsense", "0s", "-1s", "60"} {
t.Run(interval, func(t *testing.T) {
t.Setenv("SAMPLE_INTERVAL", interval)
if _, err := loadConfig(); err == nil {
t.Fatalf("accepted SAMPLE_INTERVAL=%q", interval)
}
})
}
}