Ejemplo n.º 1
0
        public async Task DeleteAsync_EntityNotExist_ReturnFalse()
        {
            // Arrange
            var authenticateUser = new AuthenticateUser <int>()
            {
                Name            = "codedesignplus",
                IdUser          = new Random().Next(0, int.MaxValue),
                IsAuthenticated = true,
                IsApplication   = false,
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(OperationBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new PermissionRepository(authenticateUser, context);

            // Act
            var success = await repository.DeleteAsync(new Random().Next(1, int.MaxValue));

            // Assert
            Assert.False(success);
        }
Ejemplo n.º 2
0
        public async Task TransactionAsync_CommitedTransaction_ReturnResultDelegate()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=UnitTestTransaction;Trusted_Connection=True;MultipleActiveResultSets=true").Options;

            var context = new CodeDesignPlusContextInMemory(options);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var repository = new ApplicationRepository(context);

            // Act
            var result = await repository.TransactionAsync <bool>(async context =>
            {
                var applicationCreated = await repository.CreateAsync(new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                });

                return(applicationCreated.Id > 0);
            });

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 3
0
        public void ConfigurationBase_ValidateConfigProperties_ConfigDefaults()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(EFCoreExtensionsTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var modelBuilder = new ModelBuilder(ConventionSet.CreateConventionSet(context));

            var entityTypeBuilder = modelBuilder.Entity <Permission>();

            var customerEntityConfiguration = new PermissionEntityConfiguration();

            customerEntityConfiguration.Configure(entityTypeBuilder);

            // Act
            var idProperty            = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.Id));
            var idUserCreatorProperty = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.IdUserCreator));
            var stateProperty         = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.State));
            var dateCreatedProperty   = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.DateCreated));

            // Assert
            Assert.True(idProperty.IsPrimaryKey());
            Assert.False(idProperty.IsNullable);
            Assert.Equal(ValueGenerated.OnAdd, idProperty.ValueGenerated);
            Assert.False(idUserCreatorProperty.IsNullable);
            Assert.False(stateProperty.IsNullable);
            Assert.False(dateCreatedProperty.IsNullable);
        }
Ejemplo n.º 4
0
        public async Task UpdateRangeAsync_AssignUpdateInfo_Success()
        {
            // Arrange
            var entities = new List <Application>
            {
                new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                },

                new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                }
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            var entitiesCreated = await repository.CreateRangeAsync(entities);

            // Act
            var entitiesUpdate = await repository.GetEntity <Application>().Where(x => x.State).ToListAsync();

            entitiesUpdate.ForEach(x =>
            {
                x.Description   = "New Description";
                x.Name          = "New Name";
                x.DateCreated   = DateTime.MaxValue;
                x.State         = false;
                x.IdUserCreator = 100;
            });

            var success = await repository.UpdateRangeAsync(entitiesUpdate);

            // Assert
            var result = await repository.GetEntity <Application>().Where(x => !x.State).ToListAsync();

            Assert.True(success);

            foreach (var item in result)
            {
                Assert.Equal("New Name", item.Name);
                Assert.Equal("New Description", item.Description);
                Assert.False(item.State);
            }
        }
Ejemplo n.º 5
0
        public async Task CreateAsync_EntityIsNotNull_IdIsGreeaterThanZero()
        {
            // Arrange
            var entity = new Application()
            {
                Name          = nameof(Application.Name),
                IdUserCreator = new Random().Next(1, 15),
                State         = true,
                DateCreated   = DateTime.Now,
                Description   = nameof(Application.Description)
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var result = await repository.CreateAsync(entity);

            // Assert
            Assert.True(result.Id > 0);
            Assert.Equal(nameof(Application.Name), result.Name);
            Assert.Equal(nameof(Application.Description), result.Description);
            Assert.Equal(entity.IdUserCreator, result.IdUserCreator);
            Assert.Equal(entity.State, result.State);
            Assert.Equal(entity.DateCreated, result.DateCreated);
        }
Ejemplo n.º 6
0
        public async Task ChangeStateAsync_EntityExist_ReturnTrue()
        {
            // Arrange
            var entity = new Application()
            {
                Name          = nameof(Application.Name),
                IdUserCreator = new Random().Next(1, 15),
                State         = true,
                DateCreated   = DateTime.Now,
                Description   = nameof(Application.Description)
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            var entityCreate = await repository.CreateAsync(entity);

            // Act
            var success = await repository.ChangeStateAsync <Application>(entityCreate.Id, !entityCreate.State);

            // Assert
            Assert.True(success);
        }
Ejemplo n.º 7
0
        public async Task UpdateAsync_UpdateEntity_ReturnTrue()
        {
            // Arrange
            var permission = new Permission()
            {
                Name        = "Create - Permissions",
                Description = "Can create to permissions",
                Controller  = "Permission",
                Action      = "Post",
                State       = true
            };

            var authenticateUser = new AuthenticateUser <int>()
            {
                Name            = "codedesignplus",
                IdUser          = new Random().Next(0, int.MaxValue),
                IsAuthenticated = true,
                IsApplication   = false,
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(OperationBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new PermissionRepository(authenticateUser, context);

            var id = await repository.CreateAsync(permission);

            // Act
            var entityUpdate = new Permission()
            {
                Name        = "Update - Permissions",
                Description = "Can update to permissions",
                Controller  = "Permission",
                Action      = "Put",
                State       = false
            };

            var success = await repository.UpdateAsync(id, entityUpdate);

            // Assert
            var entity = await repository.GetEntity <Permission>().FindAsync(id);

            Assert.True(success);
            Assert.Equal(id, entity.Id);
            Assert.Equal("Update - Permissions", entity.Name);
            Assert.Equal("Can update to permissions", entity.Description);
            Assert.Equal("Permission", entity.Controller);
            Assert.Equal("Put", entity.Action);
            Assert.False(entity.State);
            Assert.Equal(authenticateUser.IdUser, entity.IdUserCreator);
            Assert.Equal(permission.DateCreated, entity.DateCreated);
        }
Ejemplo n.º 8
0
        public async Task ToPageAsync_PageFromDb_Pager()
        {
            // Arrange
            var currentPage = 1;
            var pageSize    = 10;
            var totalItems  = 500;
            var maxPages    = 10;
            var startIndex  = (currentPage - 1) * pageSize;
            var endIndex    = Math.Min(startIndex + pageSize - 1, totalItems - 1);
            var totalPages  = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize);

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(EFCoreExtensionsTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            var applications = new List <Application>();

            for (int i = 0; i < totalItems; i++)
            {
                applications.Add(new Application()
                {
                    Name          = $"{nameof(Application.Name)}-{i}",
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = $"{nameof(Application.Description)}-{i}"
                });
            }

            await repository.CreateRangeAsync(applications);

            // Act
            var pager = await repository.GetEntity <Application>().ToPageAsync(currentPage, pageSize);

            // Assert
            Assert.Equal(totalItems, pager.TotalItems);
            Assert.Equal(currentPage, pager.CurrentPage);
            Assert.Equal(pageSize, pager.PageSize);
            Assert.Equal(totalPages, pager.TotalPages);
            Assert.Equal(pager.Pages.Min(), pager.StartPage);
            Assert.Equal(pager.Pages.Max(), pager.EndPage);
            Assert.Equal(maxPages, pager.Pages.Count());
            Assert.Equal(startIndex, pager.StartIndex);
            Assert.Equal(endIndex, pager.EndIndex);
        }
Ejemplo n.º 9
0
        public async Task DeleteAsync_EntityIsNull_ArgumentNullException()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act & Assert
            var exception = await Assert.ThrowsAsync <ArgumentNullException>(() => repository.DeleteAsync <Application>(null));

            Assert.Equal("Value cannot be null. (Parameter 'predicate')", exception.Message);
        }
Ejemplo n.º 10
0
        public async Task ChangeStateAsync_EntityNotExist_ReturnFalse()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var success = await repository.ChangeStateAsync <Application>(1, false);

            // Assert
            Assert.False(success);
        }
Ejemplo n.º 11
0
        public void GetContext_CastContext()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var result = repository.GetContext <CodeDesignPlusContextInMemory>();

            // Assert
            Assert.IsType <CodeDesignPlusContextInMemory>(result);
        }
Ejemplo n.º 12
0
        public async Task ToPageAsync_ArgumentsInvalid_Null()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(EFCoreExtensionsTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var pager = await repository.GetEntity <Application>().ToPageAsync(0, 0);

            // Assert
            Assert.Null(pager);
        }
Ejemplo n.º 13
0
        public void GetEntity_EntityExist_NotNull()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var dbset = repository.GetEntity <Application>();

            // Assert
            Assert.NotNull(dbset);
            Assert.Equal(nameof(Application), dbset.EntityType.FullName());
        }
Ejemplo n.º 14
0
        public async Task DeleteRangeAsync_ListEmpty_ReturnFalse()
        {
            // Arrange
            var entities = new List <Application>();

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var success = await repository.DeleteRangeAsync(entities);

            // Assert
            Assert.False(success);
        }
Ejemplo n.º 15
0
        public async Task UpdateAsync_AssignUpdateInfo_Success()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            var applicationCreated = await repository.CreateAsync(new Application()
            {
                Name          = nameof(Application.Name),
                IdUserCreator = new Random().Next(1, 15),
                State         = true,
                DateCreated   = DateTime.Now,
                Description   = nameof(Application.Description)
            });

            // Act
            var applicationUpdate = await repository.GetEntity <Application>().FirstOrDefaultAsync(x => x.Id == applicationCreated.Id);

            applicationUpdate.Description   = "New Description";
            applicationUpdate.Name          = "New Name";
            applicationUpdate.DateCreated   = DateTime.MaxValue;
            applicationUpdate.State         = false;
            applicationUpdate.IdUserCreator = 100;

            var success = await repository.UpdateAsync(applicationUpdate);

            // Assert
            var result = await repository.GetEntity <Application>().FirstOrDefaultAsync(x => x.Id == applicationUpdate.Id);

            Assert.True(success);
            Assert.Equal("New Name", result.Name);
            Assert.Equal("New Description", result.Description);
            Assert.False(result.State);
            Assert.Equal(applicationCreated.IdUserCreator, result.IdUserCreator);
            Assert.Equal(applicationCreated.DateCreated, result.DateCreated);
        }
Ejemplo n.º 16
0
        public async Task CreateAsync_CreateEntity_ReturnId()
        {
            // Arrange
            var permission = new Permission()
            {
                Name        = "Create - Permissions",
                Description = "Can create to permissions",
                Controller  = "Permission",
                Action      = "Post",
                State       = true
            };

            var authenticateUser = new AuthenticateUser <int>()
            {
                Name            = "codedesignplus",
                IdUser          = new Random().Next(0, int.MaxValue),
                IsAuthenticated = true,
                IsApplication   = false,
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(OperationBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new PermissionRepository(authenticateUser, context);

            // Act
            var id = await repository.CreateAsync(permission);

            // Assert
            Assert.Equal(permission.Id, id);
            Assert.Equal("Create - Permissions", permission.Name);
            Assert.Equal("Can create to permissions", permission.Description);
            Assert.Equal("Permission", permission.Controller);
            Assert.Equal("Post", permission.Action);
            Assert.True(permission.State);
            Assert.Equal(authenticateUser.IdUser, permission.IdUserCreator);
            Assert.True(permission.DateCreated > DateTime.MinValue);
        }
Ejemplo n.º 17
0
        public async Task DeleteRangeAsync_EntityExist_True()
        {
            // Arrange
            var entities = new List <Application>
            {
                new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                },
                new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                }
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            var entitiesCreated = await repository.CreateRangeAsync(entities);

            // Act
            var entitiesDelete = await repository.GetEntity <Application>().Where(x => x.State).ToListAsync();

            var success = await repository.DeleteRangeAsync(entitiesDelete);

            // Assert
            Assert.True(success);
        }
Ejemplo n.º 18
0
        public void GetEntity_EntityNotExist_Exception()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var dbset = repository.GetEntity <FakeEntity>();

            // Assert
            Assert.NotNull(dbset);

            var exception = Assert.Throws <InvalidOperationException>(() => dbset.EntityType);

            Assert.Equal($"Cannot create a DbSet for '{nameof(FakeEntity)}' because this type is not included in the model for the context.", exception.Message);
        }
Ejemplo n.º 19
0
        public async Task TransactionAsync_RollbackTransaction_InvalidOperationException()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=UnitTestTransaction;Trusted_Connection=True;MultipleActiveResultSets=true").Options;

            var context = new CodeDesignPlusContextInMemory(options);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var repository = new ApplicationRepository(context);

            // Act & Assert
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                var result = await repository.TransactionAsync <bool>(async context =>
                {
                    var applicationCreated = await repository.CreateAsync(new Application()
                    {
                        Name          = nameof(Application.Name),
                        IdUserCreator = new Random().Next(1, 15),
                        State         = true,
                        DateCreated   = DateTime.Now,
                        Description   = nameof(Application.Description)
                    });

                    if (applicationCreated.Id > 0)
                    {
                        throw new InvalidOperationException("Failed Transaction");
                    }

                    return(applicationCreated.Id > 0);
                });
            });

            // Assert
            Assert.Equal("Failed Transaction", exception.Message);
        }
Ejemplo n.º 20
0
        public async Task CreateRangeAsync_ListWithData_ReturnListAndIds()
        {
            // Arrange
            var entities = new List <Application>
            {
                new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                },
                new Application()
                {
                    Name          = nameof(Application.Name),
                    IdUserCreator = new Random().Next(1, 15),
                    State         = true,
                    DateCreated   = DateTime.Now,
                    Description   = nameof(Application.Description)
                }
            };

            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(RepositoryBaseTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var repository = new ApplicationRepository(context);

            // Act
            var result = await repository.CreateRangeAsync(entities);

            // Assert
            Assert.Contains(result, x => x.Id > 0);
        }
 public PermissionRepository(IAuthenticateUser <int> user, CodeDesignPlusContextInMemory context) : base(user, context)
 {
 }
Ejemplo n.º 22
0
 public RolePermissionRepository(CodeDesignPlusContextInMemory context) : base(context)
 {
 }
 public ApplicationRepository(CodeDesignPlusContextInMemory context) : base(context)
 {
 }