From f1d0be170d723f82dd0ff8b69d2e4cae3de5779b Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 5 Jun 2024 11:10:42 -0400 Subject: [PATCH] Revise and Add new unit tests on engine --- engine/action_trigger_test.go | 75 +++++++++++++++++++++++++++++++++++ engine/calldesc_test.go | 3 -- engine/libindex_test.go | 40 +++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 engine/libindex_test.go diff --git a/engine/action_trigger_test.go b/engine/action_trigger_test.go index 9272b8454..500e94d70 100644 --- a/engine/action_trigger_test.go +++ b/engine/action_trigger_test.go @@ -31,6 +31,7 @@ import ( "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/google/go-cmp/cmp" ) func TestActionTriggerClone(t *testing.T) { @@ -459,3 +460,77 @@ func TestStringToJson(t *testing.T) { t.Errorf("String method returned unexpected result, got: %s, want: %s", result, string(expected)) } } + +func TestUpdateInitialValue(t *testing.T) { + tests := []struct { + name string + as *AccountSummary + old *AccountSummary + expected *AccountSummary + }{ + { + name: "old is nil", + as: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Initial: 10, Value: 20}, + }, + }, + old: nil, + expected: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Initial: 10, Value: 20}, + }, + }, + }, + { + name: "update initial values", + as: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Initial: 10, Value: 20}, + {UUID: "2", Initial: 15, Value: 25}, + }, + }, + old: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Initial: 5, Value: 10}, + {UUID: "3", Initial: 20, Value: 30}, + }, + }, + expected: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Initial: 5, Value: 20}, + {UUID: "2", Initial: 15, Value: 25}, + {UUID: "3", Initial: 0, Value: 0}, + }, + }, + }, + { + name: "no matching UUIDs", + as: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "4", Initial: 10, Value: 20}, + }, + }, + old: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "5", Initial: 5, Value: 10}, + }, + }, + expected: &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "4", Initial: 10, Value: 20}, + {UUID: "5", Initial: 0, Value: 0}, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.as.UpdateInitialValue(tt.old) + if diff := cmp.Diff(tt.as, tt.expected); diff != "" { + t.Errorf("UpdateInitialValue() mismatch (-want +got):\n%s", diff) + } + }) + } +} diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index e05556291..67f3fd7fc 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -2811,13 +2811,10 @@ func TestCallDescriptorgetgetRatingPlansForPrefix(t *testing.T) { } // Default FallbackDepth is 3 got, err := cd.getRatingPlansForPrefix("testkey", 4) - if err != utils.ErrMaxRecursionDepth { t.Errorf("getRatingPlansForPrefix() expected %v, got %v, ", utils.ErrMaxRecursionDepth, err) - } else if got != 4 { t.Errorf("getRatingPlansForPrefix() expected %v, got %v, ", 4, got) } - Cache.Clear(nil) } diff --git a/engine/libindex_test.go b/engine/libindex_test.go new file mode 100644 index 000000000..01d2f0e7e --- /dev/null +++ b/engine/libindex_test.go @@ -0,0 +1,40 @@ +/* +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 +*/ + +package engine + +import "testing" + +func TestLibIndexIsDynamicDPPath(t *testing.T) { + tests := []struct { + name string + path string + want bool + }{ + {"Dynamic Path (stats)", "~*stats/", true}, + {"Static Path", "/static/", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := IsDynamicDPPath(tt.path) + if got != tt.want { + t.Errorf("IsDynamicDPPath(%q) = %v, want %v", tt.path, got, tt.want) + } + }) + } +}