示例#1
0
        public void ExecuteTest()
        {
            var user     = new User("test", "test", "test", "*****@*****.**", 99);
            var product1 = new BuyTransaction(DateTime.Now, user, new Product("test", 100, true));
            var product2 = new BuyTransaction(DateTime.Now, user, new Product("test", 0, false));

            Assert.Throws <InsufficientCreditsException>(new TestDelegate(() => { product1.Execute(); }));
            Assert.Throws <NotActiveException>(new TestDelegate(() => { product2.Execute(); }));
        }
示例#2
0
        public void ExecuteInvalidBuyTransaction_WithCreditOff_ThrowError()
        {
            User           user    = new User("first name", "last name", "username", "*****@*****.**", 0);
            Product        product = new Product(69, "TestProduct", 25, true, false);
            BuyTransaction bt      = new BuyTransaction(user, product.Price, product);


            Assert.Throws <InsufficientCreditsException>(() => bt.Execute());
        }
示例#3
0
        public void ExecuteInvalidBuyTransaction_WithInactiveProduct_ThrowError()
        {
            User           user    = new User("first name", "last name", "username", "*****@*****.**", 500);
            Product        product = new Product(69, "TestProduct", 25, false, true);
            BuyTransaction bt      = new BuyTransaction(user, product.Price, product);


            Assert.Throws <NotActiveProductException>(() => bt.Execute());
        }
        public void Execute_UserBuysProduct_IsUserBalanceUpdatedCorrectly(decimal userBalance, decimal productPrice)
        {
            IUser        user        = CreateUser(userBalance);
            IProduct     product     = CreateProduct(productPrice);
            ITransaction transaction = new BuyTransaction(user, product, DateTime.Now);

            transaction.Execute();

            Assert.AreEqual(userBalance - productPrice, user.Balance);
        }
示例#5
0
        public void ExecuteValidBuyTransaction_Succes(decimal amount)
        {
            User           user    = new User("first name", "last name", "username", "*****@*****.**", amount);
            Product        product = new Product(69, "TestProduct", 25, true, true);
            BuyTransaction bt      = new BuyTransaction(user, product.Price, product);

            bt.Execute();

            Assert.IsTrue((amount - product.Price) == user.Balance);
        }
示例#6
0
        public void BuyTransaction_buyingInactiveProduct_ShouldThrowProductNotActivatedException(string desc)
        {
            //arrange
            User           user  = new User(id: 1, firstname: "jakob1", lastname: "skov", username: "******", email: "*****@*****.**", balance: 100, lowBalanceDefinition: 50);
            Product        apple = new Product(id: 1, name: "apple", price: 10, active: false);
            BuyTransaction tr    = new BuyTransaction(user: user, product: apple);

            //act

            //assert
            Assert.Throws <ProductNotActivatedException>(() => tr.Execute(), desc);
        }
示例#7
0
        public void BuyTransaction_buyingSomethingYouCantAfford_ShouldThrowInsufficientCreditsException(string desc)
        {
            //arrange
            User           user  = new User(id: 1, firstname: "jakob1", lastname: "skov", username: "******", email: "*****@*****.**", balance: 100, lowBalanceDefinition: 50);
            Product        apple = new Product(id: 1, name: "apple", price: 199);
            BuyTransaction tr    = new BuyTransaction(user: user, product: apple);

            //act

            //assert
            Assert.Throws <InsufficientCreditsException>(() => tr.Execute(), desc);
        }
示例#8
0
        public void BuyTransaction_BuyProductWithSufficientCredits_Success(decimal productPrice)
        {
            Product product = Substitute.For <Product>("Milk", productPrice);
            User    user    = Substitute.For <User>(userArgs);

            user.Balance = productPrice;

            BuyTransaction transaction = new BuyTransaction(user, product);

            transaction.Execute();

            Assert.That(transaction.Succeeded, Is.True);
        }
示例#9
0
        public void BuyTransaction_buyingProduct_ShouldWithdrawAmountFromUserBalance(string desc)
        {
            //arrange
            User           user     = new User(id: 1, firstname: "jakob1", lastname: "skov", username: "******", email: "*****@*****.**", balance: 100, lowBalanceDefinition: 50);
            Product        apple    = new Product(id: 1, name: "apple", price: 10, active: true);
            BuyTransaction tr       = new BuyTransaction(user, apple);
            decimal        expected = 90;

            //act
            tr.Execute();

            //assert
            Assert.AreEqual(expected, user.Balance, $"{desc}: actual:{tr.User.Balance},expected:{expected}");
        }
示例#10
0
        public void BuyTransaction_UndoProductPurchase_Success()
        {
            const decimal amount  = 10;
            Product       product = Substitute.For <Product>("Milk", amount);
            User          user    = Substitute.For <User>(userArgs);

            user.Balance = amount;

            BuyTransaction transaction = new BuyTransaction(user, product);

            transaction.Execute();
            transaction.Undo();

            Assert.That(transaction.Undone, Is.True);
            Assert.That(user.Balance, Is.EqualTo(amount));
        }
        public void Execute_InsufficientCredits_ThrowInsufficientCreditsException(decimal userBalance)
        {
            // Arrange
            IUser    user    = Substitute.For <IUser>();
            IProduct product = Substitute.For <IProduct>();

            product.IsActive = true;
            decimal productPrice = userBalance + 10; // Make price greater than userBalance

            user.Balance  = userBalance;
            product.Price = productPrice;
            ITransaction transaction = new BuyTransaction(user, DateTime.Now, product);

            // Act
            void ExecuteTransaction() => transaction.Execute();

            // Assert
            Assert.Throws <InsufficientCreditsException>(ExecuteTransaction);
        }
        public void Execute_SufficientCredits_WidtdrawsTransactionAmountFromUserBalance()
        {
            // Arrange
            IUser    user    = Substitute.For <IUser>();
            IProduct product = new Product(1000, "Name", 1000, true);

            decimal startBalance = 100m;

            user.Balance = startBalance;
            decimal productPrice = 10m;

            product.Price = productPrice;

            ITransaction transaction = new BuyTransaction(user, DateTime.Now, product);

            // Act
            transaction.Execute();
            // Assert
            Assert.AreEqual(startBalance - productPrice, user.Balance);
        }