/// <summary> /// CreateContextForSqlite method. /// </summary> /// <param name="context">context.</param> public void SeedContextForSqlite(SiteAuthDbContext context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.Seed(); context.SaveChanges(); }
public async void Should_Be_Delete_Product_Returns_Success() { using var siteAuthDbContextFactory = new SiteAuthDbContextFactory(); using (var context = new SiteAuthDbContext(siteAuthDbContextFactory.Options)) { siteAuthDbContextFactory.SeedContextForSqlite(context); } using (var context = new SiteAuthDbContext(siteAuthDbContextFactory.Options)) { // Arrange int productId = 1; var logger = new Mock <ILogger <ProductsCommands> >(); var productsCommands = new ProductsCommands(context, logger.Object, Mapper); // Act var result = await productsCommands.DeleteAsync(productId); // Assert Assert.True(result); var current = Mapper.Map <ProductModel>(context.Products.FirstOrDefault(x => x.Id == productId)); Assert.Null(current); } }
public async void Should_Be_Update_Product_Returns_Object() { using var siteAuthDbContextFactory = new SiteAuthDbContextFactory(); using (var context = new SiteAuthDbContext(siteAuthDbContextFactory.Options)) { siteAuthDbContextFactory.SeedContextForSqlite(context); } using (var context = new SiteAuthDbContext(siteAuthDbContextFactory.Options)) { // Arrange var productModel = new ProductModel() { IsDeleted = false, Description = "Product 121212", ImageUrl = "http://localhost:3333/images/product121212.png", Price = 1200 }; int productId = 1; var logger = new Mock <ILogger <ProductsCommands> >(); var productsCommands = new ProductsCommands(context, logger.Object, Mapper); // Act var result = await productsCommands.UpdateAsync(productId, productModel); // Assert Assert.NotNull(result); Assert.True(result.Description == productModel.Description); } }
public async void Should_Be_Get_One_Product_Returns_Product() { using var siteAuthDbContextFactory = new SiteAuthDbContextFactory(); using (var context = new SiteAuthDbContext(siteAuthDbContextFactory.Options)) { siteAuthDbContextFactory.SeedContextForSqlite(context); } using (var context = new SiteAuthDbContext(siteAuthDbContextFactory.Options)) { // Arrange int productId = 1; var logger = new Mock <ILogger <ProductsQueries> >(); var productsQueries = new ProductsQueries(context, logger.Object, Mapper); // Act var result = await productsQueries.GetOneAsync(productId); // Assert Assert.NotNull(result); Assert.True(result.Id == productId); } }
/// <summary> /// Creates a dbContext for testing. /// </summary> /// <param name="useSqlLite">Use SqlLite. If false, uses memory provider.</param> /// <returns>dbContext.</returns> public ISiteAuthDbContext GetEntityDbContext(bool useSqlLite = false) { var builder = new DbContextOptionsBuilder <SiteAuthDbContext>(); if (useSqlLite) { builder.UseSqlite(inMemorySqlite, x => { }); } else { builder.UseInMemoryDatabase(Guid.NewGuid().ToString()); } builder.EnableSensitiveDataLogging(true); var dbContext = new SiteAuthDbContext(builder.Options); if (useSqlLite) { // SQLite needs to open connection to the DB. // Not required for in-memory-database. dbContext.Database.OpenConnection(); } dbContext.Database.EnsureCreated(); return(dbContext); }
/// <summary> /// Seed method. /// </summary> /// <param name="context">context.</param> public static void Seed(this SiteAuthDbContext context) { var products = SeedProducts(); context.Products.AddRange(products); context.SaveChanges(); }