Backend: Add unit tests for internal/entity

This commit is contained in:
Theresa Gresch 2020-07-09 15:37:16 +02:00
parent 7cd88e411a
commit 6212c8d63d
3 changed files with 140 additions and 0 deletions

View file

@ -31,3 +31,46 @@ func TestFirstOrCreateKeyword(t *testing.T) {
t.Errorf("Keyword should be the same: %s %s", result.Keyword, keyword.Keyword)
}
}
func TestKeyword_Updates(t *testing.T) {
t.Run("success", func(t *testing.T) {
keyword := NewKeyword("KeywordBeforeUpdate")
assert.Equal(t, "keywordbeforeupdate", keyword.Keyword)
err := keyword.Updates(Keyword{Keyword: "KeywordAfterUpdate", ID: 999})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "KeywordAfterUpdate", keyword.Keyword)
assert.Equal(t, uint(0x3e7), keyword.ID)
})
}
func TestKeyword_Update(t *testing.T) {
t.Run("success", func(t *testing.T) {
keyword := NewKeyword("KeywordBeforeUpdate2")
assert.Equal(t, "keywordbeforeupdate2", keyword.Keyword)
err := keyword.Update("Keyword", "new-name")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "new-name", keyword.Keyword)
})
}
func TestKeyword_Save(t *testing.T) {
t.Run("success", func(t *testing.T) {
keyword := NewKeyword("KeywordName")
err := keyword.Save()
if err != nil {
t.Fatal(err)
}
})
}

View file

@ -3,6 +3,7 @@ package entity
import (
"github.com/photoprism/photoprism/internal/classify"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -131,3 +132,90 @@ func TestLabel_Update(t *testing.T) {
})
}
func TestLabel_Save(t *testing.T) {
t.Run("success", func(t *testing.T) {
label := NewLabel("Unicorn2000", 5)
initialDate := label.UpdatedAt
err := label.Save()
if err != nil {
t.Fatal(err)
}
afterDate := label.UpdatedAt
assert.True(t, afterDate.After(initialDate))
})
}
func TestLabel_Delete(t *testing.T) {
t.Run("success", func(t *testing.T) {
label := NewLabel("LabelToBeDeleted", 5)
err := label.Save()
assert.False(t, label.Deleted())
var labels Labels
if err := Db().Where("label_name = ?", label.LabelName).Find(&labels).Error; err != nil {
t.Fatal(err)
}
assert.Len(t, labels, 1)
err = label.Delete()
if err != nil {
t.Fatal(err)
}
if err := Db().Where("label_name = ?", label.LabelName).Find(&labels).Error; err != nil {
t.Fatal(err)
}
assert.Len(t, labels, 0)
})
}
func TestLabel_Restore(t *testing.T) {
t.Run("success", func(t *testing.T) {
var deleteTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
label := &Label{DeletedAt: &deleteTime, LabelName: "ToBeRestored"}
err := label.Save()
if err != nil {
t.Fatal(err)
}
assert.True(t, label.Deleted())
err = label.Restore()
if err != nil {
t.Fatal(err)
}
assert.False(t, label.Deleted())
})
}
func TestFindLabel(t *testing.T) {
t.Run("success", func(t *testing.T) {
label := &Label{LabelSlug: "find-me-label", LabelName: "Find Me"}
err := label.Save()
if err != nil {
t.Fatal(err)
}
r := FindLabel("find-me-label")
assert.Equal(t, "Find Me", r.LabelName)
})
t.Run("nil", func(t *testing.T) {
r := FindLabel("XXX")
assert.Nil(t, r)
})
}
func TestLabel_Links(t *testing.T) {
t.Run("1 result", func(t *testing.T) {
label := LabelFixtures.Get("flower")
links := label.Links()
assert.Equal(t, "6jxf3jfn2k", links[0].LinkToken)
})
}

View file

@ -32,6 +32,15 @@ var LinkFixtures = LinkMap{
CanEdit: false,
CreatedAt: time.Date(2020, 3, 6, 2, 6, 51, 0, time.UTC),
},
"6jxf3jfn2k": {
LinkToken: "6jxf3jfn2k",
LinkExpires: 0,
ShareUID: "st9lxuqxpogaaba7",
ShareSlug: "lt9k3pw1wowuy3c3",
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.