public async Task Should_UnitOfWork_Action_Filter_Save_Changes()
        {
            var testRole = new Role
            {
                Id   = Guid.NewGuid(),
                Name = "TestRole_" + Guid.NewGuid()
            };

            var unitOfWorkActionFilter = new UnitOfWorkActionFilter(_dbContext);
            var actionContext          = new ActionContext(
                new DefaultHttpContext
            {
                Request =
                {
                    Method = "Post"
                }
            },
                new RouteData(),
                new ActionDescriptor()
                );

            var actionExecutedContext = new ActionExecutedContext(actionContext, new List <IFilterMetadata>(), null);
            await _dbContext.Roles.AddAsync(testRole);

            var dbContextFromAnotherScope = _serviceProvider.CreateScope().ServiceProvider.GetRequiredService <AspNetCoreStarterKitDbContext>();
            var insertedTestRole          = await dbContextFromAnotherScope.Roles.FindAsync(testRole.Id);

            Assert.Null(insertedTestRole);

            unitOfWorkActionFilter.OnActionExecuted(actionExecutedContext);

            insertedTestRole = await dbContextFromAnotherScope.Roles.FindAsync(testRole.Id);

            Assert.NotNull(insertedTestRole);
        }
        public async Task Should_Not_UnitOfWork_Action_Filter_Save_Changes_OnException()
        {
            var testRole = new Role
            {
                Id   = Guid.NewGuid(),
                Name = "TestRole_" + new string('*', 300)//allowed max role name length is 256
            };

            var unitOfWorkActionFilter = new UnitOfWorkActionFilter(_dbContext);
            var actionContext          = new ActionContext(
                new DefaultHttpContext
            {
                Request =
                {
                    Method = "Post"
                }
            },
                new RouteData(),
                new ActionDescriptor()
                );

            var actionExecutedContext = new ActionExecutedContext(actionContext, new List <IFilterMetadata>(), null);
            await _dbContext.Roles.AddAsync(testRole);

            var exception = Assert.Throws <DbUpdateException>(() => unitOfWorkActionFilter.OnActionExecuted(actionExecutedContext));

            Assert.Equal(typeof(DbUpdateException), exception.GetType());
        }
Exemplo n.º 3
0
        public void TestUnitOfWorkActionFilter()
        {
            var dbContext = TestServer.Host.Services.GetRequiredService <NucleusDbContext>();
            var unitOfWorkActionFilter = new UnitOfWorkActionFilter(dbContext);
            var actionContext          = new ActionContext(
                new DefaultHttpContext
            {
                Request =
                {
                    Method = "Post"
                }
            },
                new RouteData(),
                new ActionDescriptor()
                );
            var actionExecutedContext = new ActionExecutedContext(actionContext, new List <IFilterMetadata>(), null);
            var testRole = new Role {
                Name = "test_role"
            };

            dbContext.Roles.Add(testRole);

            //created a new db context to get non-local(non-tracking) data
            var dbContextForGet  = TestServer.Host.Services.GetRequiredService <NucleusDbContext>();
            var insertedTestRole = dbContextForGet.Roles.Find(testRole.Id);

            Assert.Null(insertedTestRole);

            unitOfWorkActionFilter.OnActionExecuted(actionExecutedContext);

            insertedTestRole = dbContextForGet.Roles.Find(testRole.Id);
            Assert.NotNull(insertedTestRole);
        }
        public async Task Should_Not_UnitOfWork_Action_Filter_Save_Changes_Except_Post()
        {
            var testRole = new Role
            {
                Id   = Guid.NewGuid(),
                Name = "TestRole_" + Guid.NewGuid()
            };

            var unitOfWorkActionFilter = new UnitOfWorkActionFilter(_dbContext);
            var actionContext          = new ActionContext(
                new DefaultHttpContext(),
                new RouteData(),
                new ActionDescriptor()
                );

            var actionExecutedContext = new ActionExecutedContext(actionContext, new List <IFilterMetadata>(), null);
            await _dbContext.Roles.AddAsync(testRole);

            var dbContextFromAnotherScope = TestServer.Host.Services.GetRequiredService <NucleusDbContext>();
            var insertedTestRole          = await dbContextFromAnotherScope.Roles.FindAsync(testRole.Id);

            Assert.Null(insertedTestRole);

            unitOfWorkActionFilter.OnActionExecuted(actionExecutedContext);

            insertedTestRole = await dbContextFromAnotherScope.Roles.FindAsync(testRole.Id);

            Assert.Null(insertedTestRole);
        }
Exemplo n.º 5
0
        public async Task Should_Not_UnitOfWork_Action_Filter_Save_Changes_OnException()
        {
            var testRole = new Role
            {
                Id = DefaultRoles.Admin.Id // set same id with admin role
            };

            var unitOfWorkActionFilter = new UnitOfWorkActionFilter(DbContext);
            var actionContext          = new ActionContext(
                new DefaultHttpContext
            {
                Request =
                {
                    Method = "Post"
                }
            },
                new RouteData(),
                new ActionDescriptor()
                );

            var actionExecutedContext = new ActionExecutedContext(actionContext, new List <IFilterMetadata>(), null);
            await DbContext.Roles.AddAsync(testRole);

            var exception = Assert.Throws <ArgumentException>(() => unitOfWorkActionFilter.OnActionExecuted(actionExecutedContext));

            Assert.Equal(typeof(ArgumentException), exception.GetType());
        }