Ejemplo n.º 1
0
        public async Task AddOrUpdateAsync_WhenNew_ReturnsEntityWithId()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id      = 0,
                Deleted = false
            };

            var resultEntity = new BaseEntityImplementation()
            {
                Id      = 1,
                Deleted = false
            };

            baseRepositoryMock
            .Setup(m => m.AddOrUpdateAsync(It.IsAny <Entity>()))
            .ReturnsAsync(resultEntity);

            var service = GetService();

            // Act
            var result = await service.AddOrUpdateAsync(entity);

            // Assert
            Assert.NotNull(result);
            Assert.NotEqual(entity.Id, result.Id);
            Assert.Equal(entity.Deleted, result.Deleted);

            baseRepositoryMock.Verify(m => m.AddOrUpdateAsync(It.IsAny <Entity>()), Times.Once);
        }
Ejemplo n.º 2
0
        public async Task GetAsync_WithIncludeWhenExists_ReturnsEntity()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id            = 1,
                RelatedEntity = new BaseEntityImplementation()
                {
                    Id            = 2,
                    RelatedEntity = null,
                    Deleted       = false
                },
                Deleted = false
            };

            baseRepositoryMock
            .Setup(m => m.GetSingleAsync(It.IsAny <int>(), It.IsAny <Expression <Func <Entity, object> > >()))
            .ReturnsAsync(entity);

            var service = GetService();

            // Act
            var result = await service.GetAsync(1, e => ((BaseEntityImplementation)e).RelatedEntity);

            // Assert
            Assert.NotNull(result);
            result.Should()
            .BeEquivalentTo(entity, opts => opts.IncludingProperties().IncludingNestedObjects());

            baseRepositoryMock.Verify(m => m.GetSingleAsync(It.IsAny <int>(), It.IsAny <Expression <Func <Entity, object> > >()), Times.Once);
        }
Ejemplo n.º 3
0
        public async Task AddOrUpdateAsync_WhenExisting_ReturnsEntity()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id      = 1,
                Deleted = false
            };

            var resultEntity = new BaseEntityImplementation()
            {
                Id      = 1,
                Deleted = false
            };

            baseRepositoryMock
            .Setup(m => m.AddOrUpdateAsync(It.IsAny <Entity>()))
            .ReturnsAsync(resultEntity);

            var service = GetService();

            // Act
            var result = await service.AddOrUpdateAsync(entity);

            // Assert
            Assert.NotNull(result);
            result.Should().BeEquivalentTo(resultEntity);

            baseRepositoryMock.Verify(m => m.AddOrUpdateAsync(It.IsAny <Entity>()), Times.Once);
        }
Ejemplo n.º 4
0
        public async Task GetFilteredAsync_WhenExists_ReturnsCollection()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id      = 1,
                Deleted = false
            };
            var entities = new List <Entity>()
            {
                entity
            };

            baseRepositoryMock
            .Setup(m => m.GetFilteredAsync(It.IsAny <Expression <Func <Entity, bool> > >()))
            .ReturnsAsync(entities);

            var service = GetService();

            // Act
            var result = await service.GetFilteredAsync(e => e.Id == 1);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(entities, result);

            baseRepositoryMock.Verify(m => m.GetFilteredAsync(It.IsAny <Expression <Func <Entity, bool> > >()), Times.Once);
        }
Ejemplo n.º 5
0
        public async Task GetAsync_WhenExists_ReturnsEntity()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id      = 1,
                Deleted = false
            };

            baseRepositoryMock
            .Setup(m => m.GetSingleAsync(It.IsAny <int>()))
            .ReturnsAsync(entity);

            var service = GetService();

            // Act
            var result = await service.GetAsync(1);

            // Assert
            Assert.NotNull(result);
            result.Should().BeEquivalentTo(entity);
            //Assert.Equal(entity.Id, result.Id);
            //Assert.Equal(entity.Deleted, result.Deleted);

            baseRepositoryMock.Verify(m => m.GetSingleAsync(It.IsAny <int>()), Times.Once);
        }
Ejemplo n.º 6
0
        public async Task GetPaginatedAsync_WithPredicateIncludesOrderWhenExists_ReturnsEntityPage()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id            = 1,
                RelatedEntity = new BaseEntityImplementation
                {
                    Id            = 2,
                    RelatedEntity = null,
                    Deleted       = false
                },
                Deleted = false
            };
            var entities = new List <Entity>()
            {
                entity
            };

            EntityPage <Entity> page = new EntityPage <Entity>();

            page.TotalEntities = entities.Count;
            page.TotalPages    = 1;
            page.Entities      = entities;

            baseRepositoryMock
            .Setup(m => m.GetPaginatedAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <Expression <Func <Entity, bool> > >(), It.IsAny <Expression <Func <Entity, object> > >(), It.IsAny <bool>(), It.IsAny <Expression <Func <Entity, object> > >()))
            .ReturnsAsync(entities);
            baseRepositoryMock
            .Setup(m => m.CountAsync(It.IsAny <Expression <Func <Entity, bool> > >()))
            .ReturnsAsync(entities.Count);

            var service = GetService();

            // Act
            var result = await service.GetPaginatedAsync(1, 10, e => e.Id == 1, e => e.Id, true, e => ((BaseEntityImplementation)e).RelatedEntity);

            // Assert
            Assert.NotNull(result);
            result.Should()
            .BeEquivalentTo(page, opts => opts.IncludingProperties().IncludingNestedObjects());

            baseRepositoryMock.Verify(m => m.GetPaginatedAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <Expression <Func <Entity, bool> > >(), It.IsAny <Expression <Func <Entity, object> > >(), It.IsAny <bool>(), It.IsAny <Expression <Func <Entity, object> > >()), Times.Once);
            baseRepositoryMock.Verify(m => m.CountAsync(It.IsAny <Expression <Func <Entity, bool> > >()), Times.Once);
        }
Ejemplo n.º 7
0
        public async Task GetPaginatedAsync_WhenExists_ReturnsEntityPage()
        {
            // Arrange
            var entity = new BaseEntityImplementation()
            {
                Id      = 1,
                Deleted = false
            };
            var entities = new List <Entity>()
            {
                entity
            };

            EntityPage <Entity> page = new EntityPage <Entity>();

            page.TotalEntities = entities.Count;
            page.TotalPages    = 1;
            page.Entities      = entities;

            baseRepositoryMock
            .Setup(m => m.GetPaginatedAsync(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(entities);
            baseRepositoryMock
            .Setup(m => m.CountAsync())
            .ReturnsAsync(entities.Count);

            var service = GetService();

            // Act
            var result = await service.GetPaginatedAsync(1, 10);

            // Assert
            Assert.NotNull(result);
            result.Should()
            .BeEquivalentTo(page, opts => opts.IncludingProperties().IncludingNestedObjects());

            baseRepositoryMock.Verify(m => m.GetPaginatedAsync(It.IsAny <int>(), It.IsAny <int>()), Times.Once);
            baseRepositoryMock.Verify(m => m.CountAsync(), Times.Once);
        }