volume discount storred and tested"

need to implement GetCost according to it
This commit is contained in:
Radu Ioan Fericean
2012-02-25 17:46:32 +02:00
parent bd200c18c1
commit 1c5626b9f7
2 changed files with 46 additions and 0 deletions

View File

@@ -116,6 +116,21 @@ func (ub *UserBudget) getTariffPlan(storage StorageGetter) (tp *TariffPlan, err
return ub.tariffPlan, err
}
func (ub *UserBudget) getVolumeDiscount(storage StorageGetter) (float64, error) {
tariffPlan, err := ub.getTariffPlan(storage)
if err != nil {
return 0.0, err
}
thresholds := len(tariffPlan.VolumeDiscountThresholds)
for i, vd := range tariffPlan.VolumeDiscountThresholds {
if ub.VolumeDiscountSeconds >= vd.Volume &&
(i > thresholds-2 || ub.VolumeDiscountSeconds < tariffPlan.VolumeDiscountThresholds[i+1].Volume) {
return vd.Discount, nil
}
}
return 0, nil
}
/*
Returns user's avaliable minutes for the specified destination
*/

View File

@@ -295,6 +295,37 @@ func TestResetUserBudget(t *testing.T) {
}
func TestGetVolumeDiscountHaving(t *testing.T) {
vd := &VolumeDiscount{100, 11}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, VolumeDiscountThresholds: []*VolumeDiscount{vd}}
rifsBudget := &UserBudget{Id: "other", Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10, VolumeDiscountSeconds: 100}
result, err := rifsBudget.getVolumeDiscount(nil)
if err != nil || result != 11 {
t.Errorf("Expected %v was %v", 11, result)
}
}
func TestGetVolumeDiscountNotHaving(t *testing.T) {
vd := &VolumeDiscount{100, 11}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, VolumeDiscountThresholds: []*VolumeDiscount{vd}}
rifsBudget := &UserBudget{Id: "other", Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10, VolumeDiscountSeconds: 99}
result, err := rifsBudget.getVolumeDiscount(nil)
if err != nil || result != 0 {
t.Errorf("Expected %v was %v", 0, result)
}
}
func TestGetVolumeDiscountNotSteps(t *testing.T) {
vd1 := &VolumeDiscount{100, 11}
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: 551}
result, err := rifsBudget.getVolumeDiscount(nil)
if err != nil || result != 20 {
t.Errorf("Expected %v was %v", 20, result)
}
}
/*********************************** Benchmarks *******************************/
func BenchmarkGetSecondForPrefix(b *testing.B) {