From 1eccafceed2db7216e8eb3b34604d765ed929e4f Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 1 Feb 2012 21:23:22 +0200 Subject: [PATCH] test json encoding --- timeslots/timeslots.go | 32 +++++++++++++++++++++--- timeslots/timeslots_test.go | 49 +++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/timeslots/timeslots.go b/timeslots/timeslots.go index 06d5d041c..ba5629c98 100644 --- a/timeslots/timeslots.go +++ b/timeslots/timeslots.go @@ -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. */ diff --git a/timeslots/timeslots_test.go b/timeslots/timeslots_test.go index 3047e2aa7..8ca4729ae 100644 --- a/timeslots/timeslots_test.go +++ b/timeslots/timeslots_test.go @@ -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) } } \ No newline at end of file