forked from jonsequitur/Its.Cqrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryEventStoreTests.cs
115 lines (91 loc) · 3.27 KB
/
InMemoryEventStoreTests.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using FluentAssertions;
using System.Linq;
using System.Reactive.Disposables;
using System.Threading.Tasks;
using Microsoft.Its.Domain.Sql;
using Microsoft.Its.Domain.Sql.Tests;
using Microsoft.Its.Recipes;
using NUnit.Framework;
using Test.Domain.Ordering;
namespace Microsoft.Its.Domain.Testing.Tests
{
[TestFixture]
public class InMemoryEventStoreTests
{
private CompositeDisposable disposables;
[SetUp]
public void SetUp()
{
var configuration = new Configuration()
.UseInMemoryEventStore();
disposables = new CompositeDisposable
{
ConfigurationContext.Establish(configuration),
configuration
};
}
[TearDown]
public void TearDown()
{
disposables.Dispose();
}
[Test]
public async Task Events_can_be_added_to_the_context_and_queried_back()
{
var aggregateId = Any.Guid();
var storableEvent = new Order.Created
{
CustomerName = Any.FullName(),
AggregateId = aggregateId
}.ToStorableEvent();
var eventStream = new InMemoryEventStream();
using (var db = new InMemoryEventStoreDbContext(eventStream))
{
db.Events.Add(storableEvent);
await db.SaveChangesAsync();
var orderCreated = db.Events.Single(e => e.AggregateId == aggregateId);
orderCreated.Should().NotBeNull();
}
}
[Test]
public async Task Events_in_the_currently_configured_event_stream_can_be_queried_from_the_In_memory_context()
{
var aggregateId = Any.Guid();
var storableEvent = new Order.Created
{
CustomerName = Any.FullName(),
AggregateId = aggregateId
}.ToStorableEvent();
var eventStream = new InMemoryEventStream();
await eventStream.Append(new[] { storableEvent.ToInMemoryStoredEvent() });
Configuration.Current.UseDependency(_ => eventStream);
using (var db = new InMemoryEventStoreDbContext())
{
var orderCreated = db.Events.Single(e => e.AggregateId == aggregateId);
orderCreated.Should().NotBeNull();
}
}
[Test]
public async Task Events_saved_to_the_in_memory_context_have_their_id_set()
{
var eventStream = new InMemoryEventStream();
using (var db = new InMemoryEventStoreDbContext(eventStream))
{
Events.Write(5, createEventStore: () => db);
await db.SaveChangesAsync();
Console.WriteLine(db.Events.Count());
}
using (var db = new InMemoryEventStoreDbContext(eventStream))
{
Console.WriteLine(db.Events.Count());
db.Events
.Select(e => e.Id)
.Should()
.BeEquivalentTo(1L, 2L, 3L, 4L, 5L);
}
}
}
}