Exemplo n.º 1
0
        public async Task ReturnTransactionDTO_OfTypeStakeAndWin_WhenPassedValidParams(string userName, string amount, string game, string description, string type)
        {
            //Arrange
            contextOptions = new DbContextOptionsBuilder <ItsAllAboutTheGameDbContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnTransactionDTO_OfTypeStakeAndWin_WhenPassedValidParams")
                             .Options;

            dateTimeProviderMock       = new Mock <IDateTimeProvider>();
            foreignExchangeServiceMock = new Mock <IForeignExchangeService>();
            walletServiceMock          = new Mock <IWalletService>();
            userServiceMock            = new Mock <IUserService>();
            cardServiceMock            = new Mock <ICardService>();

            foreignExchangeDTO = new ForeignExchangeDTO
            {
                Base  = GlobalConstants.BaseCurrency,
                Rates = Enum.GetNames(typeof(Currency)).ToDictionary(name => name, value => 2m)
            };

            foreignExchangeServiceMock.Setup(fesm => fesm.GetConvertionRates()).ReturnsAsync(foreignExchangeDTO);

            var user = new User
            {
                Cards        = new List <CreditCard>(),
                Transactions = new List <Transaction>(),
                UserName     = userName,
                CreatedOn    = dateTimeProviderMock.Object.Now,
                Email        = "*****@*****.**",
                FirstName    = "Edward",
                LastName     = "Evlogiev",
                DateOfBirth  = DateTime.Parse("12.03.1993"),
                Role         = UserRole.None,
            };

            var wallet = new Wallet
            {
                Balance  = 0,
                Currency = Currency.EUR,
                User     = user
            };

            //Act
            using (var actContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                await actContext.Users.AddAsync(user);

                await actContext.Wallets.AddAsync(wallet);

                await actContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                var sut = new TransactionService(assertContext, walletServiceMock.Object, userServiceMock.Object,
                                                 foreignExchangeServiceMock.Object, cardServiceMock.Object, dateTimeProviderMock.Object);
                assertContext.Attach(wallet);
                var gameResult = await sut.GameTransaction(user, int.Parse(amount),
                                                           game, description, (TransactionType)Enum.Parse(typeof(TransactionType), type));

                Assert.IsInstanceOfType(gameResult, typeof(TransactionDTO));
                Assert.IsTrue(gameResult.Description == description + game);
                Assert.IsTrue(gameResult.Type == (TransactionType)Enum.Parse(typeof(TransactionType), type));
                Assert.IsTrue(gameResult.Amount == int.Parse(amount) / 2);
                Assert.IsTrue(gameResult.Username == userName);
            }
        }
        public async Task ReturnTransactionDTO_When_PassedValidParams()
        {
            //Arrange
            var contextOptions = new DbContextOptionsBuilder <ItsAllAboutTheGameDbContext>()
                                 .UseInMemoryDatabase(databaseName: "ReturnTransactionDTO_When_PassedValidParams")
                                 .UseInternalServiceProvider(serviceProvider)
                                 .Options;

            dateTimeProvider = new Mock <IDateTimeProvider>();

            foreignExchangeServiceMock = new Mock <IForeignExchangeService>();
            walletServiceMock          = new Mock <IWalletService>();
            userServiceMock            = new Mock <IUserService>();
            cardServiceMock            = new Mock <ICardService>();
            decimal amount = 1000;

            user = new User
            {
                Cards        = new List <CreditCard>(),
                Transactions = new List <Transaction>(),
                UserName     = userName,
                CreatedOn    = dateTimeProvider.Object.Now,
                Email        = email,
                FirstName    = firstName,
                LastName     = lastName,
                DateOfBirth  = DateTime.Parse("02.01.1996"),
                Role         = UserRole.None
            };

            userWallet = new Wallet
            {
                Currency = Currency.GBP,
                Balance  = 2000, // we put 2000 in balance otherwise the method will return an null DTO (business logic)
                User     = user,
            };

            creditCard = new CreditCard
            {
                CardNumber = cardNumber,
                CVV        = cvv,
                LastDigits = lastDigits,
                ExpiryDate = DateTime.Parse("03.03.2022"),
                User       = user,
                CreatedOn  = dateTimeProvider.Object.Now
            };

            foreignExchangeDTO = new ForeignExchangeDTO
            {
                Base  = GlobalConstants.BaseCurrency,
                Rates = Enum.GetNames(typeof(Currency)).ToDictionary(name => name, value => 2m)
            };

            foreignExchangeServiceMock = new Mock <IForeignExchangeService>();
            foreignExchangeServiceMock
            .Setup(foreign => foreign.GetConvertionRates())
            .ReturnsAsync(foreignExchangeDTO);

            userWalletDTO = new WalletDTO(userWallet, foreignExchangeDTO);

            walletServiceMock = new Mock <IWalletService>();
            walletServiceMock
            .Setup(wsm => wsm.GetUserWallet(It.IsAny <User>()))
            .ReturnsAsync(userWalletDTO);

            //Act
            using (var actContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                await actContext.Users.AddAsync(user);

                await actContext.SaveChangesAsync();

                await actContext.Wallets.AddAsync(userWallet);

                await actContext.SaveChangesAsync();

                await actContext.CreditCards.AddAsync(creditCard);

                await actContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                var sut = new TransactionService(assertContext, walletServiceMock.Object,
                                                 userServiceMock.Object, foreignExchangeServiceMock.Object, cardServiceMock.Object, dateTimeProvider.Object);
                assertContext.Attach(user);//We tell the context to monitor the selected object
                var transactionDTO = await sut.MakeDeposit(user, creditCard.Id, amount);

                Assert.IsInstanceOfType(transactionDTO, typeof(TransactionDTO));
            }
        }
Exemplo n.º 3
0
        public async Task MakeTransactions_ThatAreAdded_ToTheBase(string userName, string amount, string game, string description, string type)
        {
            //Arrange
            contextOptions = new DbContextOptionsBuilder <ItsAllAboutTheGameDbContext>()
                             .UseInMemoryDatabase(databaseName: "MakeTransactions_ThatAreAdded_ToTheBase")
                             .Options;

            dateTimeProviderMock       = new Mock <IDateTimeProvider>();
            foreignExchangeServiceMock = new Mock <IForeignExchangeService>();
            walletServiceMock          = new Mock <IWalletService>();
            userServiceMock            = new Mock <IUserService>();
            cardServiceMock            = new Mock <ICardService>();

            foreignExchangeDTO = new ForeignExchangeDTO
            {
                Base  = GlobalConstants.BaseCurrency,
                Rates = Enum.GetNames(typeof(Currency)).ToDictionary(name => name, value => 2m)
            };

            foreignExchangeServiceMock.Setup(fesm => fesm.GetConvertionRates()).ReturnsAsync(foreignExchangeDTO);

            var user = new User
            {
                Cards        = new List <CreditCard>(),
                Transactions = new List <Transaction>(),
                UserName     = userName,
                CreatedOn    = dateTimeProviderMock.Object.Now,
                Email        = "*****@*****.**",
                FirstName    = "Edward",
                LastName     = "Evlogiev",
                DateOfBirth  = DateTime.Parse("12.03.1993"),
                Role         = UserRole.None,
            };

            var wallet = new Wallet
            {
                Balance  = 0,
                Currency = Currency.EUR,
                User     = user
            };

            var transaction = new Transaction()
            {
                Type        = (TransactionType)Enum.Parse(typeof(TransactionType), type),
                Description = description + game,
                User        = user,
                Amount      = int.Parse(amount) / foreignExchangeDTO.Rates[Currency.EUR.ToString()],
                CreatedOn   = dateTimeProviderMock.Object.Now,
                Currency    = wallet.Currency
            };

            //Act
            using (var actContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                await actContext.Users.AddAsync(user);

                await actContext.Wallets.AddAsync(wallet);

                await actContext.Transactions.AddAsync(transaction);

                await actContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                var sut = new TransactionService(assertContext, walletServiceMock.Object, userServiceMock.Object,
                                                 foreignExchangeServiceMock.Object, cardServiceMock.Object, dateTimeProviderMock.Object);
                assertContext.Attach(wallet);
                var gameResult = await sut.GameTransaction(user, int.Parse(amount),
                                                           game, description, (TransactionType)Enum.Parse(typeof(TransactionType), type));

                Assert.AreEqual(transaction, await assertContext.Transactions.Where(t => t.User == user).FirstOrDefaultAsync());
            }
        }
Exemplo n.º 4
0
        public async Task ReturnWalletDTO_When_PassedCorrectParams()
        {
            //Arrange
            contextOptions = new DbContextOptionsBuilder <ItsAllAboutTheGameDbContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnWalletDTO_When_PassedCorrectParams")
                             .Options;

            decimal stake = 1500;

            dateTimeProvider = new DateTimeProvider();

            user = new User
            {
                Cards        = new List <CreditCard>(),
                Transactions = new List <Transaction>(),
                UserName     = userName,
                CreatedOn    = dateTimeProvider.Now,
                Email        = email,
                FirstName    = firstName,
                LastName     = lastName,
                DateOfBirth  = DateTime.Parse("02.01.1996"),
                Role         = UserRole.None,
            };

            userWallet = new Wallet
            {
                Currency = Currency.GBP,
                Balance  = 0,
                User     = user,
            };

            foreignExchangeServiceMock = new Mock <IForeignExchangeService>();

            foreignExchangeDTO = new ForeignExchangeDTO
            {
                Base  = GlobalConstants.BaseCurrency,
                Rates = Enum.GetNames(typeof(Currency)).ToDictionary(name => name, value => 2m)
            };

            var currencies = foreignExchangeServiceMock.Setup(fesm => fesm.GetConvertionRates()).ReturnsAsync(foreignExchangeDTO);

            using (var actContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                await actContext.Users.AddAsync(user);

                await actContext.Wallets.AddAsync(userWallet);

                await actContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new ItsAllAboutTheGameDbContext(contextOptions))
            {
                var walletService = new WalletService(assertContext, foreignExchangeServiceMock.Object, dateTimeProvider);
                assertContext.Attach(userWallet);
                var updateWallet = await walletService.UpdateUserWallet(user, stake);

                var userOfReturnedWallet = await assertContext.Users.Where(u => u.Wallet.Balance * 2 == updateWallet.Balance).FirstOrDefaultAsync();

                Assert.IsInstanceOfType(updateWallet, typeof(WalletDTO));
                Assert.IsTrue(userOfReturnedWallet == user);
            }
        }