Skip to content

Commit 3cba3b9

Browse files
Tests: adds unit tests for pkg/graph/variable.go (#404)
* pkg/graph/variable: adds unit tests for variable package
1 parent d4143a7 commit 3cba3b9

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Diff for: pkg/graph/variable/variable_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2025 The Kube Resource Orchestrator Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package variable
15+
16+
import (
17+
"testing"
18+
19+
"github.com/stretchr/testify/assert"
20+
)
21+
22+
func TestResourceFieldAddDependencies(t *testing.T) {
23+
tests := []struct {
24+
name string
25+
initialDeps []string
26+
depsToAdd []string
27+
expectedFinalDeps []string
28+
}{
29+
{
30+
name: "add new dependencies",
31+
initialDeps: []string{"resource1", "resource2"},
32+
depsToAdd: []string{"resource3", "resource4"},
33+
expectedFinalDeps: []string{"resource1", "resource2", "resource3", "resource4"},
34+
},
35+
{
36+
name: "add duplicate dependencies",
37+
initialDeps: []string{"resource1", "resource2"},
38+
depsToAdd: []string{"resource2", "resource3"},
39+
expectedFinalDeps: []string{"resource1", "resource2", "resource3"},
40+
},
41+
{
42+
name: "add to empty dependencies",
43+
initialDeps: []string{},
44+
depsToAdd: []string{"resource1", "resource2"},
45+
expectedFinalDeps: []string{"resource1", "resource2"},
46+
},
47+
{
48+
name: "add empty dependencies",
49+
initialDeps: []string{"resource1", "resource2"},
50+
depsToAdd: []string{},
51+
expectedFinalDeps: []string{"resource1", "resource2"},
52+
},
53+
}
54+
55+
for _, tc := range tests {
56+
t.Run(tc.name, func(t *testing.T) {
57+
rf := ResourceField{
58+
Dependencies: tc.initialDeps,
59+
}
60+
61+
rf.AddDependencies(tc.depsToAdd...)
62+
63+
assert.ElementsMatch(t, tc.expectedFinalDeps, rf.Dependencies)
64+
65+
seen := make(map[string]bool)
66+
for _, dep := range rf.Dependencies {
67+
assert.False(t, seen[dep], "Duplicate dependency found: %s", dep)
68+
seen[dep] = true
69+
}
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)