Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tariff/fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,52 @@ func TestFixedSplitZones(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, expect, rates)
}

func TestFixedMonthsSorting(t *testing.T) {
at, err := NewFixedFromConfig(map[string]any{
"zones": []struct {
Price float64
Hours string
Months string
}{
{0.1, "0-5", ""}, // all year
{0.2, "5-0", ""}, // all year
{0.3, "2-4", "Jun"}, // Jun only
{0.4, "18-20", "Jun"}, // Jun only
// TODO: specific days
},
})
require.NoError(t, err)

tc := []struct {
m, d, h int
rate float64
}{
// all year
{0, 0, 0, 0.1},
{0, 0, 2, 0.1},
{0, 0, 5, 0.2},
{0, 0, 18, 0.2},

// Jun only
{6, 0, 0, 0.1},
{6, 0, 2, 0.3},
{6, 0, 5, 0.2},
{6, 0, 18, 0.4},
}

for _, tc := range tc {
clock := clock.NewMock()
at.(*Fixed).clock = clock

clock.Set(time.Date(2025, time.Month(tc.m), tc.d, 0, 0, 0, 0, time.UTC))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clock.Set(time.Date(2025, time.Month(tc.m), tc.d, 0, 0, 0, 0, time.UTC))
clock.Set(time.Date(2025, time.Month(tc.m), tc.d, 0, 0, 0, 0, time.Local))

richtig?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Muss einfach die gleiche Zeitzone sein wie die mack clock. Am besten von dort nehmen?

Copy link
Contributor

@iseeberg79 iseeberg79 Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

die Raten arbeiten wohl in Time.Local - dann wäre das richtig; bzw. auch die mock clock; soweit ich das finden konnte, wäre UTC hier falsch.

Wenn du also keinen konkreten Grund hast für UTC hier, nehmen wir besser Local.

Die Zeiten würden ansonsten "springen"... - hab einen Ansatz -> "wip"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…oder wir sorgen dafür, dass die Raten in der TZ der clock arbeiten, die dann normalerweise local sein sollte.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wird explizit eine "Reihenfolge" in der Spezifizierung der Tarife erwartet?
Hintergrund: wenn nein, braucht es eine Sortierung, die das berücksichtigt.

erlaubt:

  zones:
    - hours: 0-5, price: 0.20
    - hours: 0-5, months: Oct-Dec, price: 0.10
    - hours: 0-5, days: Mon-Fri, price: 0.15
    - hours: 0-5, months: Oct-Dec, days: Mon-Fri, price: 0.05
  zones:
    - hours: 0-5, price: 0.20
    - hours: 0-5, months: Oct-Dec, price: 0.22

nicht erlaubt (spezifischere Angabe zuerst):

  zones:
    - hours: 0-5, months: Oct-Dec, price: 0.22
    - hours: 0-5, price: 0.20

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Angabe ist egal. Es sollte intern nach Spezifität sortiert werden.


rr, err := at.Rates()
require.NoError(t, err)

r, err := rr.At(clock.Now().Add(time.Duration(tc.h) * time.Hour))
require.NoError(t, err)

require.Equal(t, tc.rate, r.Value)
}
}
Loading