public void Delete_User_Failed()
        {
            try
            {
                SqliteConnection.Open();

                Exception ex = null;

                ex = Record.Exception(() =>
                {
                    using (IRepository <Models.User> repository = GetRepository <Models.User>())
                    {
                        //Arrange
                        Models.User user = new Models.User()
                        {
                            Id       = Guid.NewGuid(),
                            Name     = "Test Fail",
                            Password = "******".Cript(),
                            Email    = "*****@*****.**"
                        };

                        //Try delete some unexisting user
                        repository.Remove(user);
                    }
                });

                //Assert
                Assert.True(ex != null);
                Assert.True(ex is DbUpdateConcurrencyException);
            }
            finally
            {
                SqliteConnection.Close();
            }
        }
        /// <summary>
        /// Method to create an unique User and insert on database
        /// </summary>
        /// <param name="repository">IRepository<User> with SQLite Connection</param>
        private void InsertUniqueUser(IRepository <Models.User> repository)
        {
            //Arrange
            Models.User user = new Models.User()
            {
                Email    = "*****@*****.**",
                Password = "******".Cript(),
                Name     = "Test User",
                Id       = Guid.NewGuid()
            };

            //Insert user on database
            repository.Save(user);
        }
Exemplo n.º 3
0
        public IActionResult Save([FromBody] DTO.User user)
        {
            try
            {
                Models.User result = _repository.Save(new Models.User(user));

                return(Ok(new DTO.User(result)));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex));
            }
            catch (Exception ex)
            {
                return(new UnprocessableEntityObjectResult(ex.Message));
            }
        }
        public async void Update_User_Ok()
        {
            try
            {
                SqliteConnection.Open();

                //After repository was disposed, the changes are commited
                using (IRepository <Models.User> repository = GetRepository <Models.User>())
                {
                    InsertUniqueUser(repository);
                }

                //Using another instance of repository to make sure previous data was saved
                using (IRepository <Models.User> repository = GetRepository <Models.User>())
                {
                    IEnumerable <Models.User> users = await repository.SearchAsync(u => u.Name.Equals("Test User"));

                    Models.User user = users.FirstOrDefault();

                    user.Email = "*****@*****.**";

                    repository.Update(user);
                }

                //Using another instance of repository to check if it's trully updated
                using (IRepository <Models.User> repository = GetRepository <Models.User>())
                {
                    IEnumerable <Models.User> users = await repository.SearchAsync(u => u.Email.Equals("*****@*****.**"));

                    //Assert
                    Assert.True(users.Count() == 1);
                }
            }
            finally
            {
                SqliteConnection.Close();
            }
        }
        public async void Delete_User_Ok()
        {
            try
            {
                SqliteConnection.Open();

                //After repository was disposed, the changes are commited
                using (IRepository <Models.User> repository = GetRepository <Models.User>())
                {
                    InsertUniqueUser(repository);
                }

                //Using another instance of repository to make sure previous data was saved
                using (IRepository <Models.User> repository = GetRepository <Models.User>())
                {
                    IEnumerable <Models.User> users = await repository.SearchAsync(u => u.Email.Equals("*****@*****.**"));

                    Models.User user = users.FirstOrDefault();

                    repository.Remove(user);
                }

                //Using another instance of repository to make sure previous data was removed
                using (IRepository <Models.User> repository = GetRepository <Models.User>())
                {
                    IEnumerable <Models.User> users = await repository.GetAllAsync();

                    //Assert
                    Assert.True(users.Count() == 0);
                }
            }
            finally
            {
                SqliteConnection.Close();
            }
        }