Skip to content

Commit f554214

Browse files
committed
Added Tests for InMemoryRepository
1 parent 4e2f4c1 commit f554214

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using FluentAssertions;
4+
using LinkDotNet.Blog.TestUtilities;
5+
using LinkDotNet.Infrastructure.Persistence.InMemory;
6+
using Xunit;
7+
8+
namespace LinkDotNet.Blog.UnitTests.Infrastructure.Persistence.InMemory
9+
{
10+
public class InMemoryRepositoryTests
11+
{
12+
private readonly InMemoryRepository sut;
13+
14+
public InMemoryRepositoryTests()
15+
{
16+
sut = new InMemoryRepository();
17+
}
18+
19+
[Fact]
20+
public async Task ShouldStoreAndRetrieveBlogPost()
21+
{
22+
var blogPost = new BlogPostBuilder().WithTitle("My Title").Build();
23+
await sut.StoreAsync(blogPost);
24+
25+
var blogPostFromRepo = await sut.GetByIdAsync(blogPost.Id);
26+
27+
blogPostFromRepo.Title.Should().Be("My Title");
28+
}
29+
30+
[Fact]
31+
public async Task ShouldHandleUpdates()
32+
{
33+
var blogPost = new BlogPostBuilder().WithTitle("My Title").Build();
34+
var newerPost = new BlogPostBuilder().WithTitle("My new Title").Build();
35+
await sut.StoreAsync(blogPost);
36+
blogPost.Update(newerPost);
37+
38+
await sut.StoreAsync(blogPost);
39+
40+
var blogPostFromRepository = await sut.GetByIdAsync(blogPost.Id);
41+
blogPostFromRepository.Title.Should().Be("My new Title");
42+
}
43+
44+
[Fact]
45+
public async Task ShouldFilterAndOrder()
46+
{
47+
var olderPost = new BlogPostBuilder().Build();
48+
var newerPost = new BlogPostBuilder().Build();
49+
var filteredOutPost = new BlogPostBuilder().WithTitle("FilterOut").Build();
50+
await sut.StoreAsync(olderPost);
51+
await sut.StoreAsync(newerPost);
52+
await sut.StoreAsync(filteredOutPost);
53+
54+
var blogPosts = await sut.GetAllAsync(
55+
bp => bp.Title != "FilterOut",
56+
bp => bp.UpdatedDate,
57+
false);
58+
59+
var retrievedPosts = blogPosts.ToList();
60+
retrievedPosts.Any(b => b.Id == filteredOutPost.Id).Should().BeFalse();
61+
retrievedPosts[0].Id.Should().Be(olderPost.Id);
62+
retrievedPosts[1].Id.Should().Be(newerPost.Id);
63+
}
64+
}
65+
}

LinkDotNet.Blog.UnitTests/LinkDotNet.Blog.UnitTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<ItemGroup>
4040
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" />
4141
</ItemGroup>
42+
43+
<ItemGroup>
44+
<Folder Include="Infrastructure\Persistence" />
45+
</ItemGroup>
4246
<PropertyGroup>
4347
<CodeAnalysisRuleSet>..\stylecop.analyzers.ruleset</CodeAnalysisRuleSet>
4448
</PropertyGroup>

LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public Task StoreAsync(BlogPost blogPost)
4545
blogPost.Id = blogPosts.Max(b => b.Id) + 1;
4646
}
4747

48+
var entry = blogPosts.SingleOrDefault(b => b.Id == blogPost.Id);
49+
if (entry != null)
50+
{
51+
blogPosts.Remove(entry);
52+
}
53+
4854
blogPosts.Add(blogPost);
4955
return Task.CompletedTask;
5056
}

0 commit comments

Comments
 (0)