Tests: Add tests for internal/entity

This commit is contained in:
theresa 2021-02-24 15:31:10 +01:00
parent 7714ab893b
commit 6c4c07f064
3 changed files with 159 additions and 2 deletions

View file

@ -141,6 +141,20 @@ func TestFile_Purge(t *testing.T) {
})
}
func TestFile_Found(t *testing.T) {
t.Run("success", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
assert.Equal(t, nil, file.Purge())
assert.Equal(t, true, file.FileMissing)
err := file.Found()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, false, file.FileMissing)
})
}
func TestFile_AllFilesMissing(t *testing.T) {
t.Run("true", func(t *testing.T) {
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: ""}
@ -310,3 +324,99 @@ func TestFile_Delete(t *testing.T) {
assert.Nil(t, err2)
})
}
func TestPrimaryFile(t *testing.T) {
file, err := PrimaryFile("pt9jtdre2lvl0y17")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "Video.mp4", file.FileName)
}
func TestFile_OriginalBase(t *testing.T) {
t.Run("original name empty, filename empty", 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", OriginalName: "", FileName: ""}
filename := file.OriginalBase(0)
assert.Contains(t, filename, "20190115-000000-Berlin-Morning-Mood")
assert.Contains(t, filename, fs.JpegExt)
filename2 := file.OriginalBase(1)
assert.Contains(t, filename2, "20190115-000000-Berlin-Morning-Mood")
assert.Contains(t, filename2, "(1)")
assert.Contains(t, filename2, fs.JpegExt)
})
t.Run("original name empty", 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", OriginalName: "", FileName: "sonnenaufgang.jpg"}
filename := file.OriginalBase(0)
assert.Contains(t, filename, "sonnenaufgang")
assert.Contains(t, filename, fs.JpegExt)
filename2 := file.OriginalBase(1)
assert.Contains(t, filename2, "sonnenaufgang")
assert.Contains(t, filename2, "(1)")
assert.Contains(t, filename2, fs.JpegExt)
})
t.Run("original name not empty", 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", OriginalName: "Sonnenaufgang.jpg", FileName: "123.jpg"}
filename := file.OriginalBase(0)
assert.Contains(t, filename, "Sonnenaufgang")
assert.Contains(t, filename, fs.JpegExt)
filename2 := file.OriginalBase(1)
assert.Contains(t, filename2, "Sonnenaufgang")
assert.Contains(t, filename2, "(1)")
assert.Contains(t, filename2, fs.JpegExt)
})
}
func TestFile_DownloadName(t *testing.T) {
t.Run("DownloadNameFile", 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", OriginalName: "originalName.jpg", FileName: "filename.jpg"}
filename := file.DownloadName(DownloadNameFile, 0)
assert.Contains(t, filename, "filename")
assert.Contains(t, filename, fs.JpegExt)
filename2 := file.DownloadName(DownloadNameOriginal, 1)
assert.Contains(t, filename2, "originalName")
assert.Contains(t, filename2, "(1)")
assert.Contains(t, filename2, fs.JpegExt)
filename3 := file.DownloadName("xxx", 0)
assert.Contains(t, filename3, "20190115-000000-Berlin-Morning-Mood")
})
}
func TestFile_Undelete(t *testing.T) {
t.Run("success", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
assert.Equal(t, nil, file.Purge())
assert.Equal(t, true, file.FileMissing)
err := file.Undelete()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, false, file.FileMissing)
})
t.Run("file not missing", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
assert.Equal(t, false, file.FileMissing)
err := file.Undelete()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, false, file.FileMissing)
})
}

View file

@ -82,7 +82,7 @@ func TestNewFolder(t *testing.T) {
})
t.Run("pathName equals root path", func(t *testing.T) {
folder := NewFolder("", "", time.Now().UTC())
folder := NewFolder("", RootPath, time.Now().UTC())
assert.Equal(t, "", folder.Path)
})
}
@ -140,10 +140,19 @@ func TestFolder_Title(t *testing.T) {
})
}
func TestFolder_RootPath(t *testing.T) {
t.Run("/rainbow", func(t *testing.T) {
folder := Folder{FolderTitle: "Beautiful beach", Root: "/", Path: "rainbow"}
assert.Equal(t, "/rainbow", folder.RootPath())
})
}
func TestFindFolder(t *testing.T) {
t.Run("nil", func(t *testing.T) {
assert.Nil(t, FindFolder("vvfgt", "jgfuyf"))
})
t.Run("pathName === rootPath", func(t *testing.T) {
assert.Nil(t, FindFolder("vvfgt", RootPath))
})
}
func TestFolder_Updates(t *testing.T) {
@ -185,3 +194,16 @@ func TestFolder_SetForm(t *testing.T) {
assert.Equal(t, "Beautiful beach", folder.FolderTitle)
})
}
func TestFolder_Create(t *testing.T) {
t.Run("success", func(t *testing.T) {
folder := Folder{FolderTitle: "Holiday 2020", Root: RootOriginals, Path: "2020/Greece"}
err := folder.Create()
if err != nil {
t.Fatal(err)
}
result := FindFolder(RootOriginals, "2020/Greece")
assert.Equal(t, "2020-greece", result.Slug())
assert.Equal(t, "Holiday 2020", result.Title())
})
}

View file

@ -71,7 +71,7 @@ func TestFirstOrCreateLabel(t *testing.T) {
}
}
func TestLabel_Update(t *testing.T) {
func TestLabel_UpdateClassify(t *testing.T) {
t.Run("update priority and label slug", func(t *testing.T) {
classifyLabel := &classify.Label{Name: "classify", Uncertainty: 30, Source: "manual", Priority: 5}
Label := &Label{LabelName: "label", LabelSlug: "", CustomSlug: "customslug", LabelPriority: 4}
@ -194,6 +194,14 @@ func TestLabel_Restore(t *testing.T) {
}
assert.False(t, label.Deleted())
})
t.Run("label not deleted", func(t *testing.T) {
label := &Label{DeletedAt: nil, LabelName: "NotDeleted1234"}
err := label.Restore()
if err != nil {
t.Fatal(err)
}
assert.False(t, label.Deleted())
})
}
func TestFindLabel(t *testing.T) {
@ -220,3 +228,20 @@ func TestLabel_Links(t *testing.T) {
assert.Equal(t, "6jxf3jfn2k", links[0].LinkToken)
})
}
func TestLabel_Update(t *testing.T) {
t.Run("success", func(t *testing.T) {
label := &Label{LabelSlug: "to-be-updated", LabelName: "Update Me Please"}
err := label.Save()
if err != nil {
t.Fatal(err)
}
err2 := label.Update("LabelSlug", "my-unique-slug")
if err2 != nil {
t.Fatal(err2)
}
assert.Equal(t, "my-unique-slug", label.LabelSlug)
})
}