Ejemplo n.º 1
0
        [Test] // behaviour test
        public void Create_When_entity_is_invalid_Then_it_makes_no_calls_to_repository_and_unit_of_work()
        {
            // Arrange
            var airhostessDTOToCreate = new AirhostessDTO()
            {
                FirstName = "",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 2
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).Returns(airhostessRepositoryFake);

            var airhostessService = new AirhostessService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => airhostessService.Create(airhostessDTOToCreate));

            // Assert. Just behaviour
            A.CallTo(() => airhostessRepositoryFake.Create(A <Airhostess> ._)).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.AirhostessRepository).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.SaveChanges()).MustNotHaveHappened();
        }
Ejemplo n.º 2
0
        [Test] // behaviour test
        public void Create_When_entity_is_created_Then_it_makes_calls_to_repository_and_unit_of_work()
        {
            // Arrange
            var airhostessDTOToCreate = new AirhostessDTO()
            {
                FirstName = "Airhostess1",
                LastName  = "Airhostess1",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 1
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).Returns(airhostessRepositoryFake);

            var airhostessService = new AirhostessService(unitOfWorkFake, AlwaysValidValidator);

            // Act
            var result = airhostessService.Create(airhostessDTOToCreate);

            // Assert. Just behaviour
            A.CallTo(() => airhostessRepositoryFake.Create(A <Airhostess> ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).MustHaveHappenedOnceExactly();
            A.CallTo(() => unitOfWorkFake.SaveChanges()).MustHaveHappenedOnceExactly();
        }
Ejemplo n.º 3
0
        public void Update_When_entity_is_updated_Then_existing_entity_in_db_updates()
        {
            // Arrange
            var entityToUpdate = new AirhostessDTO
            {
                Id        = 1,
                FirstName = "Airhostess1",
                LastName  = "Airhostess1",
                BirthDate = new DateTime(1970, 1, 12),
                CrewId    = 1
            };

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            var updatedEntity = service.Update(entityToUpdate);

            // Assert
            var fromDbEntity = AirportDbContext.Airhostess.FirstOrDefault(x => x.Id == entityToUpdate.Id);

            Assert.NotNull(fromDbEntity);
            Assert.AreEqual(entityToUpdate.Id, fromDbEntity.Id);
            Assert.AreEqual(entityToUpdate.FirstName, fromDbEntity.FirstName);
            Assert.AreEqual(entityToUpdate.LastName, fromDbEntity.LastName);
            Assert.AreEqual(entityToUpdate.BirthDate, fromDbEntity.BirthDate);
            Assert.AreEqual(entityToUpdate.CrewId, fromDbEntity.CrewId);
        }
Ejemplo n.º 4
0
        public void Create_When_entity_is_created_Then_new_entity_creates_in_db()
        {
            // Arrange
            var newEntity = new AirhostessDTO
            {
                FirstName = "Airhostess1",
                LastName  = "Airhostess1",
                BirthDate = new DateTime(1970, 1, 12),
                CrewId    = 1
            };

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            var createdEntity = service.Create(newEntity);

            // Assert
            var fromDbEntity = AirportDbContext.Airhostess.FirstOrDefault(x => x.Id == createdEntity.Id);

            Assert.NotNull(fromDbEntity);
            Assert.AreEqual(newEntity.FirstName, fromDbEntity.FirstName);
            Assert.AreEqual(newEntity.LastName, fromDbEntity.LastName);
            Assert.AreEqual(newEntity.BirthDate, fromDbEntity.BirthDate);
            Assert.AreEqual(newEntity.CrewId, fromDbEntity.CrewId);
        }
Ejemplo n.º 5
0
        public void Delete_by_id_When_airhostess_is_deleted_Then_other_entities_are_not_changed()
        {
            // Arrange
            var airhostessToDeleteId = 3;

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            var airhostessesBeforeUpdate = AirportDbContext.Airhostess
                                           .Where(x => x.Id != airhostessToDeleteId).ToList();

            // Act
            service.Delete(airhostessToDeleteId);

            // Assert
            var airhostessesAfterUpdate = AirportDbContext.Airhostess
                                          .Where(x => x.Id != airhostessToDeleteId).ToList();

            Assert.AreEqual(airhostessesBeforeUpdate.Count, airhostessesAfterUpdate.Count);
            foreach (var beforeUpdate in airhostessesBeforeUpdate)
            {
                var afterUpdate = airhostessesAfterUpdate.FirstOrDefault(x => x.Id == beforeUpdate.Id);
                Assert.NotNull(afterUpdate);

                Assert.AreEqual(beforeUpdate.Id, afterUpdate.Id);
                Assert.AreEqual(beforeUpdate.FirstName, afterUpdate.FirstName);
                Assert.AreEqual(beforeUpdate.LastName, afterUpdate.LastName);
                Assert.AreEqual(beforeUpdate.BirthDate, afterUpdate.BirthDate);
                Assert.AreEqual(beforeUpdate.CrewId, afterUpdate.CrewId);
            }
        }
Ejemplo n.º 6
0
        public void AssignToCrew_When_airhostesses_are_assigned_to_crew_Then_it_updates_CrewId_field_in_db()
        {
            // Arrange
            var airhostessesIdsToAssign = new List <int>()
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
            };
            var newCrewId = 10;

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            service.AssignToCrew(airhostessesIdsToAssign, newCrewId);

            // Assert
            var airhostessesFromDb = AirportDbContext.Airhostess.Where(
                x => airhostessesIdsToAssign.Any(y => y == x.Id)
                ).ToList();

            Assert.AreEqual(airhostessesFromDb.Count, airhostessesIdsToAssign.Count);
            foreach (var airhostess in airhostessesFromDb)
            {
                Assert.NotNull(airhostess.CrewId);
                Assert.AreEqual(airhostess.CrewId, newCrewId);
            }
        }
Ejemplo n.º 7
0
        public void Update_When_entity_is_updated_Then_updated_entity_is_returned()
        {
            // Arrange
            var airhostessMock = new Airhostess()
            {
                Id        = 3,
                FirstName = "Airhostess3",
                LastName  = "Airhostess3",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 3,
                Crew      = null
            };

            var airhostessDTOToUpdate = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 1
            };

            var expectedAirhostessDTO = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess3",
                LastName  = "Airhostess3",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 3
            };
            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();

            A.CallTo(() => airhostessRepositoryFake.Update(A <Airhostess> ._)).Returns(airhostessMock);

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).Returns(airhostessRepositoryFake);

            var airhostessService = new AirhostessService(unitOfWorkFake, AlwaysValidValidator);

            // Act
            var result = airhostessService.Update(airhostessDTOToUpdate);

            // Assert
            Assert.AreEqual(expectedAirhostessDTO.Id, result.Id, "Id");
            Assert.AreEqual(expectedAirhostessDTO.FirstName, result.FirstName);
            Assert.AreEqual(expectedAirhostessDTO.LastName, result.LastName);
            Assert.AreEqual(expectedAirhostessDTO.BirthDate, result.BirthDate);
            Assert.AreEqual(expectedAirhostessDTO.CrewId, result.CrewId);
        }
Ejemplo n.º 8
0
        public void Delete_by_id_When_airhostess_is_deleted_Then_it_deletes_in_db()
        {
            // Arrange
            var airhostessToDeleteId = 3;

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            service.Delete(airhostessToDeleteId);

            // Assert
            var tryToFindDeletedAirhostess = AirportDbContext.Airhostess
                                             .FirstOrDefault(x => x.Id == airhostessToDeleteId);

            Assert.IsNull(tryToFindDeletedAirhostess);
        }
Ejemplo n.º 9
0
        public void Update_When_entity_is_invalid_Then_bad_request_exception_is_thrown()
        {
            // Arrange
            var airhostessDTOToUpdate = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 2
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();
            var unitOfWorkFake           = A.Fake <IUnitOfWork>();
            var airhostessService        = new AirhostessService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => airhostessService.Update(airhostessDTOToUpdate), "");

            Assert.AreEqual(exception.Message, "Is Invalid");
        }
Ejemplo n.º 10
0
        public void Update_When_entity_is_updated_Then_other_entities_are_not_changed()
        {
            // Arrange
            var entityToUpdate = new AirhostessDTO
            {
                Id        = 1,
                FirstName = "Airhostess1",
                LastName  = "Airhostess1",
                BirthDate = new DateTime(1970, 1, 12),
                CrewId    = 1
            };

            var airhostessesBeforeUpdate = AirportDbContext.Airhostess
                                           .Where(x => x.Id != entityToUpdate.Id).ToList();

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            var updatedEntity = service.Update(entityToUpdate);

            var airhostessesAfterUpdate = AirportDbContext.Airhostess
                                          .Where(x => x.Id != entityToUpdate.Id).ToList();

            // Assert
            Assert.AreEqual(airhostessesBeforeUpdate.Count, airhostessesAfterUpdate.Count);
            foreach (var beforeUpdate in airhostessesBeforeUpdate)
            {
                var afterUpdate = airhostessesAfterUpdate.FirstOrDefault(x => x.Id == beforeUpdate.Id);
                Assert.NotNull(afterUpdate);

                Assert.AreEqual(beforeUpdate.Id, afterUpdate.Id);
                Assert.AreEqual(beforeUpdate.FirstName, afterUpdate.FirstName);
                Assert.AreEqual(beforeUpdate.LastName, afterUpdate.LastName);
                Assert.AreEqual(beforeUpdate.BirthDate, afterUpdate.BirthDate);
                Assert.AreEqual(beforeUpdate.CrewId, afterUpdate.CrewId);
            }
        }
Ejemplo n.º 11
0
        public void Create_When_entity_is_invalid_Then_bad_request_exception_is_thrown()
        {
            // Arrange
            var airhostessMock = new Airhostess()
            {
                Id        = 2,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 2,
                Crew      = null
            };

            var airhostessDTOToCreate = new AirhostessDTO()
            {
                FirstName = "",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 2
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();

            A.CallTo(() => airhostessRepositoryFake.Create(A <Airhostess> ._)).Returns(airhostessMock);

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).Returns(airhostessRepositoryFake);

            var airhostessService = new AirhostessService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => airhostessService.Create(airhostessDTOToCreate), "");

            Assert.AreEqual(exception.Message, "Is Invalid");
        }