Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

#pragma warning disable CS9113 // Parameter is unread.

namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class AzureSqlEntityTableRepository_Tests : RepositoryTests<AzureSqlEntityMovie>
public class AzureSqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<AzureSqlEntityMovie>, IAsyncLifetime
{
#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;
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
private List<AzureSqlEntityMovie> movies = [];

public AzureSqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
public async Task InitializeAsync()
{
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()];
Context = await AzureSqlDbContext.CreateContextAsync(this.connectionString, output);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
}
}

public async Task DisposeAsync()
{
if (Context is not null)
{
await Context.DisposeAsync();
}
}

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

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 async Task<AzureSqlEntityMovie> GetEntityAsync(string id)
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

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

protected override Task<IRepository<AzureSqlEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<AzureSqlEntityMovie>>(new EntityTableRepository<AzureSqlEntityMovie>(Context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class AzureSQL_Controller_Tests : LiveControllerTests<AzureSqlEntityMovie>
public class AzureSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<AzureSqlEntityMovie>, IAsyncLifetime
{
#region Setup
private readonly DatabaseFixture _fixture;
private readonly Random random = new();
private readonly string connectionString;
private readonly List<AzureSqlEntityMovie> movies;
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
private List<AzureSqlEntityMovie> movies = [];

public AzureSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
public async Task InitializeAsync()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
output.WriteLine($"AzureSqlIsInitialized = {this._fixture.AzureSqlIsInitialized}");
Context = AzureSqlDbContext.CreateContext(this.connectionString, output, clearEntities: !this._fixture.AzureSqlIsInitialized);
this.movies = [.. Context.Movies.AsNoTracking()];
this._fixture.AzureSqlIsInitialized = true;
// Note: we don't clear entities on every run to speed up the test runs. This can only be done because
// the tests are read-only (associated with the query and get capabilities). If the test being run writes
// to the database then change clearEntities to true.
output.WriteLine($"CosmosIsInitialized = {fixture.AzureSqlIsInitialized}");
Context = await AzureSqlDbContext.CreateContextAsync(this.connectionString, output, clearEntities: !fixture.AzureSqlIsInitialized);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
fixture.AzureSqlIsInitialized = true;
}
}

public async Task DisposeAsync()
{
if (Context is not null)
{
await Context.DisposeAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases;
[ExcludeFromCodeCoverage]
public class AzureSqlDbContext(DbContextOptions<AzureSqlDbContext> options) : BaseDbContext<AzureSqlDbContext, AzureSqlEntityMovie>(options)
{
public static AzureSqlDbContext CreateContext(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
public static async Task<AzureSqlDbContext> CreateContextAsync(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
{
if (string.IsNullOrEmpty(connectionString))
{
Expand All @@ -23,12 +23,12 @@ public static AzureSqlDbContext CreateContext(string connectionString, ITestOutp
.EnableLogging(output);
AzureSqlDbContext context = new(optionsBuilder.Options);

context.InitializeDatabase(clearEntities);
context.PopulateDatabase();
await context.InitializeDatabaseAsync(clearEntities);
await context.PopulateDatabaseAsync();
return context;
}

internal void InitializeDatabase(bool clearEntities)
internal async Task InitializeDatabaseAsync(bool clearEntities)
{
const string datasyncTrigger = @"
CREATE OR ALTER TRIGGER [dbo].[{0}_datasync] ON [dbo].[{0}] AFTER INSERT, UPDATE AS
Expand All @@ -43,12 +43,12 @@ internal void InitializeDatabase(bool clearEntities)
END
";

Database.EnsureCreated();
ExecuteRawSqlOnEachEntity(datasyncTrigger);
await Database.EnsureCreatedAsync();
await ExecuteRawSqlOnEachEntityAsync(datasyncTrigger);

if (clearEntities)
{
ExecuteRawSqlOnEachEntity("DELETE FROM [dbo].[{0}]");
await ExecuteRawSqlOnEachEntityAsync("DELETE FROM [dbo].[{0}]");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CommunityToolkit.Datasync.TestCommon.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Runtime.CompilerServices;
using Xunit.Abstractions;

namespace CommunityToolkit.Datasync.TestCommon.Databases;
Expand Down Expand Up @@ -45,6 +46,19 @@ protected void ExecuteRawSqlOnEachEntity(string format)
}
}

/// <summary>
/// Executes the provided SQL statement for each entity in our set of entities.
/// </summary>
/// <param name="format"></param>
protected async Task ExecuteRawSqlOnEachEntityAsync(string format)
{
foreach (IEntityType table in Model.GetEntityTypes())
{
string sql = string.Format(format, table.GetTableName());
await Database.ExecuteSqlRawAsync(sql);
}
}

/// <summary>
/// Populates the database with the core set of movies. Ensures that we have the same data for all tests.
/// </summary>
Expand Down