Finishing the Shopping Cart with TDD and Rust

Completing the Shopping Cart Module with Rust

Welcome to your fifth and final unit for this course, focused on practicing the principles of Test Driven Development (TDD) using Rust and its built-in testing framework. We're going to continue building our ShoppingCart system by integrating 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.

Remember to apply the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. Ask questions if you need any clarification!

11. Quantity Limit for Single Product

  • Description: The cart should enforce a maximum quantity limit of 10 for a single type of product, preventing more than this allowed amount from being added.
  • Details
    • Utilize the add_item(product: Product, quantity: usize) -> Result<(), String> function to add products to the cart.
    • Ensure an error is returned when adding a quantity that exceeds the limit of 10 for a single product.
    • The error message should clearly state, 'Maximum quantity exceeded' when the limit is breached.
  • Examples: Attempting to add 11 units of a product should return an error with the message 'Maximum quantity exceeded'.

12. Retrieving Product Details by ID

  • Description: When a product is added to the cart, it should be possible to retrieve that cart entry using its ID, including the product information and the quantity in the cart.
  • Details
    • Enable products to be added using the add_item() function with specific IDs.
    • Ensure get_item(id: &str) returns the correct cart item details, including the quantity, after being added to the cart.
  • Examples: Adding a Book with ID "1" and price 10.0, and retrieving by ID "1", should return a cart item whose product has ID "1", name "Book", price 10.0, and quantity 1.

13. Applying Discount Codes

  • Description: Applying a valid discount code should reduce the total price of products in the cart by the specified discount percentage.
  • Details
    • Use the apply_discount_code(code: &str) function to apply a discount.
    • Support valid discount codes such as 'HOLIDAY25' for a 25% discount.
    • Update get_total() -> f64 to reflect the discounted price.
  • Examples: Applying the discount code 'HOLIDAY25' to a product Book with a price of 100.0 should reduce the total to 75.0.

14. Invalid Discount Code

  • Description: The system should not accept invalid discount codes and should return an appropriate error when such a code is applied.
  • Details
    • Ensure apply_discount_code(code: &str) checks against a list of valid codes.
    • Return an error with the message 'Invalid discount code' if the code is not valid.
  • Examples: Applying the discount code "INVALID" after adding a product should return an error indicating that the code is invalid.

15. Adding an Existing Product

  • Description: When a product 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 a product to be added again using the add_item() function without creating duplicates in the cart.
    • Ensure get_item(id: &str) returns the updated quantity after adding the same product.
    • Update the total price to reflect the cumulative quantity of the added product.
  • Examples: Adding a Book with a price of 200.0 twice should result in a quantity of 2 for that product, with the total updated price reflecting the double addition.

16. Adding to an Existing Product Respects Maximum Quantity

  • Description: When adding more units to an existing product in the cart, the total quantity should not exceed the predefined maximum limit.
  • Details
    • Use the add_item(product: Product, quantity: usize) function to add units to an existing product.
    • Ensure that adding a quantity that 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.
  • Example: Adding 3 units of a product with ID "1", named Book, priced at 200.0, to an existing 8 units should return an error, as it exceeds the limit.

Summary and Preparation for Practice Exercises

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 functionalities for a robust ShoppingCart module.

As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle, using Rust's testing framework to validate your implementations.

You are almost done with this practice course! Keep up the great work, and soon you'll have mastered these unit testing techniques in Rust. Red! Green! Refactor!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal