photoprism/internal/entity/marker_test.go

168 lines
3.5 KiB
Go
Raw Normal View History

2021-05-25 11:38:04 +02:00
package entity
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMarker_TableName(t *testing.T) {
m := &Marker{}
assert.Contains(t, m.TableName(), "markers")
2021-05-25 11:38:04 +02:00
}
func TestNewMarker(t *testing.T) {
m := NewMarker(1000000, "lt9k3pw1wowuy3c3", SrcImage, MarkerLabel, 0.308333, 0.206944, 0.355556, 0.355556)
2021-05-25 11:38:04 +02:00
assert.IsType(t, &Marker{}, m)
assert.Equal(t, uint(1000000), m.FileID)
assert.Equal(t, "lt9k3pw1wowuy3c3", m.SubjectUID)
2021-05-25 11:38:04 +02:00
assert.Equal(t, SrcImage, m.MarkerSrc)
assert.Equal(t, MarkerLabel, m.MarkerType)
}
2021-05-26 14:41:59 +02:00
func TestUpdateOrCreateMarker(t *testing.T) {
2021-05-25 11:38:04 +02:00
t.Run("success", func(t *testing.T) {
m := NewMarker(1000000, "lt9k3pw1wowuy3c3", SrcImage, MarkerLabel, 0.308333, 0.206944, 0.355556, 0.355556)
2021-05-25 11:38:04 +02:00
assert.IsType(t, &Marker{}, m)
assert.Equal(t, uint(1000000), m.FileID)
assert.Equal(t, "lt9k3pw1wowuy3c3", m.SubjectUID)
2021-05-25 11:38:04 +02:00
assert.Equal(t, SrcImage, m.MarkerSrc)
assert.Equal(t, MarkerLabel, m.MarkerType)
2021-05-26 14:41:59 +02:00
m, err := UpdateOrCreateMarker(m)
if err != nil {
t.Fatal(err)
}
2021-05-25 11:38:04 +02:00
if m == nil {
t.Fatal("result should not be nil")
}
if m.ID <= 0 {
t.Errorf("ID should be > 0")
}
})
}
func TestMarker_Updates(t *testing.T) {
t.Run("success", func(t *testing.T) {
m := NewMarker(1000000, "lt9k3pw1wowuy3c4", SrcImage, MarkerLabel, 0.308333, 0.206944, 0.355556, 0.355556)
2021-05-26 14:41:59 +02:00
m, err := UpdateOrCreateMarker(m)
if err != nil {
t.Fatal(err)
}
2021-05-25 11:38:04 +02:00
assert.Equal(t, SrcImage, m.MarkerSrc)
assert.Equal(t, MarkerLabel, m.MarkerType)
2021-05-26 14:41:59 +02:00
if err := m.Updates(Marker{MarkerSrc: SrcMeta}); err != nil {
2021-05-25 11:38:04 +02:00
t.Fatal(err)
}
assert.Equal(t, SrcMeta, m.MarkerSrc)
assert.Equal(t, MarkerLabel, m.MarkerType)
if m.ID <= 0 {
t.Errorf("ID should be > 0")
}
})
}
func TestMarker_Update(t *testing.T) {
t.Run("success", func(t *testing.T) {
m := NewMarker(1000000, "lt9k3pw1wowuy3c4", SrcImage, MarkerLabel, 0.308333, 0.206944, 0.355556, 0.355556)
2021-05-26 14:41:59 +02:00
m, err := UpdateOrCreateMarker(m)
2021-05-25 11:38:04 +02:00
2021-05-26 14:41:59 +02:00
if err != nil {
t.Fatal(err)
}
2021-05-25 11:38:04 +02:00
2021-05-26 14:41:59 +02:00
assert.Equal(t, MarkerLabel, m.MarkerType)
2021-05-25 11:38:04 +02:00
2021-05-26 14:41:59 +02:00
if err := m.Update("MarkerSrc", SrcMeta); err != nil {
2021-05-25 11:38:04 +02:00
t.Fatal(err)
}
assert.Equal(t, SrcMeta, m.MarkerSrc)
assert.Equal(t, MarkerLabel, m.MarkerType)
if m.ID <= 0 {
t.Errorf("ID should be > 0")
}
})
}
func TestMarker_Save(t *testing.T) {
t.Run("success", func(t *testing.T) {
m := NewMarker(1000000, "lt9k3pw1wowuy3c4", SrcImage, MarkerLabel, 0.308333, 0.206944, 0.355556, 0.355556)
2021-05-26 14:41:59 +02:00
m, err := UpdateOrCreateMarker(m)
if err != nil {
t.Fatal(err)
}
2021-05-25 11:38:04 +02:00
assert.Equal(t, MarkerLabel, m.MarkerType)
m.MarkerSrc = SrcMeta
assert.Equal(t, SrcMeta, m.MarkerSrc)
initialDate := m.UpdatedAt
2021-05-26 14:41:59 +02:00
if err := m.Save(); err != nil {
2021-05-25 11:38:04 +02:00
t.Fatal(err)
}
afterDate := m.UpdatedAt
assert.Equal(t, SrcMeta, m.MarkerSrc)
assert.True(t, afterDate.After(initialDate))
if m.ID <= 0 {
t.Errorf("ID should be > 0")
}
p := PhotoFixtures.Get("19800101_000002_D640C559")
assert.Empty(t, p.Files)
p.PreloadFiles(true)
assert.NotEmpty(t, p.Files)
t.Logf("FILES: %#v", p.Files)
2021-05-25 11:38:04 +02:00
})
}
func TestMarker_ClearSubject(t *testing.T) {
t.Run("1000003-2", func(t *testing.T) {
m := MarkerFixtures.Get("1000003-2")
assert.NotEmpty(t, m.MarkerName)
err := m.ClearSubject(SrcAuto)
if err != nil {
t.Fatal(err)
}
assert.Empty(t, m.MarkerName)
})
}
func TestMarker_ClearFace(t *testing.T) {
t.Run("1000003-2", func(t *testing.T) {
m := MarkerFixtures.Get("1000003-2")
assert.NotEmpty(t, m.FaceID)
updated, err := m.ClearFace()
if err != nil {
t.Fatal(err)
}
assert.True(t, updated)
assert.Empty(t, m.FaceID)
})
}