Library/models/book_test.go

148 lines
3.9 KiB
Go
Raw Permalink Normal View History

2018-01-16 11:34:08 +00:00
package models
import (
"github.com/stretchr/testify/assert"
2018-01-16 13:06:09 +00:00
"testing"
2018-01-16 11:34:08 +00:00
)
func TestAddOrUpdateBook(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())
2018-04-13 13:03:36 +00:00
// Get our doer
doer, _, err := GetUserByID(1)
assert.NoError(t, err)
2018-01-16 12:14:56 +00:00
// Create a new author for testing purposes
2018-01-16 13:06:09 +00:00
testauthor1 := Author{Forename: "Testauthor wich", Lastname: "already exists"}
2018-04-13 13:03:36 +00:00
testauthorin1, err := AddOrUpdateAuthor(testauthor1, &doer)
2018-01-16 12:14:56 +00:00
assert.NoError(t, err)
2018-01-16 11:34:08 +00:00
// Bootstrap our test book
testbook := Book{
2018-01-16 13:06:09 +00:00
Title: "Test",
2018-01-16 11:34:08 +00:00
Description: "Lorem Ipsum",
2018-01-16 13:06:09 +00:00
Isbn: "9999999999-999-99",
Year: 2018,
Price: 9.99,
Status: 0,
Quantity: 10,
2018-01-16 12:14:56 +00:00
Publisher: Publisher{
Name: "TestPublisherWhich does not exist",
},
Authors: []Author{
{
Forename: "Test1",
Lastname: "Lorm",
},
{
Forename: "Test3",
Lastname: "Lorm",
},
{
ID: testauthorin1.ID,
},
},
2018-01-16 11:34:08 +00:00
}
// Insert one new Testbook
2018-04-13 13:03:36 +00:00
book1, err := AddOrUpdateBook(testbook, &doer)
2018-01-16 11:34:08 +00:00
assert.NoError(t, err)
2018-01-16 12:14:56 +00:00
// Check if everything was inserted correctly
2018-01-16 11:34:08 +00:00
assert.Equal(t, testbook.Title, book1.Title)
2018-01-16 12:14:56 +00:00
assert.Equal(t, testbook.Description, book1.Description)
assert.Equal(t, testbook.Isbn, book1.Isbn)
assert.Equal(t, testbook.Year, book1.Year)
assert.Equal(t, testbook.Price, book1.Price)
assert.Equal(t, testbook.Status, book1.Status)
assert.Equal(t, testbook.Quantity, book1.Quantity)
// Check if the publisher was inserted corectly
_, exists, err := GetPublisherByID(book1.Publisher.ID)
assert.NoError(t, err)
assert.True(t, exists)
// Check if the authors are there
assert.Equal(t, book1.Authors[0].Forename, testbook.Authors[0].Forename)
assert.Equal(t, book1.Authors[1].Forename, testbook.Authors[1].Forename)
assert.Equal(t, book1.Authors[2].Forename, testauthor1.Forename)
2018-01-16 11:34:08 +00:00
// And anotherone
2018-04-13 13:03:36 +00:00
book2, err := AddOrUpdateBook(testbook, &doer)
2018-01-16 11:34:08 +00:00
assert.NoError(t, err)
2018-01-16 12:14:56 +00:00
assert.Equal(t, testbook.Title, book2.Title) // If this works, the rest should work too so we don't need to recheck everythin again
2018-01-16 11:34:08 +00:00
// As of now, we should have 2 books in total. Get the list and check.
allbooks, err := ListBooks("")
assert.NoError(t, err)
for _, book := range allbooks {
assert.Equal(t, book.Title, testbook.Title)
}
2018-01-25 13:39:27 +00:00
// Search
allbooks, err = ListBooks("est")
assert.NoError(t, err)
for _, book := range allbooks {
assert.Equal(t, book.Title, testbook.Title)
}
2018-01-16 11:34:08 +00:00
// Get the new book
2018-01-16 12:14:56 +00:00
gotBook, exists, err := GetBookByID(book1.ID)
2018-01-16 11:34:08 +00:00
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, testbook.Title, gotBook.Title)
// Pass an empty Book to see if it fails
2018-04-13 13:03:36 +00:00
_, err = AddOrUpdateBook(Book{}, &doer)
2018-01-16 11:34:08 +00:00
assert.Error(t, err)
assert.True(t, IsErrBookTitleCannotBeEmpty(err))
2018-01-16 11:34:08 +00:00
// Update the book
2018-01-16 12:14:56 +00:00
testbook.ID = book1.ID
2018-01-16 11:34:08 +00:00
testbook.Title = "LormIspmus"
2018-04-13 13:03:36 +00:00
book1updated, err := AddOrUpdateBook(testbook, &doer)
2018-01-16 11:34:08 +00:00
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book1updated.Title)
2018-01-16 13:55:45 +00:00
// Get the authors for that book
authorsbybook, err := GetAuthorsByBook(book1)
assert.NoError(t, err)
// Check if they are the right ones
// Yes I know, this is not a good way to do this. But as we get additional information when selecting from the database
// (ID, Created, Updated), this would fail if we'd just directly compared authorsbybook and testbook.Authors
assert.Equal(t, len(authorsbybook), len(testbook.Authors))
2018-01-16 14:58:16 +00:00
// Test Quantity
qty1, err := book1.getQuantity()
assert.NoError(t, err)
assert.Equal(t, book1.Quantity, qty1)
// Update the quantity and check again
err = book1.setQuantity(int64(99))
assert.NoError(t, err)
qty2, err := book1.getQuantity()
assert.NoError(t, err)
assert.Equal(t, int64(99), qty2)
2018-01-16 11:34:08 +00:00
// Delete the book
2018-04-13 13:03:36 +00:00
err = DeleteBookByID(book1.ID, &doer)
2018-01-16 11:34:08 +00:00
assert.NoError(t, err)
// Check if its gone
2018-01-16 13:21:02 +00:00
_, exists, err = GetBookByID(book1.ID)
2018-01-16 11:34:08 +00:00
assert.NoError(t, err)
assert.False(t, exists)
// Try deleting one with ID = 0
2018-04-13 13:03:36 +00:00
err = DeleteBookByID(0, &doer)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
2018-01-16 13:06:09 +00:00
}