Exemplo n.º 1
0
        public void Balance_When_purchase_products_Then_balance_is_correctly_updated()
        {
            // Arrange
            var balance1 = new Account { Id = 1,  Name = "YYY", Balance = 100 };
            var balance2 = new Account { Id = 2, Name = "YY", Balance = 10 };
            var balance3 = new Account { Id = 3, Name = "Y", Balance = 101 };

            var productBalance = A.Fake<IRepository<Account>>();
            A.CallTo(() => productBalance.Query()).Returns(new[] { balance1, balance2, balance3 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 101 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(productBalance);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act
            service.Purchase(account.Id, new[] { balance1.Id, balance2.Id }, balance1.Balance + balance2.Balance);

            // Assert
            Assert.That(balance1.Balance, Is.EqualTo(102));
            Assert.That(balance2.Balance, Is.EqualTo(11));
        }
        public void Purchase_When_not_enough_balance_Then_throw_exception()
        {
            // Assert
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act and Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(1, Enumerable.Empty<int>(), 200.50m));
        }
        public void GetDiscount_When_selected_expensive_products_Then_can_get_5percent_discount()
        {
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            var discount = service.GetDiscount(2, 200m);

            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT));
        }
        public void GetDiscount_When_previous_orders_and_selected_expensive_products_Then_can_get_15percent_discount()
        {
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            var discount = service.GetDiscount(1, 200m);

            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT + PurchaseService.ORDERS_HISTORY_DISCOUNT));
        }
        public void GetDiscount_When_selected_expensive_products_and_account_has_enought_orders_Then_max_discount()
        {
            //Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            //Act
            var discount = service.GetDiscount(1, 250.5m);

            //Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT + PurchaseService.ORDERS_HISTORY_DISCOUNT));
        }
        public void GetDiscount_When_previous_orders_greater_than_500m_and_selected_products_greater_than_200m_Then_account_get_15_per_discount()
        {
            // Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act
            var discount = service.GetDiscount(3, 200m);

            // Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT + PurchaseService.ORDERS_HISTORY_DISCOUNT));
        }
        public void GetDiscount_When_account_selected_expensive_product_Then_expensive_products_discount()
        {
            // Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act
            var discount = service.GetDiscount(131, 201.5m);

            // Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT));
        }
        public void GetDiscount_When_account_has_not_enough_orders_and_selected_expensive_product_Then_orders_history_discount()
        {
            // Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act
            var discount = service.GetDiscount(2, 200m);

            // Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT));
        }
        public void GetDiscount_When_account_has_enough_orders_and_selected_cheap_product_Then_orders_history_discount()
        {
            // Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act
            var discount = service.GetDiscount(1, 10.5m);

            // Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.ORDERS_HISTORY_DISCOUNT));
        }
        public void GetDiscount_When_selected_expensive_products_price_greater_than_200m_Then_account_get_5_per_discount()
        {
            // Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act
            var discount = service.GetDiscount(2, 200m);

            // Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT));
        }
Exemplo n.º 11
0
        public void GetDiscount_When_account_has_order_history_for_discount_and_selected_expensive_product_Then_sum_discounts()
        {
            //Arreng
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            //Act
            var discount = service.GetDiscount(1, 200m);

            //Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT+PurchaseService.ORDERS_HISTORY_DISCOUNT));
        }
        public MainView()
        {
            InitializeComponent();

            // It should be constructed using the IoC container.
            var unitOfWorkFactory = new UnitOfWorkFactory();
            var dataLoadingService = new DataLoadingService(unitOfWorkFactory);
            var purchaseService = new PurchaseService(unitOfWorkFactory);

            // Create and set manually to simplify the example.
            DataContext = new MainViewModel(dataLoadingService, purchaseService);
        }
        public CashboxCalculatuinDiscountSteps()
        {
            _fakeAccountRepository = new FakeRepository<Account>();
            _fakeOrderRepository = new FakeRepository<Order>();
            _fakeProductRepository = new FakeRepository<Product>();

            _fakeUnitOfWorkFactory = new FakeUnitOfWorkFactory(
                uow =>
                {
                    uow.SetRepository(_fakeAccountRepository);
                    uow.SetRepository(_fakeOrderRepository);
                    uow.SetRepository(_fakeProductRepository);
                });
            _service = new PurchaseService(_fakeUnitOfWorkFactory);
            TestSetup();
        }
        public void GivenIMakeAPurchaseWhichIsWorth(int value)
        {
            var orderRepository = A.Fake<IRepository<Order>>();
            A.CallTo(() => orderRepository.Query()).Returns(new[] { _order }.AsQueryable());

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Query()).Returns(new[] { _account }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Order>()).Returns(orderRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            _result = service.GetDiscount(_account.Id, value);
        }
        public void WhenIGetDiscount()
        {
            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(_account);

            var orderRepository = A.Fake<IRepository<Order>>();
            A.CallTo(() => orderRepository.Query()).Returns(new[] { _order}.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Order>()).Returns(orderRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            _discount = service.GetDiscount(_account.Id, _productPrice);
        }
        public void WhenIByProducts()
        {
            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { _product }.AsQueryable());

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(_account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            service.Purchase(_account.Id, new[] { _product.Id }, _product.Price);
        }
        public void WhenIAskHowMuchIsTheBalanceNow()
        {
            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(_account);

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { _product1, _product2 }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            service.Purchase(_account.Id, new[] { _product1.Id, _product2.Id }, _product1.Price+_product2.Price);
            _result = _account.Balance;
        }
        public void GetDiscount_When_having_ordered_enough_products_Then_get_5percent_discount()
        {
            var account = new Account { Id = 1, Balance = 400m, Name = "John" };
            var order1 = new Order { Id = 1, AccountId = 1};

            var orderRepository = A.Fake<IRepository<Order>>();
            A.CallTo(() => orderRepository.Query()).Returns(new[] { order1 }.AsQueryable());

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Query()).Returns(new[] { account }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Order>()).Returns(orderRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            var discount = service.GetDiscount(account.Id, 200);

            Assert.That(discount, Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT));
        }
        public void Purchase_When_amount_of_product_equals_zero_Then_throw_exception()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 5 };
            var product2 = new Product { Id = 2, Price = 2, Amount = 0 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1, product2}.AsQueryable());

            var account = new Account { Id = 1, Balance = 10};

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act and Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(account.Id, new[] { product1.Id, product2.Id, }, product1.Price + product2.Price));
        }
        public void Purchase_When_purchase_products_with_0_amount_Then_purchase_should_throw_an_exception()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 2, Amount = 0, Title = "Keyboard" };
            var product2 = new Product { Id = 2, Amount = 2, Price = 2, Title = "Mouse" };
            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 5 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(account.Id, new[] { product1.Id, product2.Id }, product1.Price + product2.Price));
        }
Exemplo n.º 21
0
        public void Purchase_When_purchase_products_with_amount_0_Then_throw_argument_exception()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 0 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 1 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act and Assert
            Assert.Throws<ArgumentException>(() => service.Purchase(account.Id, new[] { product1.Id }, product1.Price));
        }
Exemplo n.º 22
0
        public void Purchase_When_purchase_products_Then_SaveChanges_of_UnitOfWork_called()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 1 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 1 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act
            service.Purchase(account.Id, new[] { product1.Id }, product1.Price);

            // Assert
            A.CallTo(() => unitOfWork.SaveChanges()).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 23
0
        public void Purchase_When_purchase_products_Then_products_amount_correctly_updated()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 5 };
            var product2 = new Product { Id = 2, Price = 2, Amount = 10 };
            var product3 = new Product { Id = 3, Price = 3, Amount = 7 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1, product2, product3 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 10 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act
            service.Purchase(account.Id, new[] { product1.Id, product2.Id }, product1.Price + product2.Price);

            // Assert
            Assert.That(product1.Amount, Is.EqualTo(4));
            Assert.That(product2.Amount, Is.EqualTo(9));
        }
        public void GetDiscount_When_products_value_and_previous_orders_value_enough_for_discount_Then_get_15percent_discount()
        {
            var account = new Account { Id = 1, Balance = 400m, Name = "John" };
            var order1 = new Order { Id = 1, AccountId = 1, Total = 250m};
            var order2 = new Order { Id = 2, AccountId = 1, Total = 250m};

            var orderRepository = A.Fake<IRepository<Order>>();
            A.CallTo(() => orderRepository.Query()).Returns(new[] { order1, order2 }.AsQueryable());

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Query()).Returns(new[] { account }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Order>()).Returns(orderRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            var discount = service.GetDiscount(account.Id, 200);

            Assert.That(
                discount,
                Is.EqualTo(PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT + PurchaseService.ORDERS_HISTORY_DISCOUNT));
        }
        public void Purchase_When_trying_to_purchase_product_with_zero_amount_Then_throw_purchase_exception()
        {
            var product1 = new Product { Id = 1, Amount = 0, Price = 100m };
            var account = new Account { Id = 1, Balance = 105m };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            service.Purchase(account.Id, new[] { product1.Id }, product1.Price);
        }
        public void Purchase_When_purchase_made_Then_account_balance_updates_properly()
        {
            var product1 = new Product { Id = 1, Amount = 1, Price = 100m };
            var account = new Account { Id = 1, Balance = 105m };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            service.Purchase(account.Id, new[] { product1.Id }, product1.Price);

            Assert.That(account.Balance, Is.EqualTo(5));
        }
        public void Purchase_When_not_selected_any_product_Then_throw_exception()
        {
            // Assert
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act and Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(2, Enumerable.Empty<int>(), 200.50m));
        }
Exemplo n.º 28
0
        public void GetDiscount_When_account_Then_orders_products()
        {
            // Arrange
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act
            var discount = service.GetDiscount(1, 10.5m);

            // Assert
            Assert.That(discount, Is.EqualTo(PurchaseService.ORDERS_HISTORY_DISCOUNT +PurchaseService.EXPENSIVE_PRODUCTS_DISCOUNT));
        }
        public void Purchase_When_product_amount_is_0_Then_throw_an_exception()
        {
            // Arrange
            var product = new Product { Id = 1, Title = "Test product", Price = 10, Amount = 0 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product }.AsQueryable());

            var account = new Account { Id = 1, Balance = 1000m };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(account.Id, new[] { product.Id }, product.Price));
        }