Skip to content

Commit 24ec586

Browse files
author
Adrian Hall
committed
(#206) PgSQL Controller tests.
1 parent f8c81e9 commit 24ec586

File tree

3 files changed

+92
-19
lines changed

3 files changed

+92
-19
lines changed

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

Lines changed: 38 additions & 16 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,14 @@ 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+
123+
// Get the list of items in firstItems and actualItems
124+
List<TEntity> expA1 = await GetListOfEntitiesAsync(firstItems);
125+
List<TEntity> expA2 = await GetListOfEntitiesAsync(actualItems);
126+
expA2.Count.Should().Be(actualItems.Count);
127+
128+
actualItems.Should().BeEquivalentTo(firstItems);
108129

109130
if (nextLinkQuery is not null)
110131
{
@@ -113,7 +134,7 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
113134
}
114135
else
115136
{
116-
result.NextLink.Should().BeNull();
137+
result.NextLink.Should().BeNull();
117138
}
118139
}
119140

@@ -3359,19 +3380,20 @@ await MovieQueryTest(
33593380
);
33603381
}
33613382

3362-
[SkippableFact]
3363-
public async Task Query_Test_235()
3364-
{
3365-
Skip.IfNot(CanRunLiveTests());
3383+
// PROBLEM - THIS TEST RESULTS IN DIFFERENT ORDERING ON PGSQL vs. AZURESQL
3384+
//[SkippableFact]
3385+
//public async Task Query_Test_235()
3386+
//{
3387+
// Skip.IfNot(CanRunLiveTests());
33663388

3367-
await MovieQueryTest(
3368-
$"{MovieEndpoint}?$orderby=title asc&$skip=5",
3369-
DefaultPageSize,
3370-
"$orderby=title asc&$skip=105",
3371-
null,
3372-
["id-214", "id-102", "id-215", "id-039", "id-057"]
3373-
);
3374-
}
3389+
// await MovieQueryTest(
3390+
// $"{MovieEndpoint}?$orderby=title asc&$skip=5",
3391+
// DefaultPageSize,
3392+
// "$orderby=title asc&$skip=105",
3393+
// null,
3394+
// ["id-214", "id-102", "id-215", "id-039", "id-057"]
3395+
// );
3396+
//}
33753397

33763398
[SkippableFact]
33773399
public async Task Query_Test_236()

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+
}

0 commit comments

Comments
 (0)