-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Exercise03CreateAppendEventFunction.cs
71 lines (56 loc) · 1.94 KB
/
Exercise03CreateAppendEventFunction.cs
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
using Dapper;
using FluentAssertions;
using Npgsql;
using Tools.Tools;
using Xunit;
namespace EventStoreBasics.Tests;
public class Exercise03CreateAppendEventFunction
{
public record User(
string Name
);
public record UserCreated(
string Name
);
private readonly NpgsqlConnection databaseConnection;
private readonly PostgresSchemaProvider schemaProvider;
private readonly EventStore eventStore;
private const string AppendEventFunctionName = "append_event";
/// <summary>
/// Inits Event Store
/// </summary>
public Exercise03CreateAppendEventFunction()
{
databaseConnection = PostgresDbConnectionProvider.GetFreshDbConnection();
schemaProvider = new PostgresSchemaProvider(databaseConnection);
// Create Event Store
eventStore = new EventStore(databaseConnection);
// Initialize Event Store
eventStore.Init();
}
[Fact]
[Trait("Category", "SkipCI")]
public void AppendEventFunction_ShouldBeCreated()
{
var appendFunctionExists = schemaProvider
.FunctionExists(AppendEventFunctionName);
appendFunctionExists.Should().BeTrue();
}
[Fact]
[Trait("Category", "SkipCI")]
public void AppendEventFunction_WhenStreamDoesNotExist_CreateNewStream_And_AppendNewEvent()
{
var streamId = Guid.NewGuid();
var @event = new UserCreated("John Doe");
var result = eventStore.AppendEvent<User>(streamId, @event);
result.Should().BeTrue();
var wasStreamCreated = databaseConnection.QuerySingle<bool>(
"select exists (select 1 from streams where id = @streamId)", new {streamId}
);
wasStreamCreated.Should().BeTrue();
var wasEventAppended = databaseConnection.QuerySingle<bool>(
"select exists (select 1 from events where stream_id = @streamId)", new {streamId}
);
wasEventAppended.Should().BeTrue();
}
}