Library/models/author_test.go

81 lines
2.3 KiB
Go
Raw Normal View History

2018-01-16 10:41:11 +00:00
package models
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAddOrUpdateAuthor(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())
2018-01-16 14:59:16 +00:00
// TODO delete all existing authors from eventual previuous tests
2018-01-16 10:41:11 +00:00
// Bootstrap our test author
testauthor := Author{Forename: "test", Lastname: "tsting"}
// Create a new author
author1, err := AddOrUpdateAuthor(testauthor)
assert.NoError(t, err)
2018-01-16 11:27:14 +00:00
assert.Equal(t, testauthor.Forename, author1.Forename)
2018-01-16 12:19:49 +00:00
assert.Equal(t, testauthor.Lastname, author1.Lastname)
2018-01-16 10:41:11 +00:00
2018-01-16 10:53:27 +00:00
// And anotherone
2018-01-16 11:27:14 +00:00
author2, err := AddOrUpdateAuthor(testauthor)
2018-01-16 10:53:27 +00:00
assert.NoError(t, err)
2018-01-16 11:27:14 +00:00
assert.Equal(t, testauthor.Forename, author2.Forename)
2018-01-16 12:19:49 +00:00
assert.Equal(t, testauthor.Lastname, author2.Lastname)
2018-01-16 10:53:27 +00:00
2018-01-16 11:27:14 +00:00
// As of now, we should have 2 authors in total. Get the list and check.
2018-01-16 10:53:27 +00:00
allauthors, err := ListAuthors("")
assert.NoError(t, err)
2018-01-25 13:39:27 +00:00
for _, author := range allauthors {
assert.Equal(t, testauthor.Forename, author.Forename)
assert.Equal(t, testauthor.Lastname, author.Lastname)
}
// Should find something
allauthors, err = ListAuthors("tst")
assert.NoError(t, err)
2018-01-16 10:53:27 +00:00
for _, author := range allauthors {
assert.Equal(t, testauthor.Forename, author.Forename)
assert.Equal(t, testauthor.Lastname, author.Lastname)
}
2018-01-16 10:41:11 +00:00
// Get the new author
2018-01-16 12:12:37 +00:00
gotauthor, exists, err := GetAuthorByID(author1.ID)
2018-01-16 10:41:11 +00:00
assert.NoError(t, err)
assert.True(t, exists)
2018-01-16 10:53:27 +00:00
assert.Equal(t, gotauthor.Forename, testauthor.Forename)
2018-01-16 12:19:49 +00:00
assert.Equal(t, gotauthor.Lastname, testauthor.Lastname)
2018-01-16 10:41:11 +00:00
// Pass an empty author to see if it fails
_, err = AddOrUpdateAuthor(Author{})
assert.Error(t, err)
assert.True(t, IsErrAuthorCannotBeEmpty(err))
2018-01-16 10:41:11 +00:00
// Update the author
2018-01-16 12:12:37 +00:00
testauthor.ID = author1.ID
2018-01-16 10:41:11 +00:00
testauthor.Forename = "Lorem Ipsum"
author1updated, err := AddOrUpdateAuthor(testauthor)
assert.NoError(t, err)
assert.Equal(t, testauthor.Forename, author1updated.Forename)
2018-01-16 12:19:49 +00:00
assert.Equal(t, testauthor.Lastname, author1updated.Lastname)
2018-01-16 10:53:27 +00:00
// Delete the author
err = DeleteAuthorByID(author1.ID)
assert.NoError(t, err)
// Check if it is gone
_, exists, err = GetAuthorByID(author1.ID)
assert.NoError(t, err)
assert.False(t, exists)
// Try deleting an author with ID = 0
err = DeleteAuthorByID(0)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
2018-01-16 10:41:11 +00:00
}