public RecurringTransactionServiceTest() { ContextProvider = new Mock <IContextProvider>(); EventManager = new Mock <IEventManager>(); RecurringTransactionRepository = new Mock <IRegularTransactionRepository>(); CategoryRepository = new Mock <ICategoryRepository>(); WalletRepository = new Mock <IWalletRepository>(); UserRepository = new Mock <IUserRepository>(); UserContext = new Mock <IUserContext>(); UserContext.Setup(c => c.GetUserId()).Returns(TESTING_USER_ID); ContextProvider .Setup(c => c.GetRepository <IRegularTransactionRepository>()) .Returns(RecurringTransactionRepository.Object); ContextProvider .Setup(c => c.GetRepository <IWalletRepository>()) .Returns(WalletRepository.Object); ContextProvider .Setup(c => c.GetRepository <ICategoryRepository>()) .Returns(CategoryRepository.Object); ContextProvider .Setup(c => c.GetRepository <IUserRepository>()) .Returns(UserRepository.Object); RecurringTransactionService = new RecurringTransactionService( ContextProvider.Object, UserContext.Object, EventManager.Object ); }
public async Task RecurringTransactionService_Updates_Wallet_Updated_RecurringTransaction_Increase() { DateTime now = DateTime.UtcNow; UserModel user = new UserModel("Test User", new DateTime(), "*****@*****.**", "password", api.Enums.Currency.Euro); user._id = "1234567891234567891234"; user.Role = "user"; WalletModel wallet = new WalletModel("Unit Test", 0, true, 490, null, api.Enums.WalletColor.Green, now); wallet._id = "1234567891234567891234"; wallet.DateOffsetBalance = new List <DateOffsetBalance>(); wallet.DateOffsetBalance.Add(new DateOffsetBalance(DateTime.UtcNow, 490)); user.Wallets.Add(wallet); RecurringTransactionModel oldRecurringTransactionModel = new RecurringTransactionModel(user._id, wallet._id, "Netflix", 100, 27, api.Enums.TransactionCategory.Entertainment, api.Enums.RecurringType.Subscription); RecurringTransactionModel newRecurringTransactionModel = new RecurringTransactionModel(user._id, wallet._id, "Netflix", 110, 27, api.Enums.TransactionCategory.Entertainment, api.Enums.RecurringType.Subscription); RecurringTransactionService service = new RecurringTransactionService(); WalletModel newWallet = await service.UpdateWalletForUpdatedRecurringTransaction(user, newRecurringTransactionModel, oldRecurringTransactionModel); newWallet.Balance.Should().Be(480); newWallet.LastUpdated.Should().NotBeOnOrBefore(now); newWallet.DateOffsetBalance.Should().NotBeNull(); }
public async Task Create_ReturnsNewTransaction() { string usersJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Users.json"); var user = JsonConvert.DeserializeObject <List <User> >(usersJson).First(u => u.Id == TESTING_USER_ID); string categoriesJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Categories.json"); var category = JsonConvert.DeserializeObject <List <Category> >(categoriesJson) .First(c => !c.IsCustom || c.User.Id == TESTING_USER_ID); string walletsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Wallets.json"); var wallet = JsonConvert.DeserializeObject <List <Wallet> >(walletsJson) .First(w => !w.IsDefault && w.User.Id == TESTING_USER_ID); const string newTitle = "Test title"; const double newAmount = 101; string newInterval = RegularTransaction.IntervalOptions.Month.ToString(); DateTime newNextDate = DateTime.Today.AddDays(7); var newTransaction = new RegularTransaction { Title = newTitle, Amount = newAmount, CategoryId = category.Id, NextTransactionDate = newNextDate, Interval = newInterval, WalletId = wallet.Id }; UserRepository.Setup(u => u.GetByKey(TESTING_USER_ID)).Returns(user); CategoryRepository.Setup(c => c.GetByKey(category.Id)).Returns(category); RecurringTransactionRepository .Setup(t => t.Create(newTransaction)) .ReturnsAsync(newTransaction); WalletRepository.Setup(w => w.GetByKey(wallet.Id)).Returns(wallet); var resultTransaction = await RecurringTransactionService.Create(newTransaction); Assert.NotNull(resultTransaction); Assert.NotNull(resultTransaction.User); Assert.NotNull(resultTransaction.Category); Assert.NotNull(resultTransaction.Wallet); Assert.Equal(TESTING_USER_ID, resultTransaction.User.Id); Assert.Equal(category.Id, resultTransaction.Category.Id); Assert.Equal(wallet.Id, resultTransaction.Wallet.Id); Assert.Equal(newTitle, resultTransaction.Title); Assert.Equal(newAmount, resultTransaction.Amount); Assert.Equal(newInterval, resultTransaction.Interval); Assert.Equal(DateTime.Today, resultTransaction.Date); Assert.Equal(newNextDate, resultTransaction.NextTransactionDate); }
public async Task Delete_ReturnsDeletedTransaction() { string transactionsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"RegularTransactions.json"); var transaction = JsonConvert.DeserializeObject <List <RegularTransaction> >(transactionsJson) .First(u => u.User.Id == TESTING_USER_ID); RecurringTransactionRepository.Setup(t => t.GetByKey(transaction.Id)).Returns(transaction); RecurringTransactionRepository .Setup(t => t.Delete(transaction.Id)) .ReturnsAsync(transaction); var resultTransaction = await RecurringTransactionService.Delete(transaction.Id); Assert.NotNull(resultTransaction); Assert.Equal(transaction.Title, resultTransaction.Title); Assert.Equal(transaction.Amount, resultTransaction.Amount); Assert.Equal(transaction.Interval, resultTransaction.Interval); Assert.Equal(transaction.Date, resultTransaction.Date); Assert.Equal(transaction.NextTransactionDate, resultTransaction.NextTransactionDate); }
public async Task Update_ReturnsUpdatedTransaction() { string usersJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Users.json"); var user = JsonConvert.DeserializeObject <List <User> >(usersJson).First(u => u.Id == TESTING_USER_ID); string categoriesJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Categories.json"); var category = JsonConvert.DeserializeObject <List <Category> >(categoriesJson) .First(c => c.IsCustom && c.User.Id == TESTING_USER_ID); string walletsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Wallets.json"); var wallet = JsonConvert.DeserializeObject <List <Wallet> >(walletsJson) .First(w => w.IsDefault); string transactionsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"RegularTransactions.json"); var transaction = JsonConvert.DeserializeObject <List <RegularTransaction> >(transactionsJson) .First(u => u.User.Id == TESTING_USER_ID && u.Category.Id == category.Id && u.Wallet.Id == wallet.Id); const string newTitle = "Test title"; const double newAmount = 101; // Just because "user" and "category" variables contain more relationships transaction.User = user; transaction.Category = category; transaction.Wallet = wallet; var newTransaction = new RegularTransaction { Id = transaction.Id, Title = newTitle, Amount = newAmount, Category = transaction.Category, Wallet = transaction.Wallet, User = transaction.User, NextTransactionDate = transaction.NextTransactionDate, Interval = transaction.Interval }; CategoryRepository.Setup(c => c.GetByKey(category.Id)).Returns(category); RecurringTransactionRepository.Setup(t => t.GetByKey(newTransaction.Id)).Returns(transaction); RecurringTransactionRepository .Setup(t => t.Update(transaction)) .ReturnsAsync(transaction); var resultTransaction = await RecurringTransactionService.Update(newTransaction); Assert.NotNull(resultTransaction); Assert.NotNull(resultTransaction.User); Assert.NotNull(resultTransaction.Category); Assert.NotNull(resultTransaction.Wallet); Assert.Equal(TESTING_USER_ID, resultTransaction.User.Id); Assert.Equal(category.Id, resultTransaction.Category.Id); Assert.Equal(wallet.Id, resultTransaction.Wallet.Id); Assert.Equal(newTitle, resultTransaction.Title); Assert.Equal(newAmount, resultTransaction.Amount); Assert.Equal(transaction.Interval, resultTransaction.Interval); Assert.Equal(transaction.Date, resultTransaction.Date); Assert.Equal(transaction.NextTransactionDate, resultTransaction.NextTransactionDate); }
public RecurringTransactionController(IHttpContextAccessor httpContextAccessor, RecurringTransactionService recurringtransactionService) { this._recurringTransactionService = recurringtransactionService; this.userId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value.ToString(); }