Skip to content

Commit 510bfde

Browse files
Add Sample app tests
1 parent f232846 commit 510bfde

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Atc.Cosmos;
2+
using Atc.Cosmos.Testing;
3+
using Atc.Test;
4+
using AutoFixture.Xunit2;
5+
using FluentAssertions;
6+
7+
namespace SampleApi.Tests;
8+
9+
public class FooService
10+
{
11+
private readonly ICosmosReader<FooResource> reader;
12+
private readonly ICosmosWriter<FooResource> writer;
13+
14+
public FooService(
15+
ICosmosReader<FooResource> reader,
16+
ICosmosWriter<FooResource> writer)
17+
{
18+
this.reader = reader;
19+
this.writer = writer;
20+
}
21+
22+
public Task<FooResource?> FindAsync(
23+
string id,
24+
CancellationToken cancellationToken = default) =>
25+
reader.FindAsync(id, FooResource.PartitionKey, cancellationToken);
26+
27+
public Task UpsertAsync(
28+
string? id = null,
29+
Dictionary<string, object>? data = null,
30+
CancellationToken cancellationToken = default) =>
31+
writer.UpdateOrCreateAsync(
32+
() => new FooResource { Id = id ?? Guid.NewGuid().ToString() },
33+
resource => resource.Data = data ?? new Dictionary<string, object>(),
34+
retries: 5,
35+
cancellationToken);
36+
}
37+
38+
public class FooServiceTests
39+
{
40+
[Theory]
41+
[AutoNSubstituteData]
42+
public async Task Should_Get_Existing_Data(
43+
[Frozen(Matching.ImplementedInterfaces)] FakeCosmos<FooResource> fakeCosmos,
44+
FooService sut,
45+
FooResource resource)
46+
{
47+
fakeCosmos.Documents.Add(resource);
48+
(await sut.FindAsync(resource.Id)).Should().NotBeNull();
49+
}
50+
51+
[Theory]
52+
[AutoNSubstituteData]
53+
public async Task Should_Create_New_Data(
54+
[Frozen(Matching.ImplementedInterfaces)] FakeCosmos<FooResource> fakeCosmos,
55+
FooService sut,
56+
Dictionary<string, object> data)
57+
{
58+
var count = fakeCosmos.Documents.Count;
59+
await sut.UpsertAsync(data: data);
60+
fakeCosmos.Documents.Should().HaveCount(count + 1);
61+
}
62+
63+
[Theory]
64+
[AutoNSubstituteData]
65+
public async Task Should_Update_Existing_Data(
66+
[Frozen(Matching.ImplementedInterfaces)] FakeCosmos<FooResource> fakeCosmos,
67+
FooService sut,
68+
FooResource resource,
69+
Dictionary<string, object> data)
70+
{
71+
fakeCosmos.Documents.Add(resource);
72+
await sut.UpsertAsync(resource.Id, data);
73+
74+
fakeCosmos
75+
.Documents
76+
.First(c => c.Id == resource.Id)
77+
.Data
78+
.Should()
79+
.BeEquivalentTo(data);
80+
}
81+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
12+
<LangVersion>10.0</LangVersion>
13+
</PropertyGroup>
14+
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
16+
<LangVersion>10.0</LangVersion>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Atc.Test" Version="1.0.72" />
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
22+
<PackageReference Include="xunit" Version="2.4.2" />
23+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
24+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
25+
<PrivateAssets>all</PrivateAssets>
26+
</PackageReference>
27+
<PackageReference Include="coverlet.collector" Version="3.1.2">
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
<PrivateAssets>all</PrivateAssets>
30+
</PackageReference>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\SampleApi\SampleApi.csproj" />
35+
</ItemGroup>
36+
37+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;

samples/SampleApi/SampleApi.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.5.33103.201
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApi", "SampleApi\SampleApi.csproj", "{0F16B15A-A527-489D-B2FA-C63331E671B5}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleApi", "SampleApi\SampleApi.csproj", "{0F16B15A-A527-489D-B2FA-C63331E671B5}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApi.Tests", "SampleApi.Tests\SampleApi.Tests.csproj", "{2E992F83-9B02-410B-B271-4F85FB50AABC}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
1517
{0F16B15A-A527-489D-B2FA-C63331E671B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{0F16B15A-A527-489D-B2FA-C63331E671B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{0F16B15A-A527-489D-B2FA-C63331E671B5}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{2E992F83-9B02-410B-B271-4F85FB50AABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{2E992F83-9B02-410B-B271-4F85FB50AABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{2E992F83-9B02-410B-B271-4F85FB50AABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{2E992F83-9B02-410B-B271-4F85FB50AABC}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)