Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public async Task TestReadingAlreadyExists()
        {
            using (var context = new SpendingTrackContext(options))
            {
                SpendingController         controller = new SpendingController(context, configuration);
                IEnumerable <SpendingItem> result     = controller.GetSpendingItem();

                Assert.IsNotNull(result);
                IList <SpendingItem> resultList = result.ToList();

                //Assumes same order
                Assert.IsTrue(resultList[0].Heading == headingList[0]);
                Assert.IsTrue(resultList[0].Currency == currencyList[0]);
                Assert.IsTrue(resultList[0].Cost == costList[0]);
                Assert.IsTrue(resultList[0].CreatedAt == createdAtList[0]);
                Assert.IsTrue(resultList[0].Category == categoryList[0]);
                Assert.IsTrue(resultList[0].ReceiptID == receiptIDList[0]);
                Assert.IsTrue(resultList[0].TripID == tripIDList[0]);

                Assert.IsTrue(resultList[1].Heading == headingList[1]);
                Assert.IsTrue(resultList[1].Currency == currencyList[1]);
                Assert.IsTrue(resultList[1].Cost == costList[1]);
                Assert.IsTrue(resultList[1].CreatedAt == createdAtList[1]);
                Assert.IsTrue(resultList[1].Category == categoryList[1]);
                Assert.IsTrue(resultList[1].ReceiptID == receiptIDList[1]);
                Assert.IsTrue(resultList[1].TripID == tripIDList[1]);
            }
        }
Ejemplo n.º 3
0
        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));
            }
        }
Ejemplo n.º 4
0
        public async Task TestGetAllHeadings()
        {
            using (var context = new SpendingTrackContext(options))
            {
                SpendingController controller = new SpendingController(context, configuration);
                List <string>      get_result = await controller.GetAllHeadings();

                Assert.IsTrue(get_result.All(headingList.Contains));
            }
        }
Ejemplo n.º 5
0
        public void AddSpendingMissingUser_Fails()
        {
            int missingUserId = 42;

            // Arrange
            using (SpendingContext spendingContext = new SpendingContext())
            {
                SpendingRepository spendingRepository = new SpendingRepository(spendingContext);
                SpendingController sut = new SpendingController(spendingRepository);

                // Act
                BadRequestResult actionResult = sut.AddSpending(missingUserId, DateTime.UtcNow.AddDays(-1), 100, "USD", "Misc", "Armor") as BadRequestResult;

                // Assert
                Assert.Equal(400, actionResult.StatusCode);
            }
        }
Ejemplo n.º 6
0
        public void ListOrderedByUserId()
        {
            // Arrange
            using (SpendingContext spendingContext = new SpendingContext())
            {
                SpendingRepository spendingRepository = new SpendingRepository(spendingContext);
                SpendingController sut = new SpendingController(spendingRepository);

                // Act
                ObjectResult actionResult = sut.ListByUserId(1, "Date") as ObjectResult;


                // Assert
                IEnumerable <SpendingDto> spendings = (IEnumerable <SpendingDto>)actionResult.Value;
                Assert.Empty(spendings);
            }
        }
Ejemplo n.º 7
0
        public void ListOrderedWithWrongOrderBy_Fails()
        {
            string wrongOrderBy = "loki";

            // Arrange
            using (SpendingContext spendingContext = new SpendingContext())
            {
                SpendingRepository spendingRepository = new SpendingRepository(spendingContext);
                SpendingController sut = new SpendingController(spendingRepository);

                // Act
                BadRequestResult actionResult = sut.ListByUserId(1, wrongOrderBy) as BadRequestResult;

                // Assert
                Assert.Equal(400, actionResult.StatusCode);
            }
        }
Ejemplo n.º 8
0
        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));
            }
        }
Ejemplo n.º 9
0
        public void AddSpendingAndListIt()
        {
            // Arrange
            using (SpendingContext spendingContext = new SpendingContext())
            {
                SpendingRepository spendingRepository = new SpendingRepository(spendingContext);
                SpendingController sut = new SpendingController(spendingRepository);

                // Act
                IStatusCodeActionResult actionResult = sut.AddSpending(1, DateTime.UtcNow.AddDays(-1), 100, "USD", "Misc", "Armor") as IStatusCodeActionResult;

                // Assert
                Assert.Equal(200, actionResult.StatusCode);
                ObjectResult listResult             = sut.ListByUserId(1, "Date") as ObjectResult;
                IEnumerable <SpendingDto> spendings = (IEnumerable <SpendingDto>)listResult.Value;
                Assert.Single(spendings);
            }
        }
Ejemplo n.º 10
0
        public async Task TestDelete()
        {
            using (var context = new SpendingTrackContext(options))
            {
                SpendingController         controller = new SpendingController(context, configuration);
                IEnumerable <SpendingItem> get_result = controller.GetSpendingItem();
                Assert.IsNotNull(get_result);
                IList <SpendingItem> resultList = get_result.ToList();

                IActionResult del_result = await controller.DeleteSpendingItem(resultList[0].ID) as IActionResult;

                Assert.IsInstanceOfType(del_result, typeof(OkObjectResult));

                IActionResult result_after = await controller.GetSpendingItem(resultList[0].ID) as IActionResult;

                Assert.IsInstanceOfType(result_after, typeof(NotFoundResult));
            }
        }
Ejemplo n.º 11
0
        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();
            }
        }
Ejemplo n.º 12
0
        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();
            }
        }
Ejemplo n.º 13
0
 public AddSpending()
 {
     control = new SpendingController();
     InitializeComponent();
 }