-
Notifications
You must be signed in to change notification settings - Fork 15
/
filesystem_test.go
184 lines (159 loc) · 4.87 KB
/
filesystem_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package media_library_test
import (
"image"
"image/gif"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/jinzhu/gorm"
"github.com/qor/media_library"
"github.com/qor/qor/test/utils"
)
var db = utils.TestDB()
type MyFileSystem struct {
media_library.FileSystem
}
func (MyFileSystem) GetSizes() map[string]*media_library.Size {
return map[string]*media_library.Size{
"small1": {20, 10},
"small2": {20, 10},
"square": {30, 30},
"big": {50, 50},
}
}
type User struct {
gorm.Model
Name string
Avatar MyFileSystem
}
func init() {
if err := db.DropTableIfExists(&User{}).Error; err != nil {
panic(err)
}
db.AutoMigrate(&User{})
media_library.RegisterCallbacks(db)
}
func TestURLWithoutFile(t *testing.T) {
user := User{Name: "jinzhu"}
if got, want := user.Avatar.URL(), ""; got != want {
t.Errorf(`media_library.Base#URL() == %q, want %q`, got, want)
}
if got, want := user.Avatar.URL("big"), ""; got != want {
t.Errorf(`media_library.Base#URL("big") == %q, want %q`, got, want)
}
if got, want := user.Avatar.URL("small1", "small2"), ""; got != want {
t.Errorf(`media_library.Base#URL("small1", "small2") == %q, want %q`, got, want)
}
}
func TestURLWithFile(t *testing.T) {
var filePath string
user := User{Name: "jinzhu"}
if avatar, err := os.Open("test/logo.png"); err != nil {
panic("file doesn't exist")
} else {
user.Avatar.Scan(avatar)
}
if err := db.Save(&user).Error; err != nil {
panic(err)
}
filePath = user.Avatar.URL()
if _, err := os.Stat(filepath.Join("public", filePath)); err != nil {
t.Errorf(`media_library.Base#URL() == %q, it's an invalid path`, filePath)
}
styleCases := []struct {
styles []string
}{
{[]string{"big"}},
{[]string{"small1", "small2"}},
}
for _, c := range styleCases {
filePath = user.Avatar.URL(c.styles...)
if _, err := os.Stat(filepath.Join("public", filePath)); err != nil {
t.Errorf(`media_library.Base#URL(%q) == %q, it's an invalid path`, strings.Join(c.styles, ","), filePath)
}
if strings.Split(path.Base(filePath), ".")[2] != c.styles[0] {
t.Errorf(`media_library.Base#URL(%q) == %q, it's a wrong path`, strings.Join(c.styles, ","), filePath)
}
}
}
func TestSaveIntoFileSystem(t *testing.T) {
var user = User{Name: "jinzhu"}
if avatar, err := os.Open("test/logo.png"); err == nil {
user.Avatar.Scan(avatar)
if err := db.Save(&user).Error; err == nil {
if _, err := os.Stat(filepath.Join("public", user.Avatar.URL())); err != nil {
t.Errorf("should find saved user avatar")
}
var newUser User
db.First(&newUser, user.ID)
newUser.Avatar.Scan(`{"CropOptions": {"small1": {"X": 5, "Y": 5, "Height": 10, "Width": 20}, "small2": {"X": 0, "Y": 0, "Height": 10, "Width": 20}}, "Crop": true}`)
db.Save(&newUser)
if newUser.Avatar.URL() == user.Avatar.URL() {
t.Errorf("url should be different after crop")
}
file, err := os.Open(filepath.Join("public", newUser.Avatar.URL("small1")))
if err != nil {
t.Errorf("Failed open croped image")
}
if image, _, err := image.DecodeConfig(file); err == nil {
if image.Width != 20 || image.Height != 10 {
t.Errorf("image should be croped successfully")
}
} else {
t.Errorf("Failed to decode croped image")
}
} else {
t.Errorf("should saved user successfully")
}
} else {
panic("file doesn't exist")
}
}
func TestSaveGifIntoFileSystem(t *testing.T) {
var user = User{Name: "jinzhu"}
if avatar, err := os.Open("test/test.gif"); err == nil {
var frames int
if g, err := gif.DecodeAll(avatar); err == nil {
frames = len(g.Image)
}
avatar.Seek(0, 0)
user.Avatar.Scan(avatar)
if err := db.Save(&user).Error; err == nil {
if _, err := os.Stat(filepath.Join("public", user.Avatar.URL())); err != nil {
t.Errorf("should find saved user avatar")
}
var newUser User
db.First(&newUser, user.ID)
newUser.Avatar.Scan(`{"CropOptions": {"small1": {"X": 5, "Y": 5, "Height": 10, "Width": 20}, "small2": {"X": 0, "Y": 0, "Height": 10, "Width": 20}}, "Crop": true}`)
db.Save(&newUser)
if newUser.Avatar.URL() == user.Avatar.URL() {
t.Errorf("url should be different after crop")
}
file, err := os.Open(filepath.Join("public", newUser.Avatar.URL("small1")))
if err != nil {
t.Errorf("Failed open croped image")
}
if g, err := gif.DecodeAll(file); err == nil {
if g.Config.Width != 20 || g.Config.Height != 10 {
t.Errorf("gif should be croped successfully")
}
for _, image := range g.Image {
if image.Rect.Dx() != 20 || image.Rect.Dy() != 10 {
t.Errorf("gif's frames should be croped successfully, but it is %vx%v", image.Rect.Dx(), image.Rect.Dy())
}
}
if frames != len(g.Image) || frames == 0 {
t.Errorf("Gif's frames should be same")
}
} else {
t.Errorf("Failed to decode croped gif image")
}
} else {
t.Errorf("should saved user successfully")
}
} else {
panic("file doesn't exist")
}
}