Skip to content

Commit

Permalink
stormideas#6 On /TodoList show also TodoLists containing at least one…
Browse files Browse the repository at this point in the history
… Item that user is responsible for
  • Loading branch information
pdmichalik committed Dec 8, 2021
1 parent 5aae01c commit 0a55739
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
12 changes: 9 additions & 3 deletions Todo.Tests/Builders/TestTodoListBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TestTodoListBuilder
{
private readonly string title;
private readonly IdentityUser owner;
private readonly List<(string, Importance)> items = new List<(string, Importance)>();
private readonly List<(string, Importance, string)> items = new();

public TestTodoListBuilder(IdentityUser owner, string title)
{
Expand All @@ -22,14 +22,20 @@ public TestTodoListBuilder(IdentityUser owner, string title)

public TestTodoListBuilder WithItem(string itemTitle, Importance importance)
{
items.Add((itemTitle, importance));
items.Add((itemTitle, importance, null));
return this;
}

public TestTodoListBuilder WithItem(string itemTitle, Importance importance, string responsiblePartyId)
{
items.Add((itemTitle, importance, responsiblePartyId));
return this;
}

public TodoList Build()
{
var todoList = new TodoList(owner, title);
var todoItems = items.Select(itm => new TodoItem(todoList.TodoListId, owner.Id, itm.Item1, itm.Item2));
var todoItems = items.Select(itm => new TodoItem(todoList.TodoListId, itm.Item3 ?? owner.Id, itm.Item1, itm.Item2));
todoItems.ToList().ForEach(tlItm =>
{
todoList.Items.Add(tlItm);
Expand Down
1 change: 1 addition & 0 deletions Todo.Tests/Todo.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>
Expand Down
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);
}
}
}
3 changes: 1 addition & 2 deletions Todo/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Todo.Data
{
public class ApplicationDbContext : IdentityDbContext
public class ApplicationDbContext : IdentityDbContext, IApplicationDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}


public DbSet<TodoList> TodoLists { get; set; }
public DbSet<TodoItem> TodoItems { get; set; }

Expand Down
11 changes: 11 additions & 0 deletions Todo/Data/IApplicationDbContext.cs
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; }
}
}
9 changes: 5 additions & 4 deletions Todo/Services/ApplicationDbContextConvenience.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ namespace Todo.Services
{
public static class ApplicationDbContextConvenience
{
public static IQueryable<TodoList> RelevantTodoLists(this ApplicationDbContext dbContext, string userId)
public static IQueryable<TodoList> RelevantTodoLists(this IApplicationDbContext dbContext, string userId)
{
return dbContext.TodoLists.Include(tl => tl.Owner)
.Include(tl => tl.Items)
.Where(tl => tl.Owner.Id == userId);
.Where(tl => tl.Owner.Id == userId || tl.Items
.Select(item => item.ResponsiblePartyId).Contains(userId));
}

public static TodoList SingleTodoList(this ApplicationDbContext dbContext, int todoListId)
public static TodoList SingleTodoList(this IApplicationDbContext dbContext, int todoListId)
{
return dbContext.TodoLists.Include(tl => tl.Owner)
.Include(tl => tl.Items)
.ThenInclude(ti => ti.ResponsibleParty)
.Single(tl => tl.TodoListId == todoListId);
}

public static TodoItem SingleTodoItem(this ApplicationDbContext dbContext, int todoItemId)
public static TodoItem SingleTodoItem(this IApplicationDbContext dbContext, int todoItemId)
{
return dbContext.TodoItems.Include(ti => ti.TodoList).Single(ti => ti.TodoItemId == todoItemId);
}
Expand Down

0 comments on commit 0a55739

Please sign in to comment.