public GetProductByNameQueryHandler( TemplateDbContext dbContext, IObjectAssembler objectAssembler) { _dbContext = dbContext; _objectAssembler = objectAssembler; }
/// <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(); } }
public void Dispose() { if (DbContext != null) { DbContext = null; } }
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); }
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); }
public void ConstructorOk() { // action var dbContext = new TemplateDbContext(); // assert Assert.IsType(typeof(TemplateDbContext), dbContext); }
/// <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(); }
public IActionResult Index([FromServices] TemplateDbContext templateDbContext) { _logger.LogDebug("这是一个Debug日志"); _logger.LogWarning("这是一个警告日志"); _logger.LogInformation("这是一个信息日志"); _logger.LogError("这是一个错误日志"); templateDbContext.UserPermissions.ToList(); return(View()); }
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")); }
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(); }
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")); }
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); }
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")); }
public void ConstructorOk() { // setup this.context = new TemplateDbContext(contextOptions); // action var result = new TemplateDbContextSeedData(this.context); // assert Assert.IsType(typeof(TemplateDbContextSeedData), result); }
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())); }
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); }
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); }
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); }
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")); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }