Skip to content

Commit

Permalink
(#205) AzureSQL query tests on the controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall committed Jan 18, 2025
1 parent 7ee4c9b commit 63210cf
Show file tree
Hide file tree
Showing 3 changed files with 4,428 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace CommunityToolkit.Datasync.Server.Test.Helpers;

/// <summary>
/// This can be used to share state between the various live tests. It isn't used right now.
/// </summary>
public class DatabaseFixture
{

}

[CollectionDefinition("LiveTestsCollection", DisableParallelization = true)]
public class LiveTestsCollection : ICollectionFixture<DatabaseFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Datasync.Server.EntityFrameworkCore;
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

namespace CommunityToolkit.Datasync.Server.Test.Live.AzureSQL;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class AzureSql_Controller_Query_Tests : LiveControllerTests<AzureSqlEntityMovie>
{
#region Setup
private readonly DatabaseFixture _fixture;
private readonly Random random = new();
private readonly string connectionString;
private readonly List<AzureSqlEntityMovie> movies;
private readonly Lazy<AzureSqlDbContext> _context;

public AzureSql_Controller_Query_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
this._context = new Lazy<AzureSqlDbContext>(() => AzureSqlDbContext.CreateContext(this.connectionString, output));
this.movies = [.. Context.Movies.AsNoTracking()];
}
}

private AzureSqlDbContext Context { get => this._context.Value; }

protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);

protected override Task<AzureSqlEntityMovie> GetEntityAsync(string id)
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));

protected override Task<int> GetEntityCountAsync()
=> Task.FromResult(Context.Movies.Count());

protected override Task<IRepository<AzureSqlEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<AzureSqlEntityMovie>>(new EntityTableRepository<AzureSqlEntityMovie>(Context));

protected override Task<string> GetRandomEntityIdAsync(bool exists)
=> Task.FromResult(exists ? this.movies[this.random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString());
#endregion
}
Loading

0 comments on commit 63210cf

Please sign in to comment.