From af671c96e031964dc6f2f4898fdcb52d761494ce Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Mon, 3 Jul 2017 22:49:19 +0300 Subject: [PATCH 1/4] Added test and corrected field parsing --- config/cacheconfig.go | 6 ++++++ config/config_json_test.go | 4 ++++ config/libconfig_json.go | 1 + 3 files changed, 11 insertions(+) diff --git a/config/cacheconfig.go b/config/cacheconfig.go index e14593d78..aeb2cd2ba 100644 --- a/config/cacheconfig.go +++ b/config/cacheconfig.go @@ -158,5 +158,11 @@ func (self *CacheConfig) loadFromJsonCfg(jsnCfg *CacheJsonCfg) error { return err } } + if jsnCfg.Timings != nil { + self.Timings = &CacheParamConfig{} + if err := self.Timings.loadFromJsonCfg(jsnCfg.Timings); err != nil { + return err + } + } return nil } diff --git a/config/config_json_test.go b/config/config_json_test.go index 4e18728df..7abb76c22 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/cgrates/cgrates/utils" + ) var dfCgrJsonCfg *CgrJsonCfg @@ -97,7 +98,10 @@ func TestCacheJsonCfg(t *testing.T) { Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, Resource_limits: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, + Timings: &CacheParamJsonCfg{Limit: utils.IntPointer(10000), + Ttl: utils.StringPointer("0s"), Precache: utils.BoolPointer(false)}, } + if gCfg, err := dfCgrJsonCfg.CacheJsonCfg(); err != nil { t.Error(err) } else if !reflect.DeepEqual(eCfg, gCfg) { diff --git a/config/libconfig_json.go b/config/libconfig_json.go index c27ef9f1a..837b44b57 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -263,6 +263,7 @@ type CacheJsonCfg struct { Reverse_aliases *CacheParamJsonCfg Derived_chargers *CacheParamJsonCfg Resource_limits *CacheParamJsonCfg + Timings *CacheParamJsonCfg } // Represents one connection instance towards FreeSWITCH From a32181722d401e1b4536403a3fd2dd4036841867 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Tue, 4 Jul 2017 14:46:07 +0300 Subject: [PATCH 2/4] Added some more tests --- config/config_test.go | 39 +++++++++++++++++++++++++++++++++++ utils/dateseries_test.go | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index e930ad849..5c0ef18d7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -402,6 +402,45 @@ func TestCgrCfgJSONDefaultsSMGenericCfg(t *testing.T) { } } +func TestCgrCfgJSONDefaultsCacheCFG(t *testing.T) { + eCacheCfg := &CacheConfig{ + Destinations: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + ReverseDestinations: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + RatingPlans: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: true}, + RatingProfiles: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + Lcr: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + CdrStats: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + Actions: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + ActionPlans: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + AccountActionPlans: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + ActionTriggers: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + SharedGroups: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + Aliases: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + ReverseAliases: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + DerivedChargers: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + ResourceLimits: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + Timings: &CacheParamConfig{Limit: 10000, + TTL: time.Duration(0 * time.Second), Precache: false}, + } + if !reflect.DeepEqual(eCacheCfg, cgrCfg.CacheConfig) { + t.Errorf("received: %+v, expecting: %+v", eCacheCfg, cgrCfg.CacheConfig) + } +} func TestCgrCfgJSONDefaultsSMFsConfig(t *testing.T) { eSmFsCfg := &SmFsConfig{ diff --git a/utils/dateseries_test.go b/utils/dateseries_test.go index 88e3a9e7a..ab04390c2 100644 --- a/utils/dateseries_test.go +++ b/utils/dateseries_test.go @@ -187,3 +187,47 @@ func TestDateseriesDaysInYear(t *testing.T) { t.Error("error calculating days: ", n) } } + +func TestDateseriesWeekDaysEquals(t *testing.T) { + wds1 := WeekDays{time.Monday, time.Saturday, time.Sunday} + wds2 := WeekDays{time.Monday, time.Saturday, time.Sunday} + wds3 := WeekDays{time.Monday} + if wds1.Equals(wds2) != true { + t.Errorf("Expected: true, got: %v",!true) + }else if wds1.Equals(wds3) !=false { + t.Errorf("Expected: false, got: %v",!false) + } +} + + func TestDateseriesMonthsEquals(t *testing.T) { + m1 := Months{5, 6, 7, 8} + m2 := Months{5, 6, 7, 8} + m3 := Months{} + if m1.Equals(m2) !=true { + t.Errorf("Expected: true, got: %v",!true) + }else if m1.Equals(m3) !=false { + t.Errorf("Expected: false, got: %v",!false) + } +} + + func TestDateseriesMonthDayssEquals(t *testing.T) { + md1 := MonthDays{24, 25, 26} + md2 := MonthDays{24, 25, 26} + md3 := MonthDays{} + if md1.Equals(md2) !=true { +t.Errorf("Expected: true, got: %v",!true) + }else if md1.Equals(md3) !=false { +t.Errorf("Expected: false, got: %v",!false) + } +} + +func TestDateseriesYearsEquals(t *testing.T) { + ys1 := Years{2013, 2014, 2015} + ys2 := Years{2013, 2014, 2015} + ys3 := Years{} + if ys1.Equals(ys2) !=true { + t.Errorf("Expected: true, got: %v",!true) + }else if ys1.Equals(ys3) !=false { + t.Errorf("Expected: false, got: %v",!false) + } +} From cafe5a5263beb769b5b297d1c5d10fd6fe637909 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Tue, 4 Jul 2017 23:09:28 +0300 Subject: [PATCH 3/4] Formated and added more tests --- config/config_json_test.go | 1 - config/config_test.go | 4 +- config/libconfig_json.go | 2 +- utils/dateseries_test.go | 86 ++++++++++++++++++++++++++++++-------- utils/value_formula.go | 4 +- 5 files changed, 74 insertions(+), 23 deletions(-) diff --git a/config/config_json_test.go b/config/config_json_test.go index 7abb76c22..c28e361c9 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -24,7 +24,6 @@ import ( "testing" "github.com/cgrates/cgrates/utils" - ) var dfCgrJsonCfg *CgrJsonCfg diff --git a/config/config_test.go b/config/config_test.go index 5c0ef18d7..a787750ee 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -437,8 +437,8 @@ func TestCgrCfgJSONDefaultsCacheCFG(t *testing.T) { Timings: &CacheParamConfig{Limit: 10000, TTL: time.Duration(0 * time.Second), Precache: false}, } - if !reflect.DeepEqual(eCacheCfg, cgrCfg.CacheConfig) { - t.Errorf("received: %+v, expecting: %+v", eCacheCfg, cgrCfg.CacheConfig) + if !reflect.DeepEqual(eCacheCfg, cgrCfg.CacheConfig) { + t.Errorf("received: %+v, expecting: %+v", eCacheCfg, cgrCfg.CacheConfig) } } diff --git a/config/libconfig_json.go b/config/libconfig_json.go index 837b44b57..3b21cdb17 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -263,7 +263,7 @@ type CacheJsonCfg struct { Reverse_aliases *CacheParamJsonCfg Derived_chargers *CacheParamJsonCfg Resource_limits *CacheParamJsonCfg - Timings *CacheParamJsonCfg + Timings *CacheParamJsonCfg } // Represents one connection instance towards FreeSWITCH diff --git a/utils/dateseries_test.go b/utils/dateseries_test.go index ab04390c2..225897124 100644 --- a/utils/dateseries_test.go +++ b/utils/dateseries_test.go @@ -193,31 +193,31 @@ func TestDateseriesWeekDaysEquals(t *testing.T) { wds2 := WeekDays{time.Monday, time.Saturday, time.Sunday} wds3 := WeekDays{time.Monday} if wds1.Equals(wds2) != true { - t.Errorf("Expected: true, got: %v",!true) - }else if wds1.Equals(wds3) !=false { - t.Errorf("Expected: false, got: %v",!false) + t.Errorf("Expected: true, got: %v", !true) + } else if wds1.Equals(wds3) != false { + t.Errorf("Expected: false, got: %v", !false) } } - func TestDateseriesMonthsEquals(t *testing.T) { +func TestDateseriesMonthsEquals(t *testing.T) { m1 := Months{5, 6, 7, 8} m2 := Months{5, 6, 7, 8} m3 := Months{} - if m1.Equals(m2) !=true { - t.Errorf("Expected: true, got: %v",!true) - }else if m1.Equals(m3) !=false { - t.Errorf("Expected: false, got: %v",!false) + if m1.Equals(m2) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if m1.Equals(m3) != false { + t.Errorf("Expected: false, got: %v", !false) } } - func TestDateseriesMonthDayssEquals(t *testing.T) { +func TestDateseriesMonthDaysEquals(t *testing.T) { md1 := MonthDays{24, 25, 26} md2 := MonthDays{24, 25, 26} md3 := MonthDays{} - if md1.Equals(md2) !=true { -t.Errorf("Expected: true, got: %v",!true) - }else if md1.Equals(md3) !=false { -t.Errorf("Expected: false, got: %v",!false) + if md1.Equals(md2) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if md1.Equals(md3) != false { + t.Errorf("Expected: false, got: %v", !false) } } @@ -225,9 +225,61 @@ func TestDateseriesYearsEquals(t *testing.T) { ys1 := Years{2013, 2014, 2015} ys2 := Years{2013, 2014, 2015} ys3 := Years{} - if ys1.Equals(ys2) !=true { - t.Errorf("Expected: true, got: %v",!true) - }else if ys1.Equals(ys3) !=false { - t.Errorf("Expected: false, got: %v",!false) + if ys1.Equals(ys2) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if ys1.Equals(ys3) != false { + t.Errorf("Expected: false, got: %v", !false) + } +} + +func TestDateseriesWeekDaysParse(t *testing.T) { + wds1 := WeekDays{} + in := "1,2,3" + wds2 := WeekDays{time.Monday, time.Tuesday, time.Wednesday} + if reflect.DeepEqual(wds2, wds1) != false { + t.Errorf("Expected: %+v, received: %+v", WeekDays{}, wds1) + } + wds1.Parse(in, ",") + if !reflect.DeepEqual(wds2, wds1) { + t.Errorf("Expected: %+v, received: %+v", wds2, wds1) + } +} + +func TestDateseriesMonthsParse(t *testing.T) { + m1 := Months{} + m2 := Months{5, 6, 7, 8} + in := "5,6,7,8" + if reflect.DeepEqual(m2, m1) != false { + t.Errorf("Expected: %+v, received: %+v", Months{}, m1) + } + m1.Parse(in, ",") + if !reflect.DeepEqual(m2, m1) { + t.Errorf("Expected: %+v, received: %+v", m2, m1) + } +} + +func TestDateseriesMonthDaysParse(t *testing.T) { + md1 := MonthDays{} + md2 := MonthDays{24, 25, 26} + in := "24,25,26" + if reflect.DeepEqual(md2, md1) != false { + t.Errorf("Expected: %+v, received: %+v", MonthDays{}, md1) + } + md1.Parse(in, ",") + if !reflect.DeepEqual(md2, md1) { + t.Errorf("Expected: %+v, received: %+v", md2, md1) + } +} + +func TestDateseriesYearsParse(t *testing.T) { + ys1 := Years{} + ys2 := Years{2013, 2014, 2015} + in := "2013,2014,2015" + if reflect.DeepEqual(ys2, ys1) != false { + t.Errorf("Expected: %+v, received: %+v", Years{}, ys1) + } + ys1.Parse(in, ",") + if !reflect.DeepEqual(ys2, ys1) { + t.Errorf("Expected: %+v, received: %+v", ys2, ys1) } } diff --git a/utils/value_formula.go b/utils/value_formula.go index a1a2e38f3..06caa9ffe 100644 --- a/utils/value_formula.go +++ b/utils/value_formula.go @@ -107,10 +107,10 @@ func incrementalFormula(params map[string]interface{}) float64 { return units / 24 } if interval == "month" { - return units / ( DaysInMonth(now.Year(), now.Month()) * 24 ) + return units / (DaysInMonth(now.Year(), now.Month()) * 24) } if interval == "year" { - return units / ( DaysInYear(now.Year()) * 24 ) + return units / (DaysInYear(now.Year()) * 24) } } if increment == "minute" { From 0d26e737467facf2f33bac5f90ae1a4583fc40f4 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Tue, 4 Jul 2017 23:22:15 +0300 Subject: [PATCH 4/4] added tests for Contains --- utils/dateseries_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/utils/dateseries_test.go b/utils/dateseries_test.go index 225897124..6cfe92331 100644 --- a/utils/dateseries_test.go +++ b/utils/dateseries_test.go @@ -283,3 +283,39 @@ func TestDateseriesYearsParse(t *testing.T) { t.Errorf("Expected: %+v, received: %+v", ys2, ys1) } } + +func TestDateseriesWeekDaysContains(t *testing.T) { + wds := WeekDays{time.Monday, time.Tuesday} + if wds.Contains(time.Monday) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if wds.Contains(time.Wednesday) != false { + t.Errorf("Expected: false, got: %v", !false) + } +} + +func TestDateseriesMonthsContains(t *testing.T) { + m := Months{5, 6, 7, 8} + if m.Contains(5) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if m.Contains(4) != false { + t.Errorf("Expected: false, got: %v", !false) + } +} + +func TestDateseriesMonthDaysContains(t *testing.T) { + md := MonthDays{24, 25, 26} + if md.Contains(24) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if md.Contains(23) != false { + t.Errorf("Expected: false, got: %v", !false) + } +} + +func TestDateseriesYearsContains(t *testing.T) { + ys := Years{2013, 2014, 2015} + if ys.Contains(2013) != true { + t.Errorf("Expected: true, got: %v", !true) + } else if ys.Contains(2012) != false { + t.Errorf("Expected: false, got: %v", !false) + } +}