Backend: Add unit tests for internal/entity
This commit is contained in:
parent
006e833249
commit
15d66fc304
|
@ -29,13 +29,6 @@ func TestFirstOrCreateCamera(t *testing.T) {
|
||||||
|
|
||||||
assert.GreaterOrEqual(t, result.ID, uint(1))
|
assert.GreaterOrEqual(t, result.ID, uint(1))
|
||||||
})
|
})
|
||||||
/*t.Run("not existing model and make", func(t *testing.T) {
|
|
||||||
camera := &Camera{CameraModel: "xxx", CameraMake: "xxx"}
|
|
||||||
|
|
||||||
result := FirstOrCreateCamera(camera)
|
|
||||||
|
|
||||||
assert.GreaterOrEqual(t, result.ID, uint(1))
|
|
||||||
})*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCamera(t *testing.T) {
|
func TestNewCamera(t *testing.T) {
|
||||||
|
|
|
@ -144,4 +144,10 @@ func TestDetails_Save(t *testing.T) {
|
||||||
|
|
||||||
assert.True(t, afterDate.After(initialDate))
|
assert.True(t, afterDate.After(initialDate))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("error", func(t *testing.T) {
|
||||||
|
details := Details{PhotoID: 0}
|
||||||
|
|
||||||
|
assert.Error(t, details.Save())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -502,10 +502,50 @@ var FileFixtures = map[string]File{
|
||||||
UpdatedIn: 0,
|
UpdatedIn: 0,
|
||||||
DeletedAt: nil,
|
DeletedAt: nil,
|
||||||
},
|
},
|
||||||
|
"bridge.mp4": {
|
||||||
|
ID: 1000013,
|
||||||
|
Photo: PhotoFixtures.Pointer("Photo03"),
|
||||||
|
PhotoID: 1000003,
|
||||||
|
PhotoUID: PhotoFixtures.Pointer("Photo03").PhotoUID,
|
||||||
|
FileUID: "ft2es49whhbnlqdy",
|
||||||
|
FileName: "bridge.mp4",
|
||||||
|
OriginalName: "bridgeOriginal.mp4",
|
||||||
|
FileHash: "pcad9168fa6acc5c5ba965adf6ec465ca42fd819",
|
||||||
|
FileModified: time.Date(2017, 2, 6, 2, 6, 51, 0, time.UTC),
|
||||||
|
FileSize: 921851,
|
||||||
|
FileType: "mp4",
|
||||||
|
FileMime: "image/mp4",
|
||||||
|
FilePrimary: false,
|
||||||
|
FileSidecar: false,
|
||||||
|
FileVideo: true,
|
||||||
|
FileMissing: false,
|
||||||
|
FileDuplicate: false,
|
||||||
|
FilePortrait: false,
|
||||||
|
FileWidth: 1200,
|
||||||
|
FileHeight: 1600,
|
||||||
|
FileOrientation: 6,
|
||||||
|
FileAspectRatio: 0.75,
|
||||||
|
FileMainColor: "magenta",
|
||||||
|
FileColors: "225221C1E",
|
||||||
|
FileLuminance: "DC42844C8",
|
||||||
|
FileDiff: 986,
|
||||||
|
FileChroma: 32,
|
||||||
|
FileNotes: "",
|
||||||
|
FileError: "",
|
||||||
|
Share: []FileShare{},
|
||||||
|
Sync: []FileSync{},
|
||||||
|
CreatedAt: time.Date(2019, 1, 1, 2, 6, 51, 0, time.UTC),
|
||||||
|
CreatedIn: 2,
|
||||||
|
UpdatedAt: time.Date(2020, 3, 28, 14, 6, 0, 0, time.UTC),
|
||||||
|
UpdatedIn: 0,
|
||||||
|
DeletedAt: nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var FileFixturesExampleJPG = FileFixtures["exampleFileName.jpg"]
|
var FileFixturesExampleJPG = FileFixtures["exampleFileName.jpg"]
|
||||||
var FileFixturesExampleXMP = FileFixtures["exampleXmpFile.xmp"]
|
var FileFixturesExampleXMP = FileFixtures["exampleXmpFile.xmp"]
|
||||||
|
var FileFixturesExampleBridge = FileFixtures["bridge.jpg"]
|
||||||
|
var FileFixturesExampleBridgeVideo = FileFixtures["bridge.mp4"]
|
||||||
|
|
||||||
// CreateFileFixtures inserts known entities into the database for testing.
|
// CreateFileFixtures inserts known entities into the database for testing.
|
||||||
func CreateFileFixtures() {
|
func CreateFileFixtures() {
|
||||||
|
|
|
@ -23,7 +23,7 @@ func TestFirstFileByHash(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_DownloadFileName(t *testing.T) {
|
func TestFile_ShareFileName(t *testing.T) {
|
||||||
t.Run("photo with title", func(t *testing.T) {
|
t.Run("photo with title", func(t *testing.T) {
|
||||||
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
||||||
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc"}
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc"}
|
||||||
|
@ -48,6 +48,22 @@ func TestFile_DownloadFileName(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, "e98eb86480a72bd585d228a709f0622f90e86cbc.jpg", filename)
|
assert.Equal(t, "e98eb86480a72bd585d228a709f0622f90e86cbc.jpg", filename)
|
||||||
})
|
})
|
||||||
|
t.Run("file hash < 8", func(t *testing.T) {
|
||||||
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
||||||
|
|
||||||
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98"}
|
||||||
|
|
||||||
|
filename := file.ShareFileName()
|
||||||
|
|
||||||
|
assert.NotContains(t, filename, "20190115-000000-Berlin-Morning-Mood")
|
||||||
|
})
|
||||||
|
t.Run("no file uid", func(t *testing.T) {
|
||||||
|
file := &File{Photo: nil, FileType: "jpg", FileHash: "e98ijhyt"}
|
||||||
|
|
||||||
|
filename := file.ShareFileName()
|
||||||
|
|
||||||
|
assert.Equal(t, filename, "e98ijhyt.jpg")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_Changed(t *testing.T) {
|
func TestFile_Changed(t *testing.T) {
|
||||||
|
@ -74,6 +90,30 @@ func TestFile_Changed(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFile_Create(t *testing.T) {
|
||||||
|
t.Run("photo id == 0", func(t *testing.T) {
|
||||||
|
file := File{PhotoID: 0}
|
||||||
|
|
||||||
|
assert.Error(t, file.Create())
|
||||||
|
})
|
||||||
|
t.Run("file already exists", func(t *testing.T) {
|
||||||
|
file := &File{PhotoID: 123, FileType: "jpg", FileSize: 500, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
|
||||||
|
file.Create()
|
||||||
|
assert.Error(t, file.Create())
|
||||||
|
})
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
||||||
|
|
||||||
|
file := &File{Photo: photo, FileType: "jpg", FileSize: 500, PhotoID: 766, FileName: "testname", FileRoot: "xyz"}
|
||||||
|
|
||||||
|
err := file.Create()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestFile_Purge(t *testing.T) {
|
func TestFile_Purge(t *testing.T) {
|
||||||
t.Run("success", func(t *testing.T) {
|
t.Run("success", func(t *testing.T) {
|
||||||
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
||||||
|
@ -112,4 +152,83 @@ func TestFile_Save(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, "file: photo id must not be empty (save 123)", err.Error())
|
assert.Equal(t, "file: photo id must not be empty (save 123)", err.Error())
|
||||||
})
|
})
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
||||||
|
|
||||||
|
file := &File{Photo: photo, FileType: "jpg", FileSize: 500, PhotoID: 766, FileName: "Food", FileRoot: "", UpdatedAt: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
|
||||||
|
|
||||||
|
err := file.Save()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFile_UpdateVideoInfos(t *testing.T) {
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
file := &File{FileType: "jpg", FileWidth: 600, FileName: "VideoUpdate", PhotoID: 1000003}
|
||||||
|
|
||||||
|
assert.Equal(t, "bridge.mp4", FileFixturesExampleBridgeVideo.FileName)
|
||||||
|
assert.Equal(t, int(1200), FileFixturesExampleBridgeVideo.FileWidth)
|
||||||
|
|
||||||
|
err := file.UpdateVideoInfos()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var files Files
|
||||||
|
|
||||||
|
if err := Db().Where("photo_id = ? AND file_video = 1", file.PhotoID).Find(&files).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Len(t, files, 1)
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
assert.Equal(t, "bridge.mp4", f.FileName)
|
||||||
|
assert.Equal(t, int(600), f.FileWidth)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFile_Update(t *testing.T) {
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
file := &File{FileType: "jpg", FileSize: 500, FileName: "ToBeUpdated", FileRoot: "", PhotoID: 5678}
|
||||||
|
|
||||||
|
err := file.Save()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "ToBeUpdated", file.FileName)
|
||||||
|
|
||||||
|
err2 := file.Update("FileName", "Happy")
|
||||||
|
|
||||||
|
if err2 != nil {
|
||||||
|
t.Fatal(err2)
|
||||||
|
}
|
||||||
|
assert.Equal(t, "Happy", file.FileName)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFile_Links(t *testing.T) {
|
||||||
|
t.Run("1 result", func(t *testing.T) {
|
||||||
|
file := FileFixturesExampleBridge
|
||||||
|
links := file.Links()
|
||||||
|
assert.Equal(t, "5jxf3jfn2k", links[0].LinkToken)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFile_NoJPEG(t *testing.T) {
|
||||||
|
t.Run("false", func(t *testing.T) {
|
||||||
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
||||||
|
assert.False(t, file.NoJPEG())
|
||||||
|
})
|
||||||
|
t.Run("tre", func(t *testing.T) {
|
||||||
|
file := &File{Photo: nil, FileType: "xmp", FileSize: 500}
|
||||||
|
assert.True(t, file.NoJPEG())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,15 @@ var LinkFixtures = LinkMap{
|
||||||
CanEdit: false,
|
CanEdit: false,
|
||||||
CreatedAt: time.Date(2020, 3, 6, 2, 6, 51, 0, time.UTC),
|
CreatedAt: time.Date(2020, 3, 6, 2, 6, 51, 0, time.UTC),
|
||||||
},
|
},
|
||||||
|
"5jxf3jfn2k": {
|
||||||
|
LinkToken: "5jxf3jfn2k",
|
||||||
|
LinkExpires: 0,
|
||||||
|
ShareUID: "ft2es39w45bnlqdw",
|
||||||
|
ShareSlug: "ft2es39w45bnlqdw",
|
||||||
|
CanComment: true,
|
||||||
|
CanEdit: false,
|
||||||
|
CreatedAt: time.Date(2020, 3, 6, 2, 6, 51, 0, time.UTC),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateLinkFixtures inserts known entities into the database for testing.
|
// CreateLinkFixtures inserts known entities into the database for testing.
|
||||||
|
|
Loading…
Reference in a new issue