Added unit tests for books

This commit is contained in:
konrad 2018-01-16 12:34:08 +01:00 committed by kolaente
parent 14651d198c
commit f00f473df5
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 65 additions and 0 deletions

65
models/book_test.go Normal file
View File

@ -0,0 +1,65 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddOrUpdateBook(t *testing.T) {
// Create test database
assert.NoError(t, PrepareTestDatabase())
// Bootstrap our test book
testbook := Book{
Title: "Test",
Description: "Lorem Ipsum",
Isbn: "9999999999-999-99",
Year: 2018,
Price: 9.99,
Status: 0,
}
// Insert one new Testbook
book1, err := AddOrUpdateBook(testbook)
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book1.Title)
// And anotherone
book2, err := AddOrUpdateBook(testbook)
assert.NoError(t, err)
assert.Equal(t, testbook.Title, book2.Title)
// 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
gotBook, exists, err := GetBookByID(1)
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
testbook.ID = 1
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)
}