diff --git a/models/author_test.go b/models/author_test.go index 8cacc82..e0220c9 100644 --- a/models/author_test.go +++ b/models/author_test.go @@ -16,11 +16,13 @@ func TestAddOrUpdateAuthor(t *testing.T) { author1, err := AddOrUpdateAuthor(testauthor) assert.NoError(t, err) assert.Equal(t, testauthor.Forename, author1.Forename) + assert.Equal(t, testauthor.Lastname, author1.Lastname) // And anotherone author2, err := AddOrUpdateAuthor(testauthor) assert.NoError(t, err) assert.Equal(t, testauthor.Forename, author2.Forename) + assert.Equal(t, testauthor.Lastname, author2.Lastname) // As of now, we should have 2 authors in total. Get the list and check. allauthors, err := ListAuthors("") @@ -36,6 +38,7 @@ func TestAddOrUpdateAuthor(t *testing.T) { assert.NoError(t, err) assert.True(t, exists) assert.Equal(t, gotauthor.Forename, testauthor.Forename) + assert.Equal(t, gotauthor.Lastname, testauthor.Lastname) // Pass an empty author to see if it fails _, err = AddOrUpdateAuthor(Author{}) @@ -47,6 +50,7 @@ func TestAddOrUpdateAuthor(t *testing.T) { author1updated, err := AddOrUpdateAuthor(testauthor) assert.NoError(t, err) assert.Equal(t, testauthor.Forename, author1updated.Forename) + assert.Equal(t, testauthor.Lastname, author1updated.Lastname) // Delete the author err = DeleteAuthorByID(author1.ID) diff --git a/models/item_test.go b/models/item_test.go new file mode 100644 index 0000000..d4d37cd --- /dev/null +++ b/models/item_test.go @@ -0,0 +1,75 @@ +package models + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestAddOrUpdateItem(t *testing.T) { + // Create test database + assert.NoError(t, PrepareTestDatabase()) + + // Bootstrap our test item + testitem := Item{ + Title:"Testitem", + Price:9.999, + Description: "Lorem Ipsum", + Other: "bs", + } + + // Create a new item + item1, err := AddOrUpdateItem(testitem) + assert.NoError(t, err) + assert.Equal(t, testitem.Title, item1.Title) + assert.Equal(t, testitem.Price, item1.Price) + assert.Equal(t, testitem.Description, item1.Description) + assert.Equal(t, testitem.Other, item1.Other) + + // And anotherone + item2, err := AddOrUpdateItem(testitem) + assert.NoError(t, err) + assert.Equal(t, testitem.Title, item2.Title) + + // As of now, we should have 2 items in total. Get the list and check. + allitems, err := ListItems("") + assert.NoError(t, err) + + for _, item := range allitems { + assert.Equal(t, testitem.Title, item.Title) + assert.Equal(t, testitem.Price, item.Price) + assert.Equal(t, testitem.Description, item.Description) + assert.Equal(t, testitem.Other, item.Other) + } + + // Get the new item + gotitem, exists, err := GetItemByID(item1.ID) + assert.NoError(t, err) + assert.True(t, exists) + assert.Equal(t, testitem.Title, gotitem.Title) + assert.Equal(t, testitem.Price, gotitem.Price) + assert.Equal(t, testitem.Description, gotitem.Description) + assert.Equal(t, testitem.Other, gotitem.Other) + + // Pass an empty item to see if it fails + _, err = AddOrUpdateItem(Item{}) + assert.Error(t, err) + + // Update the item + testitem.ID = item1.ID + testitem.Title = "Lorem Ipsum" + item1updated, err := AddOrUpdateItem(testitem) + assert.NoError(t, err) + assert.Equal(t, testitem.Title, item1updated.Title) + assert.Equal(t, testitem.Price, item1updated.Price) + assert.Equal(t, testitem.Description, item1updated.Description) + assert.Equal(t, testitem.Other, item1updated.Other) + + // Delete the item + err = DeleteItemByID(item1.ID) + assert.NoError(t, err) + + // Check if it is gone + _, exists, err = GetItemByID(item1.ID) + assert.NoError(t, err) + assert.False(t, exists) +}