public async Task Update_Api_Resource_Server_validator(UpdateApiResourceDto model, bool isvalid) { using (var ctx = await TestDbContext.Create(nameof(Update_Api_Resource_Server_validator))) { ApiResource entity = new ApiResource { Address = "z", HttpMethod = HttpMethods.GET, Title = "z" }; ApiResource entity2 = new ApiResource { Address = "zx", HttpMethod = HttpMethods.GET, Title = "zx" }; entity.Id = Update_Api_Resource_validatorTestData.validGuid; entity2.Id = Update_Api_Resource_validatorTestData.validGuid2; ctx.ApiResources.Add(entity); ctx.ApiResources.Add(entity2); await ctx.SaveChangesAsync(); var vaidator = new ServerSideValidator(ctx); var validation = vaidator.Validate(new UpdateApiResource(model)); _output.WriteLine(string.Join(",\n", validation.Errors.Select(x => x.ErrorMessage))); Assert.Equal(isvalid, validation.IsValid); } }
public async Task Can_Update_Api_Resource() { using (var ctx = await TestDbContext.Create(nameof(Can_Update_Api_Resource))) { var entity = new ApiResource() { Address = "http://abc", HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET, Title = "Some Title" }; ctx.ApiResources.Add(entity); await ctx.SaveChangesAsync(); var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())); var model = new UpdateApiResourceDto( entity.Id, "http://Test", DotNetAccessControl.domain.enums.HttpMethods.POST, "Test Title" ); var request = new UpdateApiResource(model); var response = await new UpdateApiResourceHandler(ctx, mapper.CreateMapper()).Handle(request, CancellationToken.None); var toCheck = await ctx.ApiResources.AsNoTracking().SingleAsync(x => x.Id == entity.Id); Assert.Equal(model.Title, toCheck.Title); Assert.Equal(model.Address, toCheck.Address); Assert.Equal(model.HttpMethod, toCheck.HttpMethod); } }
public async Task Can_RoleAccess_To_Api() { using (var ctx = await TestDbContext.Create(nameof(Can_RoleAccess_To_Api))) { var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())); var api = new ApiResource { Address = "some1", HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET, Title = "some1" }; ctx.ApiResources.Add(api); await ctx.SaveChangesAsync(); var handler = new RoleAccessToApiHandler(new GenericRoleToResourceHandler(ctx, mapper.CreateMapper())); var model = new AssignRoleToResourceDto("Role1", api.Id, DotNetAccessControl.domain.enums.Permission.Allow); var result = await handler.Handle(new RoleAccessToApi(model), CancellationToken.None); Assert.True(result); var saved = await ctx.AccessControls.OfType <RoleAccessControl>().SingleAsync(x => x.ResourceId == api.Id); Assert.Equal(model.RoleName, saved.RoleName); } }
public IQueryableExtensionsTests(ITestOutputHelper output) { _output = output; using (var context = TestDbContext.Create()) { Database.SetInitializer(new MigrateDatabaseToLatestVersion <TestDbContext, TestDbConfiguration>()); context.Database.Initialize(true); } }
public void NoMapping_WithSqlite_ReturnsIncorrectPrecision() { using var dbContext = TestDbContext.Create(); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.Id, loadedEntity.Id); Assert.Equal(65536, GetSignAndScale(loadedEntity.Id)); }
public void CreateExecutionStrategy_WithoutRetryOnOptimisticConcurrencyFailure_ShouldReturnExpectedResult() { this.OptionsBuilder.ExecutionStrategyOptions = ExecutionStrategyOptions.None; var dbContext = TestDbContext.Create(); var result = this.Provider.CreateExecutionStrategy(dbContext); Assert.IsType(dbContext.Database.CreateExecutionStrategy().GetType(), result); }
public void StoreDecimalIdsWithCorrectPrecision_WithSqlite_DoesNotAffectNonIdDecimals() { using var dbContext = TestDbContext.Create((modelBuilder, dbContext) => modelBuilder.StoreDecimalIdsWithCorrectPrecision(dbContext)); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.Number, loadedEntity.Number); Assert.NotEqual(0, GetSignAndScale(loadedEntity.Number)); }
public void StoreWithDecimalIdPrecision_WithNonIdProperty_ReturnsExpectedPrecision() { using var dbContext = TestDbContext.Create((modelBuilder, dbContext) => modelBuilder.Entity <TestEntity>(entity => entity.Property(e => e.DoesNotHaveIdSuffix).StoreWithDecimalIdPrecision(dbContext))); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.DoesNotHaveIdSuffix, loadedEntity.DoesNotHaveIdSuffix); Assert.Equal(0, GetSignAndScale(loadedEntity.DoesNotHaveIdSuffix)); }
public void StoreDecimalIdsWithCorrectPrecision_WithSqlite_ReturnsExpectedPrecision() { using var dbContext = TestDbContext.Create((modelBuilder, dbContext) => modelBuilder.StoreDecimalIdsWithCorrectPrecision(dbContext)); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.Id, loadedEntity.Id); Assert.Equal(0, GetSignAndScale(loadedEntity.Id)); }
public async Task TestDBPagedListAsync() { using (var context = TestDbContext.Create()) { var paged = await context.DistributedLocks .OrderBy(l => l.Id) .ToPagedListAsync(page: 1, pageSize: 100); Assert.True(paged.CurrentPage == 1); Assert.True(paged.PageSize <= 100); } }
public async Task <int> InsertAsync(ICollection <Product> sources, CancellationToken cancelToken) { var result = 0; using (var dbContext = TestDbContext.Create()) { dbContext.Products.AddRange(sources); result = await dbContext.SaveChangesAsync(cancelToken); } return(result); }
public void StoreWithDecimalIdPrecision_WithInMemoryAndDifferentColumnType_ReturnsExpectedPrecision() { using var dbContext = TestDbContext.Create(useInMemoryInsteadOfSqlite: true, onModelCreating: (modelBuilder, dbContext) => modelBuilder.Entity <TestEntity>(entity => entity.Property(e => e.Id).StoreWithDecimalIdPrecision(dbContext, columnType: "DECIMAL(29,1)"))); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.Id, loadedEntity.Id); //Assert.Equal(1, GetSignAndScale(loadedEntity.Id)); // Unfortunately, the in-memory provider does not honor this, but at least we know that the flow did not throw //Assert.Equal("DECIMAL(29,1)", dbContext.Model.FindEntityType(typeof(TestEntity)).FindProperty(nameof(TestEntity.Id)).GetColumnType()); // Does not work with in-memory provider }
public void StoreWithDecimalIdPrecision_WithInMemoryAndExplicitColumnType_ReturnsExpectedPrecision() { using var dbContext = TestDbContext.Create(useInMemoryInsteadOfSqlite: true, onModelCreating: (modelBuilder, dbContext) => modelBuilder.Entity <TestEntity>(entity => entity.Property(e => e.Id).StoreWithDecimalIdPrecision(dbContext, columnType: "DECIMAL(28,0)"))); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.Id, loadedEntity.Id); Assert.Equal(0, GetSignAndScale(loadedEntity.Id)); //Assert.Equal("DECIMAL(28,0)", dbContext.Model.FindEntityType(typeof(TestEntity)).FindProperty(nameof(TestEntity.Id)).GetColumnType()); // Does not work with in-memory provider }
public static void AssemblyInitialize(TestContext context) { using (var dbDbContext = TestDbContext.Create()) { if (dbDbContext.Database.Exists() == false) { dbDbContext.Database.Initialize(true); } } s_client = new HttpClient(); s_client.BaseAddress = new Uri(HOST_ADDRESS); }
public void StoreDecimalIdsWithCorrectPrecision_WithSqlite_AffectsOtherIdProperties() { using var dbContext = TestDbContext.Create((modelBuilder, dbContext) => modelBuilder.StoreDecimalIdsWithCorrectPrecision(dbContext)); var entity = new TestEntity(); var loadedEntity = this.SaveAndReload(entity, dbContext); Assert.Equal(entity.ForeignId, loadedEntity.ForeignId); Assert.Equal(entity.ForeignID, loadedEntity.ForeignID); Assert.Equal(0, GetSignAndScale(loadedEntity.ForeignId)); Assert.Equal(0, GetSignAndScale(loadedEntity.ForeignID)); }
public async Task Can_Add_ApiResource() { using (var ctx = await TestDbContext.Create(nameof(Can_Add_ApiResource))) { var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())); var model = new AddApiResourceDto( "http://abc", DotNetAccessControl.domain.enums.HttpMethods.GET, "Some Title" ); var request = new AddApiResource(model); var response = await new AddApiResourceHandler(ctx, mapper.CreateMapper()).Handle(request, CancellationToken.None); Assert.True(await ctx.ApiResources.AnyAsync(x => x.Id == response)); } }
public async Task Add_Api_Resource_Server_validator(AddApiResourceDto model, bool isvalid) { using (var ctx = await TestDbContext.Create(nameof(Add_Api_Resource_Server_validator))) { ApiResource entity = new ApiResource { Address = "z", HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET, Title = "z" }; ctx.ApiResources.Add(entity); await ctx.SaveChangesAsync(); var vaidator = new ServerSideValidator(ctx); Assert.Equal(isvalid, vaidator.Validate(new AddApiResource(model)).IsValid); } }
public void CreateExecutionStrategy_WithRetryOnOptimisticConcurrencyFailure_ShouldReturnExpectedResult() { this.OptionsBuilder.ExecutionStrategyOptions = ExecutionStrategyOptions.RetryOnOptimisticConcurrencyFailure; var dbContext = TestDbContext.Create(); var result = this.Provider.CreateExecutionStrategy(dbContext); Assert.IsType <RetryOnOptimisticConcurrencyFailureExecutionStrategy>(result); var wrappedStrategyProperty = typeof(RetryOnOptimisticConcurrencyFailureExecutionStrategy).GetProperty("WrappedStrategy", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? throw new Exception("The WrappedStrategy property could not be found."); var wrappedStrategy = wrappedStrategyProperty.GetValue(result); Assert.IsType(dbContext.Database.CreateExecutionStrategy().GetType(), wrappedStrategy); }
public async Task Can_RoleAccess_To_Menu() { using (var ctx = await TestDbContext.Create(nameof(Can_RoleAccess_To_Menu))) { var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())); var menu = new MenuResource { Title = "someMenu", Icon = "", Name = "Mn1", Order = 1, }; var page = new PageResource { Link = "http://abc", Name = "p1", Title = "Some", }; menu.Page = page; ctx.MenuResources.Add(menu); ctx.PageResources.Add(page); await ctx.SaveChangesAsync(); Application.Access.GenericRoleToResourceHandler genericRoleToResourceHandler = new Application.Access.GenericRoleToResourceHandler(ctx, mapper.CreateMapper()); var mediator = new Mock <IMediator>(); mediator .Setup(m => m.Send(It.IsAny <RoleAccessToPage>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true) .Verifiable(); var model = new AssignRoleToResourceDto("Role1", menu.Id, DotNetAccessControl.domain.enums.Permission.Allow); var handler = new AssignRoleToMenuHandler(genericRoleToResourceHandler, ctx, mediator.Object); var result = await handler.Handle(new RoleAccessToMenu(model, true), CancellationToken.None); var saved = await ctx.AccessControls.OfType <RoleAccessControl>().SingleAsync(x => x.ResourceId == menu.Id); Assert.Equal(model.RoleName, saved.RoleName); mediator.Verify(x => x.Send(It.IsAny <RoleAccessToPage>(), It.IsAny <CancellationToken>()), Times.Once); } }
public async Task Can_Get_Menu_Resource_By_Id() { using (var ctx = await TestDbContext.Create(nameof(Can_Get_Menu_Resource_By_Id))) { var entity = new ApiResource() { Address = "http://abc", HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET, Title = "Some Title" }; ctx.ApiResources.Add(entity); await ctx.SaveChangesAsync(); var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())); var handler = new GetMenuResourceByIdHandler(ctx, mapper.CreateMapper()); var result = await handler.Handle(new GetMenuResourceById(entity.Id), CancellationToken.None); Assert.True(result != null); Assert.Equal(entity.Title, result.Title); Assert.Equal(entity.Address, result.Address); Assert.Equal(entity.HttpMethod, result.HttpMethod); } }