|
1 | 1 | package cliflags
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/simulot/immich-go/internal/filetypes" |
| 9 | +) |
4 | 10 |
|
5 | 11 | func TestStringList_Include(t *testing.T) {
|
6 | 12 | tests := []struct {
|
@@ -95,3 +101,150 @@ func TestStringList_Exclude(t *testing.T) {
|
95 | 101 | })
|
96 | 102 | }
|
97 | 103 | }
|
| 104 | + |
| 105 | +func TestStringList_IncludeType(t *testing.T) { |
| 106 | + var photoTypes []string |
| 107 | + var videoTypes []string |
| 108 | + |
| 109 | + for ext, mediaType := range filetypes.DefaultSupportedMedia { |
| 110 | + switch mediaType { |
| 111 | + case filetypes.TypeImage: |
| 112 | + photoTypes = append(photoTypes, ext) |
| 113 | + case filetypes.TypeVideo: |
| 114 | + videoTypes = append(videoTypes, ext) |
| 115 | + case filetypes.TypeSidecar: |
| 116 | + // Sidecar should always be included in the extensions if it's main picture gets added. |
| 117 | + videoTypes = append(videoTypes, ext) |
| 118 | + photoTypes = append(photoTypes, ext) |
| 119 | + default: |
| 120 | + continue |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + tests := []struct { |
| 125 | + name string |
| 126 | + includeType string |
| 127 | + expectedExts ExtensionList |
| 128 | + }{ |
| 129 | + { |
| 130 | + name: "Include only images, no videos", |
| 131 | + includeType: "image", |
| 132 | + expectedExts: ExtensionList(slices.Clone(photoTypes)), |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "Include only videos no, photos", |
| 136 | + includeType: "video", |
| 137 | + expectedExts: ExtensionList(slices.Clone(videoTypes)), |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + flags := InclusionFlags{ |
| 143 | + IncludedExtensions: ExtensionList{}, |
| 144 | + IncludedType: IncludeType(strings.ToUpper(tt.includeType)), |
| 145 | + } |
| 146 | + |
| 147 | + t.Run(tt.name, func(t *testing.T) { |
| 148 | + setIncludeTypeExtensions(&flags) |
| 149 | + |
| 150 | + for _, ext := range flags.IncludedExtensions { |
| 151 | + if len(tt.expectedExts) == 0 { |
| 152 | + t.Errorf("Expected was empty but still gave %v\n", ext) |
| 153 | + } else if !slices.Contains(tt.expectedExts, ext) { |
| 154 | + t.Errorf("Extension: &v missing in %v %v\n", ext, tt.expectedExts) |
| 155 | + } |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestStringList_IncludeType_Set(t *testing.T) { |
| 162 | + validTests := []struct { |
| 163 | + name string |
| 164 | + str string |
| 165 | + expectedIncludeType IncludeType |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "Lower case video type", |
| 169 | + str: "video", |
| 170 | + expectedIncludeType: IncludeVideo, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "Upper case video type", |
| 174 | + str: "VIDEO", |
| 175 | + expectedIncludeType: IncludeVideo, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "Mixed casing video type", |
| 179 | + str: "viDEo", |
| 180 | + expectedIncludeType: IncludeVideo, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "Leading and trailing whitespace video type", |
| 184 | + str: " VIDEO ", |
| 185 | + expectedIncludeType: IncludeVideo, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "Lower case image type", |
| 189 | + str: "image", |
| 190 | + expectedIncludeType: IncludeImage, |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "Upper case image type", |
| 194 | + str: "IMAGE", |
| 195 | + expectedIncludeType: IncludeImage, |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "Mixed casing image type", |
| 199 | + str: "ImaGe", |
| 200 | + expectedIncludeType: IncludeImage, |
| 201 | + }, |
| 202 | + { |
| 203 | + name: "Leading and trailing whitespace image type", |
| 204 | + str: " IMAGE ", |
| 205 | + expectedIncludeType: IncludeImage, |
| 206 | + }, |
| 207 | + } |
| 208 | + |
| 209 | + for _, tt := range validTests { |
| 210 | + var includeType IncludeType |
| 211 | + |
| 212 | + t.Run(tt.name, func(t *testing.T) { |
| 213 | + err := includeType.Set(tt.str) |
| 214 | + |
| 215 | + if err != nil { |
| 216 | + t.Errorf("Expected no error but error is: %v", err) |
| 217 | + } |
| 218 | + |
| 219 | + if includeType != tt.expectedIncludeType { |
| 220 | + t.Errorf("IncludeType was expected to be %v, but is %v", tt.expectedIncludeType, includeType) |
| 221 | + } |
| 222 | + }) |
| 223 | + } |
| 224 | + |
| 225 | + testsThatShouldFail := []struct { |
| 226 | + name string |
| 227 | + str string |
| 228 | + }{ |
| 229 | + { |
| 230 | + name: "Empty string", |
| 231 | + str: "", |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "Invalid string", |
| 235 | + str: "imagevideo", |
| 236 | + }, |
| 237 | + } |
| 238 | + |
| 239 | + for _, tt := range testsThatShouldFail { |
| 240 | + var includeType IncludeType |
| 241 | + |
| 242 | + t.Run(tt.name, func(t *testing.T) { |
| 243 | + err := includeType.Set(tt.str) |
| 244 | + |
| 245 | + if err == nil { |
| 246 | + t.Errorf("The error was expected to be defined but is nil") |
| 247 | + } |
| 248 | + }) |
| 249 | + } |
| 250 | +} |
0 commit comments