forked from stormideas/storm-tech-test-netcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stormideas#6 On /TodoList show also TodoLists containing at least one…
… Item that user is responsible for
- Loading branch information
1 parent
5aae01c
commit 0a55739
Showing
6 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Todo.Tests/WhenFilteringTodoListByIsOwnedByUserOrUserIsResponsibleForAtLeastOneItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.EntityFrameworkCore; | ||
using Moq; | ||
using Todo.Data; | ||
using Todo.Data.Entities; | ||
using Todo.Services; | ||
using Todo.Tests.Builders; | ||
using Xunit; | ||
|
||
namespace Todo.Tests | ||
{ | ||
public class WhenFilteringTodoListByIsOwnedByUserOrUserIsResponsibleForAtLeastOneItem | ||
{ | ||
private static readonly IdentityUser TestUser = new("[email protected]"); | ||
private static readonly IdentityUser AnotherUser = new("[email protected]"); | ||
private static readonly TodoList TodoListOwnedByTestUser = new TestTodoListBuilder(TestUser, "shopping") | ||
.WithItem("bread", Importance.High) | ||
.WithItem("milk", Importance.Medium) | ||
.Build(); | ||
private static readonly TodoList TodoListOwnedByAnotherUser = new TestTodoListBuilder(AnotherUser, "shopping") | ||
.WithItem("bread", Importance.High) | ||
.WithItem("milk", Importance.Medium) | ||
.Build(); | ||
private static readonly TodoList TodoListOwnedByAnotherUserButWithItemThatTestUserIsResponsibleFor = new TestTodoListBuilder(AnotherUser, "shopping") | ||
.WithItem("bread", Importance.High) | ||
.WithItem("milk", Importance.Medium) | ||
.WithItem("milk", Importance.Low, TestUser.Id) | ||
.Build(); | ||
private readonly IApplicationDbContext dbContext; | ||
|
||
public WhenFilteringTodoListByIsOwnedByUserOrUserIsResponsibleForAtLeastOneItem() | ||
{ | ||
var data = new List<TodoList> | ||
{ | ||
TodoListOwnedByTestUser, TodoListOwnedByAnotherUser, | ||
TodoListOwnedByAnotherUserButWithItemThatTestUserIsResponsibleFor | ||
}.AsQueryable(); | ||
|
||
var mockSet = new Mock<DbSet<TodoList>>(); | ||
mockSet.As<IQueryable<TodoList>>().Setup(m => m.Provider).Returns(data.Provider); | ||
mockSet.As<IQueryable<TodoList>>().Setup(m => m.Expression).Returns(data.Expression); | ||
mockSet.As<IQueryable<TodoList>>().Setup(m => m.ElementType).Returns(data.ElementType); | ||
mockSet.As<IQueryable<TodoList>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); | ||
|
||
var mockContext = new Mock<IApplicationDbContext>(); | ||
mockContext.Setup(c => c.TodoLists).Returns(mockSet.Object); | ||
|
||
dbContext = mockContext.Object; | ||
} | ||
|
||
[Fact] | ||
public void FilteredCollectionContainsOnlyTodoListsThatUserIsResponsibleFor() | ||
{ | ||
var collectionOfTodoListsFiltered = dbContext.RelevantTodoLists(TestUser.Id); | ||
|
||
Assert.Equal(new List<TodoList>{TodoListOwnedByTestUser, | ||
TodoListOwnedByAnotherUserButWithItemThatTestUserIsResponsibleFor}, collectionOfTodoListsFiltered); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Todo.Data.Entities; | ||
|
||
namespace Todo.Data | ||
{ | ||
public interface IApplicationDbContext | ||
{ | ||
DbSet<TodoList> TodoLists { get; set; } | ||
DbSet<TodoItem> TodoItems { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters