Library/models/author_test.go

124 lines
3.2 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-04-13 13:03:36 +00:00
// Get our doer
doer, exx, err := GetUserByID(1)
assert.True(t, exx)
assert.NoError(t, err)
2018-01-16 10:41:11 +00:00
// Bootstrap our test author
testauthor := Author{Forename: "test", Lastname: "tsting"}
// Create a new author
2018-04-13 13:03:36 +00:00
author1, err := AddOrUpdateAuthor(testauthor, &doer)
2018-01-16 10:41:11 +00:00
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-04-13 13:03:36 +00:00
author2, err := AddOrUpdateAuthor(testauthor, &doer)
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
2018-04-13 13:03:36 +00:00
_, err = AddOrUpdateAuthor(Author{}, &doer)
2018-01-16 10:41:11 +00:00
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"
2018-04-13 13:03:36 +00:00
author1updated, err := AddOrUpdateAuthor(testauthor, &doer)
2018-01-16 10:41:11 +00:00
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
2018-04-13 13:03:36 +00:00
err = DeleteAuthorByID(author1.ID, &doer)
2018-01-16 10:53:27 +00:00
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
2018-04-13 13:03:36 +00:00
err = DeleteAuthorByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
2018-01-26 11:37:11 +00:00
// =======================
// Testing without a table
// Drop the table to see it fail
x.DropTables(Author{})
// Test inserting
2018-04-13 13:03:36 +00:00
_, err = AddOrUpdateAuthor(Author{Forename: "ff", Lastname: "fff"}, &doer)
2018-01-26 11:37:11 +00:00
assert.Error(t, err)
// Test updating
2018-04-13 13:03:36 +00:00
_, err = AddOrUpdateAuthor(Author{ID: 3, Forename: "ff", Lastname: "fff"}, &doer)
2018-01-26 11:37:11 +00:00
assert.Error(t, err)
// Delete from some nonexistent
2018-04-13 13:03:36 +00:00
err = DeleteAuthorByID(3, &doer)
2018-01-26 11:37:11 +00:00
assert.Error(t, err)
// And get from nonexistant
_, err = ListAuthors("")
assert.Error(t, err)
//Aaaaaaaaaand recreate it
x.Sync(Author{})
2018-01-16 10:41:11 +00:00
}
2018-01-26 11:37:11 +00:00
func TestGetAuthorsByBook(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())
// Drop the table to see it fail
x.DropTables(AuthorBook{})
2018-01-26 15:44:58 +00:00
_, err := GetAuthorsByBook(Book{ID: 1})
2018-01-26 11:37:11 +00:00
assert.Error(t, err)
//Aaaaaaaaaand recreate it
x.Sync(AuthorBook{})
2018-01-26 15:44:58 +00:00
}