Created
July 16, 2024 09:10
-
-
Save BK1031/b8d0eae2dda7c0fb12a46827e374b859 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore
This file contains 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 TestUpdateOrder(t *testing.T) { | |
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 | |
order.Items[0].Quantity = 3 | |
updatedOrder, _ := UpdateOrder(order) | |
// Assert | |
if updatedOrder.Total != gatsby.Price*3 { | |
t.Errorf("Expected order total to be %f, got %f", gatsby.Price*2, updatedOrder.Total) | |
} | |
}) | |
t.Run("InvalidQuantity", func(t *testing.T) { | |
// Act | |
order.Items[0].Quantity = 0 | |
_, err := UpdateOrder(order) | |
// Assert | |
if err == nil { | |
t.Errorf("Expected error, got nil") | |
} | |
}) | |
t.Run("Book Not Found", func(t *testing.T) { | |
// Act | |
order.Items[0].BookID = 999 | |
order.Items[0].Quantity = 2 | |
_, err := UpdateOrder(order) | |
// Assert | |
if err == nil { | |
t.Errorf("Expected error, got nil") | |
} | |
}) | |
t.Run("EmptyItems", func(t *testing.T) { | |
// Act | |
order.Items = []model.OrderItem{} | |
_, err := UpdateOrder(order) | |
// 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