public async Task DeleteAsync_UsingNonexistentEntityKey_MustThrowException()
        {
            // Arrange
            var inMemoryDatabase =
                new DbContextOptionsBuilder <TodoDbContext>()
                .UseInMemoryDatabase($"db--{Guid.NewGuid():N}")
                .EnableDetailedErrors()
                .EnableSensitiveDataLogging();

            var mockLogger = new Mock <ILogger <TodoItemService> >();

            var owner = new Mock <IPrincipal>();

            owner.SetupGet(x => x.Identity).Returns(new GenericIdentity("test"));

            var deleteTodoItemInfo = new DeleteTodoItemInfo
            {
                Id    = long.MaxValue,
                Owner = owner.Object
            };

            var todoItemService = new TodoItemService(new TodoDbContext(inMemoryDatabase.Options), mockLogger.Object);

            // Act
            Func <Task> deleteAsyncCall = async() => await todoItemService.DeleteAsync(deleteTodoItemInfo);

            // Assert
            await deleteAsyncCall.Should().ThrowExactlyAsync <EntityNotFoundException>(
                "service cannot delete data using nonexistent entity key");
        }
        public async Task GetByQueryAsync_UsingValidQuery_MustSucceed(TodoItemQuery todoItemQuery)
        {
            // Arrange
            var inMemoryDatabase =
                new DbContextOptionsBuilder <TodoDbContext>()
                .UseInMemoryDatabase($"db--{Guid.NewGuid():N}")
                .EnableDetailedErrors()
                .EnableSensitiveDataLogging();

            var mockLogger = new Mock <ILogger <TodoItemService> >();

            var owner = new Mock <IPrincipal>();

            owner.SetupGet(x => x.Identity).Returns(new GenericIdentity("test"));

            todoItemQuery.Owner = owner.Object;

            var todoItemService = new TodoItemService(new TodoDbContext(inMemoryDatabase.Options), mockLogger.Object);

            // Act
            Func <Task <IList <TodoItemInfo> > > getByQueryAsyncCall =
                async() => await todoItemService.GetByQueryAsync(todoItemQuery);

            // Assert
            await getByQueryAsyncCall.Should().NotThrowAsync("query is valid");
        }
        public async Task UpdateAsync_UsingNullAsUpdateTodoItemInfo_MustThrowException()
        {
            // Arrange
            var mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions);
            var mockLogger        = new Mock <ILogger <TodoItemService> >();
            var todoItemService   = new TodoItemService(mockTodoDbContext.Object, mockLogger.Object);
            UpdateTodoItemInfo updateTodoItemInfo = null;

            // Act
            // ReSharper disable once ExpressionIsAlwaysNull
            Func <Task> updateAsyncCall = async() => await todoItemService.UpdateAsync(updateTodoItemInfo);

            // Assert
            await updateAsyncCall.Should().ThrowExactlyAsync <ArgumentNullException>(
                "service cannot update data using a null item");
        }
        public async Task DeleteAsync_UsingNullAsDeleteTodoItemInfo_MustThrowException()
        {
            // Arrange
            var mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions);
            var mockLogger        = new Mock <ILogger <TodoItemService> >();
            var todoItemService   = new TodoItemService(mockTodoDbContext.Object, mockLogger.Object);
            DeleteTodoItemInfo deleteTodoItemInfo = null;

            // Act
            // ReSharper disable once ExpressionIsAlwaysNull
            Func <Task> deleteAsyncCall = async() => await todoItemService.DeleteAsync(deleteTodoItemInfo);

            // Assert
            await deleteAsyncCall
            .Should().ThrowExactlyAsync <ArgumentNullException>("service cannot delete data using a null item")
            .WithParameterName(nameof(deleteTodoItemInfo), "the item is null");
        }
        public async Task GetByQueryAsync_UsingNullAsQuery_MustThrowException()
        {
            // Arrange
            var           mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions);
            var           mockLogger        = new Mock <ILogger <TodoItemService> >();
            var           todoItemService   = new TodoItemService(mockTodoDbContext.Object, mockLogger.Object);
            TodoItemQuery todoItemQuery     = null;

            // Act
            // ReSharper disable once ExpressionIsAlwaysNull
            Func <Task <IList <TodoItemInfo> > > getByQueryAsyncCall =
                async() => await todoItemService.GetByQueryAsync(todoItemQuery);

            // Assert
            await getByQueryAsyncCall.Should().ThrowExactlyAsync <ArgumentNullException>(
                "service cannot fetch data using a null query");
        }