Tariff: fix chargesZones boundaries not taking effect between price zones (#32143)
This commit is contained in:
parent
75eebf131b
commit
e867ef60cf
2 changed files with 32 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ package tariff
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -77,7 +78,8 @@ func (t *Fixed) Rates() (api.Rates, error) {
|
||||||
return nil, fmt.Errorf("no zones for weekday %d", dow)
|
return nil, fmt.Errorf("no zones for weekday %d", dow)
|
||||||
}
|
}
|
||||||
|
|
||||||
markers := zones.TimeTableMarkers()
|
// include chargesZones boundaries so rate changes there are not swallowed by the coarser price zone markers
|
||||||
|
markers := append(slices.Clone(zones), t.chargesZones.ForDayAndMonth(dow, month)...).TimeTableMarkers()
|
||||||
|
|
||||||
for i, m := range markers {
|
for i, m := range markers {
|
||||||
ts := dayStart.Add(time.Minute * time.Duration(m.Minutes()))
|
ts := dayStart.Add(time.Minute * time.Duration(m.Minutes()))
|
||||||
|
|
|
||||||
|
|
@ -104,3 +104,32 @@ func TestFixedSplitZones(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expect, rates)
|
assert.Equal(t, expect, rates)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFixedChargesZonesMarkers(t *testing.T) {
|
||||||
|
at, err := NewFixedFromConfig(map[string]any{
|
||||||
|
"price": 0.5,
|
||||||
|
"chargesZones": []struct {
|
||||||
|
Charges float64
|
||||||
|
Hours string
|
||||||
|
}{
|
||||||
|
{0.1, "0-5:30"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
tf := at.(*Fixed)
|
||||||
|
tf.clock = clock.NewMock()
|
||||||
|
|
||||||
|
rates, err := tf.Rates()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
dayStart := now.With(tf.clock.Now()).BeginningOfDay()
|
||||||
|
|
||||||
|
// charges zone ends mid-hour at 05:30 - the price change must take effect there, not at 06:00
|
||||||
|
require.GreaterOrEqual(t, len(rates), 2)
|
||||||
|
assert.Equal(t, dayStart.Add(5*time.Hour), rates[5].Start)
|
||||||
|
assert.Equal(t, dayStart.Add(5*time.Hour+30*time.Minute), rates[5].End)
|
||||||
|
assert.Equal(t, 0.6, rates[5].Value)
|
||||||
|
assert.Equal(t, dayStart.Add(5*time.Hour+30*time.Minute), rates[6].Start)
|
||||||
|
assert.Equal(t, 0.5, rates[6].Value)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue