Bladeren bron

add testing

Signed-off-by: Alex Meijer <alexander.meijer@ibm.com>
Alex Meijer 1 jaar geleden
bovenliggende
commit
b0bdefe7a3
1 gewijzigde bestanden met toevoegingen van 60 en 0 verwijderingen
  1. 60 0
      pkg/costmodel/costmodel_test.go

+ 60 - 0
pkg/costmodel/costmodel_test.go

@@ -1,6 +1,7 @@
 package costmodel
 
 import (
+	"slices"
 	"testing"
 
 	"github.com/opencost/opencost/core/pkg/util"
@@ -10,6 +11,65 @@ import (
 	"k8s.io/apimachinery/pkg/api/resource"
 )
 
+func TestPruneDuplicates(t *testing.T) {
+
+	tests := []struct {
+		name     string
+		input    []string
+		expected []string
+	}{
+		{
+			name:     "empty slice",
+			input:    []string{},
+			expected: []string{},
+		},
+		{
+			name:     "single element slice",
+			input:    []string{"test1"},
+			expected: []string{"test1"},
+		},
+		{
+			name:     "basic duplicate",
+			input:    []string{"test1", "test1"},
+			expected: []string{"test1"},
+		},
+		{
+			name:     "compound duplicate",
+			input:    []string{"test1", "test2", "test1", "test2"},
+			expected: []string{"test1", "test2"},
+		},
+		{
+			name:     "mixture of duplicate/ no duplicate",
+			input:    []string{"test1", "test2", "test1", "test2", "test3"},
+			expected: []string{"test1", "test2", "test3"},
+		},
+		{
+			name:     "underscore sanitization",
+			input:    []string{"test_1", "test_2", "test_1", "test_2", "test_3"},
+			expected: []string{"test-1", "test-2", "test-3"},
+		},
+		{
+			name:     "underscore sanitization II",
+			input:    []string{"test-1", "test_2", "test_1", "test_2", "test_3"},
+			expected: []string{"test-1", "test-2", "test-3"},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			actual := pruneDuplicates(tt.expected)
+			slices.Sort(actual)
+			expected := tt.expected
+			slices.Sort(expected)
+
+			if !slices.Equal(actual, expected) {
+				t.Fatalf("test failuire for case %s. Expected %v, got %v", tt.name, expected, actual)
+			}
+		})
+	}
+
+}
+
 func TestGetGPUCount(t *testing.T) {
 	tests := []struct {
 		name          string