Skip to content

Commit 8deca42

Browse files
committed
Added Tests for Sql Layer
1 parent f4ceaea commit 8deca42

File tree

6 files changed

+124
-4
lines changed

6 files changed

+124
-4
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using FluentAssertions;
4+
using LinkDotNet.Domain;
5+
using LinkDotNet.Infrastructure.Persistence.Sql;
6+
using Microsoft.EntityFrameworkCore;
7+
using Xunit;
8+
9+
namespace LinkDotNet.Blog.IntegrationTests.Infrastructure.Persistence
10+
{
11+
public sealed class SqlRepositoryTests : IAsyncLifetime
12+
{
13+
private readonly BlogPostRepository sut;
14+
private readonly BlogPostContext dbContext;
15+
16+
public SqlRepositoryTests()
17+
{
18+
var options = new DbContextOptionsBuilder()
19+
.UseSqlite("Data Source=IntegrationTest.db")
20+
.Options;
21+
dbContext = new BlogPostContext(options);
22+
sut = new BlogPostRepository(new BlogPostContext(options));
23+
}
24+
25+
[Fact]
26+
public async Task ShouldLoadBlogPost()
27+
{
28+
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", new[] { "Tag 1", "Tag 2" });
29+
await dbContext.BlogPosts.AddAsync(blogPost);
30+
await dbContext.SaveChangesAsync();
31+
32+
var blogPostFromRepo = await sut.GetByIdAsync(blogPost.Id);
33+
34+
blogPostFromRepo.Should().NotBeNull();
35+
blogPostFromRepo.Title.Should().Be("Title");
36+
blogPostFromRepo.ShortDescription.Should().Be("Subtitle");
37+
blogPostFromRepo.Content.Should().Be("Content");
38+
blogPostFromRepo.PreviewImageUrl.Should().Be("url");
39+
blogPostFromRepo.Tags.Should().HaveCount(2);
40+
var tagContent = blogPostFromRepo.Tags.Select(t => t.Content).ToList();
41+
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
42+
}
43+
44+
[Fact]
45+
public async Task ShouldSaveBlogPost()
46+
{
47+
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", new[] { "Tag 1", "Tag 2" });
48+
49+
await sut.StoreAsync(blogPost);
50+
51+
var blogPostFromContext = await dbContext.BlogPosts.Include(b => b.Tags).AsNoTracking().SingleOrDefaultAsync(s => s.Id == blogPost.Id);
52+
blogPostFromContext.Should().NotBeNull();
53+
blogPostFromContext.Title.Should().Be("Title");
54+
blogPostFromContext.ShortDescription.Should().Be("Subtitle");
55+
blogPostFromContext.Content.Should().Be("Content");
56+
blogPostFromContext.PreviewImageUrl.Should().Be("url");
57+
blogPostFromContext.Tags.Should().HaveCount(2);
58+
var tagContent = blogPostFromContext.Tags.Select(t => t.Content).ToList();
59+
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
60+
}
61+
62+
[Fact]
63+
public async Task ShouldGetAllBlogPosts()
64+
{
65+
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", new[] { "Tag 1", "Tag 2" });
66+
await dbContext.BlogPosts.AddAsync(blogPost);
67+
await dbContext.SaveChangesAsync();
68+
69+
var blogPostsFromRepo = (await sut.GetAllAsync()).ToList();
70+
71+
blogPostsFromRepo.Should().NotBeNull();
72+
blogPostsFromRepo.Should().HaveCount(1);
73+
var blogPostFromRepo = blogPostsFromRepo.Single();
74+
blogPostFromRepo.Title.Should().Be("Title");
75+
blogPostFromRepo.ShortDescription.Should().Be("Subtitle");
76+
blogPostFromRepo.Content.Should().Be("Content");
77+
blogPostFromRepo.PreviewImageUrl.Should().Be("url");
78+
blogPostFromRepo.Tags.Should().HaveCount(2);
79+
var tagContent = blogPostFromRepo.Tags.Select(t => t.Content).ToList();
80+
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
81+
}
82+
83+
public Task InitializeAsync()
84+
{
85+
return Task.CompletedTask;
86+
}
87+
88+
async Task IAsyncLifetime.DisposeAsync()
89+
{
90+
await dbContext.Database.EnsureDeletedAsync();
91+
await dbContext.DisposeAsync();
92+
}
93+
}
94+
}

LinkDotNet.Blog.IntegrationTests/LinkDotNet.Blog.IntegrationTests.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.7" />
1012
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
13+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.25.0.33663">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.354">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
1121
<PackageReference Include="xunit" Version="2.4.1" />
1222
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -23,4 +33,11 @@
2333
<ProjectReference Include="..\LinkDotNet.Infrastructure\LinkDotNet.Infrastructure.csproj" />
2434
</ItemGroup>
2535

36+
<ItemGroup>
37+
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" />
38+
</ItemGroup>
39+
<PropertyGroup>
40+
<CodeAnalysisRuleSet>..\stylecop.analyzers.ruleset</CodeAnalysisRuleSet>
41+
</PropertyGroup>
42+
2643
</Project>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1011
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
1213
<PackageReference Include="Moq" Version="4.16.1" />

LinkDotNet.Blog.UnitTests/StorageProviderRegistrationExtensionsTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using FluentAssertions;
23
using LinkDotNet.Blog.Web.RegistrationExtensions;
34
using Microsoft.Extensions.DependencyInjection;
45
using Xunit;
@@ -11,10 +12,11 @@ public class StorageProviderRegistrationExtensionsTests
1112
public void GivenAlreadyRegisteredRepository_WhenTryingToAddAnotherOne_ThenException()
1213
{
1314
var services = new ServiceCollection();
15+
services.UseRavenDbAsStorageProvider();
1416

15-
services.UseSqliteAsStorageProvider();
17+
Action act = () => services.UseSqliteAsStorageProvider();
1618

17-
Assert.Throws<NotSupportedException>(() => services.UseRavenDbAsStorageProvider());
19+
act.Should().Throw<NotSupportedException>();
1820
}
1921
}
2022
}

LinkDotNet.Blog.Web/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public void ConfigureServices(IServiceCollection services)
2727
AppConfigurationFactory.Create(service.GetService<IConfiguration>()));
2828

2929
// This can be extended to use other repositories
30-
services.UseSqliteAsStorageProvider();
30+
services.UseSqlAsStorageProvider();
3131
/****************
3232
* Possible Storage Providers:
33-
* services.UseSqlAsStorageProvider();
33+
* services.UseSqliteAsStorageProvider();
3434
* services.UseRavenDbAsStorageProvider();
3535
* services.UseInMemoryAsStorageProvider();
3636
*/

LinkDotNet.Blog.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkDotNet.Infrastructure",
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkDotNet.Blog.UnitTests", "LinkDotNet.Blog.UnitTests\LinkDotNet.Blog.UnitTests.csproj", "{5B868911-7C93-4190-AEE4-3A6694F2FFCE}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkDotNet.Blog.IntegrationTests", "LinkDotNet.Blog.IntegrationTests\LinkDotNet.Blog.IntegrationTests.csproj", "{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{5B868911-7C93-4190-AEE4-3A6694F2FFCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{5B868911-7C93-4190-AEE4-3A6694F2FFCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{5B868911-7C93-4190-AEE4-3A6694F2FFCE}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)