-
-
Notifications
You must be signed in to change notification settings - Fork 806
Expand file tree
/
Copy pathSagaServiceRegistrationTests.cs
More file actions
102 lines (82 loc) · 3.92 KB
/
Copy pathSagaServiceRegistrationTests.cs
File metadata and controls
102 lines (82 loc) · 3.92 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
98
99
100
101
102
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Mocha.EntityFrameworkCore.Postgres.Tests.Helpers;
using Mocha.Sagas;
using Mocha.Sagas.EfCore;
using Mocha.Transport.InMemory;
namespace Mocha.EntityFrameworkCore.Postgres.Tests;
public sealed class SagaServiceRegistrationTests
{
private const string ConnectionString = "Host=localhost;Database=test";
[Fact]
public void Create_Should_ReturnSagaStore_When_ValidDependencies()
{
// Arrange
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql(ConnectionString).Options;
using var context = new TestDbContext(options);
var queries = PostgresSagaStoreQueries.From(new SagaStateTableInfo());
// Act
using var store = new PostgresSagaStore(context, queries, TimeProvider.System);
// Assert
Assert.IsAssignableFrom<ISagaStore>(store);
Assert.IsType<PostgresSagaStore>(store);
}
[Fact]
public void Queries_Should_BeConfigured_When_CreatedFromDefaultTableInfo()
{
// Arrange & Act
var queries = PostgresSagaStoreQueries.From(new SagaStateTableInfo());
// Assert - verify all query strings are populated (non-null, non-empty).
Assert.False(string.IsNullOrWhiteSpace(queries.SelectState));
Assert.False(string.IsNullOrWhiteSpace(queries.SelectVersion));
Assert.False(string.IsNullOrWhiteSpace(queries.InsertState));
Assert.False(string.IsNullOrWhiteSpace(queries.UpdateState));
Assert.False(string.IsNullOrWhiteSpace(queries.DeleteState));
}
[Fact]
public async Task AddPostgresSagas_Should_RegisterScopedSagaStore_When_Called()
{
// Arrange
await using var provider = BuildProvider();
// Act
using var scope = provider.CreateScope();
var store = scope.ServiceProvider.GetService<ISagaStore>();
// Assert
Assert.NotNull(store);
Assert.IsType<PostgresSagaStore>(store);
}
[Fact]
public async Task AddPostgresSagas_Should_ConfigureQueriesFromModel_When_DefaultTableNames()
{
// Arrange
await using var provider = BuildProvider();
// Act
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<PostgresSagaStoreOptions>>();
var contextName = typeof(TestDbContext).FullName!;
var options = optionsMonitor.Get(contextName);
// Assert
Assert.False(string.IsNullOrWhiteSpace(options.Queries.SelectState));
Assert.False(string.IsNullOrWhiteSpace(options.Queries.SelectVersion));
Assert.False(string.IsNullOrWhiteSpace(options.Queries.InsertState));
Assert.False(string.IsNullOrWhiteSpace(options.Queries.UpdateState));
Assert.False(string.IsNullOrWhiteSpace(options.Queries.DeleteState));
}
// ══════════════════════════════════════════════════════════════════════
// Helpers
// ══════════════════════════════════════════════════════════════════════
private static ServiceProvider BuildProvider()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton(TimeProvider.System);
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(ConnectionString));
var builder = services.AddMessageBus();
builder.AddEntityFramework<TestDbContext>(ef => ef.AddPostgresSagas());
builder.AddInMemory();
var provider = services.BuildServiceProvider();
// Build the runtime so that all singleton factories resolve
_ = provider.GetRequiredService<IMessagingRuntime>();
return provider;
}
}