public async Task GetIncompleteItemsAsyncForGivenDateShouldReturnElementsCorrectly()
        {
            var options  = new DbContextOptionsBuilder <AspNetCoreTodo.Data.ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem9").Options;
            var fakeUser = CreateFakeUsers(0);

            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);
                int counter = 4;
                while (counter > 0)
                {
                    if (counter % 2 == 0)
                    {
                        await service.AddItemAsync(new TodoItem { Title   = "Tarea" + counter,
                                                                  Address = "Calle Sarasa 202" + counter + " CABA",
                                                                  DueAt   = DateTime.Now.AddDays(counter) }, fakeUser);
                    }
                    else
                    {
                        await service.AddItemAsync(new TodoItem { Title   = "Tarea" + counter,
                                                                  Address = "Calle Sarasa 202" + counter + " CABA",
                                                                  DueAt   = DateTime.Now.AddHours(counter) }, fakeUser);
                    }
                    counter--;
                }
            }
            using (var context = new ApplicationDbContext(options))
            {
                var        service  = new TodoItemService(context);
                TodoItem[] todoItem = await service.GetIncompleteItemsWithDateAsync(fakeUser, DateTime.Now);

                Assert.True(todoItem.Length == 2);
            }
        }
Пример #2
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);
            }
        }
Пример #3
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 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);
            }
        }
Пример #5
0
        public async Task AddNewItemAsIncompleteWithDueDate()
        {
            //To Configure in memory DB
            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);
                var fakeUser = CreateFakeUser();
                await service.AddItemAsync(new TodoItem
                {
                    Title = "Testing?"
                }, fakeUser);
            }
            // Use a separate context to read data back from the "DB"
            using (var context = new ApplicationDbContext(options))
            {
                var itemsInDatabase = await context
                                      .Items.CountAsync();

                Assert.Equal(1, itemsInDatabase);
                var item = await context.Items.FirstAsync();

                Assert.Equal("Testing?", item.Title);
                Assert.False(item.IsDone);
                // Item should be due 3 days from now (give or take a second)
                var difference = DateTimeOffset.Now.AddDays(3) - item.DueAt;
                Assert.True(difference < TimeSpan.FromSeconds(1));
            }
        }
        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));
            }
        }
Пример #7
0
        public async Task MarkDoneAsync_ReturnsFalse_IfInputIncorrectItemId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_MarkDoneIncorrectId").Options;

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

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

                var incorrectId = Guid.NewGuid();

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

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

                Assert.False(result);
            }
        }
        public async Task MarkDoneWrongId()
        {
            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 = "******"
                };
                var SecondFakeUser = new ApplicationUser
                {
                    Id       = "fake-001",
                    UserName = "******"
                };

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

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

                Assert.False(await service.MarkDoneAsync(item.Id, SecondFakeUser));
            }
        }
Пример #9
0
        public async Task MarkDoneAsyncReturnsTrueForValidId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_MarkDone2")
                          .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);

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

                var success = await service.MarkDoneAsync(item.Id, fakeUser);

                Console.WriteLine(item.Title);
                Assert.True(success);
            }
        }
Пример #10
0
        public async Task AddNewItemAsIncompleteWithoutDueDate()
        {
            // Given
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            using (var context = new ApplicationDbContext(options)) {
                var service  = new TodoItemService(context);
                var fakeUser = CreateFakeUser();
                var todoItem = CreateTodoItem(userId: fakeUser.Id);

                // When
                await service.AddItemAsync(todoItem, fakeUser);
            }

            // Then
            using (var context = new ApplicationDbContext(options)) {
                var itemsInDatabase = await context.Items.CountAsync();

                Assert.Equal(1, itemsInDatabase);

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

                Assert.Equal(_DefaultItemTitle, item.Title);
                Assert.False(item.IsDone);

                var difference = DateTimeOffset.Now.AddDays(3) - item.DueAt;
                Assert.True(difference < TimeSpan.FromSeconds(1), $"La diferencia fue de {difference} segundos.");
            }

            ClearDataBase(options);
        }
Пример #11
0
        public async Task AddNewItemAsIncompleteWithDueDate()
        {
            // Given
            var options        = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;
            var dateTimeOffset = DateTimeOffset.Now;

            using (var context = new ApplicationDbContext(options))
            {
                var             service  = new TodoItemService(context);
                ApplicationUser fakeUser = CreateFakeUser();
                TodoItem        todoItem = CreateTodoItem(userId: fakeUser.Id, dateTimeOffset);

                // When
                await service.AddItemAsync(todoItem, fakeUser);
            }

            // Then
            using (var context = new ApplicationDbContext(options)) {
                var itemsInDatabase = await context.Items.CountAsync();

                Assert.Equal(1, itemsInDatabase);

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

                Assert.Equal(_DefaultItemTitle, item.Title);
                Assert.False(item.IsDone);
                Assert.Equal(dateTimeOffset, item.DueAt);
            }

            ClearDataBase(options);
        }
Пример #12
0
        public async Task MarkDoneAsyncMethodWithValidId()
        {
            // Given
            var options        = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;
            var dateTimeOffset = DateTimeOffset.Now;

            using (var context = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(context);
                var fakeUser = CreateFakeUser();
                var todoItem = CreateTodoItem(userId: fakeUser.Id, dateTimeOffset);

                await service.AddItemAsync(todoItem, fakeUser);
            }

            using (var context = new ApplicationDbContext(options)) {
                var service         = new TodoItemService(context);
                var fakeUser        = CreateFakeUser();
                var itemsInDatabase = await context.Items.CountAsync();

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

                bool result = await service.MarkDoneAsync(item.Id, fakeUser);

                Assert.True(result);
            }

            ClearDataBase(options);
        }
Пример #13
0
        public async Task MarkDone()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_MarkDone").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);

                var item = await inMemoryContext.Items.FirstOrDefaultAsync();

                await service.MarkDoneAsync(item.Id);
            }

            // Assertions
            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                Assert.Equal(1, await inMemoryContext.Items.CountAsync());

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

                Assert.Equal("Testing?", item.Title);
                Assert.True(item.IsDone);
                Assert.True(DateTimeOffset.Now.AddDays(3) - item.DueAt < TimeSpan.FromSeconds(1));
            }
        }
        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 AddNewItemAsIncompleteWithDueDate()
        {
            var options = new DbContextOptionsBuilder <AspNetCoreTodo.Data.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);
                var fakeUser = CreateFakeUsers(0);
                await service.AddItemAsync(CreateFakeItem("Testing?", "Sarandi 2020"), fakeUser);
            }

            using (var context = new ApplicationDbContext(options))
            {
                var itemsInDatabase = await context.Items.CountAsync();

                Assert.Equal(1, itemsInDatabase);

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

                Assert.Equal("Testing?", item.Title);
                Assert.False(item.IsDone);

                var difference = DateTimeOffset.Now.AddDays(1) - item.DueAt;
                Assert.True(difference < TimeSpan.FromSeconds(1));
            }
        }
        public async Task MarksDoneTrue()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_MarksDoneTrue").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))
            {
                Assert.Equal(1, await inMemoryContext.Items.CountAsync());

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

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

                var resultado = await service.MarkDoneAsync(item.Id, fakeUser);

                Assert.True(resultado);
            }
        }
        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);
            }
        }
        public async Task AddNewItemAsIncompleteWithDueDate()
        {
            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 = "Automated test"
                }, fakeUser);
            }
            using (var context = new ApplicationDbContext(options))
            {
                var itemsInDB = await context.Items.CountAsync();

                Assert.Equal(1, itemsInDB);
                var item = await context.Items.FirstAsync();

                Assert.Equal("Automated test", item.Title);
                Assert.Equal(false, item.IsDone);
                var difference = DateTimeOffset.Now.AddDays(3) - item.DueAt;
                Assert.True(difference < TimeSpan.FromSeconds(1));
            }
        }
Пример #19
0
        public async Task AddNewItemAsync()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem")
                          .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);
            }

            using (var inMemoryContext = new ApplicationDbContext(options))
            {
                Assert.Equal(1, await inMemoryContext.Items.CountAsync());

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

                Assert.Equal("Testing?", item.Title);
                Assert.Equal(false, item.IsDone);
                ouput.WriteLine($"Due at: {item.DueAt}");
                Assert.Null(item.DueAt);
                // Assert.True(DateTimeOffset.Now.AddDays(3) - item.DueAt < TimeSpan.FromSeconds(1));
            }
        }
        public async Task AddNewItemAsIncompleteWithDueDate()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem")
                          .Options;

            using (var context = new ApplicationDbContext(options))
            {
                var service  = new TodoItemService(context);
                var fakeUser = new IdentityUser
                {
                    Id       = "fake-000",
                    UserName = "******"
                };
                await service.AddItemAsync(new TodoItem
                {
                    Title = "Testing?"
                }, fakeUser);
            }
            using (var context = new ApplicationDbContext(options))
            {
                var itemsInDatabase = await context
                                      .Items.CountAsync();

                Assert.Equal(1, itemsInDatabase);
                var item = await context.Items.FirstAsync();

                Assert.Equal("Testing?", item.Title);
                Assert.Equal(false, item.IsDone);

                var difference = DateTimeOffset.Now.AddDays(3) - item.DueAt;
                Assert.True(difference < TimeSpan.FromSeconds(1));
            }
        }
        public async Task AddNewItemAsIncompleteWithDueDate()
        {
            var options = new DbContextOptionsBuilder <AspNetCoreTodo.Data.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);
                var fakeUser = new ApplicationUser
                {
                    Id       = "fake-000",
                    UserName = "******"
                };

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

            using (var context = new ApplicationDbContext(options))
            {
                var itemsInDatabase = await context.Items.CountAsync();

                Assert.Equal(1, itemsInDatabase);

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

                Assert.Equal("Testing?", item.Title);
                Assert.Equal(false, item.IsDone);

                // Item should be due 3 days from now (give or take a second)
                var difference = DateTimeOffset.Now.AddDays(3) - item.DueAt;
                Assert.True(difference < TimeSpan.FromSeconds(1));
            }
        }
Пример #22
0
        public async Task MarkDoneAsyncReturnsTrueWhenItemIsComplete()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_MarkDoneReturnTrue").Options;

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

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

                var itemInDatabase = await context.Items.SingleOrDefaultAsync();

                var result = await service.MarkDoneAsync(itemInDatabase.Id, fakeUser);

                Assert.True(result);
            }
        }
Пример #23
0
        public async Task AddNewItem()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

            // Set up a context (connection to the DB) for writing
            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))
            {
                Assert.AreEqual(1, await inMemoryContext.Items.CountAsync());

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

                Assert.AreEqual("Testing?", item.Title);
                Assert.AreEqual(false, item.IsDone);
            }
        }
        public async Task AddNewItemAsIncompleteWithDueDate()
        {
            //Arrange
            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

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

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

                //Act
                await service.AddItemAsync(fakeItem, fakeUser);

                //Assert
                Assert.Equal(1, await context.Items.CountAsync());

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

                Assert.Equal("Testing?", item.Title);
                Assert.Equal(false, item.IsDone);
                Assert.True(DateTimeOffset.Now.AddDays(3) - item.DueAt > TimeSpan.FromSeconds(1));
            }
        }
        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);
            }
        }
        public async Task ReturnTrueItemDoesntexist_RTrue()
        {
            //Arrange
            using (var context = new ApplicationDbContext(options))
            {
                var service = new TodoItemService(context);

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

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

                await service.AddItemAsync(fakeItem, fakeUser);

                //Act
                var result = await service.MarkDoneAsync(fakeItem.Id, fakeUser);

                //Assert
                Assert.Equal(true, result);
            }
        }
Пример #27
0
        public async Task MarkItemAsCompleteIfValid()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_MarkItemAsComplete").Options;

            await 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 itemsInDatabase = await context
                                  .Items.CountAsync();

            Assert.Equal(1, itemsInDatabase);
            var item = await context.Items.FirstAsync();

            var result = service.MarkDoneAsync(item.Id, fakeUser);

            Assert.True(result.Result);
        }
Пример #28
0
        public async Task MarkDone()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

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

            Guid realitem_id;
            Guid fakeitem_id = Guid.NewGuid();

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

                await services.AddItemAsync(new NewTodoItem { Title = "Testing?", DueAt = DateTimeOffset.Now.AddDays(3) }, fakeUser);

                realitem_id = inMemoryContext.Items.FirstAsync().Result.Id;
            }

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

                var fakeresult = services.MarkDoneAsync(fakeitem_id, fakeUser);

                Assert.False(fakeresult.Result);

                var realresult = services.MarkDoneAsync(realitem_id, fakeUser);

                Assert.True(realresult.Result);
            }
        }
Пример #29
0
        public async Task AddNewItem()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options;

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

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

                await service.AddItemAsync(new TodoItem { Title = "Testing?", DueAt = DateTimeOffset.Now.AddDays(3) }, fakeUser);
            }

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

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

                Assert.Equal("fake-000", item.UserId);
                Assert.Equal("Testing?", item.Title);
                Assert.Equal(false, item.IsDone);
                Assert.True(DateTimeOffset.Now.AddDays(3) - item.DueAt < TimeSpan.FromSeconds(1));
            }
        }
        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);
                }
            }
        }