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); }
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 IActionResult CompleteItem(long id) { var service = new TodoItemService(); service.Complete(id); return(RedirectToAction("Index")); }
public TodoItemServiceTests() { _unitOfWorkMock = new Mock <IUnitOfWork>(MockBehavior.Strict); _repositoryMock = new Mock <ITodoItemRepository>(MockBehavior.Strict); _service = new TodoItemService(_unitOfWorkMock.Object, _repositoryMock.Object); }
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 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)); } }
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 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); } }
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); } }
public async Task GetTodoItemAsync_WithValidId_ShouldReturnDTO() { // Arrange var expectedTodoItem = CreateFakeTodoItem(); var options = GetInMemoryOptions(); var mapper = GetMapper(); ClearDataBase(options); using (var context = new ApplicationDbContext(options)) { await context.TodoItems.AddAsync(expectedTodoItem); await context.SaveChangesAsync(); } // Act using (var context = new ApplicationDbContext(options)) { var service = new TodoItemService(context, mapper); var todoItem = await service.GetTodoItemAsync(expectedTodoItem.Id); // Assert Assert.NotNull(todoItem); Assert.Equal(expectedTodoItem.Name, todoItem.Name); Assert.Equal(expectedTodoItem.Description, todoItem.Description); Assert.Equal(expectedTodoItem.IsComplete, todoItem.IsComplete); Assert.Equal(expectedTodoItem.DueAt, todoItem.DueAt); Assert.Equal(expectedTodoItem.Order, todoItem.Order); } ClearDataBase(options); }
public async Task GetTodoItemAsync_WithInvalidIdAndExistingElement_ShouldThrowException(long id) { // Arrange var expectedTodoItem = CreateFakeTodoItem(); var options = GetInMemoryOptions(); var mapper = GetMapper(); ClearDataBase(options); using (var context = new ApplicationDbContext(options)) { await context.TodoItems.AddAsync(expectedTodoItem); await context.SaveChangesAsync(); } using (var context = new ApplicationDbContext(options)) { var service = new TodoItemService(context, mapper); // Act / Assert await Assert.ThrowsAsync <ArgumentException>("id", () => service.GetTodoItemAsync(id)); } ClearDataBase(options); }
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() { 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)); } }
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); }
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); } }
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 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)); } }
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); } }
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 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); } }
public async Task NoMarkDoneAnItemWithWrongId() { await SetUp("fake-000", "*****@*****.**", "Testing?", DateTimeOffset.Now.AddDays(3)); ///Se crea otro contexto, luego se crea otro usuario(con distinto Id), y se verifica: /// 1_ Que solo tenga un item. /// 2_ Que el item almacenado en la base de datos no este done (false) /// 3_ Que falle al marcar como done usando otherFakeUser usando MarkDoneAsync() using (var CDB = new ApplicationDbContext(ODB.Options)) { var service = new TodoItemService(CDB); var otherFakeUser = new ApplicationUser { Id = "fake-000-11", UserName = "******" }; var itemInDateBase = await CDB.Items.CountAsync(); Assert.Equal(1, itemInDateBase); var item = await CDB.Items.FirstAsync(); Assert.False(item.IsDone); bool resoult = await service.MarkDoneAsync(item.Id, otherFakeUser); Assert.False(resoult); Assert.False(item.IsDone); } }
public void Setup() { todoItems = new List <TodoItem>() { new TodoItem { Id = 1, UserName = firstUser, Description = firstDescription, IsCompleted = firstIsCompleted }, new TodoItem { Id = 2, UserName = secondUser, Description = secondDescription, IsCompleted = secondIsCompleted }, new TodoItem { Id = 3, UserName = secondUser, Description = thirdDescription, IsCompleted = thirdIsCompleted }, }; var queryable = todoItems.AsQueryable(); todoItemsMock = new Mock <DbSet <TodoItem> >(); todoItemsMock.As <IQueryable <TodoItem> >().Setup(m => m.Provider).Returns(queryable.Provider); todoItemsMock.As <IQueryable <TodoItem> >().Setup(m => m.Expression).Returns(queryable.Expression); todoItemsMock.As <IQueryable <TodoItem> >().Setup(m => m.ElementType).Returns(queryable.ElementType); todoItemsMock.As <IQueryable <TodoItem> >().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator()); todoItemsMock.Setup(dataset => dataset.Add(It.IsAny <TodoItem>())).Callback <TodoItem>((item) => { todoItems.Add(item); }); todoItemsMock.Setup(dataset => dataset.Remove(It.IsAny <TodoItem>())).Callback <TodoItem>((item) => { todoItems.Remove(item); }); dbContextMock = new Mock <IApplicationDbContext>(); dbContextMock.Setup(mock => mock.TodoItems).Returns(todoItemsMock.Object); todoItemService = new TodoItemService(dbContextMock.Object); }
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 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 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); }
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 IActionResult AddItem(TodoItem todoItem) { var service = new TodoItemService(); service.Add(todoItem); return(RedirectToAction("Index")); }
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 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 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); }