Skip to content

Commit 4f7fb10

Browse files
author
Adrian Hall
committed
(#204) Added InMemory and LiteDb controller test suite.
1 parent 0bb84a4 commit 4f7fb10

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

tests/CommunityToolkit.Datasync.Server.LiteDb.Test/LiteDbRepository_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using CommunityToolkit.Datasync.TestCommon;
6+
using CommunityToolkit.Datasync.TestCommon.Databases;
67
using CommunityToolkit.Datasync.TestCommon.Models;
78
using LiteDB;
89

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.Datasync.Server.InMemory;
6+
using CommunityToolkit.Datasync.Server.Test.Helpers;
7+
using CommunityToolkit.Datasync.TestCommon.Databases;
8+
9+
namespace CommunityToolkit.Datasync.Server.Test.Live;
10+
11+
[ExcludeFromCodeCoverage]
12+
[Collection("LiveTestsCollection")]
13+
public class InMemory_Controller_Tests : LiveControllerTests<InMemoryMovie>
14+
{
15+
#region Setup
16+
private readonly Random random = new();
17+
private InMemoryRepository<InMemoryMovie> repository;
18+
19+
protected override string DriverName { get; } = "InMemory";
20+
21+
protected override Task<InMemoryMovie> GetEntityAsync(string id)
22+
=> Task.FromResult(this.repository.GetEntity(id));
23+
24+
protected override Task<int> GetEntityCountAsync()
25+
=> Task.FromResult(this.repository.GetEntities().Count);
26+
27+
protected override Task<IRepository<InMemoryMovie>> GetPopulatedRepositoryAsync()
28+
{
29+
this.repository = new InMemoryRepository<InMemoryMovie>(TestCommon.TestData.Movies.OfType<InMemoryMovie>());
30+
return Task.FromResult<IRepository<InMemoryMovie>>(this.repository);
31+
}
32+
33+
protected override Task<string> GetRandomEntityIdAsync(bool exists)
34+
=> Task.FromResult(exists ? this.repository.GetEntities()[this.random.Next(this.repository.GetEntities().Count)].Id : Guid.NewGuid().ToString());
35+
#endregion
36+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.Datasync.Server.Abstractions.Http;
6+
using CommunityToolkit.Datasync.Server.InMemory;
7+
using CommunityToolkit.Datasync.Server.LiteDb;
8+
using CommunityToolkit.Datasync.Server.Test.Helpers;
9+
using CommunityToolkit.Datasync.TestCommon.Databases;
10+
using LiteDB;
11+
12+
namespace CommunityToolkit.Datasync.Server.Test.Live;
13+
14+
[ExcludeFromCodeCoverage]
15+
[Collection("LiveTestsCollection")]
16+
public class LiteDb_Controller_Tests : LiveControllerTests<LiteDbMovie>, IDisposable
17+
{
18+
#region Setup
19+
private readonly Random random = new();
20+
private string dbFilename;
21+
private LiteDatabase database;
22+
private ILiteCollection<LiteDbMovie> collection;
23+
private LiteDbRepository<LiteDbMovie> repository;
24+
private readonly List<LiteDbMovie> movies = [];
25+
26+
protected override Task<LiteDbMovie> GetEntityAsync(string id)
27+
=> Task.FromResult(this.collection.FindById(id));
28+
29+
protected override Task<int> GetEntityCountAsync()
30+
=> Task.FromResult(this.collection.Count());
31+
32+
protected override Task<IRepository<LiteDbMovie>> GetPopulatedRepositoryAsync()
33+
{
34+
this.dbFilename = Path.GetTempFileName();
35+
this.database = new LiteDatabase($"Filename={this.dbFilename};Connection=direct;InitialSize=0");
36+
this.collection = this.database.GetCollection<LiteDbMovie>("litedbmovies");
37+
38+
foreach (LiteDbMovie movie in TestCommon.TestData.Movies.OfType<LiteDbMovie>())
39+
{
40+
movie.UpdatedAt = DateTimeOffset.Now;
41+
movie.Version = Guid.NewGuid().ToByteArray();
42+
this.collection.Insert(movie);
43+
this.movies.Add(movie);
44+
}
45+
46+
this.repository = new LiteDbRepository<LiteDbMovie>(this.database);
47+
return Task.FromResult<IRepository<LiteDbMovie>>(this.repository);
48+
}
49+
50+
protected override Task<string> GetRandomEntityIdAsync(bool exists)
51+
=> Task.FromResult(exists ? this.movies[random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString());
52+
53+
public void Dispose()
54+
{
55+
Dispose(true);
56+
GC.SuppressFinalize(this);
57+
}
58+
59+
protected virtual void Dispose(bool disposing)
60+
{
61+
if (disposing)
62+
{
63+
this.database?.Dispose();
64+
if (!string.IsNullOrEmpty(this.dbFilename))
65+
{
66+
File.Delete(this.dbFilename);
67+
}
68+
}
69+
}
70+
#endregion
71+
}

tests/CommunityToolkit.Datasync.TestCommon/CommunityToolkit.Datasync.TestCommon.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj" />
2626
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.EntityFrameworkCore\CommunityToolkit.Datasync.Server.EntityFrameworkCore.csproj" />
2727
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.InMemory\CommunityToolkit.Datasync.Server.InMemory.csproj" />
28+
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.LiteDb\CommunityToolkit.Datasync.Server.LiteDb.csproj" />
2829
</ItemGroup>
2930
</Project>

tests/CommunityToolkit.Datasync.Server.LiteDb.Test/Helpers/LiteDbMovie.cs renamed to tests/CommunityToolkit.Datasync.TestCommon/Databases/LiteDb/LiteDbMovie.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Datasync.Server.LiteDb;
56
using CommunityToolkit.Datasync.TestCommon.Models;
67
using System.ComponentModel.DataAnnotations;
78

8-
namespace CommunityToolkit.Datasync.Server.LiteDb.Test;
9+
namespace CommunityToolkit.Datasync.TestCommon.Databases;
910

1011
[ExcludeFromCodeCoverage]
1112
public class LiteDbMovie : LiteDbTableData, IMovie, IEquatable<IMovie>

0 commit comments

Comments
 (0)