2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-07-16 12:57:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-07-09 15:37:16 +02:00
|
|
|
"time"
|
2019-12-11 14:10:20 +01:00
|
|
|
|
2020-11-21 18:08:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
|
|
|
|
2019-12-11 14:10:20 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-16 12:57:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewLabel(t *testing.T) {
|
|
|
|
t.Run("name Unicorn2000 priority 5", func(t *testing.T) {
|
|
|
|
label := NewLabel("Unicorn2000", 5)
|
|
|
|
assert.Equal(t, "Unicorn2000", label.LabelName)
|
|
|
|
assert.Equal(t, "unicorn2000", label.LabelSlug)
|
|
|
|
assert.Equal(t, 5, label.LabelPriority)
|
|
|
|
})
|
|
|
|
t.Run("name Unknown", func(t *testing.T) {
|
|
|
|
label := NewLabel("", -6)
|
|
|
|
assert.Equal(t, "Unknown", label.LabelName)
|
|
|
|
assert.Equal(t, "unknown", label.LabelSlug)
|
|
|
|
assert.Equal(t, -6, label.LabelPriority)
|
|
|
|
})
|
|
|
|
}
|
2020-04-30 14:32:48 +02:00
|
|
|
|
|
|
|
func TestLabel_SetName(t *testing.T) {
|
2020-05-08 10:21:03 +02:00
|
|
|
t.Run("set name", func(t *testing.T) {
|
|
|
|
entity := LabelFixtures["landscape"]
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2020-05-08 10:21:03 +02:00
|
|
|
assert.Equal(t, "Landscape", entity.LabelName)
|
|
|
|
assert.Equal(t, "landscape", entity.LabelSlug)
|
|
|
|
assert.Equal(t, "landscape", entity.CustomSlug)
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2020-05-08 10:21:03 +02:00
|
|
|
entity.SetName("Landschaft")
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2020-05-08 10:21:03 +02:00
|
|
|
assert.Equal(t, "Landschaft", entity.LabelName)
|
|
|
|
assert.Equal(t, "landscape", entity.LabelSlug)
|
|
|
|
assert.Equal(t, "landschaft", entity.CustomSlug)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("new name empty", func(t *testing.T) {
|
|
|
|
entity := LabelFixtures["flower"]
|
|
|
|
|
|
|
|
assert.Equal(t, "Flower", entity.LabelName)
|
|
|
|
assert.Equal(t, "flower", entity.LabelSlug)
|
|
|
|
assert.Equal(t, "flower", entity.CustomSlug)
|
|
|
|
|
|
|
|
entity.SetName("")
|
|
|
|
|
|
|
|
assert.Equal(t, "Flower", entity.LabelName)
|
|
|
|
assert.Equal(t, "flower", entity.LabelSlug)
|
|
|
|
assert.Equal(t, "flower", entity.CustomSlug)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
func TestFirstOrCreateLabel(t *testing.T) {
|
2020-05-10 19:43:49 +02:00
|
|
|
label := LabelFixtures.Get("flower")
|
2020-05-26 11:00:39 +02:00
|
|
|
result := FirstOrCreateLabel(&label)
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.LabelName != label.LabelName {
|
|
|
|
t.Errorf("LabelName should be the same: %s %s", result.LabelName, label.LabelName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.LabelSlug != label.LabelSlug {
|
|
|
|
t.Errorf("LabelName should be the same: %s %s", result.LabelSlug, label.LabelSlug)
|
|
|
|
}
|
2020-05-08 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabel_Update(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}
|
|
|
|
|
|
|
|
assert.Equal(t, 4, Label.LabelPriority)
|
|
|
|
assert.Equal(t, "", Label.LabelSlug)
|
|
|
|
assert.Equal(t, "customslug", Label.CustomSlug)
|
|
|
|
assert.Equal(t, "label", Label.LabelName)
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
err := Label.UpdateClassify(*classifyLabel)
|
2020-05-19 12:30:26 +02:00
|
|
|
|
2020-05-08 10:21:03 +02:00
|
|
|
if err != nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
t.Fatal(err)
|
2020-05-08 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, Label.LabelPriority)
|
|
|
|
assert.Equal(t, "customslug", Label.LabelSlug)
|
|
|
|
assert.Equal(t, "classify", Label.CustomSlug)
|
|
|
|
assert.Equal(t, "Classify", Label.LabelName)
|
|
|
|
})
|
|
|
|
t.Run("update custom slug", func(t *testing.T) {
|
|
|
|
classifyLabel := &classify.Label{Name: "classify", Uncertainty: 30, Source: "manual", Priority: 5}
|
|
|
|
Label := &Label{LabelName: "label12", LabelSlug: "labelslug", CustomSlug: "", LabelPriority: 5}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, Label.LabelPriority)
|
|
|
|
assert.Equal(t, "labelslug", Label.LabelSlug)
|
|
|
|
assert.Equal(t, "", Label.CustomSlug)
|
|
|
|
assert.Equal(t, "label12", Label.LabelName)
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
err := Label.UpdateClassify(*classifyLabel)
|
2020-05-08 10:21:03 +02:00
|
|
|
if err != nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
t.Fatal(err)
|
2020-05-08 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, Label.LabelPriority)
|
|
|
|
assert.Equal(t, "labelslug", Label.LabelSlug)
|
|
|
|
assert.Equal(t, "classify", Label.CustomSlug)
|
|
|
|
assert.Equal(t, "Classify", Label.LabelName)
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("update name and Categories", func(t *testing.T) {
|
2020-05-08 11:46:09 +02:00
|
|
|
classifyLabel := &classify.Label{Name: "classify", Uncertainty: 30, Source: "manual", Priority: 5, Categories: []string{"flower", "plant"}}
|
2020-05-10 19:43:49 +02:00
|
|
|
Label := &Label{LabelName: "label34", LabelSlug: "labelslug2", CustomSlug: "labelslug2", LabelPriority: 5, LabelCategories: []*Label{LabelFixtures.Pointer("flower")}}
|
2020-05-08 10:21:03 +02:00
|
|
|
|
|
|
|
assert.Equal(t, 5, Label.LabelPriority)
|
|
|
|
assert.Equal(t, "labelslug2", Label.LabelSlug)
|
|
|
|
assert.Equal(t, "labelslug2", Label.CustomSlug)
|
|
|
|
assert.Equal(t, "label34", Label.LabelName)
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
err := Label.UpdateClassify(*classifyLabel)
|
2020-05-08 10:21:03 +02:00
|
|
|
if err != nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
t.Fatal(err)
|
2020-05-08 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, Label.LabelPriority)
|
|
|
|
assert.Equal(t, "labelslug2", Label.LabelSlug)
|
|
|
|
assert.Equal(t, "classify", Label.CustomSlug)
|
|
|
|
assert.Equal(t, "Classify", Label.LabelName)
|
|
|
|
|
|
|
|
})
|
2020-04-30 14:32:48 +02:00
|
|
|
}
|
2020-07-09 15:37:16 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|