Library/models/book_test.go

108 lines
2.8 KiB
Go
Raw 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-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-01-16 12:14:56 +00:00
testauthorin1, err := AddOrUpdateAuthor(testauthor1)
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
book1, err := AddOrUpdateBook(testbook)
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
book2, err := AddOrUpdateBook(testbook)
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)
}
// 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
_, err = AddOrUpdateBook(Book{})
assert.Error(t, err)
// 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"
book1updated, err := AddOrUpdateBook(testbook)
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book1updated.Title)
// Delete the book
err = DeleteBookByID(1)
assert.NoError(t, err)
// Check if its gone
_, exists, err = GetBookByID(1)
assert.NoError(t, err)
assert.False(t, exists)
2018-01-16 13:06:09 +00:00
}