public async Task ThatDoesNotExistItShouldThrow()
        {
            // Arrange
            var context     = LivestockDbContextFactory.Create(_loggerFactory);
            var service     = SetUpService(context);
            var transaction = new WeightTransaction
            {
                Id              = 999,
                AnimalId        = TestConstants.AnimalId1,
                TransactionDate = TestConstants.DefaultDate,
                Weight          = 120
            };

            // Act
            var exception = await Assert.ThrowsAsync <EntityNotFoundException <WeightTransaction> >(
                async() =>
            {
                await service.UpdateAsync(transaction, CancellationToken.None)
                .ConfigureAwait(true);
            })
                            .ConfigureAwait(true);

            // Assert
            Assert.NotNull(exception);
            Assert.Equal($"{nameof(WeightTransaction)} with id {transaction.Id} not found.", exception.Message);
        }
        public async Task ThatDoesNotExistItShouldAddItToTheDatabase()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = new AnimalCrudService(_logger, context, _mockMapper.Object);

            // Act
            var animal = new Animal();
            await service.AddAsync(animal, CancellationToken.None);

            // Assert
            Assert.Equal(1, context.Animals.Count());
        }
        public async Task ThatDoesNotExistItShouldReturnNull()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = SetUpService(context);

            // Act
            var transaction = await service.GetSingleAsync(999, CancellationToken.None)
                              .ConfigureAwait(true);

            // Assert
            Assert.Null(transaction);
        }
        public async Task ItShouldRemoveTheRecordFromTheDatabase()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = SetUpService(context);
            var id      = context.WeightTransactions.Select(transaction => transaction.Id).First();

            // Act
            var result = await service.RemoveAsync(id, CancellationToken.None).ConfigureAwait(true);

            // Assert
            Assert.Equal(id, result);
            Assert.Empty(context.WeightTransactions);
        }
        public async Task ThatAlreadyExistItShouldAddItToTheDatabase()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = new AnimalCrudService(_logger, context, _mockMapper.Object);
            var animal  = new Animal
            {
                Id = 1
            };

            // Act
            await service.AddAsync(animal, CancellationToken.None);

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.AddAsync(animal, CancellationToken.None));

            // Assert
            Assert.NotNull(exception);
            Assert.Equal(1, context.Animals.Count());
        }
        public async Task ForAnAnimalThatDoesNotExistItShouldThrow()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = new WeightTransactionCrudService(_logger, context);

            // Act
            var transaction = new WeightTransaction
            {
                AnimalId        = TestConstants.AnimalId1,
                TransactionDate = TestConstants.DefaultDate,
                Weight          = 100
            };
            var exception = await Assert.ThrowsAsync <TransactionRequiresAnimalException>(async() => await service.AddAsync(transaction, CancellationToken.None));

            // Assert
            Assert.NotNull(exception);
            Assert.Equal(TestConstants.AnimalId1, exception.AnimalId);
            Assert.Equal("The animal for this transaction could not be found.", exception.Message);
        }
        public async Task ItShouldReturnTheTransaction()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = SetUpService(context);
            var model   = context.WeightTransactions.First();

            // Act
            var transaction = await service.GetSingleAsync(model.Id, CancellationToken.None)
                              .ConfigureAwait(true);

            // Assert
            Assert.NotNull(transaction);
            if (transaction != null)
            {
                Assert.Equal(model.Id, transaction.Id);
                Assert.Equal(model.AnimalId, transaction.AnimalId);
                Assert.Equal(model.TransactionDate, transaction.TransactionDate);
                Assert.Equal(model.Weight, transaction.Weight);
            }
        }
        public async Task ThatDoesNotExistItShouldThrow()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);
            var service = SetUpService(context);
            var id      = 999;

            // Act
            var exception = await Assert.ThrowsAsync <EntityNotFoundException <WeightTransaction> >(
                async() =>
            {
                await service.RemoveAsync(id, CancellationToken.None)
                .ConfigureAwait(true);
            })
                            .ConfigureAwait(true);

            // Assert
            Assert.NotNull(exception);
            Assert.Equal($"{nameof(WeightTransaction)} with id {id} not found.", exception.Message);
            Assert.NotEmpty(context.WeightTransactions);
        }
        public async Task ThatDoesNotExistItShouldAddItToTheDatabase()
        {
            // Arrange
            var context = LivestockDbContextFactory.Create(_loggerFactory);

            context.SeedTestAnimal(TestConstants.AnimalId1);
            context.SaveChanges();
            var service = new WeightTransactionCrudService(_logger, context);

            // Act
            var transaction = new WeightTransaction
            {
                AnimalId        = TestConstants.AnimalId1,
                TransactionDate = TestConstants.DefaultDate,
                Weight          = 100
            };
            await service.AddAsync(transaction, CancellationToken.None);

            // Assert
            Assert.Equal(1, context.WeightTransactions.Count());
        }
        public async Task AndAttemptingToChangeTheAnimalItShouldThrow()
        {
            // Arrange
            var context     = LivestockDbContextFactory.Create(_loggerFactory);
            var service     = SetUpService(context);
            var transaction = context.WeightTransactions.First().MapToWeightTransaction();

            transaction.AnimalId = TestConstants.AnimalId2;

            // Act
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(
                async() =>
            {
                await service.UpdateAsync(transaction, CancellationToken.None)
                .ConfigureAwait(true);
            })
                            .ConfigureAwait(true);

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Cannot move a transaction to a different animal.", exception.Message);
        }
        public async Task ThatExistsItShouldSetTheNewValues()
        {
            // Arrange
            var context     = LivestockDbContextFactory.Create(_loggerFactory);
            var service     = SetUpService(context);
            var transaction = context.WeightTransactions.First().MapToWeightTransaction();

            transaction.Weight         += 44;
            transaction.TransactionDate = transaction.TransactionDate.AddDays(3);

            // Act
            var result = await service.UpdateAsync(transaction, CancellationToken.None).ConfigureAwait(true);

            var model = context.WeightTransactions.Find(transaction.Id);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(transaction.Weight, model.Weight);
            Assert.Equal(transaction.Weight, result.Weight);
            Assert.Equal(transaction.TransactionDate, model.TransactionDate);
            Assert.Equal(transaction.TransactionDate, result.TransactionDate);
        }