Exemple #1
0
        public async Task CharactersOfTypeHumanSpecification_ShouldApplySpecification()
        {
            // Arrange
            var baseCharacters = _fixture.Build <Character>()
                                 .Without(wh => wh.CharacterEpisodes)
                                 .Without(wh => wh.CharacterFriends)
                                 .Without(wh => wh.FriendCharacters).CreateMany(2);

            var humanCharacters = _fixture.Build <Human>()
                                  .Without(wh => wh.CharacterEpisodes)
                                  .Without(wh => wh.CharacterFriends)
                                  .Without(wh => wh.FriendCharacters)
                                  .Without(wh => wh.HomePlanet).CreateMany(2);

            List <Character> characters = new List <Character>();

            characters.AddRange(baseCharacters);
            characters.AddRange(humanCharacters);

            Character[] entities = characters.ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);
            var set           = dbContextMock.CreateDbSetMock(x => x.Characters, (x, _) => (x.Id), entities);

            var repository = new ReadAsyncRepository <Character>(dbContextMock.Object);

            // Act
            Character[] charactersResult = await repository.GetAllAsync(new CharactersOfTypeHumanSpecification());

            // Assert
            charactersResult.Should().NotBeNull().And.HaveSameCount(humanCharacters);
        }
Exemple #2
0
        public async Task WithFriendsFromTatooineSpecification_ShouldApplySpecification()
        {
            // Arrange
            var tatooineHomeplant = _fixture.Build <Planet>()
                                    .Without(wh => wh.Humans).Create();

            tatooineHomeplant.Name = "Tatooine";

            var baseCharacter = _fixture.Build <Character>()
                                .Without(wh => wh.CharacterEpisodes)
                                .Without(wh => wh.CharacterFriends)
                                .Without(wh => wh.FriendCharacters).Create();

            var friendCharacter = _fixture.Build <Human>()
                                  .Without(wh => wh.CharacterEpisodes)
                                  .Without(wh => wh.CharacterFriends)
                                  .Without(wh => wh.FriendCharacters)
                                  .Without(wh => wh.HomePlanet).Create();

            friendCharacter.HomePlanet = tatooineHomeplant;

            baseCharacter.CharacterFriends = new CharacterFriend[]
            {
                new CharacterFriend
                {
                    Character   = baseCharacter,
                    CharacterId = baseCharacter.Id,
                    Friend      = friendCharacter,
                    FriendId    = friendCharacter.Id
                }
            };

            Character[] entities = new Character[]
            {
                baseCharacter
            };

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.Characters, (x, _) => (x.Id), entities);

            var repository = new ReadAsyncRepository <Character>(dbContextMock.Object);

            // Act
            Character[] characters = await repository.GetAllAsync(new WithFriendsFromTatooineSpecification());

            // Assert
            characters.Should().NotBeNull().And.NotBeEmpty();
        }
Exemple #3
0
        public async Task Should_SetCriteria()
        {
            // Arrange
            TestEntity[] entities      = _fixture.CreateMany <TestEntity>(2).ToArray();
            var          dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities);

            var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object);

            // Act
            TestEntity[] result = await repository.GetAllAsync(new TestEntityCriteriaSpecification(entities[0].Id));

            // Assert
            result.Should().NotBeNull().And.BeEquivalentTo(entities[0]);
        }
Exemple #4
0
        public async Task ListAllAsync_ShouldReturnEntities()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities);
            var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object);

            // Act
            TestEntity[] result = await repository.GetAllAsync();

            // Assert
            result.Should().NotBeNull().And.HaveCount(2);
        }
Exemple #5
0
        public async Task GetById_UnknownId_ShouldReturnNull()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities);

            var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object);

            // Act
            TestEntity result = await repository.GetByIdAsync(0);

            // Assert
            result.Should().BeNull();
        }
Exemple #6
0
        public async Task Should_OrderByNameDescending()
        {
            // Arrange
            TestEntity[] entities      = _fixture.CreateMany <TestEntity>(4).ToArray();
            var          dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities);

            var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object);

            // Act
            TestEntity[] result = await repository.GetAllAsync(new TestEntityOrderByNameDescSpecification());

            TestEntity[] orderedList = entities.OrderByDescending(o => o.Name).ToArray();

            // Assert
            result.Should().Equals(orderedList);
        }
Exemple #7
0
        public async Task Should_ApplyPaging()
        {
            // Arrange
            TestEntity[] entities      = _fixture.CreateMany <TestEntity>(4).ToArray();
            var          dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities);

            var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object);

            // Act
            TestEntity[] result = await repository.GetAllAsync(new TestEntityPaginatedSpecification(1, 2));

            // Assert
            result.Should().HaveCount(2);
            result[0].Id.Should().Equals(entities[0]);
            result[1].Id.Should().Equals(entities[1]);
        }
Exemple #8
0
        public async Task ListAllAsyncWithSpecification_ShouldApplySpecification()
        {
            // Arrange
            TestEntity[] entities = _fixture.CreateMany <TestEntity>(2).ToArray();

            var dbContextMock = new DbContextMock <TestDbContext>(_options);

            dbContextMock.CreateDbSetMock(x => x.TestEntities, (x, _) => (x.Id), entities);
            var repository = new ReadAsyncRepository <TestEntity>(dbContextMock.Object);

            var specification = new Mock <ISpecification <TestEntity> >();

            // Act
            TestEntity[] result = await repository.GetAllAsync(specification.Object);

            // Assert
            result.Should().NotBeNull().And.BeOfType(typeof(TestEntity[]));
        }