Files
api-go/internal/store/store_test.go
T

66 lines
1.7 KiB
Go

package store
import (
"testing"
)
func TestLoadFixtures(t *testing.T) {
st, err := Load("../../testdata/data")
if err != nil {
t.Fatalf("Load: %v", err)
}
if !st.Ready() {
t.Fatal("Ready()=false for the fixture dataset")
}
if st.Len() != 8 {
t.Fatalf("Len()=%d, want 8", st.Len())
}
sensors := st.Sensors()
for i := 1; i < len(sensors); i++ {
if sensors[i-1].ID >= sensors[i].ID {
t.Fatalf("sensors not sorted: %q then %q", sensors[i-1].ID, sensors[i].ID)
}
}
if _, ok := st.Sensor("hn-bahnhof-01"); !ok {
t.Fatal("missing hn-bahnhof-01")
}
if _, ok := st.Sensor("nope"); ok {
t.Fatal("unexpected sensor nope")
}
if points, ok := st.Series("hn-bahnhof-01"); !ok || len(points) == 0 {
t.Fatalf("series: ok=%v points=%d", ok, len(points))
}
if points, ok := st.Forecast("hn-bahnhof-01", "sunny-saturday-market", "7d"); !ok || len(points) != 168 {
t.Fatalf("forecast: ok=%v points=%d", ok, len(points))
}
if _, ok := st.Forecast("hn-bahnhof-01", "base", "2h"); ok {
t.Fatal("unexpected horizon 2h")
}
if ctx := st.Context(); ctx.UpdatedAt.IsZero() || len(ctx.Weather.Forecast) == 0 || len(ctx.Holidays) == 0 {
t.Fatalf("context incomplete: %+v", ctx)
}
data, modelVersion, context := st.Versions()
if data == "" || modelVersion == "" || context == "" {
t.Fatalf("versions: data=%q model=%q context=%q", data, modelVersion, context)
}
}
func TestLoadMissingDirectoryIsPartial(t *testing.T) {
st, err := Load(t.TempDir())
if err == nil {
t.Fatal("expected an error for an empty data dir")
}
if st == nil {
t.Fatal("Load returned a nil store; handlers need a usable store")
}
if st.Ready() {
t.Fatal("Ready()=true without data")
}
if st.Len() != 0 {
t.Fatalf("Len()=%d, want 0", st.Len())
}
}