Created
July 16, 2024 00:36
-
-
Save BK1031/a0fadbdd4194d3ea38a6085e93f2ede6 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore full book coverage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestUpdateBook(t *testing.T) { | |
ResetBookTable() | |
t.Run("Success", func(t *testing.T) { | |
// Arrange | |
book := model.Book{ | |
Title: "The Great Gatsby", | |
Author: "F. Scott Fitzgerald", | |
Price: 29.99, | |
Genre: "Fiction", | |
} | |
book, _ = CreateBook(book) | |
// Act | |
book.Title = "The Amazing Gatsby" | |
updatedBook, err := UpdateBook(book) | |
// Assert | |
if err != nil { | |
t.Errorf("expected nil error, got %s", err) | |
} else if updatedBook.ID == 0 { | |
t.Errorf("expected non-zero ID, got %d", updatedBook.ID) | |
} else if updatedBook.Title != book.Title { | |
t.Errorf("expected %s, got %s", book.Title, updatedBook.Title) | |
} else if updatedBook.Author != book.Author { | |
t.Errorf("expected %s, got %s", book.Author, updatedBook.Author) | |
} else if updatedBook.Price != book.Price { | |
t.Errorf("expected %f, got %f", book.Price, updatedBook.Price) | |
} else if updatedBook.Genre != book.Genre { | |
t.Errorf("expected %s, got %s", book.Genre, updatedBook.Genre) | |
} else if updatedBook.CreatedAt.IsZero() { | |
t.Errorf("expected non-zero CreatedAt, got %s", updatedBook.CreatedAt) | |
} else if updatedBook.UpdatedAt.IsZero() { | |
t.Errorf("expected non-zero UpdatedAt, got %s", updatedBook.UpdatedAt) | |
} | |
}) | |
t.Run("Error", func(t *testing.T) { | |
// Arrange | |
book := model.Book{Title: "The Great Gatsby"} | |
// Act | |
_, err := UpdateBook(book) | |
// Assert | |
if err == nil { | |
t.Errorf("expected error, got nil") | |
} | |
}) | |
} | |
func TestDeleteBook(t *testing.T) { | |
ResetBookTable() | |
t.Run("Success", func(t *testing.T) { | |
// Arrange | |
book := model.Book{ | |
Title: "To Kill a Mockingbird", | |
Author: "Harper Lee", | |
Price: 19.99, | |
Genre: "Fiction", | |
} | |
createdBook, _ := CreateBook(book) | |
// Act | |
err := DeleteBook(createdBook) | |
// Assert | |
if err != nil { | |
t.Errorf("expected nil error, got %s", err) | |
} | |
// Verify the book is deleted | |
_, getErr := GetBook(uint(createdBook.ID)) | |
if getErr == nil { | |
t.Errorf("expected error when getting deleted book, got nil") | |
} | |
}) | |
t.Run("Error", func(t *testing.T) { | |
// Arrange | |
book := model.Book{} | |
// Act | |
err := DeleteBook(book) | |
// Assert | |
if err == nil { | |
t.Errorf("expected error, got nil") | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment