From bd261ca375735249708a28e3c91fe94898c5bf47 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 16 Jan 2018 13:14:56 +0100 Subject: [PATCH] Improved books unit test --- models/book_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/models/book_test.go b/models/book_test.go index 7156a44..a84c0a8 100644 --- a/models/book_test.go +++ b/models/book_test.go @@ -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)