|
| 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.TestCommon; |
| 6 | +using CommunityToolkit.Datasync.TestCommon.Databases; |
| 7 | +using Microsoft.EntityFrameworkCore; |
| 8 | +using Xunit.Abstractions; |
| 9 | + |
| 10 | +namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test; |
| 11 | + |
| 12 | +[ExcludeFromCodeCoverage] |
| 13 | +[Collection("LiveTestsCollection")] |
| 14 | +public class MysqlEntityTableRepository_Tests : RepositoryTests<MysqlEntityMovie> |
| 15 | +{ |
| 16 | + #region Setup |
| 17 | + private readonly DatabaseFixture _fixture; |
| 18 | + private readonly Random random = new(); |
| 19 | + private readonly string connectionString; |
| 20 | + private readonly List<MysqlEntityMovie> movies; |
| 21 | + private readonly Lazy<MysqlDbContext> _context; |
| 22 | + |
| 23 | + public MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base() |
| 24 | + { |
| 25 | + this._fixture = fixture; |
| 26 | + this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING"); |
| 27 | + if (!string.IsNullOrEmpty(this.connectionString)) |
| 28 | + { |
| 29 | + this._context = new Lazy<MysqlDbContext>(() => MysqlDbContext.CreateContext(this.connectionString, output)); |
| 30 | + this.movies = Context.Movies.AsNoTracking().ToList(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private MysqlDbContext Context { get => this._context.Value; } |
| 35 | + |
| 36 | + protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString); |
| 37 | + |
| 38 | + protected override Task<MysqlEntityMovie> GetEntityAsync(string id) |
| 39 | + => Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id)); |
| 40 | + |
| 41 | + protected override Task<int> GetEntityCountAsync() |
| 42 | + => Task.FromResult(Context.Movies.Count()); |
| 43 | + |
| 44 | + protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync() |
| 45 | + => Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context)); |
| 46 | + |
| 47 | + protected override Task<string> GetRandomEntityIdAsync(bool exists) |
| 48 | + => Task.FromResult(exists ? this.movies[this.random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString()); |
| 49 | + #endregion |
| 50 | + |
| 51 | + [SkippableFact] |
| 52 | + public void EntityTableRepository_BadDbSet_Throws() |
| 53 | + { |
| 54 | + Skip.IfNot(CanRunLiveTests()); |
| 55 | + Action act = () => _ = new EntityTableRepository<EntityTableData>(Context); |
| 56 | + act.Should().Throw<ArgumentException>(); |
| 57 | + } |
| 58 | + |
| 59 | + [SkippableFact] |
| 60 | + public void EntityTableRepository_GoodDbSet_Works() |
| 61 | + { |
| 62 | + Skip.IfNot(CanRunLiveTests()); |
| 63 | + Action act = () => _ = new EntityTableRepository<MysqlEntityMovie>(Context); |
| 64 | + act.Should().NotThrow(); |
| 65 | + } |
| 66 | + |
| 67 | + [SkippableFact] |
| 68 | + public async Task WrapExceptionAsync_ThrowsConflictException_WhenDbConcurrencyUpdateExceptionThrown() |
| 69 | + { |
| 70 | + Skip.IfNot(CanRunLiveTests()); |
| 71 | + EntityTableRepository<MysqlEntityMovie> repository = await GetPopulatedRepositoryAsync() as EntityTableRepository<MysqlEntityMovie>; |
| 72 | + string id = await GetRandomEntityIdAsync(true); |
| 73 | + MysqlEntityMovie expectedPayload = await GetEntityAsync(id); |
| 74 | + |
| 75 | + static Task innerAction() => throw new DbUpdateConcurrencyException("Concurrency exception"); |
| 76 | + |
| 77 | + Func<Task> act = async () => await repository.WrapExceptionAsync(id, innerAction); |
| 78 | + (await act.Should().ThrowAsync<HttpException>()).WithStatusCode(409).And.WithPayload(expectedPayload); |
| 79 | + } |
| 80 | + |
| 81 | + [SkippableFact] |
| 82 | + public async Task WrapExceptionAsync_ThrowsRepositoryException_WhenDbUpdateExceptionThrown() |
| 83 | + { |
| 84 | + Skip.IfNot(CanRunLiveTests()); |
| 85 | + EntityTableRepository<MysqlEntityMovie> repository = await GetPopulatedRepositoryAsync() as EntityTableRepository<MysqlEntityMovie>; |
| 86 | + string id = await GetRandomEntityIdAsync(true); |
| 87 | + MysqlEntityMovie expectedPayload = await GetEntityAsync(id); |
| 88 | + |
| 89 | + static Task innerAction() => throw new DbUpdateException("Non-concurrency exception"); |
| 90 | + |
| 91 | + Func<Task> act = async () => await repository.WrapExceptionAsync(id, innerAction); |
| 92 | + await act.Should().ThrowAsync<RepositoryException>(); |
| 93 | + } |
| 94 | +} |
0 commit comments