Library/models/author_test.go

36 lines
880 B
Go

package models
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAddOrUpdateAuthor(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())
// Bootstrap our test author
testauthor := Author{Forename: "test", Lastname: "tsting"}
// Create a new author
author1, err := AddOrUpdateAuthor(testauthor)
assert.NoError(t, err)
// Get the new author
author2, exists, err := GetAuthorByID(author1.ID)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, author2.Forename, testauthor.Forename)
// Pass an empty author to see if it fails
_, err = AddOrUpdateAuthor(Author{})
assert.Error(t, err)
// Update the author
testauthor.ID = 1
testauthor.Forename = "Lorem Ipsum"
author1updated, err := AddOrUpdateAuthor(testauthor)
assert.NoError(t, err)
assert.Equal(t, testauthor.Forename, author1updated.Forename)
}