Improved books unit test

This commit is contained in:
konrad 2018-01-16 13:14:56 +01:00 committed by kolaente
parent 64ae3244ed
commit bd261ca375
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 45 additions and 3 deletions

View File

@ -9,6 +9,11 @@ func TestAddOrUpdateBook(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())
// Create a new author for testing purposes
testauthor1 := Author{Forename:"Testauthor wich", Lastname: "already exists"}
testauthorin1, err := AddOrUpdateAuthor(testauthor1)
assert.NoError(t, err)
// Bootstrap our test book
testbook := Book{
Title: "Test",
@ -17,17 +22,54 @@ func TestAddOrUpdateBook(t *testing.T) {
Year: 2018,
Price: 9.99,
Status: 0,
Quantity: 10,
Publisher: Publisher{
Name: "TestPublisherWhich does not exist",
},
Authors: []Author{
{
Forename: "Test1",
Lastname: "Lorm",
},
{
Forename: "Test3",
Lastname: "Lorm",
},
{
ID: testauthorin1.ID,
},
},
}
// Insert one new Testbook
book1, err := AddOrUpdateBook(testbook)
assert.NoError(t, err)
// Check if everything was inserted correctly
assert.Equal(t, testbook.Title, book1.Title)
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)
// And anotherone
book2, err := AddOrUpdateBook(testbook)
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book2.Title)
assert.Equal(t, testbook.Title, book2.Title) // If this works, the rest should work too so we don't need to recheck everythin again
// As of now, we should have 2 books in total. Get the list and check.
allbooks, err := ListBooks("")
@ -38,7 +80,7 @@ func TestAddOrUpdateBook(t *testing.T) {
}
// Get the new book
gotBook, exists, err := GetBookByID(1)
gotBook, exists, err := GetBookByID(book1.ID)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, testbook.Title, gotBook.Title)
@ -48,7 +90,7 @@ func TestAddOrUpdateBook(t *testing.T) {
assert.Error(t, err)
// Update the book
testbook.ID = 1
testbook.ID = book1.ID
testbook.Title = "LormIspmus"
book1updated, err := AddOrUpdateBook(testbook)
assert.NoError(t, err)