Created
July 16, 2024 00:14
-
-
Save BK1031/a6f131337d58eff055240e8c579a785c to your computer and use it in GitHub Desktop.
singlestore-go-bookstore update TestCreateBook
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 TestCreateBook(t *testing.T) { | |
ResetBookTable() | |
createdID := 0 | |
t.Run("Success", func(t *testing.T) { | |
// Arrange | |
book := model.Book{ | |
Title: "The Great Gatsby", | |
Author: "F. Scott Fitzgerald", | |
Price: 29.99, | |
Genre: "Fiction", | |
} | |
// Act | |
createdBook, err := CreateBook(book) | |
createdID = createdBook.ID | |
// Assert | |
if err != nil { | |
t.Errorf("expected nil error, got %s", err) | |
} else if createdBook.ID == 0 { | |
t.Errorf("expected non-zero ID, got %d", createdBook.ID) | |
} else if createdBook.Title != book.Title { | |
t.Errorf("expected %s, got %s", book.Title, createdBook.Title) | |
} else if createdBook.Author != book.Author { | |
t.Errorf("expected %s, got %s", book.Author, createdBook.Author) | |
} else if createdBook.Price != book.Price { | |
t.Errorf("expected %f, got %f", book.Price, createdBook.Price) | |
} else if createdBook.Genre != book.Genre { | |
t.Errorf("expected %s, got %s", book.Genre, createdBook.Genre) | |
} else if createdBook.CreatedAt.IsZero() { | |
t.Errorf("expected non-zero CreatedAt, got %s", createdBook.CreatedAt) | |
} else if createdBook.UpdatedAt.IsZero() { | |
t.Errorf("expected non-zero UpdatedAt, got %s", createdBook.UpdatedAt) | |
} | |
}) | |
t.Run("Error", func(t *testing.T) { | |
// Arrange | |
book := model.Book{ID: createdID} | |
// Act | |
_, err := CreateBook(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