mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
test json encoding
This commit is contained in:
@@ -2,6 +2,9 @@ package timeslots
|
||||
|
||||
import (
|
||||
"time"
|
||||
"fmt"
|
||||
"log"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -10,12 +13,12 @@ ActivationTime when those intervals will be applied.
|
||||
*/
|
||||
type ActivationPeriod struct {
|
||||
ActivationTime time.Time
|
||||
Interval []*Interval
|
||||
Intervals []*Interval
|
||||
}
|
||||
|
||||
func (c *ActivationPeriod) AddInterval(is ...*Interval) {
|
||||
for _, i := range is {
|
||||
c.Interval = append(c.Interval, i)
|
||||
c.Intervals = append(c.Intervals, i)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,16 +29,39 @@ ActivationPeriods slice is the value.
|
||||
*/
|
||||
type Customer struct {
|
||||
CstmId string
|
||||
Subject string
|
||||
DestinationPrefix string
|
||||
ActivationPeriods []*ActivationPeriod
|
||||
}
|
||||
|
||||
func (c *Customer) AddActivationPeriod(ap ...*ActivationPeriod) {
|
||||
/*
|
||||
Adds an activation period to the internal slice
|
||||
*/
|
||||
func (c *Customer) addActivationPeriod(ap ...*ActivationPeriod) {
|
||||
for _,a := range ap {
|
||||
c.ActivationPeriods = append(c.ActivationPeriods, a)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Customer) getKey() string {
|
||||
return fmt.Sprintf("%s%s%s", c.CstmId, c.Subject, c.DestinationPrefix)
|
||||
}
|
||||
|
||||
func (c *Customer) encodeValue() []byte {
|
||||
jo, err := json.Marshal(c.ActivationPeriods)
|
||||
if err != nil {
|
||||
log.Print("Cannot encode intervals: ", err)
|
||||
}
|
||||
return jo
|
||||
}
|
||||
|
||||
func (c *Customer) decodeValue(v []byte) {
|
||||
err := json.Unmarshal(v, &c.ActivationPeriods)
|
||||
if err != nil {
|
||||
log.Print("Cannot decode intervals: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
A unit in which a call will be split that has a specific price related interval attached to it.
|
||||
*/
|
||||
|
||||
@@ -2,30 +2,43 @@ package timeslots
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setUp() {
|
||||
c1 := &Customer{CstmId:"rif",DestinationPrefix: "40256"}
|
||||
t1 := time.Date(2012, time.January, 10, 23, 0, 0, 0, time.UTC)
|
||||
ap1 := &ActivationPeriod{ActivationTime: t1}
|
||||
c1.AddActivationPeriod(ap1)
|
||||
}
|
||||
|
||||
func TestSimple(t *testing.T){
|
||||
setUp()
|
||||
cc, err := GetCost(nil, nil)
|
||||
if err != nil {
|
||||
t.Error("Got error on getting cost")
|
||||
func TestStorageEncoding(t *testing.T){
|
||||
i1 := &Interval{Month: time.December, MonthDay: 1, StartHour: "09:00"}
|
||||
i2 := &Interval{WeekDays: []time.Weekday{time.Sunday}}
|
||||
i3 := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartHour: "14:30", EndHour: "15:00"}
|
||||
c := &Customer{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256"}
|
||||
ap := &ActivationPeriod{ActivationTime: time.Now()}
|
||||
ap.AddInterval(i1, i2, i3)
|
||||
c.addActivationPeriod(ap)
|
||||
received := c.encodeValue()
|
||||
c.decodeValue(received)
|
||||
f1 := c.ActivationPeriods[0].Intervals[0]
|
||||
if f1.Month != i1.Month || f1.MonthDay != i1.MonthDay || f1.StartHour != i1.StartHour {
|
||||
t.Errorf("Decode values are not the same: %v vs %v", f1, i1)
|
||||
}
|
||||
expected:= &CallCost{TOR: 1, CstmId:"",Subject:"",Prefix:"", Cost:1, ConnectFee:1}
|
||||
if *cc != *expected {
|
||||
t.Errorf("Expected %v got %v", expected, cc)
|
||||
f2 := c.ActivationPeriods[0].Intervals[1]
|
||||
for i,v := range f2.WeekDays {
|
||||
if v != i2.WeekDays[i] {
|
||||
t.Errorf("Decode values are not the same: %v vs %v", f2, i2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSimple(b *testing.B) {
|
||||
func BenchmarkDecoding(b *testing.B) {
|
||||
b.StopTimer()
|
||||
i1 := &Interval{Month: time.December, MonthDay: 1, StartHour: "09:00"}
|
||||
i2 := &Interval{WeekDays: []time.Weekday{time.Sunday}}
|
||||
i3 := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartHour: "14:30", EndHour: "15:00"}
|
||||
c := &Customer{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256"}
|
||||
ap := &ActivationPeriod{ActivationTime: time.Now()}
|
||||
ap.AddInterval(i1, i2, i3)
|
||||
c.addActivationPeriod(ap)
|
||||
received := c.encodeValue()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
GetCost(nil, nil)
|
||||
c.decodeValue(received)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user