mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added tests in utils
This commit is contained in:
@@ -355,12 +355,12 @@ func TestDfCdrsJsonCfg(t *testing.T) {
|
||||
Extra_fields: &[]string{},
|
||||
Store_cdrs: utils.BoolPointer(true),
|
||||
Session_cost_retries: utils.IntPointer(5),
|
||||
Chargers_conns: &[]*RemoteHostJson{},
|
||||
Rals_conns: &[]*RemoteHostJson{},
|
||||
Attributes_conns: &[]*RemoteHostJson{},
|
||||
Thresholds_conns: &[]*RemoteHostJson{},
|
||||
Stats_conns: &[]*RemoteHostJson{},
|
||||
Online_cdr_exports: &[]string{},
|
||||
Chargers_conns: &[]*RemoteHostJson{},
|
||||
Rals_conns: &[]*RemoteHostJson{},
|
||||
Attributes_conns: &[]*RemoteHostJson{},
|
||||
Thresholds_conns: &[]*RemoteHostJson{},
|
||||
Stats_conns: &[]*RemoteHostJson{},
|
||||
Online_cdr_exports: &[]string{},
|
||||
}
|
||||
if cfg, err := dfCgrJsonCfg.CdrsJsonCfg(); err != nil {
|
||||
t.Error(err)
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/cgrates/cgrates/apier/v2"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
c := &CmdAddAccount{
|
||||
name: "account_set",
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
v1 "github.com/cgrates/cgrates/apier/v1"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
c := &CmdAccountResetTriggers{
|
||||
name: "account_triggers_reset",
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
v1 "github.com/cgrates/cgrates/apier/v1"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
c := &CmdAccountSetTriggers{
|
||||
name: "account_triggers_set",
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/cgrates/cgrates/apier/v1"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
c := &CmdSetActionPlan{
|
||||
name: "actionplan_set",
|
||||
|
||||
@@ -21,7 +21,6 @@ package console
|
||||
import (
|
||||
"github.com/cgrates/cgrates/apier/v1"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -23,7 +23,7 @@ import "github.com/cgrates/cgrates/utils"
|
||||
func init() {
|
||||
c := &CmdCacheVersions{
|
||||
name: "get_load_ids",
|
||||
rpcMethod: utils.ApierV1GetLoadIDs ,
|
||||
rpcMethod: utils.ApierV1GetLoadIDs,
|
||||
}
|
||||
commands[c.Name()] = c
|
||||
c.CommandExecuter = &CommandExecuter{c}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/cgrates/cgrates/apier/v1"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
c := &CmdExecuteScheduledActions{
|
||||
name: "scheduler_execute",
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
c := &CmdGetSharedGroup{
|
||||
name: "sharedgroup",
|
||||
|
||||
@@ -178,7 +178,7 @@ func testCDRsOnExpDisableOnlineExport(t *testing.T) {
|
||||
var reply string
|
||||
if err := cdrsMasterRpc.Call(utils.CDRsV1ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags:[]string{"*export:false"},
|
||||
Flags: []string{"*export:false"},
|
||||
CGREvent: *testCdr.AsCGREvent(),
|
||||
}, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
@@ -363,7 +363,7 @@ func testCDRsOnExpAMQPReplication(t *testing.T) {
|
||||
var reply string
|
||||
if err := cdrsMasterRpc.Call(utils.CDRsV1ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags:[]string{"*export:true"},
|
||||
Flags: []string{"*export:true"},
|
||||
CGREvent: *testCdr.AsCGREvent(),
|
||||
}, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
|
||||
@@ -785,6 +785,7 @@ type RPCCDRsFilter struct {
|
||||
|
||||
func (fltr *RPCCDRsFilter) AsCDRsFilter(timezone string) (cdrFltr *CDRsFilter, err error) {
|
||||
if fltr == nil {
|
||||
cdrFltr = new(CDRsFilter)
|
||||
return
|
||||
}
|
||||
cdrFltr = &CDRsFilter{
|
||||
|
||||
@@ -560,16 +560,14 @@ func TestNewTAFromAccountKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRPCCDRsFilterAsCDRsFilter(t *testing.T) {
|
||||
//empty check
|
||||
var testStruct *RPCCDRsFilter
|
||||
rcv, err := testStruct.AsCDRsFilter("")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if rcv != nil {
|
||||
t.Errorf("Nil struct expected")
|
||||
if !reflect.DeepEqual(rcv, new(CDRsFilter)) {
|
||||
t.Errorf("Empty struct expected")
|
||||
}
|
||||
//check test
|
||||
rpcCDRsFilter := &RPCCDRsFilter{
|
||||
CGRIDs: []string{"CGRIDs"},
|
||||
NotCGRIDs: []string{"NotCGRIDs"},
|
||||
@@ -615,7 +613,6 @@ func TestRPCCDRsFilterAsCDRsFilter(t *testing.T) {
|
||||
MaxCost: Float64Pointer(0.),
|
||||
OrderBy: "OrderBy",
|
||||
}
|
||||
//check the functionality
|
||||
eOut := &CDRsFilter{
|
||||
CGRIDs: rpcCDRsFilter.CGRIDs,
|
||||
NotCGRIDs: rpcCDRsFilter.NotCGRIDs,
|
||||
@@ -673,49 +670,41 @@ func TestRPCCDRsFilterAsCDRsFilter(t *testing.T) {
|
||||
t.Errorf("Expected: %s ,received: %s ", ToJSON(eOut), ToJSON(rcv))
|
||||
}
|
||||
|
||||
//check with wrong UpdatedAtEnd
|
||||
rpcCDRsFilter.UpdatedAtEnd = "wrongUpdatedAtEnd"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong UpdatedAtEnd not processed")
|
||||
}
|
||||
//check with wrong SetupTimeEnd
|
||||
rpcCDRsFilter.UpdatedAtStart = "wrongUpdatedAtStart"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong UpdatedAtStart not processed")
|
||||
}
|
||||
//check with wrong CreatedAtEnd
|
||||
rpcCDRsFilter.CreatedAtEnd = "wrongCreatedAtEnd"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong CreatedAtEnd not processed")
|
||||
}
|
||||
//check with wrong CreatedAtStart
|
||||
rpcCDRsFilter.CreatedAtStart = "wrongCreatedAtStart"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong CreatedAtStart not processed")
|
||||
}
|
||||
//check with wrong AnswerTimeEnd
|
||||
rpcCDRsFilter.AnswerTimeEnd = "wrongAnswerTimeEnd"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong AnswerTimeEnd not processed")
|
||||
}
|
||||
//check with wrong AnswerTimeStart
|
||||
rpcCDRsFilter.AnswerTimeStart = "wrongAnswerTimeStart"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong AnswerTimeStart not processed")
|
||||
}
|
||||
//check with wrong SetupTimeEnd
|
||||
rpcCDRsFilter.SetupTimeEnd = "wrongSetupTimeEnd"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
t.Errorf("Wrong SetupTimeEnd not processed")
|
||||
}
|
||||
//check with wrong SetupTimeStart
|
||||
rpcCDRsFilter.SetupTimeStart = "wrongSetupTimeStart"
|
||||
rcv, err = rpcCDRsFilter.AsCDRsFilter("")
|
||||
if err == nil {
|
||||
|
||||
@@ -99,44 +99,59 @@ func TestUUIDSha1Prefix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundByMethodUp1(t *testing.T) {
|
||||
func TestRound(t *testing.T) {
|
||||
result := Round(12.49, 1, ROUNDING_UP)
|
||||
expected := 12.5
|
||||
if result != expected {
|
||||
t.Errorf("Error rounding up: sould be %v was %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundByMethodUp2(t *testing.T) {
|
||||
result := Round(12.21, 1, ROUNDING_UP)
|
||||
expected := 12.3
|
||||
result = Round(12.21, 1, ROUNDING_UP)
|
||||
expected = 12.3
|
||||
if result != expected {
|
||||
t.Errorf("Error rounding up: sould be %v was %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundByMethodUp3(t *testing.T) {
|
||||
result := Round(0.0701, 2, ROUNDING_UP)
|
||||
expected := 0.08
|
||||
result = Round(0.0701, 2, ROUNDING_UP)
|
||||
expected = 0.08
|
||||
if result != expected {
|
||||
t.Errorf("Error rounding up: sould be %v was %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundByMethodDown1(t *testing.T) {
|
||||
result := Round(12.49, 1, ROUNDING_DOWN)
|
||||
expected := 12.4
|
||||
result = Round(12.49, 1, ROUNDING_DOWN)
|
||||
expected = 12.4
|
||||
if result != expected {
|
||||
t.Errorf("Error rounding down: sould be %v was %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundByMethodDown2(t *testing.T) {
|
||||
result := Round(12.21, 1, ROUNDING_DOWN)
|
||||
expected := 12.2
|
||||
result = Round(12.21, 1, ROUNDING_DOWN)
|
||||
expected = 12.2
|
||||
if result != expected {
|
||||
t.Errorf("Error rounding up: sould be %v was %v", expected, result)
|
||||
}
|
||||
|
||||
//AlredyHavingPrecision
|
||||
x := 0.07
|
||||
if y := Round(x, 2, ROUNDING_UP); y != x {
|
||||
t.Error("Error rounding when already has desired precision: ", y)
|
||||
}
|
||||
if y := Round(x, 2, ROUNDING_MIDDLE); y != x {
|
||||
t.Error("Error rounding when already has desired precision: ", y)
|
||||
}
|
||||
if y := Round(x, 2, ROUNDING_DOWN); y != x {
|
||||
t.Error("Error rounding when already has desired precision: ", y)
|
||||
}
|
||||
|
||||
result = Round(14.37, 8, ROUNDING_DOWN)
|
||||
expected = 14.37
|
||||
if result != expected {
|
||||
t.Errorf("Expecting: %v, received: %v", expected, result)
|
||||
}
|
||||
result = Round(14.37, 8, "ROUNDING_NOWHERE")
|
||||
expected = 14.37
|
||||
if result != expected {
|
||||
t.Errorf("Expecting: %v, received: %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTimeDetectLayout(t *testing.T) {
|
||||
@@ -233,6 +248,32 @@ func TestParseTimeDetectLayout(t *testing.T) {
|
||||
} else if !fsTm.Equal(expectedTime) {
|
||||
t.Errorf("Unexpected time parsed: %v, expecting: %v", fsTm, expectedTime)
|
||||
}
|
||||
fsTmstampStr = "9999999999999999"
|
||||
fsTm, err = ParseTimeDetectLayout(fsTmstampStr, "")
|
||||
if err == nil {
|
||||
t.Error("Error expected: 'value out of range', received nil")
|
||||
}
|
||||
fsTmstampStr = "1394291049287234286"
|
||||
fsTm, err = ParseTimeDetectLayout(fsTmstampStr, "")
|
||||
expectedTime = time.Date(2014, 3, 8, 15, 4, 9, 287234286, time.UTC)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !fsTm.Equal(expectedTime) {
|
||||
t.Errorf("Unexpected time parsed: %v, expecting: %v", fsTm, expectedTime)
|
||||
}
|
||||
fsTmstampStr = "9999999999999999999"
|
||||
fsTm, err = ParseTimeDetectLayout(fsTmstampStr, "")
|
||||
if err == nil {
|
||||
t.Error("Error expected: 'value out of range', received nil")
|
||||
}
|
||||
var nilTime time.Time
|
||||
fsTmstampStr = "+9999999999999999999"
|
||||
fsTm, err = ParseTimeDetectLayout(fsTmstampStr, "")
|
||||
if err == nil {
|
||||
t.Error("Error expected: 'value out of range', received nil")
|
||||
} else if fsTm != nilTime {
|
||||
t.Errorf("Expecting nilTime, received: %+v", fsTm)
|
||||
}
|
||||
fsTmstampStr = "0"
|
||||
fsTm, err = ParseTimeDetectLayout(fsTmstampStr, "")
|
||||
expectedTime = time.Time{}
|
||||
@@ -335,13 +376,24 @@ func TestParseTimeDetectLayout(t *testing.T) {
|
||||
if err != nil || date.Sub(expected).Seconds() > 20 || date.Sub(expected).Seconds() < 19 {
|
||||
t.Error("error parsing date: ", date.Sub(expected).Seconds())
|
||||
}
|
||||
|
||||
expected = time.Now().AddDate(0, 0, 1)
|
||||
if date, err := ParseTimeDetectLayout("*daily", ""); err != nil {
|
||||
t.Error(err)
|
||||
} else if expected.Sub(date).Seconds() > 1 {
|
||||
t.Errorf("received: %+v", date)
|
||||
}
|
||||
expected = time.Now().AddDate(0, 1, 0)
|
||||
if date, err := ParseTimeDetectLayout("*monthly", ""); err != nil {
|
||||
t.Error(err)
|
||||
} else if expected.Sub(date).Seconds() > 1 {
|
||||
t.Errorf("received: %+v", date)
|
||||
}
|
||||
expected = time.Now().AddDate(1, 0, 0)
|
||||
if date, err := ParseTimeDetectLayout("*yearly", ""); err != nil {
|
||||
t.Error(err)
|
||||
} else if expected.Sub(date).Seconds() > 1 {
|
||||
t.Errorf("received: %+v", date)
|
||||
}
|
||||
|
||||
expected = GetEndOfMonth(time.Now())
|
||||
if date, err := ParseTimeDetectLayout("*month_end", ""); err != nil {
|
||||
@@ -355,6 +407,11 @@ func TestParseTimeDetectLayout(t *testing.T) {
|
||||
} else if !date.Equal(expected) {
|
||||
t.Errorf("expecting: %+v, received: %+v", expected, date)
|
||||
}
|
||||
if date, err := ParseTimeDetectLayout("*month_end+xyz", ""); err == nil {
|
||||
t.Error("Expecting error 'time: invalid time duration', received: nil")
|
||||
} else if date != nilTime {
|
||||
t.Errorf("Expecting nilTime, received: %+v", date)
|
||||
}
|
||||
|
||||
date, err = ParseTimeDetectLayout("2013-07-30T19:33:10Z", "")
|
||||
expected = time.Date(2013, 7, 30, 19, 33, 10, 0, time.UTC)
|
||||
@@ -414,43 +471,40 @@ func TestRoundDuration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundAlredyHavingPrecision(t *testing.T) {
|
||||
x := 0.07
|
||||
if y := Round(x, 2, ROUNDING_UP); y != x {
|
||||
t.Error("Error rounding when already has desired precision: ", y)
|
||||
}
|
||||
if y := Round(x, 2, ROUNDING_MIDDLE); y != x {
|
||||
t.Error("Error rounding when already has desired precision: ", y)
|
||||
}
|
||||
if y := Round(x, 2, ROUNDING_DOWN); y != x {
|
||||
t.Error("Error rounding when already has desired precision: ", y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPrefix(t *testing.T) {
|
||||
a := SplitPrefix("0123456789", 1)
|
||||
if len(a) != 10 {
|
||||
if a := SplitPrefix("0123456789", 1); len(a) != 10 {
|
||||
t.Error("Error splitting prefix: ", a)
|
||||
}
|
||||
if a := SplitPrefix("0123456789", 5); len(a) != 6 {
|
||||
t.Error("Error splitting prefix: ", a)
|
||||
}
|
||||
if a := SplitPrefix("", 1); len(a) != 0 {
|
||||
t.Error("Error splitting prefix: ", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPrefixFive(t *testing.T) {
|
||||
a := SplitPrefix("0123456789", 5)
|
||||
if len(a) != 6 {
|
||||
t.Error("Error splitting prefix: ", a)
|
||||
func TestCopyHour(t *testing.T) {
|
||||
var src, dst, eOut time.Time
|
||||
if rcv := CopyHour(src, dst); !reflect.DeepEqual(rcv, eOut) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPrefixEmpty(t *testing.T) {
|
||||
a := SplitPrefix("", 1)
|
||||
if len(a) != 0 {
|
||||
t.Error("Error splitting prefix: ", a)
|
||||
src = time.Date(2020, time.April, 18, 20, 10, 11, 01, time.UTC)
|
||||
dst = time.Date(2019, time.May, 25, 23, 0, 4, 0, time.UTC)
|
||||
eOut = time.Date(2019, time.May, 25, 20, 10, 11, 01, time.UTC)
|
||||
if rcv := CopyHour(src, dst); !reflect.DeepEqual(rcv, eOut) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDurationWithSecs(t *testing.T) {
|
||||
var durExpected time.Duration
|
||||
if rcv, err := ParseDurationWithSecs(""); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rcv, durExpected) {
|
||||
t.Errorf("Expecting: 0s, received: %+v", rcv)
|
||||
}
|
||||
durStr := "2"
|
||||
durExpected := time.Duration(2) * time.Second
|
||||
durExpected = time.Duration(2) * time.Second
|
||||
if parsed, err := ParseDurationWithSecs(durStr); err != nil {
|
||||
t.Error(err)
|
||||
} else if parsed != durExpected {
|
||||
@@ -485,6 +539,27 @@ func TestParseDurationWithSecs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDurationWithNanosecs(t *testing.T) {
|
||||
var eOut time.Duration
|
||||
if rcv, err := ParseDurationWithNanosecs(""); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rcv, eOut) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
eOut, _ = time.ParseDuration("-1ns")
|
||||
if rcv, err := ParseDurationWithNanosecs(UNLIMITED); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rcv, eOut) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
eOut, _ = time.ParseDuration("28ns")
|
||||
if rcv, err := ParseDurationWithNanosecs("28"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rcv, eOut) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinDuration(t *testing.T) {
|
||||
d1, _ := time.ParseDuration("1m")
|
||||
d2, _ := time.ParseDuration("59s")
|
||||
|
||||
Reference in New Issue
Block a user