From ae8564ad50a6a2055137f237a7ea2cbbc78de4ae Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Thu, 6 Feb 2025 11:15:08 -0500 Subject: [PATCH] Add coverage tests on sessionS --- sessions/session_test.go | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/sessions/session_test.go b/sessions/session_test.go index d390884d4..88f008eaa 100644 --- a/sessions/session_test.go +++ b/sessions/session_test.go @@ -24,6 +24,7 @@ import ( "testing" "time" + "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) @@ -568,3 +569,77 @@ func TestGetSessionIDsMatchingIndexes(t *testing.T) { } }) } + +func TestSRunClone(t *testing.T) { + origTime := time.Now() + + origSRun := &SRun{ + ID: "run1", + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]any{"id": "1"}, + APIOpts: map[string]any{"runID": "run1"}, + }, + ExtraUsage: 5 * time.Second, + LastUsage: 10 * time.Second, + TotalUsage: 50 * time.Second, + NextAutoDebit: &origTime, + } + + clonedSRun := origSRun.Clone() + + if clonedSRun == nil { + t.Fatal("Clone() returned nil") + } + + if clonedSRun.ID != origSRun.ID { + t.Error("ID does not match") + } + + if clonedSRun.CGREvent == nil { + t.Error("CGREvent is nil in cloned SRun") + } else { + if clonedSRun.CGREvent.Tenant != origSRun.CGREvent.Tenant { + t.Error("CGREvent.Tenant does not match") + } + if clonedSRun.CGREvent.ID != origSRun.CGREvent.ID { + t.Error("CGREvent.ID does not match") + } + } + + if clonedSRun.ExtraUsage != origSRun.ExtraUsage { + t.Error("ExtraUsage does not match") + } + + if clonedSRun.LastUsage != origSRun.LastUsage { + t.Error("LastUsage does not match") + } + + if clonedSRun.TotalUsage != origSRun.TotalUsage { + t.Error("TotalUsage does not match") + } + + if clonedSRun.NextAutoDebit == nil || *clonedSRun.NextAutoDebit != *origSRun.NextAutoDebit { + t.Error("NextAutoDebit does not match") + } +} + +func TestUpdateSRuns(t *testing.T) { + session := &Session{ + SRuns: []*SRun{ + {CGREvent: &utils.CGREvent{Event: map[string]any{"ID": "1"}}}, + }, + } + updateEvent := engine.MapEvent{"ID": "2", "UID": "101010"} + alterableFields := utils.NewStringSet([]string{"ID"}) + session.updateSRuns(updateEvent, alterableFields) + for _, sr := range session.SRuns { + if sr.CGREvent.Event["ID"] != "2" { + t.Errorf("expected ID to be updated to '2', got %v", sr.CGREvent.Event["ID"]) + } + if _, exists := sr.CGREvent.Event["UID"]; exists { + t.Errorf("UID should not exist in event") + } + } +}