-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjson_structs_test.go
138 lines (129 loc) · 3.24 KB
/
json_structs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package smugmug
import (
"testing"
"text/template"
)
func Test_albumImage_buildFilename(t *testing.T) {
type fields struct {
FileName string
ImageKey string
ArchivedMD5 string
UploadKey string
DateTimeOriginal string
}
f := fields{
FileName: "ti_7095_R.jpg",
ImageKey: "ImageKeyValue",
ArchivedMD5: "ArchivedMD5Value",
UploadKey: "UploadKeyValue",
DateTimeOriginal: "2012-06-06T21:08:48+00:00",
}
f_wo_date := fields{
FileName: "ti_7095_R.jpg",
ImageKey: "ImageKeyValue",
ArchivedMD5: "ArchivedMD5Value",
UploadKey: "UploadKeyValue",
}
tests := []struct {
name string
fields fields
filenameConf string
want string
wantErr bool
}{
{
name: "filename",
fields: f,
filenameConf: "{{.FileName}}",
want: "ti_7095_R.jpg",
wantErr: false,
},
{
name: "with apostrophe",
fields: fields{
FileName: "FileName'Value",
},
filenameConf: "{{.FileName}}",
want: "FileName'Value",
wantErr: false,
},
{
name: "empty",
fields: f,
filenameConf: "",
want: "",
wantErr: true,
},
{
name: "wrong",
fields: f,
filenameConf: "{{.WrongKey}}",
want: "",
wantErr: true,
},
{
name: "wrong with extra chars",
fields: f,
filenameConf: "{{.WrongKey}}-",
want: "-",
wantErr: true,
},
{
name: "complex",
fields: f,
filenameConf: "{{.ImageKey}}-{{.FileName}}",
want: "ImageKeyValue-ti_7095_R.jpg",
wantErr: false,
},
{
name: "all",
fields: f,
filenameConf: "prefix-{{.UploadKey}}/{{.ImageKey}}-{{.FileName}}_{{.ArchivedMD5}}",
want: "prefix-UploadKeyValue/ImageKeyValue-ti_7095_R.jpg_ArchivedMD5Value",
wantErr: false,
},
{
name: "date and time",
fields: f,
filenameConf: "prefix-{{.Date}}/{{.Time}}",
want: "prefix-2012-06-06/21_08_48",
wantErr: false,
},
{
name: "date and time but empty",
fields: f_wo_date,
filenameConf: "prefix-{{.UploadKey}}/{{.ImageKey}}-{{.Date}}/{{.Time}}",
want: "prefix-UploadKeyValue/ImageKeyValue-/",
wantErr: false,
},
{
name: "Filename with FileNameNoExt and Extension",
fields: f_wo_date,
filenameConf: "prefix-{{.FileName}}_{{.FileNameNoExt}}_{{.Extension}}",
want: "prefix-ti_7095_R.jpg_ti_7095_R_.jpg",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &albumImage{
FileName: tt.fields.FileName,
ImageKey: tt.fields.ImageKey,
ArchivedMD5: tt.fields.ArchivedMD5,
UploadKey: tt.fields.UploadKey,
DateTimeOriginal: tt.fields.DateTimeOriginal,
}
tmpl, err := template.New("image_filename").Option("missingkey=error").Parse(tt.filenameConf)
if err != nil {
t.Fatal(err)
}
err = a.buildFilename(tmpl)
if (err != nil) != tt.wantErr {
t.Fatalf("error: %v, wantErr %v", err, tt.wantErr)
}
if err == nil && a.Name() != tt.want {
t.Fatalf("want: %s, got: %s", tt.want, a.Name())
}
})
}
}