コード例 #1
0
        public async Task ShouldNot_CreateDuplicateTask()
        {
            var items = new Dictionary<string, TaskToDo>()
            {
                { 
                    "task1", new TaskToDo() 
                    {
                        Name = "task1",
                        StatusCode = "NotStarted"
                    } 
                }
            };
            var collection = new InMemoryItemsCollection<TaskToDo>(items);

            var handler = new DefaultCommandHandlers(collection);
            var newTaskCmd = new CreateTaskCommand()
            {
                Name = "task1",
                Priority = 1,
                Statues = "NotStarted"
            };

            var ex = await Assert.ThrowsAsync<AppException>(async () 
                => await handler.Handle(newTaskCmd, CancellationToken.None));
            Assert.Equal("Task with the same name already exists", ex.Message);
        }
コード例 #2
0
        public async Task ShouldNot_Delete_Incomplete_Task()
        {
            var items = new Dictionary <string, TaskToDo>()
            {
                {
                    "task1", new TaskToDo()
                    {
                        Name       = "task1",
                        StatusCode = "NotStarted"
                    }
                }
            };
            var collection = new InMemoryItemsCollection <TaskToDo>(items);

            var handler       = new DefaultCommandHandlers(collection);
            var deleteTaskCmd = new DeleteTaskCommand()
            {
                Id = "task1"
            };

            var ex = await Assert.ThrowsAsync <AppException>(async()
                                                             => await handler.Handle(deleteTaskCmd, CancellationToken.None));

            Assert.Equal("Only 'Completed' task can be deleted", ex.Message);
        }
コード例 #3
0
        public async Task Should_CreateNewTask()
        {
            var items = new Dictionary<string, TaskToDo>();
            var collection = new InMemoryItemsCollection<TaskToDo>(items);

            var handler = new DefaultCommandHandlers(collection);
            var id = await handler.Handle(new CreateTaskCommand()
            {
                Name = "task1",
                Priority = 1,
                Statues = "NotStarted"
            }, CancellationToken.None);

            Assert.True(collection.Items.Any());
        }
コード例 #4
0
        public async Task ShouldNot_CreateTask_Without_Name()
        {
            var items = new Dictionary<string, TaskToDo>();
            var collection = new InMemoryItemsCollection<TaskToDo>(items);

            var handler = new DefaultCommandHandlers(collection);
            var newTaskCmd = new CreateTaskCommand()
            {
                Name = "",
                Statues = "NotStarted"
            };

            var ex = await Assert.ThrowsAsync<AppException>(async ()
                => await handler.Handle(newTaskCmd, CancellationToken.None));
            Assert.Equal("Task name can't be empty", ex.Message);
        }
コード例 #5
0
        public void Should_CreateAllTasks_In_Parallel()
        {
            var items = new Dictionary<string, TaskToDo>();
            var collection = new InMemoryItemsCollection<TaskToDo>(items);

            var handler = new DefaultCommandHandlers(collection);
            var total = 100;

            var result = Parallel.For(1, total + 1, async (i, state) =>
            {
                var newTaskCmd = new CreateTaskCommand()
                {
                    Name = $"task {i}",
                    Statues = "NotStarted"
                };
                await handler.Handle(newTaskCmd, CancellationToken.None);
            });
            
            Assert.Equal(total, collection.Items.Count());
        }
コード例 #6
0
        public async Task Should_DeleteTask()
        {
            var items = new Dictionary <string, TaskToDo>()
            {
                {
                    "task1", new TaskToDo()
                    {
                        Name       = "task1",
                        StatusCode = "Completed"
                    }
                }
            };
            var collection = new InMemoryItemsCollection <TaskToDo>(items);

            var handler = new DefaultCommandHandlers(collection);
            var deleted = await handler.Handle(new DeleteTaskCommand()
            {
                Id = "task1"
            }, CancellationToken.None);

            Assert.True(deleted);
            Assert.False(collection.Items.Any());
        }
コード例 #7
0
 public DefaultQueryHandlers(InMemoryItemsCollection <TaskToDo> collection)
 {
     _collection = collection;
 }