Skip to content

Commit 9819dce

Browse files
committed
Added RavenDB Unit test sample
1 parent 280109e commit 9819dce

File tree

12 files changed

+247
-0
lines changed

12 files changed

+247
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| ---------------------------------------------------------------------------------------- | ------------ |
7+
| [How to unit test a RavenDB](RavenDBUnitTest/) | 25.05.2022 |
78
| [Blazor with CancellationToken support](BlazorCancellation/) | 18.05.2022 |
89
| [Stop using Finalizers in C#](Finalizers/) | 15.05.2022 |
910
| [Tail-Recursion - Explained with the Fibonacci series](TailRecursion/) | 13.05.2022 |

RavenDBUnitTest/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How to unit test a RavenDB
2+
3+
RavenDB is a well known **open-source document-oriented databse** for .NET. And of course we want to test our logic and not only locally while developing, but also our continuous integration pipeline should be able to run our tests. So let's tackle exactly that.
4+
5+
Found [here](https://steven-giesel.com/blogPost/0cbe9770-40e7-43b1-980f-d1d3a8b5203d)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
13+
<PackageReference Include="RavenDB.TestDriver" Version="5.3.102" />
14+
<PackageReference Include="xunit" Version="2.4.1" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.1.2">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\RavenDBUnitTest\RavenDBUnitTest.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Raven.Client.Documents;
2+
using Raven.TestDriver;
3+
using RavenDBUnitTest.Infrastructure;
4+
5+
namespace RavenDBUnitTest.Tests;
6+
7+
public sealed class BlogPostRepositoryTests : RavenTestDriver
8+
{
9+
private static bool serverRunning;
10+
private readonly IDocumentStore store;
11+
private readonly Repository<BlogPost> sut;
12+
13+
public BlogPostRepositoryTests()
14+
{
15+
StartServerIfNotRunning();
16+
store = GetDocumentStore();
17+
sut = new Repository<BlogPost>(store);
18+
}
19+
20+
public override void Dispose()
21+
{
22+
base.Dispose();
23+
store.Dispose();
24+
}
25+
26+
[Fact]
27+
public async Task ShouldLoadBlogPost()
28+
{
29+
var blogPost = new BlogPost { Title = "Title", Content = "Content"};
30+
await SaveBlogPostAsync(blogPost);
31+
32+
var blogPostFromRepo = await sut.GetByIdAsync(blogPost.Id);
33+
34+
Assert.Equal("Title", blogPostFromRepo.Title);
35+
Assert.Equal("Content", blogPostFromRepo.Content);
36+
}
37+
38+
[Fact]
39+
public async Task ShouldSaveBlogPost()
40+
{
41+
var blogPost = new BlogPost { Title = "Title", Content = "Content"};
42+
43+
await sut.StoreAsync(blogPost);
44+
45+
var blogPostFromContext = await GetBlogPostByIdAsync(blogPost.Id);
46+
Assert.NotNull(blogPostFromContext);
47+
Assert.Equal("Title", blogPostFromContext.Title);
48+
Assert.Equal("Content", blogPostFromContext.Content);
49+
}
50+
51+
private static void StartServerIfNotRunning()
52+
{
53+
if (!serverRunning)
54+
{
55+
serverRunning = true;
56+
ConfigureServer(new TestServerOptions
57+
{
58+
DataDirectory = "./RavenDbTest/",
59+
});
60+
}
61+
}
62+
63+
private async Task SaveBlogPostAsync(params BlogPost[] blogPosts)
64+
{
65+
using var session = store.OpenAsyncSession();
66+
foreach (var blogPost in blogPosts)
67+
{
68+
await session.StoreAsync(blogPost);
69+
}
70+
71+
await session.SaveChangesAsync();
72+
}
73+
74+
private async Task<BlogPost> GetBlogPostByIdAsync(string id)
75+
{
76+
using var session = store.OpenAsyncSession();
77+
return await session.Query<BlogPost>().SingleOrDefaultAsync(s => s.Id == id);
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;

RavenDBUnitTest/RavenDBUnitTest.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RavenDBUnitTest", "RavenDBUnitTest\RavenDBUnitTest.csproj", "{92EA3175-2BEE-4BF2-8995-5C44D591A549}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RavenDBUnitTest.Tests", "RavenDBUnitTest.Tests\RavenDBUnitTest.Tests.csproj", "{987974CC-7031-4DFD-ADC9-23EE2C461800}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{92EA3175-2BEE-4BF2-8995-5C44D591A549}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{92EA3175-2BEE-4BF2-8995-5C44D591A549}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{92EA3175-2BEE-4BF2-8995-5C44D591A549}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{92EA3175-2BEE-4BF2-8995-5C44D591A549}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{987974CC-7031-4DFD-ADC9-23EE2C461800}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{987974CC-7031-4DFD-ADC9-23EE2C461800}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{987974CC-7031-4DFD-ADC9-23EE2C461800}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{987974CC-7031-4DFD-ADC9-23EE2C461800}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace RavenDBUnitTest;
2+
3+
public abstract class Aggregate
4+
{
5+
public string Id { get; set; }
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RavenDBUnitTest;
2+
3+
public class BlogPost : Aggregate
4+
{
5+
public string Title { get; set; }
6+
7+
public string Content { get; set; }
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Linq.Expressions;
2+
using Raven.Client.Documents;
3+
using Raven.Client.Documents.Linq;
4+
5+
namespace RavenDBUnitTest.Infrastructure;
6+
7+
public class Repository<TAggregate>
8+
where TAggregate : Aggregate
9+
{
10+
private readonly IDocumentStore documentStore;
11+
12+
public Repository(IDocumentStore documentStore)
13+
{
14+
this.documentStore = documentStore;
15+
}
16+
17+
public async Task<TAggregate> GetByIdAsync(string id)
18+
{
19+
using var session = documentStore.OpenAsyncSession();
20+
return await session.LoadAsync<TAggregate>(id);
21+
}
22+
23+
public async Task<IReadOnlyCollection<TAggregate>> GetAllAsync(
24+
Expression<Func<TAggregate, bool>> filter = null,
25+
Expression<Func<TAggregate, object>> orderBy = null,
26+
bool descending = false)
27+
{
28+
using var session = documentStore.OpenSession();
29+
30+
var query = session.Query<TAggregate>();
31+
if (filter != null)
32+
{
33+
query = query.Where(filter);
34+
}
35+
36+
if (orderBy != null)
37+
{
38+
query = descending
39+
? query.OrderByDescending(orderBy)
40+
: query.OrderBy(orderBy);
41+
}
42+
43+
return (await query.ToListAsync());
44+
}
45+
46+
public async Task StoreAsync(TAggregate entity)
47+
{
48+
using var session = documentStore.OpenAsyncSession();
49+
await session.StoreAsync(entity);
50+
await session.SaveChangesAsync();
51+
}
52+
53+
public async ValueTask DeleteAsync(string id)
54+
{
55+
using var session = documentStore.OpenAsyncSession();
56+
session.Delete(id);
57+
await session.SaveChangesAsync();
58+
}
59+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using Raven.Client.Documents;
4+
using RavenDBUnitTest;
5+
using RavenDBUnitTest.Infrastructure;
6+
7+
var documentStore = new DocumentStore
8+
{
9+
Urls = new[] { "http://127.0.0.1:8080" },
10+
Database = "StevenSample",
11+
};
12+
documentStore.Initialize();
13+
14+
var repository = new Repository<BlogPost>(documentStore);
15+
var blogPost = new BlogPost { Title = "Hello World", Content = "Some text" };
16+
await repository.StoreAsync(blogPost);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RavenDB.Client" Version="5.3.102" />
12+
</ItemGroup>
13+
14+
</Project>

RavenDBUnitTest/global.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "6.0.0",
4+
"rollForward": "latestMinor",
5+
"allowPrerelease": false
6+
}
7+
}

0 commit comments

Comments
 (0)