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