public async Task TestCostByTrip() { using (var context = new SpendingTrackContext(options)) { SpendingItem TestItem = new SpendingItem() { Heading = "Flight to Sydney", Category = "flight", Cost = 400, TripID = 1, Currency = "AU$", CreatedAt = "", ReceiptID = "", Note = "QANTAS Flight", }; SpendingController spendingController = new SpendingController(context, configuration); IActionResult result = await spendingController.PostSpendingItem(TestItem) as IActionResult; Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); double result_2 = await spendingController.CostByTrip(1); Assert.IsTrue(result_2 == 455.89); } }
public void SetupDb() { using (var context = new SpendingTrackContext(options)) { SpendingItem spendingItem1 = new SpendingItem() { Heading = headingList[0], Category = categoryList[0], Cost = costList[0], TripID = tripIDList[0], Currency = currencyList[0], CreatedAt = createdAtList[0], ReceiptID = receiptIDList[0], Note = noteList[0] }; SpendingItem spendingItem2 = new SpendingItem() { Heading = headingList[1], Category = categoryList[1], Cost = costList[1], TripID = tripIDList[1], Currency = currencyList[1], CreatedAt = createdAtList[1], ReceiptID = receiptIDList[1], Note = noteList[1] }; context.SpendingItem.Add(spendingItem1); context.SpendingItem.Add(spendingItem2); context.SaveChanges(); } }
public async Task TestAddNewZeroCostItem() { using (var context = new SpendingTrackContext(options)) { SpendingItem TestItem = new SpendingItem() { Heading = "Lunch at Yumekaze", //what can be in the category field is restricted, see the helper function in SpendingTrack to see what's //allowed to be in the category field (lunch is not permitted) Category = "food", Cost = 0, TripID = 3, Currency = "JPY", CreatedAt = "", ReceiptID = "", Note = "Very good sushi", }; SpendingController spendingController = new SpendingController(context, configuration); IActionResult result = await spendingController.PostSpendingItem(TestItem) as IActionResult; //should return unprocessableentity Assert.IsInstanceOfType(result, typeof(UnprocessableEntityObjectResult)); } }
public async Task TestPutInvalidItem() { using (var context = new SpendingTrackContext(options)) { // Given SpendingItem grabItem = context.SpendingItem.Where(x => x.Heading == headingList[1]).Single(); grabItem.Heading = "Flight to Chiang Rai"; grabItem.Cost = 0; //cost should never be 0 // When SpendingController controller = new SpendingController(context, configuration); IActionResult result = await controller.PutSpendingItem(grabItem.ID, grabItem) as IActionResult; Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(UnprocessableEntityResult)); } }
public async Task TestPutItem() { using (var context = new SpendingTrackContext(options)) { // Given SpendingItem grabItem = context.SpendingItem.Where(x => x.Heading == headingList[0]).Single(); grabItem.Heading = "Dinner at the Opera House"; grabItem.Cost = 145; // When SpendingController controller = new SpendingController(context, configuration); IActionResult result = await controller.PutSpendingItem(grabItem.ID, grabItem) as IActionResult; // Then check that item was updated successfully / NOCONTENT response Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(NoContentResult)); //Then check if there's only one entry in db with heading "Dinner at the Opera House" grabItem = context.SpendingItem.Where(x => x.Heading == "Dinner at the Opera House").Single(); } }
public async Task <IActionResult> PostSpendingItem([FromBody] SpendingItem spendingItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Do sanity check before posting if (SpendingItemHelper.ValidCategory(spendingItem.Category) && spendingItem.Heading != null && spendingItem.Cost != 0 && spendingItem.Currency != null) { _context.SpendingItem.Add(spendingItem); await _context.SaveChangesAsync(); return(CreatedAtAction("GetSpendingItem", new { id = spendingItem.ID }, spendingItem)); } else { return(UnprocessableEntity(ModelState)); } }
public async Task <IActionResult> PutSpendingItem([FromRoute] int id, [FromBody] SpendingItem spendingItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != spendingItem.ID) { return(BadRequest()); } if (!SpendingItemHelper.ValidCategory(spendingItem.Category) || spendingItem.Heading == null || spendingItem.Cost == 0 || spendingItem.Currency == null) { return(UnprocessableEntity()); } _context.Entry(spendingItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpendingItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task TestAddNewItem() { using (var context = new SpendingTrackContext(options)) { SpendingItem TestItem = new SpendingItem() { Heading = "Lunch at Yumekaze", Category = "food", Cost = 65.4, TripID = 3, Currency = "JPY", CreatedAt = "", ReceiptID = "", Note = "Very good sushi", }; SpendingController spendingController = new SpendingController(context, configuration); IActionResult result = await spendingController.PostSpendingItem(TestItem) as IActionResult; Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); var checkQuery = (from m in context.SpendingItem where m.Heading == TestItem.Heading && m.Category == TestItem.Category && m.Cost == TestItem.Cost && m.TripID == TestItem.TripID && m.Currency == TestItem.Currency && m.CreatedAt == TestItem.CreatedAt && m.ReceiptID == TestItem.ReceiptID && m.Note == TestItem.Note select m); SpendingItem fromMockDB = context.SpendingItem.Where(x => (x.Heading == TestItem.Heading) && (x.Category == TestItem.Category) && (x.Cost == TestItem.Cost) && (x.TripID == TestItem.TripID) && (x.Currency == TestItem.Currency) && (x.CreatedAt == TestItem.CreatedAt) && (x.ReceiptID == TestItem.ReceiptID && (x.Note == TestItem.Note))).Single(); } }