diff --git a/engine/account_test.go b/engine/account_test.go index 3d0aafd74..68711fe84 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -3335,10 +3335,91 @@ func TestEngineToStringJSON(t *testing.T) { } want, err := json.Marshal(acc) if err != nil { - t.Errorf("Error marshalling Account to JSON: %v", err) + t.Error(err) } got := acc.String() if got != string(want) { - t.Errorf("Expected JSON: %s, got: %s", want, got) + t.Errorf("acc.String()=%s, want%s", got, want) + } +} + +func TestEngineGetID(t *testing.T) { + tests := []struct { + name string + accID string + want string + }{ + { + name: "Valid ID format", + accID: "prefix" + utils.ConcatenatedKeySep + "suffix", + want: "suffix", + }, + { + name: "Invalid ID format (missing separator)", + accID: "invalidID", + want: "", + }, + { + name: "Invalid ID format (too many parts)", + accID: "prefix" + utils.ConcatenatedKeySep + "suffix" + utils.ConcatenatedKeySep + "extra", + want: "", + }, + { + name: "Empty ID", + accID: "", + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + acc := &Account{ID: tt.accID} + got := acc.GetID() + if got != tt.want { + t.Errorf("Expected ID: %s, Got: %s", tt.want, got) + } + }) + } +} + +func TestEngineNewAccountSummaryFromJSON(t *testing.T) { + tests := []struct { + name string + jsonStr string + want *AccountSummary + }{ + { + name: "Valid JSON", + jsonStr: `{"tenant": "cgrates.org", "id": "1234", "balanceSummaries": [], "allowNegative": false, "disabled": true}`, + want: &AccountSummary{ + Tenant: "cgrates.org", + ID: "1234", + BalanceSummaries: BalanceSummaries{}, + AllowNegative: false, + Disabled: true, + }, + }, + { + name: "Empty JSON", + jsonStr: "", + want: nil, + }, + { + name: "Null JSON", + jsonStr: "null", + want: nil, + }, + { + name: "Invalid JSON", + jsonStr: "{invalid: json}", + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, _ := NewAccountSummaryFromJSON(tt.jsonStr) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewAccountSummaryFromJSON(%q) got = %v, want %v", tt.jsonStr, got, tt.want) + } + }) } } diff --git a/engine/ratingprofile_test.go b/engine/ratingprofile_test.go index 5993689cb..67e98a3db 100644 --- a/engine/ratingprofile_test.go +++ b/engine/ratingprofile_test.go @@ -18,12 +18,12 @@ along with this program. If not, see package engine import ( - "reflect" "testing" "time" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/google/go-cmp/cmp" ) func TestGetRatingProfileForPrefix(t *testing.T) { @@ -479,14 +479,13 @@ func TestEngineSwapRpasIndex(t *testing.T) { FallbackKeys: []string{"key3"}, } initialRPAs := RatingPlanActivations{plan1, plan2} - index1 := 0 - index2 := 1 want := RatingPlanActivations{plan2, plan1} rpas := make(RatingPlanActivations, len(initialRPAs)) copy(rpas, initialRPAs) - rpas.Swap(index1, index2) - if !reflect.DeepEqual(rpas, want) { - t.Errorf("Expected RatingPlanActivations after swap: %v, got: %v", want, rpas) + rpas.Swap(0, 1) + diff := cmp.Diff(rpas, want) + if diff != "" { + t.Errorf("Expected RatingPlanActivations after swap to be equal to want: (-got, +want)\n%s", diff) } } diff --git a/engine/units_counter_test.go b/engine/units_counter_test.go index dccf07e11..5bfefaea1 100644 --- a/engine/units_counter_test.go +++ b/engine/units_counter_test.go @@ -957,7 +957,7 @@ func TestEngineCounterFilterString(t *testing.T) { } want, err := json.Marshal(testFilter) if err != nil { - t.Errorf("Error marshalling CounterFilter to JSON: %v", err) + t.Error(err) } got := testFilter.String() if got != string(want) { @@ -971,7 +971,7 @@ func TestEngineUnitCounterString(t *testing.T) { } want, err := json.Marshal(testCounter) if err != nil { - t.Errorf("Error marshalling UnitCounter to JSON: %v", err) + t.Error(err) } got := testCounter.String() if got != string(want) {