Skip to content

Commit

Permalink
Added test for NewTimeStringConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
adragusin committed Jul 2, 2020
1 parent e4409e0 commit 60e9bee
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 2 deletions.
182 changes: 180 additions & 2 deletions engine/model_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ func TestTpDestinationsAPItoModelDestination(t *testing.T) {
if !reflect.DeepEqual(rcv, eOut) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}

}

}

func TestTpDestinationsAsTPDestinations(t *testing.T) {
Expand All @@ -177,6 +175,144 @@ func TestTpDestinationsAsTPDestinations(t *testing.T) {

}

func TestMapTPTimings(t *testing.T) {
var tps []*utils.ApierTPTiming
eOut := map[string]*utils.TPTiming{}
if rcv, err := MapTPTimings(tps); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
}

tps = []*utils.ApierTPTiming{
&utils.ApierTPTiming{
TPid: "TPid1",
ID: "ID1",
},
}
eOut = map[string]*utils.TPTiming{
"ID1": &utils.TPTiming{
ID: "ID1",
Years: utils.Years{},
Months: utils.Months{},
MonthDays: utils.MonthDays{},
WeekDays: utils.WeekDays{},
},
}
if rcv, err := MapTPTimings(tps); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
tps = []*utils.ApierTPTiming{
&utils.ApierTPTiming{
TPid: "TPid1",
ID: "ID1",
Months: "1;2;3;4",
},
}
eOut = map[string]*utils.TPTiming{
"ID1": &utils.TPTiming{
ID: "ID1",
Years: utils.Years{},
Months: utils.Months{1, 2, 3, 4},
MonthDays: utils.MonthDays{},
WeekDays: utils.WeekDays{},
},
}
if rcv, err := MapTPTimings(tps); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
//same id error
tps = []*utils.ApierTPTiming{
&utils.ApierTPTiming{
TPid: "TPid1",
ID: "ID1",
Months: "1;2;3;4",
},
&utils.ApierTPTiming{
TPid: "TPid1",
ID: "ID1",
Months: "1;2;3;4",
},
}
eOut = map[string]*utils.TPTiming{
"ID1": &utils.TPTiming{
ID: "ID1",
Years: utils.Years{},
Months: utils.Months{1, 2, 3, 4},
MonthDays: utils.MonthDays{},
WeekDays: utils.WeekDays{},
},
}
if rcv, err := MapTPTimings(tps); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
}

func TestAPItoModelTimings(t *testing.T) {
ts := []*utils.ApierTPTiming{}
eOut := TpTimings{}
if rcv := APItoModelTimings(ts); rcv != nil {
t.Errorf("Expecting: nil, received: %+v", utils.ToJSON(rcv))
}

ts = []*utils.ApierTPTiming{
&utils.ApierTPTiming{
TPid: "TPid1",
ID: "ID1",
Months: "1;2;3;4",
},
}
eOut = TpTimings{
TpTiming{
Tpid: "TPid1",
Months: "1;2;3;4",
Tag: "ID1",
},
}
if rcv := APItoModelTimings(ts); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
ts = []*utils.ApierTPTiming{
&utils.ApierTPTiming{
TPid: "TPid1",
ID: "ID1",
Months: "1;2;3;4",
},
&utils.ApierTPTiming{
TPid: "TPid2",
ID: "ID2",
Months: "1;2;3;4",
MonthDays: "1;2;3;4;28",
Years: "2020;2019",
WeekDays: "4;5",
},
}
eOut = TpTimings{
TpTiming{
Tpid: "TPid1",
Months: "1;2;3;4",
Tag: "ID1",
},
TpTiming{
Tpid: "TPid2",
Tag: "ID2",
Months: "1;2;3;4",
MonthDays: "1;2;3;4;28",
Years: "2020;2019",
WeekDays: "4;5",
},
}
if rcv := APItoModelTimings(ts); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
}

func TestTPRateAsExportSlice(t *testing.T) {
tpRate := &utils.TPRateRALs{
TPid: "TEST_TPID",
Expand Down Expand Up @@ -215,6 +351,48 @@ func TestTPRateAsExportSlice(t *testing.T) {
}
}

func TestAPItoModelRates(t *testing.T) {
rs := []*utils.TPRateRALs{}
eOut := TpRates{}
if rcv := APItoModelRates(rs); rcv != nil {
t.Errorf("Expecting: nil, received: %+v", utils.ToJSON(rcv))
}

rs = []*utils.TPRateRALs{
&utils.TPRateRALs{
ID: "SomeID",
TPid: "TPid",
RateSlots: []*utils.RateSlot{
&utils.RateSlot{
ConnectFee: 0.7,
Rate: 0.8,
},
&utils.RateSlot{
ConnectFee: 0.77,
Rate: 0.88,
},
},
},
}
eOut = TpRates{
TpRate{
Tpid: "TPid",
Tag: "SomeID",
ConnectFee: 0.7,
Rate: 0.8,
},
TpRate{
Tpid: "TPid",
Tag: "SomeID",
ConnectFee: 0.77,
Rate: 0.88,
},
}
if rcv := APItoModelRates(rs); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
}

func TestTPDestinationRateAsExportSlice(t *testing.T) {
tpDstRate := &utils.TPDestinationRate{
TPid: "TEST_TPID",
Expand Down
71 changes: 71 additions & 0 deletions utils/dataconverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,74 @@ func TestNewDataConverterMustCompile2(t *testing.T) {
}()
NewDataConverterMustCompile(MetaMultiply)
}

func TestNewTimeStringConverter(t *testing.T) {
//empty
eOut := &TimeStringConverter{Layout: EmptyString}
if rcv, err := NewTimeStringConverter(EmptyString); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
}

//default
eOut = &TimeStringConverter{Layout: time.RFC3339}
var rcv DataConverter
var err error
if rcv, err = NewTimeStringConverter(time.RFC3339); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
}
exp := "2015-07-07T14:52:08Z"
if rcv, err := rcv.Convert("1436280728"); err != nil {
t.Error(err)
} else if rcv.(string) != exp {
t.Errorf("Expecting: %+v, received: %+v", exp, rcv)
}
exp = "2013-07-30T19:33:10Z"
if rcv, err := rcv.Convert("1375212790"); err != nil {
t.Error(err)
} else if rcv.(string) != exp {
t.Errorf("Expecting: %+v, received: %+v", exp, rcv)
}

//other
eOut = &TimeStringConverter{"020106150400"}
if rcv, err = NewTimeStringConverter("020106150400"); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
}
exp = "070715145200"
if rcv, err := rcv.Convert("1436280728"); err != nil {
t.Error(err)
} else if rcv.(string) != exp {
t.Errorf("Expecting: %+v, received: %+v", exp, rcv)
}
exp = "290720175900"
if rcv, err := rcv.Convert("2020-07-29T17:59:59Z"); err != nil {
t.Error(err)
} else if rcv.(string) != exp {
t.Errorf("Expecting: %+v, received: %+v", exp, rcv)
}

//wrong cases
eOut = &TimeStringConverter{"not really a good time"}
if rcv, err = NewTimeStringConverter("not really a good time"); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
}
exp = "not really a good time"
if rcv, err := rcv.Convert(EmptyString); err != nil {
t.Error(err)
} else if rcv.(string) != exp {
t.Errorf("Expecting: %+v, received: %+v", exp, rcv)
}
if rcv, err := rcv.Convert("1375212790"); err != nil {
t.Error(err)
} else if rcv.(string) != exp {
t.Errorf("Expecting: %+v, received: %+v", exp, rcv)
}
}

0 comments on commit 60e9bee

Please sign in to comment.