From 36c623599ec132b7d8d675640a22f0d2445b4ffc Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Tue, 4 Jun 2024 10:53:37 -0400 Subject: [PATCH] Add new unit tests on engine --- engine/action_trigger_test.go | 25 +++++++++++++++++ engine/calldesc_test.go | 53 +++++++++++++++++++++++++++++++++++ engine/cdrs_test.go | 22 +++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/engine/action_trigger_test.go b/engine/action_trigger_test.go index 9e6f0c017..9272b8454 100644 --- a/engine/action_trigger_test.go +++ b/engine/action_trigger_test.go @@ -20,6 +20,7 @@ package engine import ( "bytes" + "encoding/json" "fmt" "log" "os" @@ -434,3 +435,27 @@ func TestEngineActionTriggerEquals(t *testing.T) { t.Errorf("Expected %v not to equal %v, but it did.", at1, at3) } } + +func TestStringToJson(t *testing.T) { + at := &ActionTrigger{ + ID: "321", + UniqueID: "122", + ThresholdType: "*min_event_counter", + ThresholdValue: 10.0, + Recurrent: true, + MinSleep: 5 * time.Second, + ExpirationDate: time.Now().AddDate(0, 0, 7), + ActivationDate: time.Now(), + Balance: nil, + Weight: 0.5, + ActionsID: "123", + MinQueuedItems: 100, + Executed: false, + LastExecutionTime: time.Now().Add(-time.Hour), + } + result := at.String() + expected, _ := json.Marshal(at) + if result != string(expected) { + t.Errorf("String method returned unexpected result, got: %s, want: %s", result, string(expected)) + } +} diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index d7d5db7c1..e05556291 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -2768,3 +2768,56 @@ func TestEngineCallDescriptorString(t *testing.T) { t.Errorf("Expected CallDescriptor.String() to return %s, got %s", want, got) } } + +func TestCallDescriptorClone(t *testing.T) { + originalCD := &CallDescriptor{ + Category: "call", + Tenant: "cgrates.org", + Subject: "user", + Account: "testAccount", + Destination: "local", + TimeStart: time.Date(2022, time.January, 7, 16, 60, 0, 0, time.UTC), + TimeEnd: time.Date(2022, time.January, 7, 16, 60, 0, 0, time.UTC), + LoopIndex: 1, + DurationIndex: time.Minute * 3, + FallbackSubject: "testfallbacksubject", + ToR: utils.MetaVoice, + ExtraFields: map[string]string{"key1": "value1"}, + MaxRate: 10.0, + MaxRateUnit: time.Minute, + MaxCostSoFar: 0.5, + CgrID: "grid1", + RunID: "runID123", + ForceDuration: false, + PerformRounding: true, + DenyNegativeAccount: true, + DryRun: false, + } + clonedCD := originalCD.Clone() + if originalCD == clonedCD { + t.Errorf("Expected cloned CallDescriptor to be a new instance, got pointer to original") + } +} + +func TestCallDescriptorgetgetRatingPlansForPrefix(t *testing.T) { + Cache.Clear(nil) + cd := CallDescriptor{ + Category: "sms", + Tenant: "cgrates.org", + Subject: "testAccITAddBalanceWithDestinations", + Account: "testAccITAddBalanceWithDestinations", + Destination: "1003", + DurationIndex: 0, + } + // 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/cdrs_test.go b/engine/cdrs_test.go index cac730f4b..07a43ebfb 100644 --- a/engine/cdrs_test.go +++ b/engine/cdrs_test.go @@ -2361,3 +2361,25 @@ func TestV1RateCDRsSuccesful(t *testing.T) { t.Error("Expected reply to be ok") } } + +func TestCdrsSetCloneable(t *testing.T) { + tests := []struct { + input bool + expected bool + }{ + {input: true, expected: true}, + {input: false, expected: false}, + } + for _, tt := range tests { + attr := &ArgV1ProcessEvents{ + Flags: []string{}, + CGREvents: []*utils.CGREvent{}, + APIOpts: make(map[string]any), + clnb: !tt.input, + } + attr.SetCloneable(tt.input) + if attr.clnb != tt.expected { + t.Errorf("SetCloneable(%v) = %v; expected %v", tt.input, attr.clnb, tt.expected) + } + } +}