Skip to content

Instantly share code, notes, and snippets.

@BK1031
Created July 16, 2024 16:25
Show Gist options
  • Save BK1031/b654dd4d23e959f66db5e9ed5e6996d9 to your computer and use it in GitHub Desktop.
Save BK1031/b654dd4d23e959f66db5e9ed5e6996d9 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore TestGetOrder and TestGetAllOrders
func TestGetOrder(t *testing.T) {
// Arrange
ResetOrderTable()
ResetBookTable()
// Arrange
gatsby, _ := CreateBook(model.Book{
Title: "The Great Gatsby",
Author: "F. Scott Fitzgerald",
Price: 29.99,
Genre: "Fiction",
})
order, _ := CreateOrder(model.Order{
Items: []model.OrderItem{
{
BookID: gatsby.ID,
Quantity: 2,
},
},
})
t.Run("Success", func(t *testing.T) {
// Act
retrievedOrder, _ := GetOrder(order.ID)
// Assert
if retrievedOrder.ID != order.ID {
t.Errorf("Expected order ID %d, got %d", order.ID, retrievedOrder.ID)
} else if retrievedOrder.Items[0].BookID != gatsby.ID {
t.Errorf("Expected order item book ID to be %d, got %d", gatsby.ID, retrievedOrder.Items[0].BookID)
}
})
t.Run("NotFound", func(t *testing.T) {
// Act
_, err := GetOrder(999)
// Assert
if err == nil {
t.Errorf("Expected error, got nil")
}
})
}
func TestGetAllOrders(t *testing.T) {
// Arrange
ResetOrderTable()
ResetBookTable()
// Arrange
gatsby, _ := CreateBook(model.Book{
Title: "The Great Gatsby",
Author: "F. Scott Fitzgerald",
Price: 29.99,
Genre: "Fiction",
})
CreateOrder(model.Order{
Items: []model.OrderItem{
{
BookID: gatsby.ID,
Quantity: 2,
},
},
})
t.Run("Success", func(t *testing.T) {
// Act
orders := GetAllOrders()
// Assert
if len(orders) != 1 {
t.Errorf("Expected 1 order, got %d", len(orders))
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment