-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypes_test.go
More file actions
132 lines (118 loc) · 2.93 KB
/
Copy pathtypes_test.go
File metadata and controls
132 lines (118 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// SPDX-License-Identifier: Apache-2.0
package git
import (
"testing"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/ConfigButler/gitops-reverser/internal/types"
)
// TestBranchKeyString verifies BranchKey string representation.
func TestBranchKeyString(t *testing.T) {
tests := []struct {
name string
key BranchKey
expected string
}{
{
name: "Standard branch key",
key: BranchKey{
RepoNamespace: "gitops-system",
RepoName: "my-repo",
Branch: "main",
},
expected: "gitops-system/my-repo/main",
},
{
name: "Development branch",
key: BranchKey{
RepoNamespace: "default",
RepoName: "test-repo",
Branch: "develop",
},
expected: "default/test-repo/develop",
},
{
name: "Feature branch",
key: BranchKey{
RepoNamespace: "prod",
RepoName: "app",
Branch: "feature/new-api",
},
expected: "prod/app/feature/new-api",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.key.String()
if result != tt.expected {
t.Errorf("BranchKey.String() = %q, want %q", result, tt.expected)
}
})
}
}
// TestBranchKeyEquality verifies BranchKey can be used as map keys.
func TestBranchKeyEquality(t *testing.T) {
key1 := BranchKey{
RepoNamespace: "ns1",
RepoName: "repo1",
Branch: "main",
}
key2 := BranchKey{
RepoNamespace: "ns1",
RepoName: "repo1",
Branch: "main",
}
key3 := BranchKey{
RepoNamespace: "ns1",
RepoName: "repo1",
Branch: "develop",
}
// Test map key usage
testMap := make(map[BranchKey]string)
testMap[key1] = "value1"
testMap[key2] = "value2" // Should overwrite value1
testMap[key3] = "value3"
if len(testMap) != 2 {
t.Errorf("Expected 2 entries in map (key1==key2), got %d", len(testMap))
}
if testMap[key1] != "value2" {
t.Errorf("key1 should map to 'value2' (overwritten by key2), got %q", testMap[key1])
}
if testMap[key3] != "value3" {
t.Errorf("key3 should map to 'value3', got %q", testMap[key3])
}
}
// TestEventWithObject verifies event creation with objects.
func TestEventWithObject(t *testing.T) {
obj := &unstructured.Unstructured{}
obj.SetName("test-pod")
obj.SetNamespace("default")
obj.SetKind("Pod")
event := Event{
Object: obj,
Identifier: types.ResourceIdentifier{
Group: "",
Version: "v1",
Resource: "pods",
Namespace: "default",
Name: "test-pod",
},
Operation: "CREATE",
UserInfo: UserInfo{
Username: "admin",
UID: "12345",
},
Path: "clusters/prod",
}
if event.Object == nil {
t.Error("Expected Object to be set")
}
if event.Object.GetName() != "test-pod" {
t.Errorf("Object name = %q, want 'test-pod'", event.Object.GetName())
}
if event.Operation != "CREATE" {
t.Errorf("Operation = %q, want 'CREATE'", event.Operation)
}
if event.Path != "clusters/prod" {
t.Errorf("Path = %q, want 'clusters/prod'", event.Path)
}
}