示例#1
0
 public GetProductByNameQueryHandler(
     TemplateDbContext dbContext,
     IObjectAssembler objectAssembler)
 {
     _dbContext       = dbContext;
     _objectAssembler = objectAssembler;
 }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Insert"/> class.
        /// Seeds the database.
        /// </summary>
        public Insert()
        {
            // Create a service provider to be shared by all test methods
            var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

            // Create options telling the context to use an
            // InMemory database and the service provider.
            var builder = new DbContextOptionsBuilder <TemplateDbContext>();

            builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider);
            this.contextOptions = builder.Options;

            // seed in constructor.
            using (var context = new TemplateDbContext(this.contextOptions))
            {
                var roleStore = new RoleStore <IdentityRole>(context);
                if (!context.Roles.Any(r => r.Name == "Administrator"))
                {
                    roleStore.CreateAsync(new IdentityRole {
                        Name = "Administrator", NormalizedName = "Administrator"
                    });
                }

                context.SaveChangesAsync();
            }
        }
示例#3
0
 public void Dispose()
 {
     if (DbContext != null)
     {
         DbContext = null;
     }
 }
示例#4
0
        public static TemplateDbContext Create()
        {
            var options = new DbContextOptionsBuilder <TemplateDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var context = new TemplateDbContext(options);

            context.Database.EnsureCreated();

            context.TemplateEntities.AddRange(new[] {
                new TemplateEntity {
                    Id = 1, Name = "Test1"
                },
                new TemplateEntity {
                    Id = 2, Name = "Test2"
                },
                new TemplateEntity {
                    Id = 3, Name = "Test3"
                },
            });

            context.SaveChanges();

            return(context);
        }
示例#5
0
        public void InsertOk()
        {
            // setup
            string            password = "******";
            TemplateDbContext context  = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            var user = new ApplicationUser
            {
                Name               = "User for test purposes",
                UserName           = "******",
                NormalizedUserName = "******",
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**",
                EmailConfirmed     = true,
                LockoutEnabled     = false,
                SecurityStamp      = Guid.NewGuid().ToString()
            };

            var hasher = new PasswordHasher <ApplicationUser>();
            var hashed = hasher.HashPassword(user, password);

            user.PasswordHash = hashed;

            // act
            int beforeInsert = unitOfWork.UserRepository.GetAll().ToList().Count;

            unitOfWork.UserRepository.Insert(user);
            unitOfWork.Commit();
            int afterInsert = unitOfWork.UserRepository.GetAll().ToList().Count;

            // assert
            Assert.True(afterInsert > beforeInsert);
        }
示例#6
0
        public void ConstructorOk()
        {
            // action
            var dbContext = new TemplateDbContext();

            // assert
            Assert.IsType(typeof(TemplateDbContext), dbContext);
        }
示例#7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GetRolesByUserId"/> class.
        /// Seeds the database.
        /// </summary>
        public GetRolesByUserId()
        {
            // Create a service provider to be shared by all test methods
            var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

            // Create options telling the context to use an
            // InMemory database and the service provider.
            var builder = new DbContextOptionsBuilder <TemplateDbContext>();

            builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider);
            this.contextOptions = builder.Options;

            // seed in constructor.
            using (var context = new TemplateDbContext(this.contextOptions))
            {
                string password  = "******";
                var    roleStore = new RoleStore <IdentityRole>(context);

                var user = new ApplicationUser
                {
                    Name               = "User for test purposes",
                    UserName           = "******",
                    NormalizedUserName = "******",
                    Email              = "*****@*****.**",
                    NormalizedEmail    = "*****@*****.**",
                    EmailConfirmed     = true,
                    LockoutEnabled     = false,
                    SecurityStamp      = Guid.NewGuid().ToString()
                };

                var role = new IdentityRole {
                    Name = "User", NormalizedName = "User"
                };
                roleStore.CreateAsync(role);

                if (!context.Users.Any(u => u.UserName == user.UserName))
                {
                    var hasher = new PasswordHasher <ApplicationUser>();
                    var hashed = hasher.HashPassword(user, password);
                    user.PasswordHash = hashed;
                    var userStore = new UserStore <ApplicationUser>(context);
                    userStore.CreateAsync(user);
                }

                context.SaveChangesAsync();

                // after the user and roles are inserted...
                // this has to work..
                context.UserRoles.Add(new IdentityUserRole <string> {
                    RoleId = role.Id, UserId = user.Id
                });

                // save again.
                context.SaveChanges();

                this.userId = user.Id;
            }
        }
        public void InitializeOk()
        {
            // setup
            this.context = new TemplateDbContext(this.contextOptions);
            var seeder = new TemplateDbContextSeedData(context);

            // action
            seeder.Initialize();
        }
示例#9
0
        public IActionResult Index([FromServices] TemplateDbContext templateDbContext)
        {
            _logger.LogDebug("这是一个Debug日志");
            _logger.LogWarning("这是一个警告日志");
            _logger.LogInformation("这是一个信息日志");
            _logger.LogError("这是一个错误日志");
            templateDbContext.UserPermissions.ToList();

            return(View());
        }
示例#10
0
        public void DeleteRoleByNameThrowsExceptionDueRoleNotFound()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.roleService = new RoleService(unitOfWork);

            // action & assert
            Assert.Throws <ArgumentNullException>(() => this.roleService.DeleteRoleByName("Administrator"));
        }
示例#11
0
        public static void SeedHostDb(TemplateDbContext context)
        {
            context.SuppressAutoSetTenantId = true;

            // Host seed
            new InitialHostDbBuilder(context).Create();

            // Default tenant seed (in host database).
            new DefaultTenantBuilder(context).Create();
            new TenantRoleAndUserBuilder(context, 1).Create();
        }
示例#12
0
        public void UpdateUserInfoThrowsException()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // action && assert
            Assert.Throws <InvalidOperationException>(() => this.userService.UpdateUserInfo(Guid.NewGuid().ToString(), "Test"));
        }
示例#13
0
        public void DbProviderAssignedAccurately(string providerString, DbProvider expectedProvider)
        {
            var context = new TemplateDbContext()
            {
                Provider = providerString
            };

            var expectedProviderString = Enum.GetName(typeof(DbProvider), expectedProvider);

            context.Provider.Should().Be(expectedProviderString);
        }
示例#14
0
        public void InsertThrowsExceptionDueParameter()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            var repository            = new Repository <ApplicationUser>(context);

            // action
            var exception = Assert.Throws <ArgumentNullException>(() => repository.Insert(null));

            Assert.True(exception.Message.Equals("Value cannot be null.\r\nParameter name: entity"));
        }
示例#15
0
        public void ConstructorOk()
        {
            // setup
            this.context = new TemplateDbContext(contextOptions);

            // action
            var result = new TemplateDbContextSeedData(this.context);

            // assert
            Assert.IsType(typeof(TemplateDbContextSeedData), result);
        }
示例#16
0
        public void GetUserByIdThrowsExceptionDueUserNotFound()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // act && assert
            Assert.Throws <InvalidOperationException>(() => this.userService.GetUserById(Guid.NewGuid().ToString()));
        }
示例#17
0
        public void DisposeOk()
        {
            // setup
            this.context        = new TemplateDbContext(this.contextOptions);
            this.userRepository = new Repository <ApplicationUser>(this.context);

            // act
            var user = this.userRepository.FindBy(x => x.Id.Equals(this.userId, StringComparison.CurrentCultureIgnoreCase));

            // assert
            Assert.IsType(typeof(ApplicationUser), user);
        }
示例#18
0
        public void RoleServiceOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(CreateNewContextOptions());
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            // action
            this.roleService = new RoleService(unitOfWork);

            // assert
            Assert.IsType(typeof(RoleService), this.roleService);
        }
示例#19
0
        public void FindByOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);

            this.unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            // act
            var user = this.unitOfWork.UserRepository.FindBy(x => x.Id.Equals(this.userId, StringComparison.CurrentCultureIgnoreCase));

            // assert
            Assert.IsType(typeof(ApplicationUser), user);
        }
示例#20
0
        public void DeleteRoleByNameThrowsExceptionDueParameter()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.roleService = new RoleService(unitOfWork);

            // action & assert
            var exception = Assert.Throws <ArgumentNullException>(() => this.roleService.DeleteRoleByName(string.Empty));

            Assert.True(exception.Message.Equals("Value cannot be null.\r\nParameter name: name"));
        }
示例#21
0
        internal static TemplateDbContext CreateInMemoryContext(string name = "default")
        {
            var optionsBuilder = new DbContextOptionsBuilder <TemplateDbContext>();

            optionsBuilder.UseInMemoryDatabase(name);
            var context = new TemplateDbContext(optionsBuilder.Options);

            context.NameAddresses.AddRange(CreateNameAddresseses());

            context.SaveChanges();

            return(context);
        }
示例#22
0
        public void ConstructorOk()
        {
            // setup
            var options = CreateNewContextOptions();
            var context = new TemplateDbContext(options);
            IUnitOfWork <TemplateDbContext> unitOFWork = new UnitOfWork <TemplateDbContext>(context);

            // action
            this.userService = new UserService(unitOFWork);

            // assert
            Assert.IsType(typeof(UserService), this.userService);
        }
        public void CanInsertUserNameFalse()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // action
            var result = this.userService.CanInsertUserName("test");

            // assert
            Assert.False(result);
        }
示例#24
0
        public void FindManyByOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);

            this.unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            // act
            var user = this.unitOfWork.UserRepository.FindManyBy(x => x.Name.Contains("test")).ToList();

            // assert
            Assert.IsType(typeof(List <ApplicationUser>), user);
            Assert.True(user.Count == 2);
        }
示例#25
0
        public void GetUserByIdOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // act
            var result = this.userService.GetUserById(this.userId);

            // assert
            Assert.IsType(typeof(ApplicationUser), result);
        }
示例#26
0
        public void GetAllRoleNamesOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.roleService = new RoleService(unitOfWork);

            // action
            var result = this.roleService.GetAllRoleNames();

            // assert
            Assert.True(result.ToList().Count == 3);
        }
示例#27
0
        public void GetAllOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);

            this.unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            // act
            var user = this.unitOfWork.UserRepository.GetAll().ToList();

            // assert
            Assert.IsType(typeof(List <ApplicationUser>), user);
            Assert.True(user.Count == 2);
        }
示例#28
0
        public void ExistTrue()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // action
            var result = this.userService.Exist(this.userId);

            // assert
            Assert.True(result);
        }
示例#29
0
        public void ExistFalse()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // action
            var result = this.userService.Exist(Guid.NewGuid().ToString());

            // assert
            Assert.False(result);
        }
示例#30
0
        public void GetAllOk()
        {
            // setup
            TemplateDbContext context = new TemplateDbContext(this.contextOptions);
            IUnitOfWork <TemplateDbContext> unitOfWork = new UnitOfWork <TemplateDbContext>(context);

            this.userService = new UserService(unitOfWork);

            // action
            var result = this.userService.GetAll();

            // assert
            Assert.IsType(typeof(List <AspNetUser>), result);
        }