public async void GetAllByUserId_ShouldReturnAllUsersAddressesByUserId() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "GetAllByUserId_ShouldReturnAllUsersAddressesByUserId") .Options; TechAndToolsDbContext context = new TechAndToolsDbContext(options); await SeedData(context); IUserService userService = new UserService(context); IAddressService addressService = new AddressService(context, userService); TechAndToolsUser testUser = new TechAndToolsUser { Id = "testId", UserName = "******", Email = "testEmail" }; await context.AddAsync(testUser); await context.SaveChangesAsync(); int expectedResult = context.Addresses.Where(x => x.TechAndToolsUserId == "testId").ToList().Count; int actualResult = addressService.GetAllByUserId("testId").ToList().Count; Assert.Equal(expectedResult, actualResult); }
private async Task SeedData(TechAndToolsDbContext context) { context.AddRange(this.GetProductsData()); context.AddRange(this.GetArticlesData()); context.AddRange(this.GetImagesData()); await context.SaveChangesAsync(); }
public async void CreateAsync_WithIncorrectUserShouldThrowException() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "CreateAsync_WithIncorrectUserShouldThrowException") .Options; TechAndToolsDbContext context = new TechAndToolsDbContext(options); IUserService userService = new UserService(context); IAddressService addressService = new AddressService(context, userService); TechAndToolsUser testUser = new TechAndToolsUser { UserName = "******", Email = "testEmail" }; await context.AddAsync(testUser); await context.SaveChangesAsync(); AddressServiceModel serviceModel = new AddressServiceModel { Id = 1, City = "CityTest1", CityAddress = "CityAddressTest1", PostCode = 9000, TechAndToolsUserId = Guid.NewGuid().ToString() }; await Assert.ThrowsAsync <ArgumentNullException>(() => addressService.CreateAsync(serviceModel, "wrongUsername")); }
public async Task SeedAsync(TechAndToolsDbContext dbContext, IServiceProvider serviceProvider) { if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(TechAndToolsDbContext)); var seeders = new List <ISeeder> { new RolesSeeder(), new PaymentStatusesSeeder(), new PaymentTypesSeeder(), new OrderStatusesSeeder(), new SeedRootAdminUser() }; foreach (var seeder in seeders) { await seeder.SeedAsync(dbContext, serviceProvider); await dbContext.SaveChangesAsync(); logger.LogInformation($"Seeder {seeder.GetType().Name} done."); } }
private async Task SeedData(TechAndToolsDbContext context) { context.AddRange(this.GetProductsData()); context.Users.Add(this.GetTestUser()); context.FavoriteProducts.AddRange(this.GetFavoriteProductsData()); await context.SaveChangesAsync(); }
public async void CreateCategoryAsync_ShouldCreateCategory() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "CreateCategoryAsync_ShouldCreateCategory") .Options; var context = new TechAndToolsDbContext(options); context.Database.EnsureDeleted(); var mainCategory = new MainCategory { Id = 1, Name = "PC" }; await context.MainCategories.AddAsync(mainCategory); await context.SaveChangesAsync(); ICategoryService categoryService = new CategoryService(context); await categoryService.CreateCategoryAsync(new CategoryServiceModel { Name = "category1", MainCategoryId = 1 }); await categoryService.CreateCategoryAsync(new CategoryServiceModel { Name = "category2", MainCategoryId = 1 }); int expectedResult = 2; int actualResult = context.Categories.Count(); Assert.Equal(expectedResult, actualResult); }
private static async Task SeedPaymentStatusAsync(string paymentName, TechAndToolsDbContext dbContext) { var paymentStatus = new PaymentStatus { Name = paymentName }; await dbContext.PaymentStatuses.AddAsync(paymentStatus); await dbContext.SaveChangesAsync(); }
public async Task SeedAsync(TechAndToolsDbContext dbContext, IServiceProvider serviceProvider) { bool hasPaymentTypes = await dbContext.PaymentMethods.AnyAsync(); if (!hasPaymentTypes) { await SeedPaymentTypesAsync(GlobalConstants.CashOnDeliver, dbContext); await dbContext.SaveChangesAsync(); } }
public async Task SeedAsync(TechAndToolsDbContext dbContext, IServiceProvider serviceProvider) { bool hasOrderStatuses = await dbContext.OrderStatuses.AnyAsync(); if (!hasOrderStatuses) { await SeedOrderStatusAsync("Необработена", dbContext); await SeedOrderStatusAsync("Обработена", dbContext); await SeedOrderStatusAsync("Доставена", dbContext); await dbContext.SaveChangesAsync(); } }
public async void CreateAsync_ShouldCreateAndAddAddressToDatabase() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "CreateArticleAsync_ShouldCreateAndAddArticlesToDatabase") .Options; TechAndToolsDbContext context = new TechAndToolsDbContext(options); IUserService userService = new UserService(context); IAddressService addressService = new AddressService(context, userService); TechAndToolsUser testUser = new TechAndToolsUser { UserName = "******", Email = "testEmail" }; await context.AddAsync(testUser); await context.SaveChangesAsync(); await addressService.CreateAsync( new AddressServiceModel { Id = 1, City = "CityTest1", CityAddress = "CityAddressTest1", PostCode = 9000, TechAndToolsUserId = Guid.NewGuid().ToString() }, "testUsername"); await addressService.CreateAsync( new AddressServiceModel { Id = 2, City = "CityTest2", CityAddress = "CityAddressTest2", PostCode = 9000, TechAndToolsUserId = Guid.NewGuid().ToString() }, "testUsername"); int expectedCount = 2; int actualCount = context.Addresses.ToList().Count; Assert.Equal(expectedCount, actualCount); }
private async Task SeedData(TechAndToolsDbContext context) { context.AddRange(GetBrandsData()); await context.SaveChangesAsync(); }
private async Task SeedData(TechAndToolsDbContext context) { context.AddRange(GetCategoriesData()); context.AddRange(GetMainCategoriesData()); await context.SaveChangesAsync(); }