Skip to content

Commit 51af0ed

Browse files
committed
getbugsquery tests
1 parent 65dbad3 commit 51af0ed

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

BugLab.Business/QueryHandlers/Bugs/GetBugsHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<PagedList<BugResponse>> Handle(GetBugsQuery request, Cancellat
2727

2828
query = request.ProjectId.HasValue
2929
? query.Where(b => b.ProjectId == request.ProjectId)
30-
: query.Where(b => b.CreatedById == request.UserId);
30+
: query.Where(b => b.CreatedById == request.UserId || b.AssignedToId == request.UserId);
3131

3232
if (!string.IsNullOrWhiteSpace(request.Title)) query = query.Where(b => b.Title.Contains(request.Title));
3333

BugLab.Tests/Business/QueryHandlers/GetBugsHandlerTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BugLab.Business.Queries.Bugs;
22
using BugLab.Data.Entities;
3+
using BugLab.Shared.Responses;
34
using BugLab.Tests.Helpers;
45
using FluentAssertions;
56
using Microsoft.EntityFrameworkCore;
@@ -14,17 +15,51 @@ public class GetBugsHandlerTests
1415
{
1516
private GetBugsHandler _sut;
1617

18+
private bool AssignedToOrCreatedBy(BugResponse bug, string userId)
19+
{
20+
if (bug.CreatedBy.Id == userId) return true;
21+
22+
var assignedToId = bug.AssignedTo?.Id;
23+
return assignedToId != null && assignedToId == userId;
24+
}
25+
1726
[Fact]
1827
public async Task GetBugs_ShouldNot_ReturnDeletedBugs()
1928
{
2029
using var context = await DbContextHelpers.CreateAsync();
2130
_sut = new(context);
2231

23-
var deletedBug = context.Bugs.IgnoreQueryFilters().FirstOrDefault(x => x.Deleted != null);
32+
var deletedBug = context.Bugs.IgnoreQueryFilters().FirstOrDefault(x => x.Deleted.HasValue);
2433
var bugs = await _sut.Handle(new GetBugsQuery(DbContextHelpers.CurrentUserId), default);
2534

2635
bugs.Should().NotBeNullOrEmpty();
2736
bugs.Should().NotContain(x => x.Title == deletedBug.Title);
2837
}
38+
39+
[Fact]
40+
public async Task GetBugs_WithUserId_ReturnsUsersBugs()
41+
{
42+
using var context = await DbContextHelpers.CreateAsync();
43+
_sut = new(context);
44+
45+
var bugs = await _sut.Handle(new GetBugsQuery(DbContextHelpers.CurrentUserId), default);
46+
47+
bugs.Should().NotBeNullOrEmpty();
48+
bugs.Should().Match(b => b.All(b => AssignedToOrCreatedBy(b, DbContextHelpers.CurrentUserId)));
49+
}
50+
51+
[Fact]
52+
public async Task GetBugs_WithProjectId_ReturnsBugsInProject()
53+
{
54+
using var context = await DbContextHelpers.CreateAsync();
55+
_sut = new(context);
56+
57+
var request = new GetBugsQuery(DbContextHelpers.CurrentUserId);
58+
request.ProjectId = context.ProjectUsers.AsNoTracking().First(x => x.UserId == DbContextHelpers.CurrentUserId).ProjectId;
59+
var bugs = await _sut.Handle(request, default);
60+
61+
bugs.Should().NotBeNullOrEmpty();
62+
bugs.Should().Match(b => b.All(b => b.ProjectId == request.ProjectId));
63+
}
2964
}
3065
}

BugLab.Tests/Helpers/DbContextHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public static async Task<AppDbContext> CreateAsync(string currentUserId = "757b2
2222

2323
var context = new AppDbContext(options, mockHttpAccessor.Object);
2424
context.Database.EnsureCreated();
25-
2625
context.SeedBugs();
2726

2827
await context.SaveChangesAsync();
2928

29+
context.ChangeTracker.Clear();
3030
return context;
3131
}
3232
}

0 commit comments

Comments
 (0)