Tariff: base cache validity on update interval instead of rate coverage (#32168)

This commit is contained in:
andig 2026-07-26 13:39:59 +02:00 • committed by GitHub
parent 60f313cbfb
commit be81052667
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 85 additions and 45 deletions

View file

@ -0,0 +1,29 @@
package tariff
import (
"testing"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/server/db"
"github.com/stretchr/testify/require"
)
func TestCacheInterval(t *testing.T) {
require.NoError(t, db.NewInstance("sqlite", ":memory:"))
p := &cachingProxy{key: "test", interval: time.Hour}
require.NoError(t, p.cachePut(api.TariffTypeSolar, api.Rates{
{Start: time.Now(), End: time.Now().Add(time.Hour), Value: 1},
}))
// within interval
res, err := p.cacheGet()
require.NoError(t, err)
require.Equal(t, api.TariffTypeSolar, res.Type)
// interval elapsed
p.cached.Updated = time.Now().Add(-2 * time.Hour)
_, err = p.cacheGet()
require.Error(t, err)
}