From 98fa42055e021f2a77c36cd25e8ed37f2907c403 Mon Sep 17 00:00:00 2001 From: adragusin Date: Tue, 26 Nov 2019 17:51:43 +0200 Subject: [PATCH] Added tests in utils --- config/config_json_test.go | 12 +- console/account_set.go | 1 + console/account_trigger_reset.go | 1 + console/account_trigger_set.go | 1 + console/actionplan_set.go | 1 + console/cdre_config_reload.go | 1 - console/load_ids.go | 2 +- console/scheduler_execute.go | 1 + console/sharedgroup.go | 1 + general_tests/cdrs_onlexp_it_test.go | 4 +- utils/apitpdata.go | 1 + utils/apitpdata_test.go | 15 +-- utils/coreutils_test.go | 163 +++++++++++++++++++-------- 13 files changed, 137 insertions(+), 67 deletions(-) diff --git a/config/config_json_test.go b/config/config_json_test.go index ae2da6b8f..92fa3bde8 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -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) diff --git a/console/account_set.go b/console/account_set.go index 527d8ab33..0e2f9d1a0 100644 --- a/console/account_set.go +++ b/console/account_set.go @@ -22,6 +22,7 @@ import ( "github.com/cgrates/cgrates/apier/v2" "github.com/cgrates/cgrates/utils" ) + func init() { c := &CmdAddAccount{ name: "account_set", diff --git a/console/account_trigger_reset.go b/console/account_trigger_reset.go index 7847f0a8f..d8235965b 100644 --- a/console/account_trigger_reset.go +++ b/console/account_trigger_reset.go @@ -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", diff --git a/console/account_trigger_set.go b/console/account_trigger_set.go index f93814e95..e6b85d315 100644 --- a/console/account_trigger_set.go +++ b/console/account_trigger_set.go @@ -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", diff --git a/console/actionplan_set.go b/console/actionplan_set.go index 62ebec372..5258342a7 100644 --- a/console/actionplan_set.go +++ b/console/actionplan_set.go @@ -22,6 +22,7 @@ import ( "github.com/cgrates/cgrates/apier/v1" "github.com/cgrates/cgrates/utils" ) + func init() { c := &CmdSetActionPlan{ name: "actionplan_set", diff --git a/console/cdre_config_reload.go b/console/cdre_config_reload.go index 46cce6c38..ad25c434b 100644 --- a/console/cdre_config_reload.go +++ b/console/cdre_config_reload.go @@ -21,7 +21,6 @@ package console import ( "github.com/cgrates/cgrates/apier/v1" "github.com/cgrates/cgrates/utils" - ) func init() { diff --git a/console/load_ids.go b/console/load_ids.go index a22891c41..ac146988c 100644 --- a/console/load_ids.go +++ b/console/load_ids.go @@ -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} diff --git a/console/scheduler_execute.go b/console/scheduler_execute.go index 6da026492..ecd311f2c 100644 --- a/console/scheduler_execute.go +++ b/console/scheduler_execute.go @@ -22,6 +22,7 @@ import ( "github.com/cgrates/cgrates/apier/v1" "github.com/cgrates/cgrates/utils" ) + func init() { c := &CmdExecuteScheduledActions{ name: "scheduler_execute", diff --git a/console/sharedgroup.go b/console/sharedgroup.go index 1ee6100ea..eb5937317 100644 --- a/console/sharedgroup.go +++ b/console/sharedgroup.go @@ -22,6 +22,7 @@ import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) + func init() { c := &CmdGetSharedGroup{ name: "sharedgroup", diff --git a/general_tests/cdrs_onlexp_it_test.go b/general_tests/cdrs_onlexp_it_test.go index f8165485b..cc9cfcde5 100644 --- a/general_tests/cdrs_onlexp_it_test.go +++ b/general_tests/cdrs_onlexp_it_test.go @@ -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()) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index f1f78c7ab..8b863fadf 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -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{ diff --git a/utils/apitpdata_test.go b/utils/apitpdata_test.go index 132be31b3..c503b309d 100644 --- a/utils/apitpdata_test.go +++ b/utils/apitpdata_test.go @@ -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 { diff --git a/utils/coreutils_test.go b/utils/coreutils_test.go index a746d2c76..79298f387 100644 --- a/utils/coreutils_test.go +++ b/utils/coreutils_test.go @@ -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")