From 15d66fc30493ee9bf2779e0681989bc918b84142 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Thu, 9 Jul 2020 11:27:44 +0200 Subject: [PATCH] Backend: Add unit tests for internal/entity --- internal/entity/camera_test.go | 7 -- internal/entity/details_test.go | 6 ++ internal/entity/file_fixtures.go | 40 ++++++++++ internal/entity/file_test.go | 121 ++++++++++++++++++++++++++++++- internal/entity/link_fixtures.go | 9 +++ 5 files changed, 175 insertions(+), 8 deletions(-) diff --git a/internal/entity/camera_test.go b/internal/entity/camera_test.go index d52db757a..99a57b3be 100644 --- a/internal/entity/camera_test.go +++ b/internal/entity/camera_test.go @@ -29,13 +29,6 @@ func TestFirstOrCreateCamera(t *testing.T) { 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) { diff --git a/internal/entity/details_test.go b/internal/entity/details_test.go index 2cdd43972..8124c883c 100644 --- a/internal/entity/details_test.go +++ b/internal/entity/details_test.go @@ -144,4 +144,10 @@ func TestDetails_Save(t *testing.T) { assert.True(t, afterDate.After(initialDate)) }) + + t.Run("error", func(t *testing.T) { + details := Details{PhotoID: 0} + + assert.Error(t, details.Save()) + }) } diff --git a/internal/entity/file_fixtures.go b/internal/entity/file_fixtures.go index cd86f8b95..5a8f0a643 100644 --- a/internal/entity/file_fixtures.go +++ b/internal/entity/file_fixtures.go @@ -502,10 +502,50 @@ var FileFixtures = map[string]File{ UpdatedIn: 0, 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 FileFixturesExampleXMP = FileFixtures["exampleXmpFile.xmp"] +var FileFixturesExampleBridge = FileFixtures["bridge.jpg"] +var FileFixturesExampleBridgeVideo = FileFixtures["bridge.mp4"] // CreateFileFixtures inserts known entities into the database for testing. func CreateFileFixtures() { diff --git a/internal/entity/file_test.go b/internal/entity/file_test.go index 2ab864e82..5ef6e2fb2 100644 --- a/internal/entity/file_test.go +++ b/internal/entity/file_test.go @@ -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) { 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"} @@ -48,6 +48,22 @@ func TestFile_DownloadFileName(t *testing.T) { 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) { @@ -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) { t.Run("success", func(t *testing.T) { 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()) }) + 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()) + }) } diff --git a/internal/entity/link_fixtures.go b/internal/entity/link_fixtures.go index 44d391f2a..13c68ae9f 100644 --- a/internal/entity/link_fixtures.go +++ b/internal/entity/link_fixtures.go @@ -23,6 +23,15 @@ var LinkFixtures = LinkMap{ CanEdit: false, 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.