-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1359 from openmeterio/feat/notification-handler
feat: notification handler
- Loading branch information
Showing
12 changed files
with
1,129 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
internal/notification/consumer/balancethreshold_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package consumer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/openmeterio/openmeter/api" | ||
"github.com/openmeterio/openmeter/internal/entitlement/snapshot" | ||
"github.com/openmeterio/openmeter/internal/notification" | ||
"github.com/openmeterio/openmeter/pkg/convert" | ||
) | ||
|
||
func newNumericThreshold(v float64) notification.BalanceThreshold { | ||
return notification.BalanceThreshold{ | ||
Value: v, | ||
Type: api.NUMBER, | ||
} | ||
} | ||
|
||
func newPercentThreshold(v float64) notification.BalanceThreshold { | ||
return notification.BalanceThreshold{ | ||
Value: v, | ||
Type: api.PERCENT, | ||
} | ||
} | ||
|
||
func TestGetHighestMatchingBalanceThreshold(t *testing.T) { | ||
tcs := []struct { | ||
Name string | ||
BalanceThresholds []notification.BalanceThreshold | ||
EntitlementValue snapshot.EntitlementValue | ||
Expect *notification.BalanceThreshold | ||
}{ | ||
{ | ||
Name: "Numerical values only", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(20), | ||
newNumericThreshold(10), | ||
newNumericThreshold(30), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(10.0), | ||
Usage: convert.ToPointer(20.0), | ||
}, | ||
// Already used 20, so the matching threshold is the 20 | ||
Expect: convert.ToPointer(newNumericThreshold(20)), | ||
}, | ||
{ | ||
Name: "Numerical values only - 100%", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(20), | ||
newNumericThreshold(10), | ||
newNumericThreshold(30), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(0.0), | ||
Usage: convert.ToPointer(30.0), | ||
}, | ||
Expect: convert.ToPointer(newNumericThreshold(30)), | ||
}, | ||
{ | ||
Name: "Numerical values only - 100%+ with overage", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(20), | ||
newNumericThreshold(10), | ||
newNumericThreshold(30), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(0.0), | ||
Usage: convert.ToPointer(30.0), | ||
Overage: convert.ToPointer(10.0), | ||
}, | ||
Expect: convert.ToPointer(newNumericThreshold(30)), | ||
}, | ||
{ | ||
Name: "Percentages with overage", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newPercentThreshold(50), | ||
newPercentThreshold(100), | ||
newPercentThreshold(110), | ||
newPercentThreshold(120), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(0.0), | ||
Usage: convert.ToPointer(110.0), | ||
Overage: convert.ToPointer(10.0), | ||
}, | ||
Expect: convert.ToPointer(newPercentThreshold(110)), | ||
}, | ||
{ | ||
Name: "Mixed values", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(20), | ||
newNumericThreshold(10), | ||
newNumericThreshold(30), | ||
newPercentThreshold(50), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(14.0), | ||
Usage: convert.ToPointer(16.0), | ||
}, | ||
Expect: convert.ToPointer(newPercentThreshold(50)), | ||
}, | ||
// Corner cases | ||
{ | ||
Name: "No grants", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(20), | ||
newPercentThreshold(100), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(0.0), | ||
Usage: convert.ToPointer(0.0), | ||
}, | ||
Expect: nil, | ||
}, | ||
{ | ||
Name: "Last threshold is ", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(20), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(0.0), | ||
Usage: convert.ToPointer(30.0), | ||
}, | ||
Expect: convert.ToPointer(newNumericThreshold(20)), | ||
}, | ||
{ | ||
Name: "Same threshold in percentage and number", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(15), | ||
newPercentThreshold(50), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(14.0), | ||
Usage: convert.ToPointer(16.0), | ||
}, | ||
Expect: convert.ToPointer(newPercentThreshold(50)), | ||
}, | ||
{ | ||
Name: "Exact threshold match", | ||
BalanceThresholds: []notification.BalanceThreshold{ | ||
newNumericThreshold(15), | ||
newPercentThreshold(50), | ||
}, | ||
EntitlementValue: snapshot.EntitlementValue{ | ||
Balance: convert.ToPointer(15.0), | ||
Usage: convert.ToPointer(15.0), | ||
}, | ||
Expect: convert.ToPointer(newPercentThreshold(50)), | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
got, err := getHighestMatchingThreshold(tc.BalanceThresholds, tc.EntitlementValue) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.Expect, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.