Skip to content

Commit fedad68

Browse files
Implement integration tests
1 parent 1ef4f50 commit fedad68

File tree

6 files changed

+892
-0
lines changed

6 files changed

+892
-0
lines changed

JsonApiDotNetCore.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestBuildingBlocks", "test\
4646
EndProject
4747
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosDbExample", "src\Examples\CosmosDbExample\CosmosDbExample.csproj", "{3AD5D1B4-8456-4639-98DB-F0D0EDAB2AD2}"
4848
EndProject
49+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosDbTests", "test\CosmosDbTests\CosmosDbTests.csproj", "{2E387408-8689-4335-A7C4-363DDB28E701}"
50+
EndProject
4951
Global
5052
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5153
Debug|Any CPU = Debug|Any CPU
@@ -224,6 +226,18 @@ Global
224226
{3AD5D1B4-8456-4639-98DB-F0D0EDAB2AD2}.Release|x64.Build.0 = Release|Any CPU
225227
{3AD5D1B4-8456-4639-98DB-F0D0EDAB2AD2}.Release|x86.ActiveCfg = Release|Any CPU
226228
{3AD5D1B4-8456-4639-98DB-F0D0EDAB2AD2}.Release|x86.Build.0 = Release|Any CPU
229+
{2E387408-8689-4335-A7C4-363DDB28E701}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
230+
{2E387408-8689-4335-A7C4-363DDB28E701}.Debug|Any CPU.Build.0 = Debug|Any CPU
231+
{2E387408-8689-4335-A7C4-363DDB28E701}.Debug|x64.ActiveCfg = Debug|Any CPU
232+
{2E387408-8689-4335-A7C4-363DDB28E701}.Debug|x64.Build.0 = Debug|Any CPU
233+
{2E387408-8689-4335-A7C4-363DDB28E701}.Debug|x86.ActiveCfg = Debug|Any CPU
234+
{2E387408-8689-4335-A7C4-363DDB28E701}.Debug|x86.Build.0 = Debug|Any CPU
235+
{2E387408-8689-4335-A7C4-363DDB28E701}.Release|Any CPU.ActiveCfg = Release|Any CPU
236+
{2E387408-8689-4335-A7C4-363DDB28E701}.Release|Any CPU.Build.0 = Release|Any CPU
237+
{2E387408-8689-4335-A7C4-363DDB28E701}.Release|x64.ActiveCfg = Release|Any CPU
238+
{2E387408-8689-4335-A7C4-363DDB28E701}.Release|x64.Build.0 = Release|Any CPU
239+
{2E387408-8689-4335-A7C4-363DDB28E701}.Release|x86.ActiveCfg = Release|Any CPU
240+
{2E387408-8689-4335-A7C4-363DDB28E701}.Release|x86.Build.0 = Release|Any CPU
227241
EndGlobalSection
228242
GlobalSection(SolutionProperties) = preSolution
229243
HideSolutionNode = FALSE
@@ -243,6 +257,7 @@ Global
243257
{EC3202C6-1D4C-4B14-A599-B9D3F27FE3BA} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
244258
{210FD61E-FF5D-4CEE-8E0D-C739ECCCBA21} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
245259
{3AD5D1B4-8456-4639-98DB-F0D0EDAB2AD2} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
260+
{2E387408-8689-4335-A7C4-363DDB28E701} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
246261
EndGlobalSection
247262
GlobalSection(ExtensibilityGlobals) = postSolution
248263
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}

test/CosmosDbTests/CosmosDbFixture.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using CosmosDbExample;
5+
using CosmosDbExample.Data;
6+
using CosmosDbExample.Models;
7+
using JetBrains.Annotations;
8+
using JsonApiDotNetCore.Resources;
9+
using Microsoft.AspNetCore.Mvc.Testing;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Xunit;
12+
13+
#pragma warning disable AV1115 // Member or local function contains the word 'and', which suggests doing multiple things
14+
15+
namespace CosmosDbTests
16+
{
17+
/// <summary>
18+
/// Test fixture that deletes and recreates the TodoItemDB database and PeopleAndTodoItems container before all of the test methods are run. It does not
19+
/// delete the database nor the container at the end.
20+
/// </summary>
21+
[PublicAPI]
22+
public class CosmosDbFixture : WebApplicationFactory<Startup>, IAsyncLifetime
23+
{
24+
public Guid BonnieId { get; } = Guid.NewGuid();
25+
26+
public Guid ClydeId { get; } = Guid.NewGuid();
27+
28+
public Guid TodoItemOwnedByBonnieId { get; } = Guid.NewGuid();
29+
30+
public Guid TodoItemOwnedByClydeId { get; } = Guid.NewGuid();
31+
32+
public Guid TodoItemWithOwnerAndAssigneeId { get; } = Guid.NewGuid();
33+
34+
public async Task InitializeAsync()
35+
{
36+
await RunOnDatabaseAsync(async context =>
37+
{
38+
await context.Database.EnsureDeletedAsync();
39+
await context.Database.EnsureCreatedAsync();
40+
41+
Person bonnie = new()
42+
{
43+
Id = BonnieId,
44+
FirstName = "Bonnie",
45+
LastName = "Parker"
46+
};
47+
48+
Person clyde = new()
49+
{
50+
Id = ClydeId,
51+
FirstName = "Clyde",
52+
LastName = "Barrow"
53+
};
54+
55+
TodoItem todoItemOwnedByBonnie = new()
56+
{
57+
Id = TodoItemOwnedByBonnieId,
58+
Description = "Rob bank",
59+
Priority = TodoItemPriority.High,
60+
OwnerId = bonnie.Id,
61+
Tags = new HashSet<Tag>
62+
{
63+
new()
64+
{
65+
Name = "Job"
66+
}
67+
}
68+
};
69+
70+
TodoItem todoItemOwnedByClyde = new()
71+
{
72+
Id = TodoItemOwnedByClydeId,
73+
Description = "Wash car",
74+
Priority = TodoItemPriority.Low,
75+
OwnerId = clyde.Id
76+
};
77+
78+
TodoItem todoItemWithOwnerAndAssignee = new()
79+
{
80+
Id = TodoItemWithOwnerAndAssigneeId,
81+
Description = "Go shopping",
82+
Priority = TodoItemPriority.Medium,
83+
OwnerId = clyde.Id,
84+
AssigneeId = bonnie.Id,
85+
Tags = new HashSet<Tag>
86+
{
87+
new()
88+
{
89+
Name = "Errands"
90+
},
91+
new()
92+
{
93+
Name = "Groceries"
94+
}
95+
}
96+
};
97+
98+
// @formatter:off
99+
100+
context.People.AddRange(bonnie, clyde);
101+
context.TodoItems.AddRange(todoItemOwnedByBonnie, todoItemOwnedByClyde, todoItemWithOwnerAndAssignee);
102+
103+
// @formatter:on
104+
105+
await context.SaveChangesAsync();
106+
});
107+
}
108+
109+
public Task DisposeAsync()
110+
{
111+
return Task.CompletedTask;
112+
}
113+
114+
public async Task<TEntity> CreateEntityAsync<TEntity>(TEntity entity)
115+
where TEntity : IIdentifiable
116+
{
117+
await RunOnDatabaseAsync(async context =>
118+
{
119+
// ReSharper disable once MethodHasAsyncOverload
120+
context.Add(entity);
121+
await context.SaveChangesAsync();
122+
});
123+
124+
return entity;
125+
}
126+
127+
public async Task RunOnDatabaseAsync(Func<AppDbContext, Task> asyncAction)
128+
{
129+
using IServiceScope scope = Services.CreateScope();
130+
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
131+
132+
await asyncAction(dbContext);
133+
}
134+
}
135+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<None Update="xunit.runner.json">
8+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9+
</None>
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\Examples\CosmosDbExample\CosmosDbExample.csproj" />
14+
<ProjectReference Include="..\TestBuildingBlocks\TestBuildingBlocks.csproj" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="All" />
19+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(AspNetCoreVersion)" />
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
21+
</ItemGroup>
22+
</Project>

0 commit comments

Comments
 (0)