Skip to content

Commit f8c81e9

Browse files
authored
(#205) AzureSQL query tests on the controller. (#207)
1 parent 7ee4c9b commit f8c81e9

File tree

3 files changed

+4428
-0
lines changed

3 files changed

+4428
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
namespace CommunityToolkit.Datasync.Server.Test.Helpers;
6+
7+
/// <summary>
8+
/// This can be used to share state between the various live tests. It isn't used right now.
9+
/// </summary>
10+
public class DatabaseFixture
11+
{
12+
13+
}
14+
15+
[CollectionDefinition("LiveTestsCollection", DisableParallelization = true)]
16+
public class LiveTestsCollection : ICollectionFixture<DatabaseFixture>
17+
{
18+
// This class has no code, and is never created. Its purpose is simply
19+
// to be the place to apply [CollectionDefinition] and all the
20+
// ICollectionFixture<> interfaces.
21+
}
22+
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.AzureSQL;
12+
13+
[ExcludeFromCodeCoverage]
14+
[Collection("LiveTestsCollection")]
15+
public class AzureSql_Controller_Query_Tests : LiveControllerTests<AzureSqlEntityMovie>
16+
{
17+
#region Setup
18+
private readonly DatabaseFixture _fixture;
19+
private readonly Random random = new();
20+
private readonly string connectionString;
21+
private readonly List<AzureSqlEntityMovie> movies;
22+
private readonly Lazy<AzureSqlDbContext> _context;
23+
24+
public AzureSql_Controller_Query_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
25+
{
26+
this._fixture = fixture;
27+
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
28+
if (!string.IsNullOrEmpty(this.connectionString))
29+
{
30+
this._context = new Lazy<AzureSqlDbContext>(() => AzureSqlDbContext.CreateContext(this.connectionString, output));
31+
this.movies = [.. Context.Movies.AsNoTracking()];
32+
}
33+
}
34+
35+
private AzureSqlDbContext Context { get => this._context.Value; }
36+
37+
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
38+
39+
protected override Task<AzureSqlEntityMovie> 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<AzureSqlEntityMovie>> GetPopulatedRepositoryAsync()
46+
=> Task.FromResult<IRepository<AzureSqlEntityMovie>>(new EntityTableRepository<AzureSqlEntityMovie>(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)