|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + promoterv1alpha1 "github.com/argoproj-labs/gitops-promoter/api/v1alpha1" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestTruncateString(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + input string |
| 14 | + length int |
| 15 | + expected string |
| 16 | + }{ |
| 17 | + {"Empty string", "", 5, ""}, |
| 18 | + {"Short string", "abc", 5, "abc"}, |
| 19 | + {"Exact length", "abcde", 5, "abcde"}, |
| 20 | + {"Truncated string", "abcdef", 5, "abcde"}, |
| 21 | + {"Negative length", "abcdef", -1, ""}, |
| 22 | + } |
| 23 | + |
| 24 | + for _, tt := range tests { |
| 25 | + t.Run(tt.name, func(t *testing.T) { |
| 26 | + result := TruncateString(tt.input, tt.length) |
| 27 | + assert.Equal(t, tt.expected, result) |
| 28 | + }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestUpsertEnvironmentStatus(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + initial []promoterv1alpha1.EnvironmentStatus |
| 36 | + insert promoterv1alpha1.EnvironmentStatus |
| 37 | + expected []promoterv1alpha1.EnvironmentStatus |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "Upsert on empty slice", |
| 41 | + initial: []promoterv1alpha1.EnvironmentStatus{}, |
| 42 | + insert: promoterv1alpha1.EnvironmentStatus{Branch: "main"}, |
| 43 | + expected: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Append new element", |
| 47 | + initial: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}}, |
| 48 | + insert: promoterv1alpha1.EnvironmentStatus{Branch: "dev"}, |
| 49 | + expected: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}, {Branch: "dev"}}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Edge case with one element", |
| 53 | + initial: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}}, |
| 54 | + insert: promoterv1alpha1.EnvironmentStatus{Branch: "dev"}, |
| 55 | + expected: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}, {Branch: "dev"}}, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + // Call the function under test |
| 62 | + result := UpsertEnvironmentStatus(tt.initial, tt.insert) |
| 63 | + |
| 64 | + // Assert the result matches the expected value using testify assert |
| 65 | + assert.Equal(t, tt.expected, result) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments