public void UpdateSpecificExpenseShouldModifyGivenExpense() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(UpdateSpecificExpenseShouldModifyGivenExpense)) .Options; using (var context = new DataDbContext(options)) { var expenseService = new ExpensesService(context); var toAddExpense = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Utilities", Comments = new List <Comment>() { new Comment() { Text = "A test comment", Important = false } } }; var addedExpense = expenseService.AddExpense(toAddExpense); // new modified expense var toUpdatedExpense = new ExpensesPostModel() { Description = "Test description updated", Sum = 30, Location = "Brasov", Date = DateTime.ParseExact("05/31/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "Euro", Type = "Food", Comments = new List <Comment>() { new Comment() { Text = "An updated test comment", Important = true } } }; var updatedExpense = expenseService.UpsertExpense(addedExpense.Id, toUpdatedExpense); Assert.IsNotNull(updatedExpense); Assert.AreEqual(toUpdatedExpense.Description, updatedExpense.Description); Assert.AreEqual(toUpdatedExpense.Sum, updatedExpense.Sum); Assert.AreEqual(toUpdatedExpense.Location, updatedExpense.Location); Assert.AreEqual(toUpdatedExpense.Date, updatedExpense.Date); Assert.AreEqual(toUpdatedExpense.Currency, updatedExpense.Currency); Assert.AreEqual(toUpdatedExpense.Type, updatedExpense.Type.ToString()); Assert.AreEqual(toUpdatedExpense.Comments, updatedExpense.Comments); } }
public void AddExpenseWithInvalidTypeFieldShouldNotCreateANewExpense() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(AddExpenseWithInvalidTypeFieldShouldNotCreateANewExpense)) .Options; using (var context = new DataDbContext(options)) { var expenseService = new ExpensesService(context); var expected = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Invalid Type", Comments = null }; var actual = expenseService.AddExpense(expected); Assert.IsNull(actual); } }
public void DeleteValidExpenseShouldDeleteExpense() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(DeleteValidExpenseShouldDeleteExpense)) .Options; using (var context = new DataDbContext(options)) { var expenseService = new ExpensesService(context); var expected = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Food", Comments = null }; // add the expense to db var actual = expenseService.AddExpense(expected); // delete expense and put return expense in obj var afterDelete = expenseService.DeleteExpense(actual.Id); // search for the added expense to see if exists in db var result = context.Expensess.Find(actual.Id); Assert.IsNotNull(afterDelete); Assert.IsNull(result); } }
public void GetExpenseByInvalidIdShouldNotFindAnyExpense() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetExpenseByInvalidIdShouldNotFindAnyExpense)) .Options; using (var context = new DataDbContext(options)) { var expenseService = new ExpensesService(context); var toAddExpense = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Utilities", Comments = new List <Comment>() { new Comment() { Text = "A test comment", Important = false } } }; var actual = expenseService.AddExpense(toAddExpense); var expected = expenseService.GetExpenseById(55555); Assert.IsNull(expected); } }
public IActionResult Post([FromBody] ExpensesPostModel expenses) { if (!ModelState.IsValid) { return(BadRequest(expenses)); } expensesService.AddExpense(expenses); return(Ok()); }
/// <summary> /// Create a new Expense, using ExpensesPostModel so we can use string for type /// </summary> /// <param name="expenses">New Expense object</param> /// <returns>Expense added</returns> public ExpensesPostModel Create(ExpensesPostModel expenses) { Expenses toAdd = ExpensesPostModel.ToExpenses(expenses); context.Expensess.Add(toAdd); context.SaveChanges(); return(expenses); }
public IActionResult Put(int id, [FromBody] ExpensesPostModel expenses) { if (!ModelState.IsValid) { return(BadRequest(expenses)); } var result = expensesService.UpsertExpense(id, expenses); return(Ok()); }
/// <summary> /// Create a new Expense, using ExpensesPostModel so we can use string for type /// </summary> /// <param name="expenses">New Expense object</param> /// <returns>Expense added</returns> public Expenses AddExpense(ExpensesPostModel expenses) { Expenses toAdd = ExpensesPostModel.ToExpenses(expenses); if (toAdd == null) { return(null); } context.Expensess.Add(toAdd); context.SaveChanges(); return(toAdd); }
/// <summary> /// Update/Inseret /// </summary> /// <param name="id">Id for update, otherwise add</param> /// <param name="expenses">New Expense Object that we want to add</param> /// <returns>Expense we gave</returns> public Expenses UpsertExpense(int id, ExpensesPostModel expenses) { var existing = context.Expensess.AsNoTracking().Include(c => c.Comments).FirstOrDefault(c => c.Id == id); if (existing == null) { var expense = context.Expensess.Add(ExpensesPostModel.ToExpenses(expenses)); context.SaveChanges(); return(expense.Entity); } var res = ExpensesPostModel.ToUpdateExpenses(expenses, existing); context.SaveChanges(); return(res); }
public void GetCommentsFilteredByValueShouldReturnAListOfFilteredComments() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetCommentsFilteredByValueShouldReturnAListOfFilteredComments)) .Options; using (var context = new DataDbContext(options)) { var commentsService = new CommentsService(context); var expensesService = new ExpensesService(context); var expenseWithCommentToAdd = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Utilities", Comments = new List <Comment>() { new Comment() { Text = "A test comment 1 filtered", Important = false }, new Comment() { Text = "A test comment 2 filtered", Important = true }, new Comment() { Text = "A test comment 3", Important = false } } }; expensesService.AddExpense(expenseWithCommentToAdd); List <CommentGetModel> comments = commentsService.GetAll("filtered"); int numberOfComments = comments.Count; Assert.IsNotNull(comments); Assert.AreEqual(2, numberOfComments); } }
public void DeleteValidExpenseWithCommentsShouldDeleteExpenseAndComments() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(DeleteValidExpenseWithCommentsShouldDeleteExpenseAndComments)) .Options; using (var context = new DataDbContext(options)) { var expenseService = new ExpensesService(context); var expected = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Food", Comments = new List <Comment>() { new Comment() { Text = "A test comment", Important = false } } }; // add the expense to db var actual = expenseService.AddExpense(expected); // delete expense and put return expense in obj var afterDelete = expenseService.DeleteExpense(actual.Id); // find the number of comments that exists in db int numberOfCommentsInDb = context.Comments.CountAsync().Result; // search for the added expense to see if exists in db var resultExpense = context.Expensess.Find(actual.Id); Assert.IsNotNull(afterDelete); Assert.IsNull(resultExpense); Assert.AreEqual(0, numberOfCommentsInDb); } }
public void AddAValidExpenseShouldCreateANewExpense() { var options = new DbContextOptionsBuilder <DataDbContext>() .UseInMemoryDatabase(databaseName: nameof(AddAValidExpenseShouldCreateANewExpense)) .Options; using (var context = new DataDbContext(options)) { var expenseService = new ExpensesService(context); var expected = new ExpensesPostModel() { Description = "Test description", Sum = 20, Location = "Cluj", Date = DateTime.ParseExact("05/30/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture), Currency = "RON", Type = "Utilities", Comments = new List <Comment>() { new Comment() { Text = "A test comment", Important = false } } }; var actual = expenseService.AddExpense(expected); Assert.IsNotNull(actual); Assert.AreEqual(expected.Description, actual.Description); Assert.AreEqual(expected.Sum, actual.Sum); Assert.AreEqual(expected.Location, actual.Location); Assert.AreEqual(expected.Date, actual.Date); Assert.AreEqual(expected.Currency, actual.Currency); Assert.AreEqual(expected.Type, actual.Type.ToString()); Assert.AreEqual(expected.Comments, actual.Comments); } }