PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/v3/samples/VSArchitectureGuide/Tailspin/Start/Tailspin.Test.Model/CartTests/ShoppingCartTests.cs

#
C# | 249 lines | 197 code | 34 blank | 18 comment | 0 complexity | 1e3c272c69164189f8e4adf2c201446d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Tailspin.Model;
  7. namespace Tailspin.Tests {
  8. /*
  9. Things we know about shopping carts
  10. *
  11. * Carts hold products
  12. * Carts hold the "meta data" of products such as quantity
  13. * Carts should have a Total Cost of products
  14. * Carts should have a Line Item Total (price*quantity)
  15. * When an item is added to a cart, the quantity is incremented by items added
  16. * When an item is removed from a cart, all items of same SKU are removed
  17. * If an item is added to a cart with quantity 0, nothing happens
  18. * if an item is added to a cart with -quantity, items are removed
  19. * if an item quantity is adjusted leaving 0 items, the item is completely removed - ONLY when RemoveItem is used
  20. * A cart should hold Billing/Shipping addresses
  21. * A cart should hold TaxAmount
  22. * A cart should hold SelectedShipping
  23. * It's OK to fail - name it "Monkey"
  24. */
  25. [TestClass]
  26. public class ShoppingCartTests {
  27. public ShoppingCartTests()
  28. {
  29. }
  30. [TestMethod]
  31. public void Total_Should_Be_1_When_1_Product_Added() {
  32. ShoppingCart cart = new ShoppingCart("TEST");
  33. cart.AddItem(new Product("SKU"));
  34. Assert.AreEqual(1, cart.TotalItems);
  35. }
  36. [TestMethod]
  37. public void Total_Should_Be_2_When_2_Different_Products_Added() {
  38. ShoppingCart cart = new ShoppingCart("TEST");
  39. cart.AddItem(new Product("SKU1"));
  40. cart.AddItem(new Product("SKU2"));
  41. Assert.AreEqual(2, cart.TotalItems);
  42. }
  43. [TestMethod]
  44. public void Total_Should_Be_2_When_2_Same_Products_Added() {
  45. ShoppingCart cart = new ShoppingCart("TEST");
  46. Product p = new Product("SKU");
  47. cart.AddItem(p);
  48. cart.AddItem(p);
  49. Assert.AreEqual(2, cart.TotalItems);
  50. }
  51. [TestMethod]
  52. public void Total_Should_Be_0_When_1_of_2_Same_Products_Removed() {
  53. ShoppingCart cart = new ShoppingCart("TEST");
  54. Product p = new Product("SKU");
  55. cart.AddItem(p);
  56. cart.AddItem(p);
  57. Assert.AreEqual(2, cart.TotalItems);
  58. cart.RemoveItem(p);
  59. Assert.AreEqual(0, cart.TotalItems);
  60. }
  61. [TestMethod]
  62. public void Items_Count_Should_Be_1_When_2_of_Same_Product_Added() {
  63. ShoppingCart cart = new ShoppingCart("TEST");
  64. Product p = new Product("SKU");
  65. cart.AddItem(p);
  66. cart.AddItem(p);
  67. Assert.AreEqual(1, cart.Items.Count);
  68. }
  69. [TestMethod]
  70. public void Item_Should_Have_LineTotal_Of_10_With_2_Products_With_Price_5() {
  71. ShoppingCart cart = new ShoppingCart("TEST");
  72. Product p = new Product("SKU");
  73. p.Price = 5;
  74. cart.AddItem(p);
  75. cart.AddItem(p);
  76. Assert.AreEqual(10, cart.Items[0].LineTotal);
  77. }
  78. [TestMethod]
  79. public void Cart_Item_Quantity_Should_Adjust_To_10() {
  80. ShoppingCart cart = new ShoppingCart("TEST");
  81. Product p = new Product("SKU");
  82. cart.AddItem(p);
  83. cart.AdjustQuantity(p, 10);
  84. Assert.AreEqual(10, cart.TotalItems);
  85. }
  86. [TestMethod]
  87. public void Items_Count_Should_Be_0_When_10_Items_Adjusted_To_0() {
  88. ShoppingCart cart = new ShoppingCart("TEST");
  89. Product p = new Product("SKU");
  90. cart.AddItem(p, 10);
  91. cart.AdjustQuantity(p, 0);
  92. Assert.AreEqual(0, cart.Items.Count);
  93. }
  94. [TestMethod]
  95. public void Items_Count_Should_Be_0_When_10_Items_Adjusted_To_Negative_10() {
  96. ShoppingCart cart = new ShoppingCart("TEST");
  97. Product p = new Product("SKU");
  98. cart.AddItem(p,10);
  99. cart.AdjustQuantity(p, -10);
  100. Assert.AreEqual(0, cart.Items.Count);
  101. }
  102. [TestMethod]
  103. public void Cart_Can_Return_Item_By_SKU() {
  104. ShoppingCart cart = new ShoppingCart("TEST");
  105. Product p = new Product("SKU");
  106. cart.AddItem(p, 1);
  107. var item = cart.FindItem("SKU");
  108. Assert.IsNotNull(item);
  109. }
  110. [TestMethod]
  111. public void Cart_Returns_Null_When_No_Sku_Found() {
  112. ShoppingCart cart = new ShoppingCart("TEST");
  113. Product p = new Product("SKU");
  114. cart.AddItem(p, 1);
  115. var item = cart.FindItem("SKU1");
  116. Assert.IsNull(item);
  117. }
  118. [TestMethod]
  119. public void Items_Count_Should_Be_0_When_SKU_Removed() {
  120. ShoppingCart cart = new ShoppingCart("TEST");
  121. Product p = new Product("SKU");
  122. cart.AddItem(p, 1);
  123. Assert.AreEqual(1, cart.Items.Count);
  124. cart.RemoveItem("SKU");
  125. Assert.AreEqual(0, cart.Items.Count);
  126. }
  127. [TestMethod]
  128. public void Items_Count_Should_Be_0_When_2_Items_Cleared() {
  129. ShoppingCart cart = new ShoppingCart("TEST");
  130. Product p = new Product("SKU");
  131. Product p2 = new Product("SKU2");
  132. //Clock-foolery
  133. cart.AddItem(p, 1);
  134. cart.AddItem(p2, 1);
  135. Assert.AreEqual(2, cart.Items.Count);
  136. cart.ClearItems();
  137. Assert.AreEqual(0, cart.Items.Count);
  138. }
  139. [TestMethod]
  140. public void ItemLastAdded_Should_Be_Sku2_When_SKu1_Sku2_Added_In_Sequence() {
  141. ShoppingCart cart = new ShoppingCart("TEST");
  142. Product p = new Product("SKU1");
  143. Product p2 = new Product("SKU2");
  144. cart.AddItem(p, 1,DateTime.Now.AddSeconds(-1));
  145. cart.AddItem(p2, 1,DateTime.Now.AddSeconds(1));
  146. Assert.AreEqual("SKU2", cart.ItemLastAdded.Product.SKU);
  147. }
  148. [TestMethod]
  149. public void ItemLastAdded_Should_Be_Sku2_When_SKu1_Sku2_Added_In_Sequence_Regardless_Of_Adjustments() {
  150. ShoppingCart cart = new ShoppingCart("TEST");
  151. Product p = new Product("SKU1");
  152. Product p2 = new Product("SKU2");
  153. cart.AddItem(p, 1, DateTime.Now.AddSeconds(-1));
  154. cart.AddItem(p2, 1, DateTime.Now.AddSeconds(1));
  155. cart.AdjustQuantity(p, 10);
  156. Assert.AreEqual("SKU2", cart.ItemLastAdded.Product.SKU);
  157. }
  158. [TestMethod]
  159. public void Nothing_Should_Be_Added_When_0_Passed_as_Quantity_To_AddItem() {
  160. ShoppingCart cart = new ShoppingCart("TEST");
  161. Product p = new Product("SKU");
  162. cart.AddItem(p, 0);
  163. Assert.AreEqual(0, cart.Items.Count);
  164. }
  165. [TestMethod]
  166. public void TotalItems_Should_Be_9_When_Negative_1_Passed_As_Quantity_To_Existing_10_Items() {
  167. ShoppingCart cart = new ShoppingCart("TEST");
  168. Product p = new Product("SKU");
  169. cart.AddItem(p, 10);
  170. cart.AddItem(p, -1);
  171. Assert.AreEqual(9, cart.TotalItems);
  172. }
  173. [TestMethod]
  174. public void SubTotal_Should_Be_10_When_2_Items_of_Price_5() {
  175. ShoppingCart cart = new ShoppingCart("TEST");
  176. Product p = new Product("SKU");
  177. p.Price = 5;
  178. cart.AddItem(p, 1);
  179. cart.AddItem(p, 1);
  180. Assert.AreEqual(10, cart.SubTotal);
  181. }
  182. [TestMethod]
  183. public void SubTotal_Should_Be_100_When_2_Items_of_Price_5_Adjusted_to_20() {
  184. ShoppingCart cart = new ShoppingCart("TEST");
  185. Product p = new Product("SKU");
  186. p.Price = 5;
  187. cart.AddItem(p, 1);
  188. cart.AddItem(p, 1);
  189. Assert.AreEqual(10, cart.SubTotal);
  190. cart.AdjustQuantity(p, 20);
  191. Assert.AreEqual(100, cart.SubTotal);
  192. }
  193. [TestMethod]
  194. public void Total_Should_Be_100_With_90_Subtotal_10_Tax() {
  195. ShoppingCart cart = new ShoppingCart("TEST");
  196. Product p = new Product("SKU");
  197. p.Price = 90;
  198. cart.AddItem(p, 1);
  199. Assert.AreEqual(90, cart.SubTotal);
  200. cart.TaxAmount = 10;
  201. Assert.AreEqual(100, cart.Total);
  202. }
  203. [TestMethod]
  204. public void Total_Should_Be_110_With_90_Subtotal_10_Tax_and_10_Shipping() {
  205. ShoppingCart cart = new ShoppingCart("TEST");
  206. Product p = new Product("SKU");
  207. p.Price = 90;
  208. cart.AddItem(p, 1);
  209. Assert.AreEqual(90, cart.SubTotal);
  210. cart.TaxAmount = 10;
  211. cart.ShippingAmount = 10;
  212. Assert.AreEqual(110, cart.Total);
  213. }
  214. }
  215. }