From 5e5d615aad6196a380950ed744268da5f883f79f Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 17 Jul 2024 09:06:21 -0400 Subject: [PATCH] Add new unit tests on migrator --- migrator/alias_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++ migrator/user_test.go | 43 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/migrator/alias_test.go b/migrator/alias_test.go index 28ebb8091..0c61a1f03 100644 --- a/migrator/alias_test.go +++ b/migrator/alias_test.go @@ -316,3 +316,62 @@ func TestAlias2AtttributeProfile(t *testing.T) { } } } + +func TestAliasV1AliasSetId(t *testing.T) { + tests := []struct { + input string + expectedValues v1Alias + expectError bool + }{ + { + input: "dir:cgrates.org:category:1001:subject:context", + expectedValues: v1Alias{ + Direction: "dir", + Tenant: "cgrates.org", + Category: "category", + Account: "1001", + Subject: "subject", + Context: "context", + }, + expectError: false, + }, + { + input: "invalidKeyFormat", + expectError: true, + }, + } + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + alias := &v1Alias{} + err := alias.SetId(tt.input) + + if tt.expectError { + if err == nil { + t.Errorf("Expected error but got none") + } + return + } + if err != nil { + t.Errorf("Expected no error but got %v", err) + } + if alias.Direction != tt.expectedValues.Direction { + t.Errorf("SetId() Direction = %v, want %v", alias.Direction, tt.expectedValues.Direction) + } + if alias.Tenant != tt.expectedValues.Tenant { + t.Errorf("SetId() Tenant = %v, want %v", alias.Tenant, tt.expectedValues.Tenant) + } + if alias.Category != tt.expectedValues.Category { + t.Errorf("SetId() Category = %v, want %v", alias.Category, tt.expectedValues.Category) + } + if alias.Account != tt.expectedValues.Account { + t.Errorf("SetId() Account = %v, want %v", alias.Account, tt.expectedValues.Account) + } + if alias.Subject != tt.expectedValues.Subject { + t.Errorf("SetId() Subject = %v, want %v", alias.Subject, tt.expectedValues.Subject) + } + if alias.Context != tt.expectedValues.Context { + t.Errorf("SetId() Context = %v, want %v", alias.Context, tt.expectedValues.Context) + } + }) + } +} diff --git a/migrator/user_test.go b/migrator/user_test.go index 606a4b73a..dc699c347 100644 --- a/migrator/user_test.go +++ b/migrator/user_test.go @@ -213,3 +213,46 @@ func TestUserProfile2attributeProfile(t *testing.T) { } } } + +func TestUserV1UserProfileGetId(t *testing.T) { + profile := &v1UserProfile{ + Tenant: "cgrates.org", + UserName: "1001", + } + expected := "cgrates.org:1001" + actual := profile.GetId() + if actual != expected { + t.Errorf("GetId() = %v, want %v", actual, expected) + } +} + +func TestUserV1UserProfileSetId(t *testing.T) { + tests := []struct { + input string + expectedTenant string + expectedUserName string + expectError bool + }{ + {"cgrates.org:1001", "cgrates.org", "1001", false}, + {"invalidKeyFormat", "", "", true}, + } + for _, tt := range tests { + profile := &v1UserProfile{} + err := profile.SetId(tt.input) + if tt.expectError { + if err == nil { + t.Errorf("SetId(%q) expected error but got none", tt.input) + } + continue + } + if err != nil { + t.Errorf("SetId(%q) unexpected error: %v", tt.input, err) + } + if profile.Tenant != tt.expectedTenant { + t.Errorf("SetId(%q) Tenant = %v, want %v", tt.input, profile.Tenant, tt.expectedTenant) + } + if profile.UserName != tt.expectedUserName { + t.Errorf("SetId(%q) UserName = %v, want %v", tt.input, profile.UserName, tt.expectedUserName) + } + } +}