Ejemplo n.º 1
0
        public async Task  GetIncompleteItemsAsyncReturnsOnlyItemsOwnedByUser()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_GetIncompleteItems")
                          .Options;
            var fakeUser = new ApplicationUser
            {
                Id       = "fake-000",
                UserName = "******"
            };
            var fakeUser2 = new ApplicationUser
            {
                Id       = "fake2-001",
                UserName = "******"
            };

            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(inMemoryContext);


                await service.AddItemAsync(new NewTodoItem { Title = "Item 1" }, fakeUser);

                await service.AddItemAsync(new NewTodoItem { Title = "Item 2" }, fakeUser);

                await service.AddItemAsync(new NewTodoItem { Title = "Item 3" }, fakeUser2);

                await service.AddItemAsync(new NewTodoItem { Title = "Item 4" }, fakeUser2);
            }
            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                Assert.Equal(4, await inMemoryContext.Items.CountAsync());
                var service = new TodoItemService(inMemoryContext);
                var items   = await service.GetIncompleteItemsAsync(fakeUser);

                var itemsArray = items.ToArray();
                Assert.Equal(2, items.Count());
                Assert.Equal("Item 1", itemsArray[0].Title);
                Assert.Equal("Item 2", itemsArray[1].Title);

                var items2 = await service.GetIncompleteItemsAsync(fakeUser2);

                var itemsArray2 = items2.ToArray();
                Assert.Equal(2, items2.Count());
                Assert.Equal("Item 3", itemsArray2[0].Title);
                Assert.Equal("Item 4", itemsArray2[1].Title);
            }
        }
        public async Task GetIncompleteItemsAsyncSortByAddressWithDescOrderShouldReturnElementsCorrectly()
        {
            var options  = new DbContextOptionsBuilder <AspNetCoreTodo.Data.ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem8").Options;
            var fakeUser = CreateFakeUsers(0);

            using (var context = new ApplicationDbContext(options))
            {
                var       service = new TodoItemService(context);
                string [] titles  = new string[] { "Sarasa", "Carnasa", "Mamasa", "Fafasa", "Papasa", "Zarnasa", "Arnasa", "01asa", "Babasa", "4asa" };
                Random    rnd     = new Random();
                foreach (var item in titles)
                {
                    await service.AddItemAsync(new TodoItem { Title = item, Address = "Calle " + item, DueAt = DateTime.Today.AddDays(rnd.Next(-1000, 1000)) }, fakeUser);
                }
            }
            using (var context = new ApplicationDbContext(options))
            {
                var        service  = new TodoItemService(context);
                TodoItem[] todoItem = await service.GetIncompleteItemsAsync(fakeUser, TodoQueries.IncompleteByAddressDesc);

                var        sortedByAddress = from items in todoItem orderby items.Address descending select items;
                TodoItem[] sortedList      = sortedByAddress.ToArray();

                for (int i = 0; i < todoItem.Length; i++)
                {
                    if (sortedList[i].Address != todoItem[i].Address)
                    {
                        Assert.True(false);
                    }
                }
                Assert.True(true);
            }
        }
        public async Task GetIncompleteItemsAsyncSortByTitleWithDescOrderShouldReturnElementsCorrectly()
        {
            var options  = new DbContextOptionsBuilder <AspNetCoreTodo.Data.ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem4").Options;
            var fakeUser = CreateFakeUsers(0);

            string [] titles = new string[] { "Sarasa", "Carnasa", "Mamasa", "Fafasa", "Papasa", "Zarnasa", "Arnasa", "01asa", "Babasa", "4asa" };
            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);
                foreach (var item in titles)
                {
                    await service.AddItemAsync(CreateFakeItem(item, "Calle " + item), fakeUser);
                }
            }
            using (var context = new ApplicationDbContext(options))
            {
                var        service  = new TodoItemService(context);
                TodoItem[] todoItem = await service.GetIncompleteItemsAsync(fakeUser, TodoQueries.IncompleteByTitleDesc);

                Array.Sort(titles);
                Array.Reverse(titles);
                for (int i = 0; i < titles.Length; i++)
                {
                    if (titles[i] != todoItem[i].Title)
                    {
                        Assert.True(false);
                    }
                }
                Assert.True(true);
            }
        }
        public async Task AddNewItemWithoutAddressShouldFail()
        {
            var options  = new DbContextOptionsBuilder <AspNetCoreTodo.Data.ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem2").Options;
            var fakeUser = CreateFakeUsers(0);

            try
            {
                using (var context = new ApplicationDbContext(options))
                {
                    var service = new TodoItemService(context);
                    await service.AddItemAsync(CreateFakeItem("fake", ""), fakeUser);
                }
                throw new Exception();
            }
            catch (NoAddressException)
            {
                using (var context = new ApplicationDbContext(options))
                {
                    var        service  = new TodoItemService(context);
                    TodoItem[] todoItem = await service.GetIncompleteItemsAsync(fakeUser, TodoQueries.IncompleteByTitleAsc);

                    Assert.Empty(todoItem);
                }
            }
        }
        public async Task GetIncompleteItemsAsync_ItemsAreReturned()
        {
            using (var context = new ApplicationDbContext(Utilities.TestDbContextOptions()))
            {
                // Arrange
                var categories        = Categories;
                var expectedTodoItems = Items;
                var service           = new TodoItemService(context);

                await context.Categories.AddRangeAsync(categories);

                await context.Items.AddRangeAsync(expectedTodoItems);

                await context.SaveChangesAsync();

                // Act
                var result = await service.GetIncompleteItemsAsync(FakeUser);

                // Assert
                var actualTodoItems = Assert.IsAssignableFrom <TodoItem[]>(result);
                Assert.Equal(
                    expectedTodoItems.OrderBy(m => m.Id).Select(m => m.Title),
                    actualTodoItems.OrderBy(m => m.Id).Select(m => m.Title));
            }
        }
Ejemplo n.º 6
0
        public async Task GetIncompleteItemsAsyncOwnedByUser()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_IncompleteItemByUser").Options;


            using (var context = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(context);
                var fakeUser = new ApplicationUser
                {
                    Id       = "fake-003",
                    UserName = "******"
                };

                var itemOne = await service.AddItemAsync(new TodoItem
                {
                    Title = "Testing?"
                }, fakeUser);

                var itemTwo = await service.AddItemAsync(new TodoItem
                {
                    Title = "Tested?"
                }, fakeUser);

                var items = await service.GetIncompleteItemsAsync(fakeUser);

                Assert.Collection(items, item => Assert.Contains("Testing?", item.Title),
                                  item => Assert.Contains("Tested?", item.Title));

                Assert.Contains("Testing?", items[0].Title);
                Assert.Contains("Tested?", items[1].Title);
            }
        }
Ejemplo n.º 7
0
        public async Task MarkDoneTestingWithIncorrectParameter()
        {
            DbContextOptions <ApplicationDbContext> options = new DbContextOptionsBuilder <ApplicationDbContext>()
                                                              .UseInMemoryDatabase(databaseName: "Test_MarkDone").Options;

            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

                var fakeUser = new ApplicationUser {
                    Id = "fake-1"
                };

                await service.AddItemAsync(new TodoItem { Title = "DoneTest" }, fakeUser);

                List <TodoItem> tasks = await service.GetIncompleteItemsAsync(fakeUser);

                Assert.Single(tasks);

                Assert.False(await service.MarkDoneAsync(Guid.NewGuid(), fakeUser));
                Assert.False(await service.MarkDoneAsync(tasks[0].Id, new ApplicationUser {
                    Id = "fake-2"
                }));
                Assert.True(await service.MarkDoneAsync(tasks[0].Id, fakeUser));
            }
        }
        public async Task ReturnTrueIfValidItemIsPassedToMarkDoneAsync()
        {
            var options = new DbContextOptionsBuilder <TodoListDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            // Set up a context (connection to the "DB") for writing
            using (var context = new TodoListDbContext(options))
            {
                var service = new TodoItemService(context);

                var fakeUser = new IdentityUser
                {
                    Id       = "fake-000",
                    UserName = "******"
                };

                await service.AddItemAsync(new TodoItem
                {
                    Title = "Testing?"
                }, fakeUser);

                var items = await service.GetIncompleteItemsAsync(fakeUser);

                Assert.True(await service.MarkDoneAsync(items[0].Id, fakeUser));
            }
        }
        public async Task GetIncompleteItems()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_GetIncompleteItems").Options;

            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(inMemoryContext);
                var fakeUser = new ApplicationUser {
                    Id = "fake-000", UserName = "******"
                };
                await service.AddItemAsync(new NewTodoItem()
                {
                    Title = "Testing?"
                }, fakeUser);
            }

            // Use a separate context to read the data back from the DB
            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(inMemoryContext);
                var fakeUser = new ApplicationUser {
                    Id = "fake-000", UserName = "******"
                };

                IEnumerable <TodoItem> list = await service.GetIncompleteItemsAsync(fakeUser);

                var result = list.Any(x => x.IsDone == false);

                Assert.True(result);
            }
        }
Ejemplo n.º 10
0
        public async Task ReturnOnlyItemsOwnedByAParticularUser()
        {
            var options = new DbContextOptionsBuilder <TodoListDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            // Set up a context (connection to the "DB") for writing
            using (var context = new TodoListDbContext(options))
            {
                var service = new TodoItemService(context);

                var fakeUser = new IdentityUser
                {
                    Id       = "fake-000",
                    UserName = "******"
                };

                await service.AddItemAsync(new TodoItem
                {
                    Title = "Testing?"
                }, fakeUser);

                var items = await service.GetIncompleteItemsAsync(fakeUser);

                Assert.Equal("Testing?", items[0].Title);
            }
        }
Ejemplo n.º 11
0
        public async Task GetIncompleteItemsAsyncTesting()
        {
            DbContextOptions <ApplicationDbContext> options = new DbContextOptionsBuilder <ApplicationDbContext>()
                                                              .UseInMemoryDatabase(databaseName: "Test_GetIncompleteItem").Options;

            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

                var fakeUser1 = new ApplicationUser {
                    Id = "fake-1"
                };
                var fakeUser2 = new ApplicationUser {
                    Id = "fake-2"
                };

                await service.AddItemAsync(new TodoItem { Title = "DoneTest1" }, fakeUser1);

                await service.AddItemAsync(new TodoItem { Title = "DoneTest2" }, fakeUser1);

                await service.AddItemAsync(new TodoItem { Title = "DoneTest3" }, fakeUser2);

                await service.AddItemAsync(new TodoItem { Title = "DoneTest4" }, fakeUser2);

                await service.AddItemAsync(new TodoItem { Title = "DoneTest5" }, fakeUser2);

                List <TodoItem> tasks1 = await service.GetIncompleteItemsAsync(fakeUser1);

                Assert.Equal(2, tasks1.Count());
                Assert.True(tasks1[0].Title == "DoneTest1" && tasks1[1].Title == "DoneTest2");

                List <TodoItem> tasks2 = await service.GetIncompleteItemsAsync(fakeUser2);

                Assert.Equal(3, tasks2.Count());
                Assert.True(tasks2[0].Title == "DoneTest3" && tasks2[1].Title == "DoneTest4" && tasks2[2].Title == "DoneTest5");
            }
        }
Ejemplo n.º 12
0
        public async Task GetIncompleteItems()
        {
            //To Configure in memory DB
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

                IEnumerable <TodoItem> result = await service.GetIncompleteItemsAsync(CreateFakeUser());

                var item = result.First();
                Assert.Equal(item.UserId, CreateFakeUser().Id);
            }
        }
Ejemplo n.º 13
0
        public async Task GetIncompleteItemsAsync_ReturnsOnlyItemsOwnedByParticularUser()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_GetIncompleteItemsAsync").Options;

            var user1 = new ApplicationUser
            {
                Id       = "fake-003",
                UserName = "******"
            };

            var user2 = new ApplicationUser
            {
                Id       = "fake-004",
                UserName = "******"
            };

            // Set up context (connection to the DB) for writing
            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(inMemoryContext);

                await service.AddItemAsync(new NewTodoItem { Title = "User1-1" }, user1);

                await service.AddItemAsync(new NewTodoItem { Title = "User1-2" }, user1);

                await service.AddItemAsync(new NewTodoItem { Title = "User2-1" }, user2);

                await service.AddItemAsync(new NewTodoItem { Title = "User2-2" }, user2);
            }

            // Use a separate context to read the data back from the DB
            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                Assert.Equal(4, await inMemoryContext.Items.CountAsync());

                var service = new TodoItemService(inMemoryContext);

                var items = await service.GetIncompleteItemsAsync(user1);

                var result = items.OrderBy(i => i.Title).Select(i => i.Title).SequenceEqual(new[] { "User1-1", "User1-2" });

                Assert.True(result);
            }
        }
Ejemplo n.º 14
0
        public async Task MakrDoneAsync()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            // Set up a context (connection to the "DB") for writing
            //using (var context = new ApplicationDbContext(options))
            //{
            //    var service = new TodoItemService(context);
            //}

            using (var context = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(context);
                var fakeUser = new IdentityUser
                {
                    Id       = "fake-001",
                    UserName = "******"
                };
                context.Users.Add(fakeUser);

                var fakeItem = new TodoItem
                {
                    Id     = Guid.NewGuid(),
                    Title  = "fake item",
                    UserId = fakeUser.Id,
                    IsDone = false
                };
                context.Items.Add(fakeItem);
                await context.SaveChangesAsync();

                var currentItems = await context.Items.Where(i => i.UserId == fakeUser.Id).ToArrayAsync();;
                var expectedItem = await service.GetIncompleteItemsAsync(fakeUser);

                Assert.Equal(currentItems, expectedItem);


                var result = await service.MarkDoneAsync(Guid.NewGuid(), fakeUser);

                var result2 = await service.MarkDoneAsync(fakeItem.Id, fakeUser);

                Assert.Equal(false, result);
                Assert.Equal(true, result2);
            }
        }
        public async Task ReturnItemsOwnedByUser()
        {
            //Arrange
            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

                var fakeUser = new ApplicationUser
                {
                    Id       = "fake-003",
                    UserName = "******"
                };

                var fakeItem = new TodoItem
                {
                    Title = "Testing?",
                    DueAt = DateTimeOffset.Now.AddDays(0)
                };

                await service.AddItemAsync(fakeItem, fakeUser);

                var fakeUser2 = new ApplicationUser
                {
                    Id       = "fake-004",
                    UserName = "******"
                };

                var fakeItem2 = new TodoItem
                {
                    Title = "Testing?",
                    DueAt = DateTimeOffset.Now.AddDays(0)
                };

                await service.AddItemAsync(fakeItem2, fakeUser2);

                //Act
                var listaItems = await service.GetIncompleteItemsAsync(fakeUser);

                //Assert
                Assert.Equal(1, listaItems.Length);
            }
        }
        public async Task AddNewItemWithAddressShouldAddItCorrectly()
        {
            var options  = new DbContextOptionsBuilder <AspNetCoreTodo.Data.ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;
            var fakeUser = CreateFakeUsers(0);

            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);
                await service.AddItemAsync(CreateFakeItem("fake", "sarandi 2020"), fakeUser);
            }
            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);
                var item    = await context.Items.FirstAsync();

                TodoItem[] todoItem = await service.GetIncompleteItemsAsync(fakeUser, TodoQueries.IncompleteByTitleAsc);

                Assert.True(todoItem[0].UserId == "fake-0");
            }
        }
        public async Task GetIncompleteItemsAsyncUserCheck()
        {
            var options   = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_GetIncompleteItems").Options;
            var fakeUser1 = new ApplicationUser
            {
                Id       = "fake-000",
                UserName = "******"
            };

            var fakeUser2 = new ApplicationUser
            {
                Id       = "fake-999",
                UserName = "******"
            };

            var itemComplete   = NewTodoItem("Testing", fakeUser1.Id, DateTimeOffset.Now.AddDays(3), true);
            var itemIncomplete = NewTodoItem("Testing", fakeUser1.Id, DateTimeOffset.Now.AddDays(3));
            var itemOtherUser  = NewTodoItem("Testing", fakeUser2.Id, DateTimeOffset.Now.AddDays(3));

            using (var context = new ApplicationDbContext(options))
            {
                context.Items.Add(itemComplete);
                context.Items.Add(itemIncomplete);
                context.Items.Add(itemOtherUser);

                context.SaveChanges();
            }

            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

                var items = await service.GetIncompleteItemsAsync(fakeUser1);

                Assert.Single(items);
                Assert.Equal(itemIncomplete.Id, items[0].Id);
                Assert.False(items[0].IsDone);
            }
            ClearDataBase(options);
        }
        public async Task GetItemIncompleteAsycOfTheCorrectUser()
        {
            var options = new DbContextOptionsBuilder <AspNetCoreTodo.Data.ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            using (var context = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(context);
                var fakeUser = new ApplicationUser
                {
                    Id       = "fake-000",
                    UserName = "******"
                };

                await service.AddItemAsync(new TodoItem { Title = "Testing?" }, fakeUser);

                var item = await context.Items.FirstAsync();

                TodoItem[] todoItem = await service.GetIncompleteItemsAsync(fakeUser);

                Assert.True(todoItem[0].UserId == "fake-000");
            }
        }
        public async Task GetIncompleteItemsAsync_ShouldReturnOneTodoItem()
        {
            var             options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("Test_AddNewItem").Options;
            DateTime        dueAt   = DateTime.Today.AddDays(5);
            ApplicationUser userWithIncompletedItems = CreateFakeUser("fake-000", "*****@*****.**");
            ApplicationUser otherUser = CreateFakeUser("fake-001", "*****@*****.**");

            TodoItem todoItemCompleted     = CreateTodoItem("TodoItemCompleted", dueAt.AddDays(-10), userWithIncompletedItems.Id, true);
            TodoItem todoItemIncompleted   = CreateTodoItem("TodoItemIncompleted", dueAt, userWithIncompletedItems.Id);
            TodoItem todoItemFromOtherUser = CreateTodoItem("TodoItemFromOtherUser", dueAt.AddDays(1), otherUser.Id);

            using (var context = new ApplicationDbContext(options))
            {
                await context.Items.AddAsync(todoItemCompleted);

                await context.Items.AddAsync(todoItemIncompleted);

                await context.Items.AddAsync(todoItemFromOtherUser);

                await context.SaveChangesAsync();
            }


            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);
                var todoItemsIncompletedForUser = await service.GetIncompleteItemsAsync(userWithIncompletedItems);

                Assert.Equal(1, todoItemsIncompletedForUser.Length);
                var todoItem = todoItemsIncompletedForUser[0];
                Assert.Equal(todoItemIncompleted.Title, todoItem.Title);
                Assert.False(todoItem.IsDone);
                Assert.Equal(todoItemIncompleted.DueAt, todoItem.DueAt);
                Assert.Equal(todoItemIncompleted.UserId, todoItem.UserId);
            }

            ClearDataBase(options);
        }
Ejemplo n.º 20
0
        public async Task OnlyReturnItemsOwnedByAParticularUser()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "TestDb").Options;

            using (var context = new ApplicationDbContext(options))
            {
                var service   = new TodoItemService(context);
                var fakeUser1 = new IdentityUser {
                    Id = "fake-001", UserName = "******"
                };
                var fakeUser2 = new IdentityUser {
                    Id = "fake-002", UserName = "******"
                };

                await service.AddItemAsync(new TodoItem { Title = "Testing?" }, fakeUser1);

                await service.AddItemAsync(new TodoItem { Title = "Testing?" }, fakeUser2);

                var items = service.GetIncompleteItemsAsync(fakeUser1).Result;

                Assert.Single(items);
            }
        }