Welcome to your fifth and final unit for this course, focused on practicing the principles of Test Driven Development (TDD) using Go and Testify. We're going to continue building our ShoppingCart
system by integrating even more sophisticated features into our module.
In this course, the emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to write tests AND implement code that makes each test pass, simulating a real-world TDD environment. In previous lessons, tests were provided to guide you, but now it’s your turn to define them!
Remember to apply the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. Ask questions if you need any clarification!
- Description: The cart should enforce a maximum quantity limit of 10 for a single type of item, preventing more than this allowed amount from being added.
- Details
- Utilize the
AddItem(item, quantity)
function to add items to the cart. - Ensure an error is returned when adding a quantity that exceeds the limit of 10 for a single item.
- The error message should clearly state,
'Maximum quantity exceeded'
when the limit is breached.
- Utilize the
- Examples: Attempting to add
11
units of an item should return an error with the message'Maximum quantity exceeded'
.
- Description: When an item is added to the cart, it should be possible to retrieve that item's details using its ID, including the product information and the quantity in the cart.
- Details
- Enable items to be added using the
AddItem()
function with specific IDs. - Ensure that
GetItem(id string) (Item, error)
returns the correct item details, including the quantity, after being added to the cart.
- Enable items to be added using the
- Examples: Adding a
Book
with ID"1"
and price10
, and retrieving by ID"1"
, should returnItem{ID: "1", Name: "Book", Price: 10, Quantity: 1}
.
- Description: Applying a valid discount code should reduce the total price of items in the cart by the specified discount percentage.
- Details
- Use the
ApplyDiscountCode(code string) error
function to apply a discount. - Support valid discount codes such as
'HOLIDAY25'
for a 25% discount. - Update
GetTotal() float64
to reflect the discounted price.
- Use the
- Examples: Applying the discount code
'HOLIDAY25'
to a productBook
with a price of100
should reduce the total to75
.
- Description: The system should not accept invalid discount codes and should return an appropriate error when such a code is applied.
- Details
- Ensure
ApplyDiscountCode(code string) error
checks against a list of valid codes. - Return an error with the message
'Invalid discount code'
if the code is not valid.
- Ensure
- Examples: Applying the discount code
"INVALID"
after adding an item should return an error indicating that the code is invalid.
- Description: When an item that already exists in the cart is added again, its quantity should increase without creating duplicates, and the total price should reflect the cumulative quantity.
- Details
- Allow items to be added again using the
AddItem()
function without creating duplicates in the cart. - Ensure
GetItem(id string) (Item, error)
returns the updated quantity after adding the same item. - Update the total price to reflect the cumulative quantity of the added items.
- Allow items to be added again using the
- Examples: Adding a
Book
with a price of200
twice should result in a quantity of2
for that item, with the total updated price reflecting the double addition.
- Description: When adding more units to existing items in the cart, the total quantity should not exceed the predefined maximum limit.
- Details
- Use the
AddItem(item, quantity int) error
function to add units to an existing item. - Ensure that adding a quantity which causes the total to exceed the maximum allowed quantity returns an error.
- The error message should be
'Maximum quantity exceeded'
when the quantity limit is breached.
- Use the
- Example: Adding
3
units of an item with ID1
, namedBook
, priced at200
to an existing8
units should return an error, as it exceeds the limit.
In this unit, you reviewed descriptions for more advanced test cases to expand the functionality of the ShoppingCart
module. The focus was on utilizing TDD practices for advanced features like handling non-existent items, applying discounts, and updating item quantities. With solidified TDD skills, let's proceed to writing targeted test cases that capture essential functionality for a robust ShoppingCart
module.
As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle.
You are almost done with this practice course! Keep up the great work, and soon you'll have mastered these unit testing techniques. Red! Green! Refactor!
