From ef3db9a458d91230f1f73cbcf5d5b926cf29c0b7 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 27 Feb 2012 15:03:13 +0200 Subject: [PATCH] tested negative debit --- timespans/calldesc_test.go | 2 +- timespans/userbudget.go | 4 +-- timespans/userbudget_test.go | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/timespans/calldesc_test.go b/timespans/calldesc_test.go index f63c10bcc..64613a633 100644 --- a/timespans/calldesc_test.go +++ b/timespans/calldesc_test.go @@ -239,7 +239,7 @@ func TestGetCostWithVolumeDiscount(t *testing.T) { vd1 := &VolumeDiscount{100, 10} vd2 := &VolumeDiscount{500, 20} seara := &TariffPlan{Id: "seara", SmsCredit: 100, VolumeDiscountThresholds: []*VolumeDiscount{vd1, vd2}} - rifsBudget := &UserBudget{Id: "other", Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10, VolumeDiscountSeconds: 105} + rifsBudget := &UserBudget{Id: "rif", Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10, VolumeDiscountSeconds: 105} t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 2, 18, 30, 0, 0, time.UTC) cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0723", TimeStart: t1, TimeEnd: t2, storageGetter: getter, userBudget: rifsBudget} diff --git a/timespans/userbudget.go b/timespans/userbudget.go index 0f2b58625..477c42a70 100644 --- a/timespans/userbudget.go +++ b/timespans/userbudget.go @@ -18,7 +18,7 @@ along with this program. If not, see package timespans import ( - "log" + // "log" "sort" "strconv" "strings" @@ -139,7 +139,7 @@ Returns user's avaliable minutes for the specified destination */ func (ub *UserBudget) getSecondsForPrefix(sg StorageGetter, prefix string) (seconds float64, bucketList bucketsorter) { if len(ub.MinuteBuckets) == 0 { - log.Print("There are no minute buckets to check for user: ", ub.Id) + // log.Print("There are no minute buckets to check for user: ", ub.Id) return } diff --git a/timespans/userbudget_test.go b/timespans/userbudget_test.go index 1569d7ee7..754b1d3e2 100644 --- a/timespans/userbudget_test.go +++ b/timespans/userbudget_test.go @@ -148,6 +148,18 @@ func TestDebitMoreMoneyBudget(t *testing.T) { } } +func TestDebitNegativeMoneyBudget(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: 0.0, DestinationId: "retea"} + rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, ResetDayOfTheMonth: 10} + result := rifsBudget.debitMoneyBudget(getter, -15) + if rifsBudget.Credit != 36 || result != rifsBudget.Credit { + t.Errorf("Expected %v was %v", 36, rifsBudget.Credit) + } +} + func TestDebitMinuteBudget(t *testing.T) { getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10) defer getter.Close() @@ -236,6 +248,32 @@ func TestDebitPriceMoreMinuteBudget(t *testing.T) { } } +func TestDebitPriceNegativeMinuteBudget(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, -15, "0723") + if b2.Seconds != 115 || b1.Seconds != 10 || err != nil || rifsBudget.Credit != 36 { + t.Log(b1, b2, err) + t.Errorf("Expected %v was %v", 36, rifsBudget.Credit) + } +} + +func TestDebitNegativeMinuteBudget(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: 0.0, DestinationId: "retea"} + rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, ResetDayOfTheMonth: 10} + err := rifsBudget.debitMinutesBudget(getter, -15, "0723") + if b2.Seconds != 115 || 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() @@ -272,6 +310,18 @@ func TestDebitMoreSMSBudget(t *testing.T) { } } +func TestDebitNegativeSMSBudget(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: 0.0, DestinationId: "retea"} + rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, SmsCredit: 100, ResetDayOfTheMonth: 10} + result, err := rifsBudget.debitSMSBuget(getter, -15) + if rifsBudget.SmsCredit != 115 || result != rifsBudget.SmsCredit || err != nil { + t.Errorf("Expected %v was %v", 115, rifsBudget.SmsCredit) + } +} + func TestResetUserBudget(t *testing.T) { getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10) defer getter.Close()