-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMySqlEntityTableRepository_Tests.cs
More file actions
97 lines (77 loc) · 3.91 KB
/
MySqlEntityTableRepository_Tests.cs
File metadata and controls
97 lines (77 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 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.TestCommon;
using CommunityToolkit.Datasync.TestCommon.Databases;
using CommunityToolkit.Datasync.TestCommon.Fixtures;
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;
#pragma warning disable CS9113 // Parameter is unread.
namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test;
[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class MysqlEntityTableRepository_Tests(MySqlDatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<MysqlEntityMovie>, IClassFixture<MySqlDatabaseFixture>, IAsyncLifetime
{
#region Setup
private readonly Random random = new();
private List<MysqlEntityMovie> movies = [];
public async Task InitializeAsync()
{
Context = await MysqlDbContext.CreateContextAsync(fixture.ConnectionString, output);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
}
public async Task DisposeAsync()
{
if (Context is not null)
{
await Context.DisposeAsync();
}
}
private MysqlDbContext Context { get; set; }
protected override bool CanRunLiveTests() => true;
protected override async Task<MysqlEntityMovie> GetEntityAsync(string id)
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
protected override async Task<int> GetEntityCountAsync()
=> await Context.Movies.CountAsync();
protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context));
protected override Task<string> GetRandomEntityIdAsync(bool exists)
=> Task.FromResult(exists ? this.movies[this.random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString());
#endregion
[SkippableFact]
public void EntityTableRepository_BadDbSet_Throws()
{
Skip.IfNot(CanRunLiveTests());
Action act = () => _ = new EntityTableRepository<EntityTableData>(Context);
act.Should().Throw<ArgumentException>();
}
[SkippableFact]
public void EntityTableRepository_GoodDbSet_Works()
{
Skip.IfNot(CanRunLiveTests());
Action act = () => _ = new EntityTableRepository<MysqlEntityMovie>(Context);
act.Should().NotThrow();
}
[SkippableFact]
public async Task WrapExceptionAsync_ThrowsConflictException_WhenDbConcurrencyUpdateExceptionThrown()
{
Skip.IfNot(CanRunLiveTests());
EntityTableRepository<MysqlEntityMovie> repository = await GetPopulatedRepositoryAsync() as EntityTableRepository<MysqlEntityMovie>;
string id = await GetRandomEntityIdAsync(true);
MysqlEntityMovie expectedPayload = await GetEntityAsync(id);
static Task innerAction() => throw new DbUpdateConcurrencyException("Concurrency exception");
Func<Task> act = async () => await repository.WrapExceptionAsync(id, innerAction);
(await act.Should().ThrowAsync<HttpException>()).WithStatusCode(409).And.WithPayload(expectedPayload);
}
[SkippableFact]
public async Task WrapExceptionAsync_ThrowsRepositoryException_WhenDbUpdateExceptionThrown()
{
Skip.IfNot(CanRunLiveTests());
EntityTableRepository<MysqlEntityMovie> repository = await GetPopulatedRepositoryAsync() as EntityTableRepository<MysqlEntityMovie>;
string id = await GetRandomEntityIdAsync(true);
MysqlEntityMovie expectedPayload = await GetEntityAsync(id);
static Task innerAction() => throw new DbUpdateException("Non-concurrency exception");
Func<Task> act = async () => await repository.WrapExceptionAsync(id, innerAction);
await act.Should().ThrowAsync<RepositoryException>();
}
}