-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileModel_test.go
128 lines (99 loc) · 3.94 KB
/
FileModel_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
package files
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
func TestUpdateFieldFileById_Multiple(t *testing.T) {
assert := assert.New(t)
app := GetAppInstance()
var cfg = NewFileFieldConfiguration("content", "attachments")
t.Run("Should create a new file and associate", func(t *testing.T) {
modelId := "11"
fakeFiles := []FileModel{
GetFileModelStub(), GetFileModelStub(),
}
err := fakeFiles[0].Save()
assert.Nil(err)
err = fakeFiles[1].Save()
assert.Nil(err)
fileIds := []string{fakeFiles[0].GetIDString(), fakeFiles[1].GetIDString()}
err = UpdateFieldFilesById(modelId, fileIds, cfg)
assert.Nil(err)
afterSaveFiles := []FileModel{}
err = FileFindManyInRecord(cfg.GetModelName(), cfg.GetFieldName(), modelId, &afterSaveFiles)
assert.Nil(err)
assert.Equal(2, len(afterSaveFiles))
assert.Equal(fileIds[0], afterSaveFiles[0].GetIDString())
assert.Equal(fileIds[1], afterSaveFiles[1].GetIDString())
})
t.Run("Should remove 1 file and add 2 new ones", func(t *testing.T) {
modelId := "12"
fakeFiles := []FileModel{
GetFileModelStub(), GetFileModelStub(), GetFileModelStub(),
}
err := fakeFiles[0].Save()
assert.Nil(err)
err = fakeFiles[1].Save()
assert.Nil(err)
err = fakeFiles[2].Save()
assert.Nil(err)
oldFileIds := []string{fakeFiles[0].GetIDString()}
fileIds := []string{fakeFiles[1].GetIDString(), fakeFiles[2].GetIDString()}
err = UpdateFieldFilesById(modelId, oldFileIds, cfg)
assert.Nil(err)
afterSave1Files := []FileModel{}
err = FileFindManyInRecord(cfg.GetModelName(), cfg.GetFieldName(), modelId, &afterSave1Files)
assert.Nil(err)
assert.Equal(1, len(afterSave1Files))
assert.Equal(oldFileIds[0], afterSave1Files[0].GetIDString())
err = UpdateFieldFilesById(modelId, fileIds, cfg)
assert.Nil(err)
afterSave2Files := []FileModel{}
err = FileFindManyInRecord(cfg.GetModelName(), cfg.GetFieldName(), modelId, &afterSave2Files)
assert.Nil(err)
assert.Equal(2, len(afterSave2Files))
assert.Equal(fileIds[0], afterSave2Files[0].GetIDString())
assert.Equal(fileIds[1], afterSave2Files[1].GetIDString())
})
t.Run("Should remove 2, keep 2 files and add 2 new ones", func(t *testing.T) {
modelId := "13"
fakeFiles := []FileModel{
GetFileModelStub(), GetFileModelStub(), GetFileModelStub(), GetFileModelStub(), GetFileModelStub(), GetFileModelStub(),
}
for i := range fakeFiles {
err := fakeFiles[i].Save()
assert.Nil(err)
}
oldFileIds := []string{fakeFiles[0].GetIDString(), fakeFiles[1].GetIDString(), fakeFiles[2].GetIDString(), fakeFiles[3].GetIDString()}
fileIds := []string{fakeFiles[2].GetIDString(), fakeFiles[3].GetIDString(), fakeFiles[4].GetIDString(), fakeFiles[5].GetIDString()}
err := UpdateFieldFilesById(modelId, oldFileIds, cfg)
assert.Nil(err)
afterSave1Files := []FileModel{}
err = FileFindManyInRecord(cfg.GetModelName(), cfg.GetFieldName(), modelId, &afterSave1Files)
assert.Nil(err)
assert.Equal(4, len(afterSave1Files))
assert.Equal(oldFileIds[0], afterSave1Files[0].GetIDString())
assert.Equal(oldFileIds[1], afterSave1Files[1].GetIDString())
assert.Equal(oldFileIds[2], afterSave1Files[2].GetIDString())
assert.Equal(oldFileIds[3], afterSave1Files[3].GetIDString())
err = UpdateFieldFilesById(modelId, fileIds, cfg)
assert.Nil(err)
afterSave2Files := []FileModel{}
err = FileFindManyInRecord(cfg.GetModelName(), cfg.GetFieldName(), modelId, &afterSave2Files)
assert.Nil(err)
assert.Equal(4, len(afterSave2Files))
assert.Equal(fileIds[0], afterSave2Files[0].GetIDString())
assert.Equal(fileIds[1], afterSave2Files[1].GetIDString())
assert.Equal(fileIds[2], afterSave2Files[2].GetIDString())
assert.Equal(fileIds[3], afterSave2Files[3].GetIDString())
})
t.Cleanup(func() {
db := app.GetDB()
r := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&FileAssocsModel{})
if r.Error != nil {
log.Println("Error on delete db file Assocs", r.Error, r.RowsAffected)
}
})
}