Skip to content

Commit 704529d

Browse files
authored
(#206) PgSQL Controller tests. (#208)
* (#206) PgSQL Controller tests. * (#206) Ensures stable ordering across database types. * (#206) Removed unnecessary warning comment.
1 parent f8c81e9 commit 704529d

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

tests/CommunityToolkit.Datasync.Server.Test/Live/Base/LiveControllerTests.cs renamed to tests/CommunityToolkit.Datasync.Server.Test/Helpers/LiveControllerTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
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 Microsoft.AspNetCore.Http;
65
using Microsoft.AspNetCore.Mvc;
76
using System.Net;
87

9-
namespace CommunityToolkit.Datasync.Server.Test.Live;
8+
namespace CommunityToolkit.Datasync.Server.Test.Helpers;
109

1110
/// <summary>
1211
/// The base set of tests for the controller tests going against a live server.
@@ -82,6 +81,21 @@ protected virtual async Task CreateControllerAsync(HttpMethod method = null, str
8281
this.tableController.ControllerContext.HttpContext = CreateHttpContext(method ?? HttpMethod.Get, uri);
8382
}
8483

84+
private async Task<List<TEntity>> GetListOfEntitiesAsync(IEnumerable<string> ids)
85+
{
86+
List<TEntity> entities = [];
87+
foreach (string id in ids)
88+
{
89+
TEntity entity = await GetEntityAsync(id);
90+
if (entity != null)
91+
{
92+
entities.Add(entity);
93+
}
94+
}
95+
96+
return entities;
97+
}
98+
8599
/// <summary>
86100
/// This is the base test for the individual query tests.
87101
/// </summary>
@@ -104,7 +118,8 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
104118
List<TEntity> items = result.Items.Cast<TEntity>().ToList();
105119
items.Should().HaveCount(itemCount);
106120
result.Count.Should().Be(totalCount);
107-
items.Select(m => m.Id).Take(firstItems.Length).Should().BeEquivalentTo(firstItems);
121+
List<string> actualItems = items.Select(m => m.Id).Take(firstItems.Length).ToList();
122+
actualItems.Should().BeEquivalentTo(firstItems);
108123

109124
if (nextLinkQuery is not null)
110125
{
@@ -113,7 +128,7 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
113128
}
114129
else
115130
{
116-
result.NextLink.Should().BeNull();
131+
result.NextLink.Should().BeNull();
117132
}
118133
}
119134

tests/CommunityToolkit.Datasync.Server.Test/Live/AzureSQL/Controller_Query_Tests.cs renamed to tests/CommunityToolkit.Datasync.Server.Test/Live/AzureSQL_Controller_Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
using Microsoft.EntityFrameworkCore;
99
using Xunit.Abstractions;
1010

11-
namespace CommunityToolkit.Datasync.Server.Test.Live.AzureSQL;
11+
namespace CommunityToolkit.Datasync.Server.Test.Live;
1212

1313
[ExcludeFromCodeCoverage]
1414
[Collection("LiveTestsCollection")]
15-
public class AzureSql_Controller_Query_Tests : LiveControllerTests<AzureSqlEntityMovie>
15+
public class AzureSQL_Controller_Tests : LiveControllerTests<AzureSqlEntityMovie>
1616
{
1717
#region Setup
1818
private readonly DatabaseFixture _fixture;
@@ -21,7 +21,7 @@ public class AzureSql_Controller_Query_Tests : LiveControllerTests<AzureSqlEntit
2121
private readonly List<AzureSqlEntityMovie> movies;
2222
private readonly Lazy<AzureSqlDbContext> _context;
2323

24-
public AzureSql_Controller_Query_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
24+
public AzureSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
2525
{
2626
this._fixture = fixture;
2727
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.EntityFrameworkCore;
6+
using CommunityToolkit.Datasync.Server.Test.Helpers;
7+
using CommunityToolkit.Datasync.TestCommon.Databases;
8+
using Microsoft.EntityFrameworkCore;
9+
using Xunit.Abstractions;
10+
11+
namespace CommunityToolkit.Datasync.Server.Test.Live;
12+
13+
[ExcludeFromCodeCoverage]
14+
[Collection("LiveTestsCollection")]
15+
public class PgSQL_Controller_Tests : LiveControllerTests<PgEntityMovie>
16+
{
17+
#region Setup
18+
private readonly DatabaseFixture _fixture;
19+
private readonly Random random = new();
20+
private readonly string connectionString;
21+
private readonly List<PgEntityMovie> movies;
22+
private readonly Lazy<PgDbContext> _context;
23+
24+
public PgSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
25+
{
26+
this._fixture = fixture;
27+
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_PGSQL_CONNECTIONSTRING");
28+
if (!string.IsNullOrEmpty(this.connectionString))
29+
{
30+
this._context = new Lazy<PgDbContext>(() => PgDbContext.CreateContext(this.connectionString, output));
31+
this.movies = Context.Movies.AsNoTracking().ToList();
32+
}
33+
}
34+
35+
private PgDbContext Context { get => this._context.Value; }
36+
37+
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
38+
39+
protected override Task<PgEntityMovie> GetEntityAsync(string id)
40+
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));
41+
42+
protected override Task<int> GetEntityCountAsync()
43+
=> Task.FromResult(Context.Movies.Count());
44+
45+
protected override Task<IRepository<PgEntityMovie>> GetPopulatedRepositoryAsync()
46+
=> Task.FromResult<IRepository<PgEntityMovie>>(new EntityTableRepository<PgEntityMovie>(Context));
47+
48+
protected override Task<string> GetRandomEntityIdAsync(bool exists)
49+
=> Task.FromResult(exists ? this.movies[this.random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString());
50+
#endregion
51+
}

tests/CommunityToolkit.Datasync.TestCommon/Databases/Postgresql/PgDbContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ FOR EACH ROW EXECUTE PROCEDURE
5353

5454
protected override void OnModelCreating(ModelBuilder modelBuilder)
5555
{
56-
modelBuilder.Entity<PgEntityMovie>()
57-
.Property(m => m.UpdatedAt).HasDefaultValueSql("NOW() AT TIME ZONE 'UTC'");
56+
modelBuilder.Entity<PgEntityMovie>().Property(m => m.UpdatedAt)
57+
.HasDefaultValueSql("NOW() AT TIME ZONE 'UTC'");
58+
59+
// Ensures stable ordering across all database types.
60+
modelBuilder.Entity<PgEntityMovie>().Property(m => m.Title)
61+
.UseCollation("POSIX");
62+
5863
base.OnModelCreating(modelBuilder);
5964
}
6065
}

0 commit comments

Comments
 (0)