mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
debit methods are in place and tested
This commit is contained in:
@@ -153,6 +153,29 @@ func (ub *UserBudget) debitMinutesBudget(sg StorageGetter, amount float64, prefi
|
||||
if avaliableNbSeconds < amount {
|
||||
return new(AmountTooBig)
|
||||
}
|
||||
credit := ub.Credit
|
||||
// calculating money debit
|
||||
// this is needed because if the credit is less then the amount needed to be debited
|
||||
// we need to keep everithing in place and return an error
|
||||
for _, mb := range bucketList {
|
||||
if mb.Seconds < amount {
|
||||
if mb.Price > 0 { // debit the money if the bucket has price
|
||||
credit -= mb.Seconds * mb.Price
|
||||
}
|
||||
} else {
|
||||
if mb.Price > 0 { // debit the money if the bucket has price
|
||||
credit -= amount * mb.Price
|
||||
}
|
||||
break
|
||||
}
|
||||
if credit < 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if credit < 0 {
|
||||
return new(AmountTooBig)
|
||||
}
|
||||
ub.Credit = credit // credit is > 0
|
||||
for _, mb := range bucketList {
|
||||
if mb.Seconds < amount {
|
||||
amount -= mb.Seconds
|
||||
|
||||
@@ -162,7 +162,6 @@ func TestDebitAllMinuteBudget(t *testing.T) {
|
||||
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, ResetDayOfTheMonth: 10}
|
||||
err := rifsBudget.debitMinutesBudget(getter, 110, "0723")
|
||||
if b2.Seconds != 0 || b1.Seconds != 0 || err != nil {
|
||||
t.Log(err)
|
||||
t.Errorf("Expected %v was %v", 0, b2.Seconds)
|
||||
}
|
||||
}
|
||||
@@ -179,6 +178,44 @@ func TestDebitMoreMinuteBudget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebitPriceMinuteBudget(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.0, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 1.0, DestinationId: "retea"}
|
||||
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, ResetDayOfTheMonth: 10}
|
||||
err := rifsBudget.debitMinutesBudget(getter, 5, "0723")
|
||||
if b2.Seconds != 95 || b1.Seconds != 10 || err != nil || rifsBudget.Credit != 16 {
|
||||
t.Log(rifsBudget.Credit)
|
||||
t.Errorf("Expected %v was %v", 16, rifsBudget.Credit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebitPriceAllMinuteBudget(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.0, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 1.0, DestinationId: "retea"}
|
||||
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, ResetDayOfTheMonth: 10}
|
||||
err := rifsBudget.debitMinutesBudget(getter, 21, "0723")
|
||||
if b2.Seconds != 79 || b1.Seconds != 10 || err != nil || rifsBudget.Credit != 0 {
|
||||
t.Errorf("Expected %v was %v", 0, rifsBudget.Credit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebitPriceMoreMinuteBudget(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.0, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 1.0, DestinationId: "retea"}
|
||||
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, ResetDayOfTheMonth: 10}
|
||||
err := rifsBudget.debitMinutesBudget(getter, 25, "0723")
|
||||
if b2.Seconds != 100 || b1.Seconds != 10 || err == nil || rifsBudget.Credit != 21 {
|
||||
t.Log(b1, b2, err)
|
||||
t.Errorf("Expected %v was %v", 21, rifsBudget.Credit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebitSMSBudget(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
|
||||
Reference in New Issue
Block a user