Files
cgrates/sessionmanager/session_test.go
Radu Ioan Fericean f5eedc4498 event handlers
2012-05-03 23:14:23 +03:00

81 lines
2.5 KiB
Go

/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2012 Radu Ioan Fericean
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package sessionmanager
import (
"testing"
"time"
)
func TestSessionDurationSingle(t *testing.T) {
s := NewSession("", "")
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("", start)
twoSeconds, _ := time.ParseDuration("2s")
if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 {
t.Errorf("Wrong session duration %v", d)
}
}
func TestSessionDurationMultiple(t *testing.T) {
s := NewSession("", "")
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("", start)
s.AddCallToSession("", start)
s.AddCallToSession("", start)
twoSeconds, _ := time.ParseDuration("2s")
if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 {
t.Errorf("Wrong session duration %v", d)
}
}
func TestSessionCostSingle(t *testing.T) {
s := NewSession("vdf", "rif")
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("0723", start)
twoSeconds, _ := time.ParseDuration("60s")
if ccs, err := s.GetSessionCostFrom(start.Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)
} else {
if len(ccs) != 1 || ccs[0].Cost < 1 || ccs[0].Cost > 1.1 {
t.Errorf("Expected %v got %v", "between 1 and 1.1", ccs[0].Cost)
}
}
}
func TestSessionCostMultiple(t *testing.T) {
s := NewSession("vdf", "rif")
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("0723", start)
s.AddCallToSession("0257", start)
s.AddCallToSession("0256", start)
twoSeconds, _ := time.ParseDuration("60s")
if ccs, err := s.GetSessionCostFrom(start.Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)
} else {
sum := 0.0
for _, cc := range ccs {
sum += cc.Cost
}
if len(ccs) != 3 || sum < 23 || sum > 23.1 {
t.Errorf("Expected %v got %v", "between 23 and 23.1", sum)
}
}
}