2021-08-16 00:29:36 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSubject_TableName(t *testing.T) {
|
|
|
|
m := &Subject{}
|
|
|
|
assert.Contains(t, m.TableName(), "subjects")
|
|
|
|
}
|
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
func TestNewSubject(t *testing.T) {
|
2021-08-16 00:29:36 +02:00
|
|
|
t.Run("Jens_Mander", func(t *testing.T) {
|
|
|
|
m := NewSubject("Jens Mander", SubjectPerson, SrcAuto)
|
|
|
|
assert.Equal(t, "Jens Mander", m.SubjectName)
|
|
|
|
assert.Equal(t, "jens-mander", m.SubjectSlug)
|
2021-08-27 09:53:47 +02:00
|
|
|
assert.Equal(t, "person", m.SubjectType)
|
|
|
|
})
|
|
|
|
t.Run("subject Type empty", func(t *testing.T) {
|
|
|
|
m := NewSubject("Anna Mander", "", SrcAuto)
|
|
|
|
assert.Equal(t, "Anna Mander", m.SubjectName)
|
|
|
|
assert.Equal(t, "anna-mander", m.SubjectSlug)
|
|
|
|
assert.Equal(t, "person", m.SubjectType)
|
|
|
|
})
|
|
|
|
t.Run("subject name empty", func(t *testing.T) {
|
|
|
|
m := NewSubject("", "", SrcAuto)
|
|
|
|
assert.Nil(t, m)
|
2021-08-16 00:29:36 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_SetName(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Jens Mander", SubjectPerson, SrcAuto)
|
|
|
|
|
|
|
|
assert.Equal(t, "Jens Mander", m.SubjectName)
|
|
|
|
assert.Equal(t, "jens-mander", m.SubjectSlug)
|
|
|
|
|
|
|
|
if err := m.SetName("Foo McBar"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "Foo McBar", m.SubjectName)
|
|
|
|
assert.Equal(t, "foo-mcbar", m.SubjectSlug)
|
|
|
|
})
|
2021-08-27 09:53:47 +02:00
|
|
|
t.Run("new name empty", func(t *testing.T) {
|
|
|
|
m := NewSubject("Jens Mander", SubjectPerson, SrcAuto)
|
|
|
|
|
|
|
|
assert.Equal(t, "Jens Mander", m.SubjectName)
|
|
|
|
assert.Equal(t, "jens-mander", m.SubjectSlug)
|
|
|
|
|
|
|
|
err := m.SetName("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "subject: name must not be empty", err.Error())
|
|
|
|
assert.Equal(t, "Jens Mander", m.SubjectName)
|
|
|
|
})
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirstOrCreatePerson(t *testing.T) {
|
2021-08-27 09:53:47 +02:00
|
|
|
t.Run("not yet existing person", func(t *testing.T) {
|
|
|
|
m := NewSubject("Create Me", SubjectPerson, SrcAuto)
|
|
|
|
result := FirstOrCreateSubject(m)
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "Create Me", m.SubjectName)
|
|
|
|
assert.Equal(t, "create-me", m.SubjectSlug)
|
|
|
|
})
|
|
|
|
t.Run("existing person", func(t *testing.T) {
|
|
|
|
m := SubjectFixtures.Pointer("john-doe")
|
|
|
|
result := FirstOrCreateSubject(m)
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-08-27 09:53:47 +02:00
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-08-27 09:53:47 +02:00
|
|
|
assert.Equal(t, "John Doe", m.SubjectName)
|
|
|
|
assert.Equal(t, "john-doe", m.SubjectSlug)
|
|
|
|
assert.Equal(t, "Short Note", m.SubjectNotes)
|
|
|
|
})
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_Save(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Save Me", SubjectPerson, SrcAuto)
|
|
|
|
initialDate := m.UpdatedAt
|
|
|
|
err := m.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
afterDate := m.UpdatedAt
|
|
|
|
|
|
|
|
assert.True(t, afterDate.After(initialDate))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_Delete(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Jens Mander", SubjectPerson, SrcAuto)
|
|
|
|
err := m.Save()
|
|
|
|
assert.False(t, m.Deleted())
|
|
|
|
|
|
|
|
var subj Subjects
|
|
|
|
|
|
|
|
if err := Db().Where("subject_name = ?", m.SubjectName).Find(&subj).Error; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, subj, 1)
|
|
|
|
|
|
|
|
err = m.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Db().Where("subject_name = ?", m.SubjectName).Find(&subj).Error; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, subj, 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_Restore(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
var deleteTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
m := &Subject{DeletedAt: &deleteTime, SubjectName: "ToBeRestored"}
|
|
|
|
err := m.Save()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.True(t, m.Deleted())
|
|
|
|
|
|
|
|
err = m.Restore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.False(t, m.Deleted())
|
|
|
|
})
|
|
|
|
t.Run("subject not deleted", func(t *testing.T) {
|
|
|
|
m := &Subject{DeletedAt: nil, SubjectName: "NotDeleted1234"}
|
|
|
|
err := m.Restore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.False(t, m.Deleted())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindSubject(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Find Me", SubjectPerson, SrcAuto)
|
2021-08-24 14:27:34 +02:00
|
|
|
|
|
|
|
if err := m.Save(); err != nil {
|
2021-08-16 00:29:36 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 14:27:34 +02:00
|
|
|
|
|
|
|
if s := FindSubject(m.SubjectName); s != nil {
|
|
|
|
t.Fatal("result must be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := FindSubject(m.SubjectUID); s != nil {
|
|
|
|
assert.Equal(t, "Find Me", s.SubjectName)
|
|
|
|
} else {
|
|
|
|
t.Fatal("result must not be nil")
|
|
|
|
}
|
|
|
|
})
|
2021-08-16 00:29:36 +02:00
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
r := FindSubject("XXX")
|
|
|
|
assert.Nil(t, r)
|
|
|
|
})
|
2021-08-27 09:53:47 +02:00
|
|
|
t.Run("empty uid", func(t *testing.T) {
|
|
|
|
r := FindSubject("")
|
|
|
|
assert.Nil(t, r)
|
|
|
|
})
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_Links(t *testing.T) {
|
|
|
|
t.Run("no-result", func(t *testing.T) {
|
2021-09-01 20:46:15 +02:00
|
|
|
m := SubjectFixtures.Pointer("john-doe")
|
2021-08-16 00:29:36 +02:00
|
|
|
links := m.Links()
|
|
|
|
assert.Empty(t, links)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_Update(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Update Me", SubjectPerson, SrcAuto)
|
|
|
|
|
|
|
|
if err := m.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Update("SubjectName", "Updated Name"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "Updated Name", m.SubjectName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2021-08-27 09:53:47 +02:00
|
|
|
func TestSubject_Updates(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Update Me", SubjectPerson, SrcAuto)
|
|
|
|
|
|
|
|
if err := m.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Updates(Subject{SubjectName: "UpdatedName", SubjectType: "UpdatedType"}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "UpdatedName", m.SubjectName)
|
|
|
|
assert.Equal(t, "UpdatedType", m.SubjectType)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubject_UpdateName(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := NewSubject("Test Person", SubjectPerson, SrcAuto)
|
|
|
|
|
|
|
|
if err := m.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "Test Person", m.SubjectName)
|
|
|
|
assert.Equal(t, "test-person", m.SubjectSlug)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
if s, err := m.UpdateName("New New"); err != nil {
|
2021-08-27 09:53:47 +02:00
|
|
|
t.Fatal(err)
|
2021-09-01 20:46:15 +02:00
|
|
|
} else if s == nil {
|
|
|
|
t.Fatal("subject is nil")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "New New", m.SubjectName)
|
|
|
|
assert.Equal(t, "new-new", m.SubjectSlug)
|
|
|
|
assert.Equal(t, "New New", s.SubjectName)
|
|
|
|
assert.Equal(t, "new-new", s.SubjectSlug)
|
2021-08-27 09:53:47 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("empty name", func(t *testing.T) {
|
|
|
|
m := NewSubject("Test Person2", SubjectPerson, SrcAuto)
|
|
|
|
|
|
|
|
if err := m.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "Test Person2", m.SubjectName)
|
|
|
|
assert.Equal(t, "test-person2", m.SubjectSlug)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
if s, err := m.UpdateName(""); err == nil {
|
|
|
|
t.Error("error expected")
|
|
|
|
} else if s == nil {
|
|
|
|
t.Fatal("subject is nil")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "Test Person2", m.SubjectName)
|
|
|
|
assert.Equal(t, "test-person2", m.SubjectSlug)
|
|
|
|
assert.Equal(t, "Test Person2", s.SubjectName)
|
|
|
|
assert.Equal(t, "test-person2", s.SubjectSlug)
|
2021-08-27 09:53:47 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|