Files
cgrates/utils/dateseries_test.go
2019-12-18 17:58:07 +02:00

457 lines
13 KiB
Go

/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
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 utils
import (
"encoding/json"
"reflect"
"testing"
"time"
)
func TestYearsSort(t *testing.T) {
ys := &Years{}
ys.Sort()
if !reflect.DeepEqual(ys, &Years{}) {
t.Errorf("Expecting %+v received: %+v", &Years{}, ys)
}
ys = &Years{2019, 2010, 2020, 2005, 2018, 2007}
ysSorted := &Years{2005, 2007, 2010, 2018, 2019, 2020}
ys.Sort()
if !reflect.DeepEqual(ys, ysSorted) {
t.Errorf("Expecting %+v received: %+v", ysSorted, ys)
}
}
func TestYearsLen(t *testing.T) {
ys := &Years{}
if rcv := ys.Len(); rcv != 0 {
t.Errorf("Expecting %+v received: %+v", 0, rcv)
}
ys = &Years{2019, 2010, 2020, 2005, 2018, 2007}
if rcv := ys.Len(); rcv != 6 {
t.Errorf("Expecting %+v received: %+v", 6, rcv)
}
}
func TestYearsSwap(t *testing.T) {
ys := &Years{2019, 2010, 2020, 2005, 2018, 2007}
ys.Swap(0, 1)
ysSwapped := &Years{2010, 2019, 2020, 2005, 2018, 2007}
if !reflect.DeepEqual(ys, ysSwapped) {
t.Errorf("Expecting %+v received: %+v", ysSwapped, ys)
}
}
func TestYearsLess(t *testing.T) {
ys := &Years{2019, 2010, 2020, 2005, 2018, 2007}
if ys.Less(0, 1) {
t.Errorf("Expecting false received: true")
}
if !ys.Less(1, 2) {
t.Errorf("Expecting true received: false")
}
}
func TestYearsContains(t *testing.T) {
ys := &Years{2019, 2010, 2020, 2005, 2018, 2007}
if !ys.Contains(2019) {
t.Errorf("Expecting true received: false")
}
if ys.Contains(1989) {
t.Errorf("Expecting false received: true")
}
ys = &Years{2013, 2014, 2015}
if !ys.Contains(2013) {
t.Errorf("Expected: true, received: false")
} else if ys.Contains(2012) {
t.Errorf("Expected: false, received: true")
}
}
func TestYearsParse(t *testing.T) {
ys1 := Years{}
ys1.Parse(META_ANY, EmptyString)
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, FIELDS_SEP)
if !reflect.DeepEqual(ys2, ys1) {
t.Errorf("Expected: %+v, received: %+v", ys2, ys1)
}
}
func TestYearsSerialize(t *testing.T) {
ys := &Years{}
eOut := META_ANY
if yString := ys.Serialize(INFIELD_SEP); eOut != yString {
t.Errorf("Expected: %s, received: %s", eOut, yString)
}
ys = &Years{2012}
eOut = "2012"
if yString := ys.Serialize(INFIELD_SEP); eOut != yString {
t.Errorf("Expected: %s, received: %s", eOut, yString)
}
ys = &Years{2013, 2014, 2015}
eOut = "2013;2014;2015"
if yString := ys.Serialize(INFIELD_SEP); eOut != yString {
t.Errorf("Expected: %s, received: %s", eOut, yString)
}
}
func TestYearsEquals(t *testing.T) {
ys1 := Years{2013, 2014, 2015}
ys2 := Years{2013, 2014, 2015}
ys3 := Years{2013, 2014, 2020}
ys4 := Years{}
if !ys1.Equals(ys2) {
t.Errorf("Expected: true, received: true")
} else if ys1.Equals(ys4) {
t.Errorf("Expected: false, received: true")
} else if ys3.Equals(ys2) {
t.Errorf("Expected: false, received: true")
}
}
func TestMonthsSort(t *testing.T) {
m := &Months{}
m.Sort()
if !reflect.DeepEqual(m, &Months{}) {
t.Errorf("Expecting %+v received: %+v", &Months{}, m)
}
m = &Months{time.November, time.July, time.April, time.December, time.October, time.August}
mSorted := &Months{time.April, time.July, time.August, time.October, time.November, time.December}
m.Sort()
if !reflect.DeepEqual(m, mSorted) {
t.Errorf("Expecting %+v received: %+v", mSorted, m)
}
}
func TestMonthsLen(t *testing.T) {
m := &Months{}
if rcv := m.Len(); rcv != 0 {
t.Errorf("Expecting %+v received: %+v", 0, rcv)
}
m = &Months{time.November, time.July, time.April, time.December, time.October, time.August}
if rcv := m.Len(); rcv != 6 {
t.Errorf("Expecting %+v received: %+v", 6, rcv)
}
}
func TestMonthsSwap(t *testing.T) {
m := &Months{time.November, time.July, time.April, time.December, time.October, time.August}
m.Swap(0, 1)
mSwapped := &Months{time.July, time.November, time.April, time.December, time.October, time.August}
if !reflect.DeepEqual(mSwapped, m) {
t.Errorf("Expecting %+v received: %+v", mSwapped, m)
}
}
func TestMonthsLess(t *testing.T) {
m := &Months{time.November, time.July, time.December, time.April, time.October, time.August}
if !m.Less(1, 2) {
t.Errorf("Expecting true received: false")
}
if m.Less(0, 1) {
t.Errorf("Expecting false received: true")
}
}
func TestMonthsContains(t *testing.T) {
m := Months{time.May, time.June, time.July, time.August}
if !m.Contains(time.May) {
t.Errorf("Expected: true, received: false")
} else if m.Contains(time.April) {
t.Errorf("Expected: false, received: true")
}
}
func TestMonthsParse(t *testing.T) {
m1 := Months{}
m1.Parse(META_ANY, EmptyString)
eOut := Months{time.May, time.June, time.July, time.August}
if m1.Parse("5,6,7,8", FIELDS_SEP); !reflect.DeepEqual(eOut, m1) {
t.Errorf("Expected: %+v, received: %+v", eOut, m1)
}
}
func TestMonthsSerialize(t *testing.T) {
mths := &Months{}
if rcv := mths.Serialize(INFIELD_SEP); !reflect.DeepEqual(META_ANY, rcv) {
t.Errorf("Expected: %s, received: %s", META_ANY, rcv)
}
mths = &Months{time.January}
if rcv := mths.Serialize(INFIELD_SEP); !reflect.DeepEqual("1", rcv) {
t.Errorf("Expected: '1', received: %s", rcv)
}
mths = &Months{time.January, time.December}
if rcv := mths.Serialize(INFIELD_SEP); !reflect.DeepEqual("1;12", rcv) {
t.Errorf("Expected: '1;12', received: %s", rcv)
}
}
func TestMonthsIsCompleteYes(t *testing.T) {
months := Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}
if !months.IsComplete() {
t.Error("Error months IsComplete: ", months)
}
}
func TestMonthsEquals(t *testing.T) {
m1 := Months{time.May, time.June, time.July, time.August}
m2 := Months{time.May, time.June, time.July, time.August}
m3 := Months{time.May, time.June, time.July, time.September}
m4 := Months{}
if !m1.Equals(m2) {
t.Errorf("Expected: true, received: fasle")
} else if m1.Equals(m4) {
t.Errorf("Expected: false, received: true")
} else if m3.Equals(m2) {
t.Errorf("Expected: false, received: true")
}
}
func TestMonthDaysSort(t *testing.T) {
md := &MonthDays{}
md.Sort()
if !reflect.DeepEqual(md, &MonthDays{}) {
t.Errorf("Expecting %+v received: %+v", &MonthDays{}, md)
}
md = &MonthDays{7, 3, 5}
mdSorted := &MonthDays{3, 5, 7}
md.Sort()
if !reflect.DeepEqual(md, mdSorted) {
t.Errorf("Expecting %+v received: %+v", mdSorted, md)
}
}
func TestMonthDaysLen(t *testing.T) {
ys := &MonthDays{}
if rcv := ys.Len(); rcv != 0 {
t.Errorf("Expecting %+v received: %+v", 0, rcv)
}
ys = &MonthDays{3, 5, 7, 21, 25, 18}
if rcv := ys.Len(); rcv != 6 {
t.Errorf("Expecting %+v received: %+v", 6, rcv)
}
}
func TestMonthDaysSwap(t *testing.T) {
ys := &MonthDays{3, 5, 7, 21, 25, 18}
ys.Swap(0, 1)
ysSwapped := &MonthDays{5, 3, 7, 21, 25, 18}
if !reflect.DeepEqual(ys, ysSwapped) {
t.Errorf("Expecting %+v received: %+v", ysSwapped, ys)
}
}
func TestMonthDaysLess(t *testing.T) {
ys := &MonthDays{3, 7, 5, 21, 25, 18}
if !ys.Less(2, 3) {
t.Errorf("Expecting true received: false")
}
if ys.Less(1, 2) {
t.Errorf("Expecting false received: true")
}
}
//
func TestMonthStoreRestoreJson(t *testing.T) {
m := Months{time.May, time.June, time.July, time.August}
r, _ := json.Marshal(m)
if string(r) != "[5,6,7,8]" {
t.Errorf("Error serializing months: %v", string(r))
}
o := Months{}
json.Unmarshal(r, &o)
if !reflect.DeepEqual(o, m) {
t.Errorf("Expected %v was %v", m, o)
}
}
func TestMonthDayStoreRestoreJson(t *testing.T) {
md := MonthDays{24, 25, 26}
r, _ := json.Marshal(md)
if string(r) != "[24,25,26]" {
t.Errorf("Error serializing month days: %v", string(r))
}
o := MonthDays{}
json.Unmarshal(r, &o)
if !reflect.DeepEqual(o, md) {
t.Errorf("Expected %v was %v", md, o)
}
}
func TestWeekDayStoreRestoreJson(t *testing.T) {
wd := WeekDays{time.Saturday, time.Sunday}
r, _ := json.Marshal(wd)
if string(r) != "[6,0]" {
t.Errorf("Error serializing week days: %v", string(r))
}
o := WeekDays{}
json.Unmarshal(r, &o)
if !reflect.DeepEqual(o, wd) {
t.Errorf("Expected %v was %v", wd, o)
}
}
func TestMonthDaysSerialize(t *testing.T) {
mds := &MonthDays{}
mdsString := mds.Serialize(";")
expectString := "*any"
if expectString != mdsString {
t.Errorf("Expected: %s, got: %s", expectString, mdsString)
}
mds2 := &MonthDays{1}
mdsString2 := mds2.Serialize(";")
expectString2 := "1"
if expectString2 != mdsString2 {
t.Errorf("Expected: %s, got: %s", expectString2, mdsString2)
}
mds3 := &MonthDays{1, 2, 3, 4, 5}
mdsString3 := mds3.Serialize(";")
expectString3 := "1;2;3;4;5"
if expectString3 != mdsString3 {
t.Errorf("Expected: %s, got: %s", expectString3, mdsString3)
}
}
func TestWeekDaysSerialize(t *testing.T) {
wds := &WeekDays{}
wdsString := wds.Serialize(";")
expectString := "*any"
if expectString != wdsString {
t.Errorf("Expected: %s, got: %s", expectString, wdsString)
}
wds2 := &WeekDays{time.Monday}
wdsString2 := wds2.Serialize(";")
expectString2 := "1"
if expectString2 != wdsString2 {
t.Errorf("Expected: %s, got: %s", expectString2, wdsString2)
}
wds3 := &WeekDays{time.Monday, time.Saturday, time.Sunday}
wdsString3 := wds3.Serialize(";")
expectString3 := "1;6;0"
if expectString3 != wdsString3 {
t.Errorf("Expected: %s, got: %s", expectString3, wdsString3)
}
}
func TestMonthsIsCompleteNot(t *testing.T) {
months := Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November}
if months.IsComplete() {
t.Error("Error months IsComplete: ", months)
}
}
func TestDaysInMonth(t *testing.T) {
if n := DaysInMonth(2016, 4); n != 30 {
t.Error("error calculating days: ", n)
}
if n := DaysInMonth(2016, 2); n != 29 {
t.Error("error calculating days: ", n)
}
if n := DaysInMonth(2016, 1); n != 31 {
t.Error("error calculating days: ", n)
}
if n := DaysInMonth(2016, 12); n != 31 {
t.Error("error calculating days: ", n)
}
if n := DaysInMonth(2015, 2); n != 28 {
t.Error("error calculating days: ", n)
}
}
func TestDaysInYear(t *testing.T) {
if n := DaysInYear(2016); n != 366 {
t.Error("error calculating days: ", n)
}
if n := DaysInYear(2015); n != 365 {
t.Error("error calculating days: ", n)
}
}
func TestWeekDaysEquals(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 TestMonthDaysEquals(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 TestWeekDaysParse(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 TestMonthDaysParse(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 TestWeekDaysContains(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 TestMonthDaysContains(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)
}
}