People: Add unit tests #22

This commit is contained in:
theresa 2021-08-27 09:53:47 +02:00
parent af83a51496
commit 49ad7bb64a

View file

@ -17,6 +17,17 @@ func TestNewPerson(t *testing.T) {
m := NewSubject("Jens Mander", SubjectPerson, SrcAuto)
assert.Equal(t, "Jens Mander", m.SubjectName)
assert.Equal(t, "jens-mander", m.SubjectSlug)
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)
})
}
@ -34,18 +45,45 @@ func TestSubject_SetName(t *testing.T) {
assert.Equal(t, "Foo McBar", m.SubjectName)
assert.Equal(t, "foo-mcbar", m.SubjectSlug)
})
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)
})
}
func TestFirstOrCreatePerson(t *testing.T) {
m := NewSubject("Create Me", SubjectPerson, SrcAuto)
result := FirstOrCreateSubject(m)
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")
}
if result == nil {
t.Fatal("result should not be nil")
}
assert.Equal(t, "Create Me", m.SubjectName)
assert.Equal(t, "create-me", m.SubjectSlug)
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)
if result == nil {
t.Fatal("result should not be nil")
}
assert.Equal(t, "John Doe", m.SubjectName)
assert.Equal(t, "john-doe", m.SubjectSlug)
assert.Equal(t, "Short Note", m.SubjectNotes)
})
}
func TestSubject_Save(t *testing.T) {
@ -148,6 +186,10 @@ func TestFindSubject(t *testing.T) {
r := FindSubject("XXX")
assert.Nil(t, r)
})
t.Run("empty uid", func(t *testing.T) {
r := FindSubject("")
assert.Nil(t, r)
})
}
func TestSubject_Links(t *testing.T) {
@ -174,3 +216,58 @@ func TestSubject_Update(t *testing.T) {
})
}
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)
if err := m.UpdateName("New New"); err != nil {
t.Fatal(err)
}
assert.Equal(t, "New New", m.SubjectName)
assert.Equal(t, "new-new", m.SubjectSlug)
})
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)
err := m.UpdateName("")
if err == nil {
t.Fatal(err)
}
assert.Equal(t, "Test Person2", m.SubjectName)
assert.Equal(t, "test-person2", m.SubjectSlug)
})
}