PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/MusicStore.UnitTest/AddProductToCartCommandUnitTest.cs

https://github.com/satish860/MusicStoreAPI
C# | 86 lines | 76 code | 10 blank | 0 comment | 0 complexity | 03b42ce803170d2ee737c6dac0a8b4c6 MD5 | raw file
  1. using MusicStore.Api;
  2. using MusicStore.Api.CommandHandlers;
  3. using Simple.Data;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using FluentAssertions;
  10. using MusicStore.Api.Models;
  11. using NUnit.Framework;
  12. using System.Web.Http.SelfHost;
  13. namespace MusicStore.UnitTest
  14. {
  15. [TestFixture]
  16. public class AddProductToCartCommandUnitTest
  17. {
  18. [TestFixtureSetUp]
  19. public void FixtureSetUp()
  20. {
  21. new MusicStoreBootStrap(new HttpSelfHostConfiguration("http://localhost:8008"))
  22. .ConfigureDatabaseForTest()
  23. .SeedDatabase();
  24. }
  25. [SetUp]
  26. public void SetUp()
  27. {
  28. var Db = Database.Open();
  29. Db.Cart.Insert(new Cart { Id = 1 });
  30. }
  31. [TearDown]
  32. public void TearDown()
  33. {
  34. var Db = Database.Open();
  35. Db.CartItem.DeleteByCartId(1);
  36. Db.Cart.DeleteById(1);
  37. }
  38. [Test]
  39. public void Should_be_able_to_Add_The_Item_to_The_Cart()
  40. {
  41. ICommandHandler<AddProductToCartCommand> command = TestInstance.AddProductToCommandHandler;
  42. command.Execute(new AddProductToCartCommand { CartId = 1, ProductId = 1, Quantity = 1 });
  43. CartItem Item=Database.Open().CartItem.FindByCartId(1);
  44. Item.Price.Should().Be(20000);
  45. Item.ProductName.Should().Be("Samsung s2");
  46. }
  47. [Test]
  48. public void After_Adding_the_Item_to_the_Cart_We_Should_be_able_to_Calculate_price()
  49. {
  50. ICommandHandler<AddProductToCartCommand> command = TestInstance.AddProductToCommandHandler;
  51. command.Execute(new AddProductToCartCommand { CartId = 1, ProductId = 1, Quantity = 2 });
  52. CartItem Item = Database.Open().CartItem.FindByCartId(1);
  53. Item.Price.Should().Be(40000);
  54. Item.ProductName.Should().Be("Samsung s2");
  55. }
  56. [Test]
  57. public void After_adding_Two_products_the_Cart_Item_price_Should_be_Caluclated()
  58. {
  59. ICommandHandler<AddProductToCartCommand> command = TestInstance.AddProductToCommandHandler;
  60. command.Execute(new AddProductToCartCommand { CartId = 1, ProductId = 1, Quantity = 2 });
  61. command.Execute(new AddProductToCartCommand { CartId = 1, ProductId = 2, Quantity = 2 });
  62. Cart Item = Database.Open().Cart.FindById(1);
  63. Item.Price.Should().Be(100000);
  64. }
  65. [Test]
  66. public void If_the_Item_Is_already_availble_the_it_Should_Just_Change_the_Quantity()
  67. {
  68. ICommandHandler<AddProductToCartCommand> command = TestInstance.AddProductToCommandHandler;
  69. command.Execute(new AddProductToCartCommand { CartId = 1, ProductId = 1, Quantity = 1 });
  70. command.Execute(new AddProductToCartCommand { CartId = 1, ProductId = 1, Quantity = 3 });
  71. CartItem Item = Database.Open().CartItem.FindByCartId(1);
  72. Item.Price.Should().Be(60000);
  73. Item.ProductName.Should().Be("Samsung s2");
  74. }
  75. }
  76. }