-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataModelSimpleChanges.cs
More file actions
248 lines (213 loc) · 9.7 KB
/
Copy pathDataModelSimpleChanges.cs
File metadata and controls
248 lines (213 loc) · 9.7 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using SIL.Harmony.Changes;
using SIL.Harmony.Db;
using SIL.Harmony.Sample.Changes;
using SIL.Harmony.Sample.Models;
using Microsoft.EntityFrameworkCore;
namespace SIL.Harmony.Tests;
public class DataModelSimpleChanges : DataModelTestBase
{
private readonly Guid _entity1Id = Guid.NewGuid();
private readonly Guid _entity2Id = Guid.NewGuid();
[Fact]
public async Task WritingAChangeMakesASnapshot()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
var snapshot = DbContext.Snapshots.Should().ContainSingle().Subject;
snapshot.Entity.Is<Word>().Text.Should().Be("test-value");
await Verify(AllData());
}
[Fact]
public async Task CanUpdateTheNoteField()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
await WriteNextChange(new SetWordNoteChange(_entity1Id, "a word note"));
var word = await DataModel.GetLatest<Word>(_entity1Id);
word!.Text.Should().Be("test-value");
word.Note.Should().Be("a word note");
}
[Fact]
public async Task CanUpdateAWordAfterRestarting()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
var instance2 = ForkDatabase();//creates new services, but copies database. Simulates restarting the application
await instance2.WriteNextChange(new SetWordNoteChange(_entity1Id, "a word note"));
var word = await instance2.DataModel.GetLatest<Word>(_entity1Id);
word!.Text.Should().Be("test-value");
word.Note.Should().Be("a word note");
}
[Fact]
public async Task WritingA2ndChangeDoesNotEffectTheFirstSnapshot()
{
await WriteNextChange(SetWord(_entity1Id, "change1"));
await WriteNextChange(SetWord(_entity1Id, "change2"));
DbContext.Snapshots.Should()
.SatisfyRespectively(
snap1 => snap1.Entity.Is<Word>().Text.Should().Be("change1"),
snap2 => snap2.Entity.Is<Word>().Text.Should().Be("change2")
);
await Verify(AllData());
}
[Fact]
public async Task WritingACommitWithMultipleChangesWorks()
{
await WriteNextChange([
SetWord(_entity1Id, "first"),
SetWord(_entity2Id, "second")
]);
await Verify(AllData());
}
[Fact]
public async Task WriteMultipleCommits()
{
await WriteNextChange(SetWord(Guid.NewGuid(), "change 1"));
await WriteNextChange(SetWord(Guid.NewGuid(), "change 2"));
DbContext.Snapshots.Should().HaveCount(2);
await Verify(DbContext.Commits.DefaultOrder().Include(c => c.ChangeEntities).Include(c => c.Snapshots));
await WriteNextChange(SetWord(Guid.NewGuid(), "change 3"));
DbContext.Snapshots.Should().HaveCount(3);
DataModel.QueryLatest<Word>().ToBlockingEnumerable(TestContext.Current.CancellationToken).Should().HaveCount(3);
}
[Fact]
public async Task WritingNoChangesWorks()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
await AddCommitsViaSync(Array.Empty<Commit>());
var snapshot = DbContext.Snapshots.Should().ContainSingle().Subject;
snapshot.Entity.Is<Word>().Text.Should().Be("test-value");
}
[Fact]
public async Task Writing2ChangesSecondOverwritesFirst()
{
await WriteNextChange(SetWord(_entity1Id, "first"));
await WriteNextChange(SetWord(_entity1Id, "second"));
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
snapshot.Entity.Is<Word>().Text.Should().Be("second");
}
[Fact]
public async Task CanWriteChangesWhenAnUnchangedWordExists()
{
await WriteNextChange(SetWord(_entity2Id, "word-2"));
await WriteNextChange(SetWord(_entity1Id, "word-1"));
await WriteNextChange(SetWord(_entity1Id, "second"));
await WriteNextChange(SetWord(_entity1Id, "third"));
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
snapshot.Entity.Is<Word>().Text.Should().Be("third");
}
[Fact]
public async Task Writing2ChangesSecondOverwritesFirstWithLocalFirst()
{
var firstDate = DateTimeOffset.Now;
var secondDate = DateTimeOffset.UtcNow.AddSeconds(1);
await WriteChange(_localClientId, firstDate, SetWord(_entity1Id, "first"));
await WriteChange(_localClientId, secondDate, SetWord(_entity1Id, "second"));
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
snapshot.Entity.Is<Word>().Text.Should().Be("second");
}
[Fact]
public async Task Writing2ChangesSecondOverwritesFirstWithUtcFirst()
{
var firstDate = DateTimeOffset.UtcNow;
var secondDate = DateTimeOffset.Now.AddSeconds(1);
await WriteChange(_localClientId, firstDate, SetWord(_entity1Id, "first"));
await WriteChange(_localClientId, secondDate, SetWord(_entity1Id, "second"));
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
snapshot.Entity.Is<Word>().Text.Should().Be("second");
}
[Fact]
public async Task Writing2ChangesAtOnceWithMergedHistory()
{
await WriteNextChange(SetWord(_entity1Id, "first"));
var second = await WriteNextChange(SetWord(_entity1Id, "second"));
//add vis sync has some additional logic that depends on proper commit ordering
await AddCommitsViaSync([
await WriteChangeBefore(second, new SetWordNoteChange(_entity1Id, "a word note"), false),
await WriteNextChange(SetWord(_entity1Id, "third"), false)
]);
var word = await DataModel.GetLatest<Word>(_entity1Id);
word!.Text.Should().Be("third");
word.Note.Should().Be("a word note");
await Verify(AllData());
}
[Fact]
public async Task ChangeInsertedInTheMiddleOfHistoryWorks()
{
var first = await WriteNextChange(SetWord(_entity1Id, "first"));
await WriteNextChange(SetWord(_entity1Id, "second"));
await WriteChangeAfter(first, new SetWordNoteChange(_entity1Id, "a word note"));
var word = await DataModel.GetLatest<Word>(_entity1Id);
word!.Text.Should().Be("second");
word.Note.Should().Be("a word note");
}
[Fact]
public async Task CanTrackMultipleEntries()
{
await WriteNextChange(SetWord(_entity1Id, "entity1"));
await WriteNextChange(SetWord(_entity2Id, "entity2"));
(await DataModel.GetLatest<Word>(_entity1Id))!.Text.Should().Be("entity1");
(await DataModel.GetLatest<Word>(_entity2Id))!.Text.Should().Be("entity2");
}
[Fact]
public async Task CanCreate2EntriesOutOfOrder()
{
var commit1 = await WriteNextChange(SetWord(_entity1Id, "entity1"));
await WriteChangeBefore(commit1, SetWord(_entity2Id, "entity2"));
}
[Fact]
public async Task CanDeleteAnEntry()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
var deleteCommit = await WriteNextChange(new DeleteChange<Word>(_entity1Id));
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
snapshot.Entity.DeletedAt.Should().Be(deleteCommit.DateTime);
}
[Fact]
public async Task ApplyChange_ModifiesADeletedEntry()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
var deleteCommit = await WriteNextChange(new DeleteChange<Word>(_entity1Id));
var newNoteChange = new SetWordNoteChange(_entity1Id, "note-after-delete");
newNoteChange.SupportsApplyChange().Should().BeTrue();
newNoteChange.SupportsNewEntity().Should().BeFalse(); // otherwise it will override the delete
await WriteNextChange(newNoteChange);
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
var word = snapshot.Entity.Is<Word>();
word.Text.Should().Be("test-value");
word.Note.Should().Be("note-after-delete");
word.DeletedAt.Should().Be(deleteCommit.DateTime);
}
[Fact]
public async Task NewEntity_UndeletesAnEntry()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
await WriteNextChange(new DeleteChange<Word>(_entity1Id));
var recreateChange = SetWord(_entity1Id, "after-undo-delete");
recreateChange.SupportsNewEntity().Should().BeTrue();
await WriteNextChange(recreateChange);
var snapshot = await DbContext.Snapshots.DefaultOrder().LastAsync(TestContext.Current.CancellationToken);
var word = snapshot.Entity.Is<Word>();
word.Text.Should().Be("after-undo-delete");
word.DeletedAt.Should().Be(null);
}
[Fact]
public async Task ChangesToSnapshotsAreNotSaved()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
var word = await DataModel.GetLatest<Word>(_entity1Id);
word!.Text.Should().Be("test-value");
word.Note.Should().BeNull();
//change made outside the model, should not be saved when writing the next change
word.Note = "a note";
var commit = await WriteNextChange(SetWord(_entity1Id, "after-change"));
var objectSnapshot = commit.Snapshots.Should().ContainSingle().Subject;
objectSnapshot.Entity.Is<Word>().Text.Should().Be("after-change");
objectSnapshot.Entity.Is<Word>().Note.Should().BeNull();
}
// [Fact]
// public async Task CanGetEntryLinq2Db()
// {
// await WriteNextChange(SetWord(_entity1Id, "test-value"));
//
// var entries = await DataModel.GetLatestObjects<Word>().ToArrayAsyncLinqToDB();
// entries.Should().ContainSingle();
// }
}