Пример #1
0
        public void CreateAndGetByIdTest()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateAndGetByIdTest))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var expenseService = new ExpenseService(context);
                var addedExpense   = expenseService.Create(new Lab2Expense.ViewModels.ExpensePostModel
                {
                    Description = "jshdkhsakjd",
                    Sum         = 1.23,
                    Location    = "jsfkdsf",
                    Date        = new DateTime(),
                    Currency    = "euro",
                    ExpenseType = "food",

                    Comments = new List <Comment>()
                    {
                        new Comment
                        {
                            Important = true,
                            Text      = "asd",
                            Owner     = null
                        }
                    },
                }, null);

                var expenseCreated = expenseService.GetById(addedExpense.Id);
                Assert.NotNull(expenseCreated);
                Assert.AreEqual(addedExpense, expenseCreated);
            }
        }
Пример #2
0
        public void InvalidTypeOfExpenseShouldNotCreatANewExpense()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(InvalidTypeOfExpenseShouldNotCreatANewExpense))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var expenseService = new ExpenseService(context);

                var expected = expenseService.Create(new Labo2.ViewModels.ExpensePostModel
                {
                    Description = "Expense for test",
                    Sum         = 34,
                    Location    = "Cluj",
                    Date        = DateTime.ParseExact("07/10/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture),
                    Currency    = "RON",
                    Type        = "Invalid Type",
                    Comments    = null
                }, null);

                var actual = expenseService.GetById(expected.Id);

                Assert.IsNull(actual);
            }
        }
Пример #3
0
        public void ValidExpenseShouldCreateANewExpenseAndGetById()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidExpenseShouldCreateANewExpenseAndGetById))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var expenseService = new ExpenseService(context);

                var expected = expenseService.Create(new Labo2.ViewModels.ExpensePostModel
                {
                    Description = "Expense for test",
                    Sum         = 34,
                    Location    = "Cluj",
                    Date        = DateTime.ParseExact("07/10/2019", "MM/dd/yyyy", CultureInfo.InvariantCulture),
                    Currency    = "RON",
                    Type        = "Other",
                    Comments    = new List <Comment>()
                    {
                        new Comment()
                        {
                            Text      = "Comment test for expense",
                            Important = false,
                            AddedBy   = null
                        }
                    }
                }, null);

                var actual = expenseService.GetById(expected.Id);

                Assert.IsNotNull(actual);
                Assert.AreEqual(expected, actual);
            }
        }
Пример #4
0
        public ActionResult GetById(long id)
        {
            try
            {
                //Pegando o id do usuario pelo token
                long?idUser = GetIdUser();

                if (idUser is null)
                {
                    return(NotFound("Usuário não encontrado!"));
                }

                var response = _expenseService.GetById(Convert.ToInt64(idUser), id);

                return(SetResponse(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Пример #5
0
        public void GetByIdShouldReturnExpenseWithCorrectId()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnExpenseWithCorrectId))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var expenseService = new ExpenseService(context);
                var added          = new ExpensePostModel()

                {
                    Description = "Variable",
                    Type        = "5",
                    Location    = "Sibiu",
                    Date        = Convert.ToDateTime("2019-05-05T11:11:11"),
                    Currency    = "USD",
                    Sum         = 555.77,
                    Comments    = new List <Comment>()
                    {
                        new Comment
                        {
                            Important = true,
                            Text      = "Very important expense",
                            Owner     = null
                        }
                    },
                };

                var current  = expenseService.Create(added, null);
                var expected = expenseService.GetById(current.Id);

                Assert.IsNotNull(expected);
                Assert.AreEqual(expected.Description, current.Description);
                Assert.AreEqual(expected.Location, current.Location);
                Assert.AreEqual(expected.Sum, current.Sum);
                Assert.AreEqual(expected.Id, current.Id);
            }
        }
Пример #6
0
        public void DeleteExistingExpense()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(DeleteExistingExpense))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var  expenseService = new ExpenseService(context);
                User user           = new User
                {
                    Username = "******"
                };
                var result = new Lab2.DTOs.PostExpenseDto

                {
                    //Added = DateTime.Now,
                    // Deadline = DateTime.Now.AddDays(15),
                    // ClosedAt = null,

                    Description = "Read1",
                    Sum         = 3,
                    Location    = "Read1",
                    Date        = DateTime.Now.AddDays(15),
                    Currency    = "ADF",
                    Type        = "FOOD",
                    // ClosedAt = null,
                    // Importance = "Medium",
                    // State = "InProgress",
                    Comments = null
                };
                Expense saveexpense = expenseService.Create(result, user);
                expenseService.Delete(saveexpense.Id);

                Assert.IsNull(expenseService.GetById(saveexpense.Id));
            }
        }