|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestTags_GetTags(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + tags Tags |
| 13 | + expected []string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "empty tags", |
| 17 | + tags: Tags{}, |
| 18 | + expected: nil, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "single tag", |
| 22 | + tags: Tags{Tags: []string{"production"}}, |
| 23 | + expected: []string{"production"}, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "multiple tags", |
| 27 | + tags: Tags{Tags: []string{"production", "critical", "web"}}, |
| 28 | + expected: []string{"production", "critical", "web"}, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + for _, tt := range tests { |
| 33 | + t.Run(tt.name, func(t *testing.T) { |
| 34 | + result := tt.tags.GetTags() |
| 35 | + assert.Equal(t, tt.expected, result) |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestTags_AppendTags(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + initial Tags |
| 44 | + toAppend []string |
| 45 | + expected []string |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "append to empty", |
| 49 | + initial: Tags{}, |
| 50 | + toAppend: []string{"production"}, |
| 51 | + expected: []string{"production"}, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "append single tag", |
| 55 | + initial: Tags{Tags: []string{"production"}}, |
| 56 | + toAppend: []string{"critical"}, |
| 57 | + expected: []string{"production", "critical"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "append multiple tags", |
| 61 | + initial: Tags{Tags: []string{"production"}}, |
| 62 | + toAppend: []string{"critical", "web", "database"}, |
| 63 | + expected: []string{"production", "critical", "web", "database"}, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "append duplicate tags (no deduplication in AppendTags)", |
| 67 | + initial: Tags{Tags: []string{"production"}}, |
| 68 | + toAppend: []string{"production", "critical"}, |
| 69 | + expected: []string{"production", "production", "critical"}, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + tt.initial.AppendTags(tt.toAppend...) |
| 76 | + assert.Equal(t, tt.expected, tt.initial.Tags) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestTags_Merge(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + existing Tags |
| 85 | + update Tags |
| 86 | + expected []string |
| 87 | + }{ |
| 88 | + { |
| 89 | + name: "merge into empty", |
| 90 | + existing: Tags{}, |
| 91 | + update: Tags{Tags: []string{"production", "critical"}}, |
| 92 | + expected: []string{"production", "critical"}, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "merge with empty", |
| 96 | + existing: Tags{Tags: []string{"production"}}, |
| 97 | + update: Tags{}, |
| 98 | + expected: []string{"production"}, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "merge replaces existing", |
| 102 | + existing: Tags{Tags: []string{"staging", "test"}}, |
| 103 | + update: Tags{Tags: []string{"production", "critical"}}, |
| 104 | + expected: []string{"production", "critical"}, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "merge with nil", |
| 108 | + existing: Tags{Tags: []string{"production"}}, |
| 109 | + update: Tags{Tags: nil}, |
| 110 | + expected: []string{"production"}, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + tt.existing.Merge(tt.update) |
| 117 | + assert.Equal(t, tt.expected, tt.existing.Tags) |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestTags_Visit(t *testing.T) { |
| 123 | + tests := []struct { |
| 124 | + name string |
| 125 | + existing Tags |
| 126 | + update Tags |
| 127 | + expected []string |
| 128 | + }{ |
| 129 | + { |
| 130 | + name: "visit with empty tags", |
| 131 | + existing: Tags{Tags: []string{"production"}}, |
| 132 | + update: Tags{}, |
| 133 | + expected: []string{"production"}, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "visit empty with tags", |
| 137 | + existing: Tags{}, |
| 138 | + update: Tags{Tags: []string{"production", "critical"}}, |
| 139 | + expected: []string{"production", "critical"}, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "visit with new tags", |
| 143 | + existing: Tags{Tags: []string{"production"}}, |
| 144 | + update: Tags{Tags: []string{"critical", "web"}}, |
| 145 | + expected: []string{"production", "critical", "web"}, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "visit with duplicate tags", |
| 149 | + existing: Tags{Tags: []string{"production", "web"}}, |
| 150 | + update: Tags{Tags: []string{"production", "critical"}}, |
| 151 | + expected: []string{"production", "web", "critical"}, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "visit with all duplicate tags", |
| 155 | + existing: Tags{Tags: []string{"production", "critical"}}, |
| 156 | + update: Tags{Tags: []string{"production", "critical"}}, |
| 157 | + expected: []string{"production", "critical"}, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "visit preserves order", |
| 161 | + existing: Tags{Tags: []string{"a", "b", "c"}}, |
| 162 | + update: Tags{Tags: []string{"d", "e", "f"}}, |
| 163 | + expected: []string{"a", "b", "c", "d", "e", "f"}, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "visit with nil", |
| 167 | + existing: Tags{Tags: []string{"production"}}, |
| 168 | + update: Tags{Tags: nil}, |
| 169 | + expected: []string{"production"}, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tt := range tests { |
| 174 | + t.Run(tt.name, func(t *testing.T) { |
| 175 | + tt.existing.Visit(tt.update) |
| 176 | + assert.Equal(t, tt.expected, tt.existing.Tags) |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestTags_VisitDeduplication(t *testing.T) { |
| 182 | + // Test that Visit properly deduplicates tags |
| 183 | + existing := Tags{Tags: []string{"tag1", "tag2"}} |
| 184 | + |
| 185 | + // First visit |
| 186 | + existing.Visit(Tags{Tags: []string{"tag3", "tag1"}}) |
| 187 | + assert.Equal(t, []string{"tag1", "tag2", "tag3"}, existing.Tags, "should not duplicate tag1") |
| 188 | + |
| 189 | + // Second visit |
| 190 | + existing.Visit(Tags{Tags: []string{"tag4", "tag2", "tag5"}}) |
| 191 | + assert.Equal(t, []string{"tag1", "tag2", "tag3", "tag4", "tag5"}, existing.Tags, "should not duplicate tag2") |
| 192 | + |
| 193 | + // Third visit with all duplicates |
| 194 | + existing.Visit(Tags{Tags: []string{"tag1", "tag2", "tag3"}}) |
| 195 | + assert.Equal(t, []string{"tag1", "tag2", "tag3", "tag4", "tag5"}, existing.Tags, "should not add any duplicates") |
| 196 | +} |
| 197 | + |
| 198 | +func TestTaggableInterface(t *testing.T) { |
| 199 | + // Test that Tags implements Taggable interface |
| 200 | + var taggable Taggable = &Tags{} |
| 201 | + |
| 202 | + taggable.AppendTags("test1", "test2") |
| 203 | + tags := taggable.GetTags() |
| 204 | + |
| 205 | + assert.Equal(t, []string{"test1", "test2"}, tags) |
| 206 | +} |
0 commit comments