Skip to content

Commit

Permalink
fix(discussions): return Not Found if no discussions found by author
Browse files Browse the repository at this point in the history
  • Loading branch information
undrcrxwn committed Mar 23, 2024
1 parent 98cc142 commit e639b72
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public async Task<IEnumerable<DiscussionDto>> GetAllAsync()
}
""");

var record = await data.SingleAsync();
return record[0].Adapt<IEnumerable<DiscussionDto>>();
var records = await data.ToListAsync();
return records.Select(x => x[0]).Adapt<IEnumerable<DiscussionDto>>();
});
}

Expand All @@ -85,8 +85,8 @@ public async Task<IEnumerable<DiscussionDto>> GetByAuthorAsync(Guid authorId)
}
""", new { authorId = authorId.ToString() });

var record = await data.SingleAsync();
return record[0].Adapt<IEnumerable<DiscussionDto>>();
var records = await data.ToListAsync();
return records.Select(x => x[0]).Adapt<IEnumerable<DiscussionDto>>();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace CrowdParlay.Social.IntegrationTests.Tests;

public class DiscussionsRepositoryTests(WebApplicationContext context) : IClassFixture<WebApplicationContext>
{
private readonly IServiceProvider _services = context.Services;

[Fact(DisplayName = "Get discussions by author")]
public async Task GetDiscussionsByAuthor()
{
// Arrange
await using var scope = _services.CreateAsyncScope();
var authors = scope.ServiceProvider.GetRequiredService<IAuthorRepository>();
var discussions = scope.ServiceProvider.GetRequiredService<IDiscussionRepository>();

var author = await authors.CreateAsync(
id: Guid.NewGuid(),
username: "compartmental",
displayName: "Степной ишак",
avatarUrl: null);

DiscussionDto[] expected =
[
await discussions.CreateAsync(author.Id, "Discussion 1", "bla bla bla"),
await discussions.CreateAsync(author.Id, "Discussion 2", "numa numa e")
];

// Act
var response = await discussions.GetByAuthorAsync(author.Id);

// Assert
response.Should().BeEquivalentTo(expected);
}

[Fact(DisplayName = "Get discussions by author of no discussions")]
public async Task GetNoDiscussionsByAuthor()
{
// Arrange
await using var scope = _services.CreateAsyncScope();
var authors = scope.ServiceProvider.GetRequiredService<IAuthorRepository>();
var discussions = scope.ServiceProvider.GetRequiredService<IDiscussionRepository>();

var author = await authors.CreateAsync(
id: Guid.NewGuid(),
username: "compartmental",
displayName: "Степной ишак",
avatarUrl: null);

// Act
var response = await discussions.GetByAuthorAsync(author.Id);

// Assert
response.Should().BeEmpty();
}
}

0 comments on commit e639b72

Please sign in to comment.