evcc-io/tariff/fixed_test.go
2026-05-16 14:18:33 +02:00

106 lines
2.3 KiB
Go

package tariff
import (
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/tariff/fixed"
"github.com/jinzhu/now"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFixed(t *testing.T) {
tf := &Fixed{
embed: &embed{},
clock: clock.NewMock(),
zones: []fixed.Zone{
{Price: 0.3},
},
}
var expect api.Rates
for dow := range 7 {
dayStart := now.With(tf.clock.Now()).BeginningOfDay().AddDate(0, 0, dow)
for hour := range 24 {
expect = append(expect, api.Rate{
Value: 0.3,
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
}
rates, err := tf.Rates()
require.NoError(t, err)
assert.Equal(t, expect, rates)
}
func TestFixedSplitZones(t *testing.T) {
at, err := NewFixedFromConfig(map[string]any{
"price": 0.5,
"zones": []struct {
Price float64
Hours string
}{
{0.1, "0-5:30,20-21,21-0"},
},
})
require.NoError(t, err)
tf := at.(*Fixed)
tf.clock = clock.NewMock()
var expect api.Rates
for i := range 7 {
dayStart := now.With(tf.clock.Now()).BeginningOfDay().AddDate(0, 0, i)
// 00:00-05:00 0.1
for hour := range 5 {
expect = append(expect, api.Rate{
Value: 0.1,
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
// 05:00-05:30 0.1
expect = append(expect, api.Rate{
Value: 0.1,
Start: dayStart.Add(5 * time.Hour),
End: dayStart.Add(5*time.Hour + 30*time.Minute),
})
// 05:30-06:00 0.5
expect = append(expect, api.Rate{
Value: 0.5,
Start: dayStart.Add(5*time.Hour + 30*time.Minute),
End: dayStart.Add(6 * time.Hour),
})
// 06:00-20:00 0.5
for hour := 6; hour < 20; hour++ {
expect = append(expect, api.Rate{
Value: 0.5,
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
// 20:00-21:00,21:00-00:00 0.1
for hour := 20; hour < 24; hour++ {
expect = append(expect, api.Rate{
Value: 0.1,
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
}
rates, err := tf.Rates()
require.NoError(t, err)
assert.Equal(t, expect, rates)
}