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