public async Task TestGetAllSpendingsGoodData() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending1 = new Spending { Amount = 10, Text = "beer1", IsPayed = true }; Spending spending2 = new Spending { Amount = 20, Text = "beer2", IsPayed = false }; await context.Spendings.AddAsync(spending2); await context.Spendings.AddAsync(spending1); await context.SaveChangesAsync(); List <SpendingDTO> output = spendingService.GetAllSpendings(); Assert.Equal(2, output.Count); Assert.True(context.Spendings.Any(x => x.Id == spending1.Id)); Assert.Equal(20, output[0].Amount); Assert.Equal("beer2", output[0].Text); Assert.False(output[0].IsPayed); Assert.True(context.Spendings.Any(x => x.Id == spending2.Id)); Assert.Equal(10, output[1].Amount); Assert.Equal("beer1", output[1].Text); Assert.True(output[1].IsPayed); }
public async Task TestEditSpedningGoodData() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); SpendingDTO model = new SpendingDTO { Amount = 100, Text = "alot of beer", IsPayed = false, Id = spending.Id }; bool output = await spendingService.EditSpending(model); Assert.True(output); Assert.Equal(100, context.Spendings.Where(x => x.Id == spending.Id).FirstOrDefault().Amount); Assert.Equal("alot of beer", context.Spendings.Where(x => x.Id == spending.Id).FirstOrDefault().Text); Assert.False(context.Spendings.Where(x => x.Id == spending.Id).FirstOrDefault().IsPayed); }
public void TestGetAllSpendingsEmptyTable() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); List <SpendingDTO> output = spendingService.GetAllSpendings(); Assert.Empty(output); }
public async Task TestDeleteSpendingInvalidId() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); await Assert.ThrowsAsync <ACMException>(() => spendingService.DeleteSpending(spending.Id + "Random string")); }
public async Task TestCreateSpending() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); SpendingDTO model = new SpendingDTO { Amount = 10, Text = "beer", IsPayed = true }; string id = await spendingService.CreateSpending(model); Assert.Single(context.Spendings.ToList()); Assert.True(context.Spendings.Any(x => x.Id == id)); Assert.Equal("beer", context.Spendings.Where(x => x.Id == id).FirstOrDefault().Text); Assert.Equal(10, context.Spendings.Where(x => x.Id == id).FirstOrDefault().Amount); Assert.True(context.Spendings.Where(x => x.Id == id).FirstOrDefault().IsPayed); }
public async Task TestDeleteSpendingGoodData() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); bool output = await spendingService.DeleteSpending(spending.Id); Assert.True(output); Assert.Empty(context.Spendings.ToList()); }
public async Task TestGetOneSpendingGoodData() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); SpendingDTO output = spendingService.GetOneSpending(spending.Id); Assert.Equal(spending.Id, output.Id); Assert.Equal(10, output.Amount); Assert.Equal("beer", output.Text); Assert.True(output.IsPayed); }
public async Task TestEditSpedningInvalidId() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); SpendingDTO model = new SpendingDTO { Amount = 100, Text = "alot of beer", IsPayed = false, Id = spending.Id + "Random string" }; await Assert.ThrowsAsync <ACMException>(() => spendingService.EditSpending(model)); }
public void AddSpending(int userId, int monthToAdd, decimal amount, string currency, Nature nature, string comment, SpendingCreationError expectedOutcome) { // Arrange User ironMan = new User("Stark", "Anthony", new RegionInfo("US").ISOCurrencySymbol); User userNotFound = null; Mock <ISpendingRepository> spendingRepositoryMock = new Mock <ISpendingRepository>(MockBehavior.Strict); spendingRepositoryMock.Setup(r => r.AddSpending(It.IsAny <Spending>())).Callback <Spending>(s => CheckDateAreCorrect(s.DateInUtc)); spendingRepositoryMock.Setup(r => r.LoadUserById(1)).Returns(ironMan); spendingRepositoryMock.Setup(r => r.LoadUserById(3)).Returns(userNotFound); spendingRepositoryMock.Setup(r => r.SpendingExists(It.IsAny <int>(), It.IsAny <DateTime>(), 42)).Returns(true); spendingRepositoryMock.Setup(r => r.SpendingExists(It.IsAny <int>(), It.IsAny <DateTime>(), 100)).Returns(false); spendingRepositoryMock.Setup(r => r.SpendingExists(It.IsAny <int>(), It.IsAny <DateTime>(), -100)).Returns(false); // Act // Adding/removing months to today DateTime spendingDate = DateTime.Today.AddMonths(monthToAdd); ISpendingService sut = new SpendingService(spendingRepositoryMock.Object); SpendingCreationError outcome = sut.TryCreateSpending(userId, spendingDate, amount, currency, nature, comment); // Assert Assert.Equal(expectedOutcome, outcome); Assert.Equal(expectedOutcome, outcome); if (expectedOutcome == SpendingCreationError.None) { spendingRepositoryMock.Verify(s => s.AddSpending(It.IsAny <Spending>()), Times.Once); } else { spendingRepositoryMock.Verify(s => s.AddSpending(It.IsAny <Spending>()), Times.Never); } }
public async Task <ActionResult> DebtSettlement(long debtorId, long lenderId, long groupId) { await SpendingService.DebtSettlement(GetCurrentUserID(), debtorId, lenderId, groupId); return(Ok()); }
public async Task <IActionResult> Update([FromBody] API.Request.SpendingUpdate spendingUpdate) { await SpendingService.UpdateSpending(GetCurrentUserID(), spendingUpdate); return(Ok()); }
public async Task <IActionResult> Create([FromBody] API.Request.NewSpending newSpending) { await SpendingService.CreateNewSpending(GetCurrentUserID(), newSpending); return(Ok()); }
public async Task <ActionResult <IList <OptimisedDebtData> > > GetOptimizedDebts(long groupId) { var optimisedDebtDatas = SpendingService.ToOptimisedDebtData(await SpendingService.GetOptimizedDebtForGroup(GetCurrentUserID(), groupId)); return(Ok(optimisedDebtDatas)); }
public async Task <ActionResult <IList <SpendingData> > > GetSpendingData(long groupId) { var spendingDatas = SpendingService.ToSpendingData(await SpendingService.GetSpendingsForGroup(GetCurrentUserID(), groupId)); return(Ok(spendingDatas)); }
public async Task <IActionResult> Delete(long groupId, long spendingId) { await SpendingService.DeleteSpending(GetCurrentUserID(), spendingId, groupId); return(Ok()); }