From f579e0522105f5bb4ce01319eb844f8480d5c7da Mon Sep 17 00:00:00 2001 From: arberkatellari Date: Thu, 17 Nov 2022 10:01:57 -0500 Subject: [PATCH] Improving coverage at utils --- utils/eventcharges_test.go | 167 +++++++++++++++++++++++++++++++++++++ utils/librates_test.go | 5 +- utils/logger_test.go | 29 +++++-- utils/marshaler_test.go | 53 ++++++++---- utils/reflect_test.go | 25 ++++++ 5 files changed, 253 insertions(+), 26 deletions(-) diff --git a/utils/eventcharges_test.go b/utils/eventcharges_test.go index 1c2eef487..681a924ce 100644 --- a/utils/eventcharges_test.go +++ b/utils/eventcharges_test.go @@ -809,6 +809,18 @@ func TestEventChargerMerge(t *testing.T) { CompressFactor: 2, }, }, + Rates: map[string]*IntervalRate{ + "RATE_3": { + IntervalStart: NewDecimal(0, 0), + FixedFee: NewDecimal(4, 1), + RecurrentFee: NewDecimal(24, 1), + }, + "RATE_4": { + IntervalStart: NewDecimal(12, 1), + FixedFee: NewDecimal(1, 0), + RecurrentFee: NewDecimal(5, 2), + }, + }, Accounts: map[string]*Account{ "ACC3": { Tenant: CGRateSorg, @@ -937,6 +949,17 @@ func TestEventChargerMerge(t *testing.T) { FixedFee: NewDecimal(1, 0), RecurrentFee: NewDecimal(5, 2), }, + + "RATE_3": { + IntervalStart: NewDecimal(0, 0), + FixedFee: NewDecimal(4, 1), + RecurrentFee: NewDecimal(24, 1), + }, + "RATE_4": { + IntervalStart: NewDecimal(12, 1), + FixedFee: NewDecimal(1, 0), + RecurrentFee: NewDecimal(5, 2), + }, }, Accounts: map[string]*Account{ "ACC1": { @@ -1666,4 +1689,148 @@ func TestEqualsAccountCharges(t *testing.T) { if rcv := ac.equals(nAc); rcv != true { t.Errorf("Expected , Recevied <%v>", rcv) } + + ac = &AccountCharge{ + AttributeIDs: []string{"test", "range"}, + } + nAc = &AccountCharge{ + AttributeIDs: []string{"test2", "range"}, + } + + if rcv := ac.equals(nAc); rcv != false { + t.Error(rcv) + } +} + +func TestSyncIDsEventCharges(t *testing.T) { + eEvChgs := &EventCharges{ + Charges: []*ChargeEntry{ + { + ChargingID: "GENUUID3", + }, + }, + Accounting: map[string]*AccountCharge{ + "THIS_GENUUID1": { + AccountID: "TestEventChargesEquals", + }, + "GENUUID3": { + AccountID: "TestEventChargesMerge", + BalanceID: "CONCRETE1", + Units: NewDecimal(8, 1), + BalanceLimit: NewDecimal(200, 0), + UnitFactorID: "GENUUID_FACTOR3", + RatingID: "GENUUID_RATING1", + JoinedChargeIDs: []string{"THIS_GENUUID1"}, + }, + }, + UnitFactors: map[string]*UnitFactor{ + "GENUUID_FACTOR3": { + Factor: NewDecimal(100, 0), + FilterIDs: []string{"*string:~*req.Account:1003"}, + }, + }, + Rating: map[string]*RateSInterval{ + "GENUUID_RATING1": { + Increments: []*RateSIncrement{ + { + Usage: NewDecimal(int64(time.Minute), 0), + CompressFactor: 1, + }, + }, + IntervalStart: NewDecimal(int64(time.Second), 0), + CompressFactor: 1, + }, + }, + } + + newEc := &EventCharges{ + Charges: []*ChargeEntry{ + { + ChargingID: "GENUUID2", + CompressFactor: 1, + }, + }, + Accounting: map[string]*AccountCharge{ + "THIS_GENUUID2": { + AccountID: "TestEventChargesEquals", + BalanceID: "CONCRETE1", + Units: NewDecimal(8, 1), + BalanceLimit: NewDecimal(200, 0), + UnitFactorID: "GENUUID_FACTOR1", + }, + "GENUUID2": { + AccountID: "TestEventChargesMerge", + BalanceID: "CONCRETE1", + Units: NewDecimal(8, 1), + BalanceLimit: NewDecimal(200, 0), + UnitFactorID: "GENUUID_FACTOR2", + RatingID: "GENUUID_RATING2", + JoinedChargeIDs: []string{"THIS_GENUUID2"}, + }, + }, + UnitFactors: map[string]*UnitFactor{ + "GENUUID_FACTOR2": { + Factor: NewDecimal(100, 0), + FilterIDs: []string{"*string:~*req.Account:1003"}, + }, + }, + Rating: map[string]*RateSInterval{ + "GENUUID_RATING2": { + Increments: []*RateSIncrement{ + { + Usage: NewDecimal(int64(time.Minute), 0), + RateIntervalIndex: 0, + RateID: "RATE_2", + CompressFactor: 1, + }, + }, + IntervalStart: NewDecimal(int64(time.Second), 0), + CompressFactor: 1, + }, + }, + } + + expEc := &EventCharges{ + Charges: []*ChargeEntry{ + { + ChargingID: "GENUUID3", + }, + }, + Accounting: map[string]*AccountCharge{ + "THIS_GENUUID1": { + AccountID: "TestEventChargesEquals", + }, + "GENUUID3": { + AccountID: "TestEventChargesMerge", + BalanceID: "CONCRETE1", + Units: NewDecimal(8, 1), + BalanceLimit: NewDecimal(200, 0), + UnitFactorID: "GENUUID_FACTOR3", + RatingID: "GENUUID_RATING1", + JoinedChargeIDs: []string{"THIS_GENUUID1"}, + }, + }, + UnitFactors: map[string]*UnitFactor{ + "GENUUID_FACTOR3": { + Factor: NewDecimal(100, 0), + FilterIDs: []string{"*string:~*req.Account:1003"}, + }, + }, + Rating: map[string]*RateSInterval{ + "GENUUID_RATING1": { + Increments: []*RateSIncrement{ + { + Usage: NewDecimal(int64(time.Minute), 0), + CompressFactor: 1, + }, + }, + IntervalStart: NewDecimal(int64(time.Second), 0), + CompressFactor: 1, + }, + }, + } + eEvChgs.SyncIDs(newEc) + if !reflect.DeepEqual(expEc, eEvChgs) { + t.Errorf("Expected %v \n but received \n %v", ToJSON(expEc), ToJSON(eEvChgs)) + } } diff --git a/utils/librates_test.go b/utils/librates_test.go index 780852224..32d16dd1d 100644 --- a/utils/librates_test.go +++ b/utils/librates_test.go @@ -2255,11 +2255,8 @@ func TestAsRateAPIConvert(t *testing.T) { Weights: "notEmpty", } expErr := "invalid DynamicWeight format for string " - var exp *Rate - if rcv, err := ext.AsRate(); err == nil || err.Error() != expErr { + if _, err := ext.AsRate(); err == nil || err.Error() != expErr { t.Errorf("%+v", err) - } else if !reflect.DeepEqual(exp, rcv) { - t.Errorf("Expected <%+v %T>, Recevied <%+v %T>", ToJSON(exp), exp, ToJSON(rcv), rcv) } } diff --git a/utils/logger_test.go b/utils/logger_test.go index 5703e02dc..e651720b9 100644 --- a/utils/logger_test.go +++ b/utils/logger_test.go @@ -385,10 +385,25 @@ func TestCloseStdLogger(t *testing.T) { } } -// unfinished -// func TestWriteStdLogger(t *testing.T) { -// sl := &StdLogger{w: Logger} -// if rcv, err := sl.Write([]byte{}); err != nil { -// t.Errorf("Expected , received %v %v", err, rcv) -// } -// } +func TestWriteStdLogger(t *testing.T) { + sl := &StdLogger{ + w: &logWriter{ + log.New(io.Discard, EmptyString, log.LstdFlags), + }, + } + exp := 1 + if rcv, err := sl.Write([]byte{222}); err != nil { + t.Error(err) + } else if rcv != exp { + t.Errorf("Expected <%v>, received <%v>", exp, rcv) + } +} + +func TestGetLogLevelStdLogger(t *testing.T) { + sl := &StdLogger{} + exp := 0 + if rcv := sl.GetLogLevel(); rcv != exp { + t.Errorf("Expected <%v>, received %v %T", exp, rcv, rcv) + } + +} diff --git a/utils/marshaler_test.go b/utils/marshaler_test.go index af04712cb..b4305538c 100644 --- a/utils/marshaler_test.go +++ b/utils/marshaler_test.go @@ -23,6 +23,7 @@ import ( "reflect" "testing" + "github.com/cgrates/ugocodec/codec" "go.mongodb.org/mongo-driver/bson" ) @@ -72,22 +73,22 @@ type dummyStruct struct { Field string } -// func TestJSONUnmarshaler(t *testing.T) { -// data := []byte(`{"Field": "some_string"}`) -// jsnM := &JSONMarshaler{} -// dS := dummyStruct{ -// Field: "some_string", -// } +func TestJSONUnmarshaler(t *testing.T) { + data := []byte(`{"Field": "some_string"}`) + jsnM := &JSONMarshaler{} + dS := dummyStruct{ + Field: "some_string", + } -// var ndS dummyStruct -// err := jsnM.Unmarshal(data, &ndS) -// if err != nil { -// t.Error(err) -// } -// if reflect.DeepEqual(dS, ndS) { -// t.Errorf("Expected: %s , received: %s", ToJSON(dS), ToJSON(ndS)) -// } -// } + var ndS dummyStruct + err := jsnM.Unmarshal(data, &ndS) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(dS, ndS) { + t.Errorf("Expected: %s , received: %s", ToJSON(dS), ToJSON(ndS)) + } +} func TestBSONMarshaler(t *testing.T) { v := bson.M{"ts": "test"} @@ -143,3 +144,25 @@ func TestJSONBufUnmarshaler(t *testing.T) { t.Errorf("Expected %v\n but received %v", s, rcv) } } +func TestBsonUnmarshal(t *testing.T) { + v := bson.M{"ts": "test"} + bsnM := &BSONMarshaler{} + rcvM, _ := bsnM.Marshal(v) + dS := dummyStruct{} + + var ndS dummyStruct + err := bsnM.Unmarshal(rcvM, &ndS) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(dS, ndS) { + t.Errorf("Expected: %s , received: %s", ToJSON(dS), ToJSON(ndS)) + } +} + +func TestNewBincMarshler(t *testing.T) { + exp := &BincMarshaler{new(codec.BincHandle)} + if rcv := NewBincMarshaler(); !reflect.DeepEqual(rcv, exp) { + t.Errorf("Expected <%+v>, Received <%+v>", ToJSON(exp), ToJSON(rcv)) + } +} diff --git a/utils/reflect_test.go b/utils/reflect_test.go index 45141241b..ea4ec1055 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -1755,3 +1755,28 @@ func TestStringAsBig(t *testing.T) { t.Errorf("Expected %v \n but received \n %v", exp, v) } } + +func TestIfaceAsTInt(t *testing.T) { + + var testInt32 int32 = 132 + var testInt64 int64 = 164 + var testFloat32 float32 = 15.32 + var expInt32 int32 = 132 + var expInt64 int64 = 164 + var expFloat32 float32 = 15.32 + if rcv, err := IfaceAsInt(testInt32); err != nil { + t.Error(err) + } else if rcv != int(expInt32) { + t.Errorf("Expected <%+v>, received <%+v>", expInt32, rcv) + } + if rcv, err := IfaceAsInt(testInt64); err != nil { + t.Error(err) + } else if rcv != int(expInt64) { + t.Errorf("Expected <%+v>, received <%+v>", expInt32, rcv) + } + if rcv, err := IfaceAsInt(testFloat32); err != nil { + t.Error(err) + } else if rcv != int(expFloat32) { + t.Errorf("Expected <%+v>, received <%+v>", expInt32, rcv) + } +}