Exemplo n.º 1
0
        public void Upsert()
        {
            var options = new DbContextOptionsBuilder <UsersDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(Upsert))
                          .Options;

            using (var context = new UsersDbContext(options))
            {
                var usersService = new UserService(context, config);

                var defaultuserRole = new UserRole
                {
                    Name = RoleConstants.REGULAR
                };
                context.UserRoles.Add(defaultuserRole);
                context.Entry(defaultuserRole).State = EntityState.Detached;

                var defaultuserRole2 = new UserRole
                {
                    Name = RoleConstants.ADMIN
                };
                context.UserRoles.Add(defaultuserRole2);
                context.Entry(defaultuserRole2).State = EntityState.Detached;

                UserRole userRoleRegular = new UserRole
                {
                    Name = RoleConstants.REGULAR
                };
                HistoryUserRole historyRegular = new HistoryUserRole
                {
                    UserRole = userRoleRegular,
                };
                List <HistoryUserRole> listRegular = new List <HistoryUserRole>
                {
                    historyRegular
                };
                User radu69 = new User
                {
                    Username        = "******",
                    HistoryUserRole = listRegular,
                    DateRegister    = DateTime.Now,
                    Password        = "******",
                    Email           = "*****@*****.**",
                    FirstName       = "radu",
                    LastName        = "radu"
                };
                context.Users.Add(radu69);
                context.SaveChanges();
            }

            using (var context = new UsersDbContext(options))
            {
                var usersService = new UserService(context, config);

                var defaultuserRole = new UserRole
                {
                    Name = RoleConstants.ADMIN
                };
                context.UserRoles.Add(defaultuserRole);
                context.SaveChanges();
                context.Entry(defaultuserRole).State = EntityState.Detached;

                UserRole userRole = new UserRole
                {
                    Name = RoleConstants.ADMIN
                };
                HistoryUserRole history = new HistoryUserRole
                {
                    UserRole = userRole,
                };
                List <HistoryUserRole> list = new List <HistoryUserRole>
                {
                    history
                };
                User user2 = new User
                {
                    Username        = "******",
                    HistoryUserRole = list,
                    DateRegister    = DateTime.Now,
                    Password        = "******",
                    Email           = "*****@*****.**",
                    FirstName       = "cristina",
                    LastName        = "cristina"
                };

                User firstAdded = new User
                {
                    Username     = "******",
                    DateRegister = DateTime.Now,
                    Password     = "******",
                    Email        = "*****@*****.**",
                    FirstName    = "radu",
                    LastName     = "radu"
                };

                var toUpdateWith = new UserPostModel
                {
                    FirstName = "Gimli",
                    LastName  = "Axeman",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                    UserRole  = RoleConstants.ADMIN
                };

                var result = context.ChangeTracker.Entries()
                             .Where(t => t.State == EntityState.Unchanged);

                UserGetModel lalala = usersService.Upsert(1, toUpdateWith, user2);

                Assert.AreNotEqual(lalala.Username, firstAdded.Username);
                Assert.AreNotEqual(lalala.Email, firstAdded.Email);
                Assert.AreNotEqual(lalala.UserRole, RoleConstants.ADMIN);
            }
        }