From ba8cd0cd9a815a00c5f581e3d414adb00d89be10 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 11 Dec 2024 10:39:00 -0500 Subject: [PATCH] Add new unit tests on engine && utils --- engine/balance_filter_test.go | 63 +++++++++++++++++++++++++ utils/dataconverter_test.go | 88 +++++++++++++++++++++++++++++++++++ utils/reflect_test.go | 67 ++++++++++++++++++++++++++ 3 files changed, 218 insertions(+) diff --git a/engine/balance_filter_test.go b/engine/balance_filter_test.go index deec2625b..b1ae198ac 100644 --- a/engine/balance_filter_test.go +++ b/engine/balance_filter_test.go @@ -18,6 +18,9 @@ along with this program. If not, see package engine import ( + "bytes" + "log" + "strings" "testing" "time" @@ -648,3 +651,63 @@ func TestBalanceFilterString(t *testing.T) { t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, result) } } + +func TestShowStatistics(t *testing.T) { + tpr := &TpReader{ + destinations: map[string]*Destination{ + "dest1": {Id: "dest1", Prefixes: []string{"123", "456"}}, + "dest2": {Id: "dest2", Prefixes: []string{"789"}}, + }, + ratingPlans: map[string]*RatingPlan{ + "rp1": {Id: "rp1", DestinationRates: map[string]RPRateList{"rate1": {}, "rate2": {}}}, + }, + ratingProfiles: map[string]*RatingProfile{ + "profile1": {Id: "profile1", RatingPlanActivations: RatingPlanActivations{ + &RatingPlanActivation{ActivationTime: time.Now(), RatingPlanId: "rp1"}, + }}, + }, + actions: map[string][]*Action{ + "action1": {{Id: "action1", ActionType: "type1"}}, + }, + actionPlans: map[string]*ActionPlan{ + "plan1": {}, + }, + } + + var logBuffer bytes.Buffer + log.SetOutput(&logBuffer) + defer log.SetOutput(nil) + + tpr.ShowStatistics() + + logOutput := logBuffer.String() + + if !strings.Contains(logOutput, "Destinations: 2") { + t.Errorf("Expected log to contain 'Destinations: 2', got: %s", logOutput) + } + if !strings.Contains(logOutput, "Avg Prefixes: 1") { + t.Errorf("Expected log to contain 'Avg Prefixes: 1', got: %s", logOutput) + } + + if !strings.Contains(logOutput, "Rating plans: 1") { + t.Errorf("Expected log to contain 'Rating plans: 1', got: %s", logOutput) + } + if !strings.Contains(logOutput, "Avg Destination Rates: 2") { + t.Errorf("Expected log to contain 'Avg Destination Rates: 2', got: %s", logOutput) + } + + if !strings.Contains(logOutput, "Rating profiles: 1") { + t.Errorf("Expected log to contain 'Rating profiles: 1', got: %s", logOutput) + } + if !strings.Contains(logOutput, "Avg Activations: 1") { + t.Errorf("Expected log to contain 'Avg Activations: 1', got: %s", logOutput) + } + + if !strings.Contains(logOutput, "Actions: 1") { + t.Errorf("Expected log to contain 'Actions: 1', got: %s", logOutput) + } + + if !strings.Contains(logOutput, "Action plans: 1") { + t.Errorf("Expected log to contain 'Action plans: 1', got: %s", logOutput) + } +} diff --git a/utils/dataconverter_test.go b/utils/dataconverter_test.go index 9eceaf6cf..a69c22ba8 100644 --- a/utils/dataconverter_test.go +++ b/utils/dataconverter_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package utils import ( + "errors" "math" "net" "reflect" @@ -1726,3 +1727,90 @@ func TestURLEncodeConverter(t *testing.T) { }) } } + +func TestStripConverterConvert(t *testing.T) { + tests := []struct { + name string + converter StripConverter + input any + wantOutput any + wantErr error + }{ + { + name: "Valid prefix strip", + converter: StripConverter{ + amount: 3, + side: MetaPrefix, + substr: EmptyString, + }, + input: "DatSms", + wantOutput: "Sms", + wantErr: nil, + }, + { + name: "Valid suffix strip", + converter: StripConverter{ + amount: 3, + side: MetaSuffix, + substr: EmptyString, + }, + input: "abcdef", + wantOutput: "abc", + wantErr: nil, + }, + { + name: "Invalid input type", + converter: StripConverter{ + amount: 3, + side: MetaPrefix, + }, + input: 123, + wantOutput: nil, + wantErr: ErrCastFailed, + }, + { + name: "No strip with non-positive amount", + converter: StripConverter{ + amount: 0, + side: MetaPrefix, + }, + input: "cgrates", + wantOutput: "cgrates", + wantErr: nil, + }, + { + name: "Trim prefix using substring", + converter: StripConverter{ + amount: -1, + side: MetaPrefix, + substr: "data", + }, + input: "dataTariff", + wantOutput: "Tariff", + wantErr: nil, + }, + { + name: "Trim both sides using substring", + converter: StripConverter{ + amount: -1, + side: MetaBoth, + substr: "a", + }, + input: "aaaabcdefaaa", + wantOutput: "bcdef", + wantErr: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.converter.Convert(tt.input) + if got != tt.wantOutput { + t.Errorf("Convert() = %v, want %v", got, tt.wantOutput) + } + if !errors.Is(err, tt.wantErr) { + t.Errorf("Convert() error = %v, want %v", err, tt.wantErr) + } + }) + } +} diff --git a/utils/reflect_test.go b/utils/reflect_test.go index eddf185f7..58b2eb442 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -2028,3 +2028,70 @@ func TestMapIfaceTimeAsString(t *testing.T) { t.Errorf("Expected <%q>, received <%v>", "simpleValue", mapAny["simpleKey"]) } } + +func TestDifferences(t *testing.T) { + tests := []struct { + name string + tm string + items []any + wantDiff any + wantErr bool + wantErrMsg string + }{ + { + name: "Valid duration difference", + tm: "", + items: []any{time.Duration(5 * time.Hour), time.Duration(2 * time.Hour)}, + wantDiff: time.Duration(3 * time.Hour), + wantErr: false, + }, + { + name: "Valid float64 difference", + tm: "", + items: []any{10.5, 3.2}, + wantDiff: 7.3, + wantErr: false, + }, + { + name: "Valid int64 difference", + tm: "", + items: []any{int64(100), int64(30)}, + wantDiff: int64(70), + wantErr: false, + }, + { + name: "Valid int difference", + tm: "", + items: []any{100, 30}, + wantDiff: int64(70), + wantErr: false, + }, + { + name: "Unsupported type", + tm: "", + items: []any{"unsupported", "another"}, + wantDiff: nil, + wantErr: true, + wantErrMsg: "unsupported type", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotDiff, err := Difference(tt.tm, tt.items...) + if (err != nil) != tt.wantErr { + t.Errorf("Difference() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil && tt.wantErrMsg != "" { + if len(err.Error()) < len(tt.wantErrMsg) || err.Error()[:len(tt.wantErrMsg)] != tt.wantErrMsg { + t.Errorf("Difference() error = %v, wantErrMsg %v", err.Error(), tt.wantErrMsg) + return + } + } + if gotDiff != tt.wantDiff { + t.Errorf("Difference() = %v, want %v", gotDiff, tt.wantDiff) + } + }) + } +}