-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffstoreTest.cs
162 lines (140 loc) · 5.24 KB
/
DiffstoreTest.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Diffstore.Entities;
using Xunit;
namespace Diffstore.Tests
{
public class DiffstoreTest
{
public DiffstoreTest() => Cleanup();
~DiffstoreTest() => Cleanup();
private void Cleanup()
{
if (Directory.Exists("storage"))
Directory.Delete("storage", true);
}
[Theory]
[MemberData(nameof(TestBuilderGenerator<SampleDataWithCollections>.Builders),
MemberType = typeof(TestBuilderGenerator<SampleDataWithCollections>))]
public void ShouldProvideBusinessLogic(
Func<IDiffstore<long, SampleDataWithCollections>> builder)
{
// Configure
var db = builder();
// Create
Assert.Empty(db.Keys);
var key = 1L;
var value = new SampleDataWithCollections()
{
IntProperty = 1337,
PublicString = "Diffstore rules",
MyList = new List<string> { "Apple", "Orange" },
MyDictionary = new Dictionary<string, int> {
{ "Apples", 5 },
{ "Oranges", 3 }
}
};
var entity = Entity.Create(key, value);
Assert.False(entity.ExistsIn(db));
entity.SaveIn(db);
Assert.True(entity.ExistsIn(db));
// Read
var expectedEntities = new[] { entity }.AsEnumerable();
var actualEntities = db.Entities;
Assert.Equal(expectedEntities, actualEntities);
Assert.Equal(key, db[key].Key);
Assert.Equal(value, db[key].Value);
// Update
value.PublicString = "Check me";
entity.SaveIn(db);
Assert.Equal("Check me", db[key].Value.PublicString);
// Delete
entity.DeleteFrom(db);
Assert.False(entity.ExistsIn(db));
}
[Theory]
[MemberData(nameof(TestBuilderGenerator<SampleData>.Builders),
MemberType = typeof(TestBuilderGenerator<SampleData>))]
public void ShouldRaiseEvents(Func<IDiffstore<long, SampleData>> builder)
{
// Arrange
var db = builder();
var value = new SampleData() { PublicString = "Hello" };
var entity = Entity.Create(1L, value);
int saveEventCount = 0; int deleteEventCount = 0;
SetupAssertEvents(entity, db);
db.OnSave += (s, e) => saveEventCount++;
db.OnDelete += (s, e) => deleteEventCount++;
// Act
entity.SaveIn(db);
var duplicateEntity = Entity.Create(1L, value);
duplicateEntity.SaveIn(db);
duplicateEntity.DeleteFrom(db);
entity.DeleteFrom(db);
// Assert
Assert.Equal(1, saveEventCount); // must not fire on duplicate
Assert.Equal(1, deleteEventCount); // same here
}
[Theory]
[MemberData(nameof(TestBuilderGenerator<SampleData>.Builders),
MemberType = typeof(TestBuilderGenerator<SampleData>))]
public void ShouldCloseStreams(Func<IDiffstore<long, SampleData>> builder)
{
var db = builder();
for (int i = 1; i < 5; i++)
{
var entity = Entity.Create(1L, new SampleData() { IntProperty = i });
entity.SaveIn(db);
var actual = db.Get(1L);
Assert.Equal(entity, actual);
}
}
// Helper method
private void SetupAssertEvents(Entity<long, SampleData> entity,
IDiffstore<long, SampleData> db)
{
db.OnSave += (sender, saved) => {
Assert.Equal(db, sender);
Assert.Equal(entity, saved);
};
db.OnDelete += (sender, key) => {
Assert.Equal(db, sender);
Assert.Equal(key, entity.Key);
};
}
private static class TestBuilderGenerator<T>
where T : class, new()
{
private static readonly List<Func<IDiffstore<long, T>>> _builders =
new List<Func<IDiffstore<long, T>>>()
{
BinaryStatisticsOptimized,
() => SingleFile(FileFormat.XML),
() => SingleFile(FileFormat.JSON)
};
private static IDiffstore<long, T> BinaryStatisticsOptimized()
{
return new DiffstoreBuilder<long, T>()
.WithMemoryStorage()
.WithFileBasedEntities()
.WithLastFirstOptimizedSnapshots()
.Setup();
}
private static IDiffstore<long, T> SingleFile(FileFormat format)
{
return new DiffstoreBuilder<long, T>()
.WithMemoryStorage()
.WithFileBasedEntities(format)
.WithSingleFileSnapshots(format)
.Setup();
}
public static IEnumerable<object[]> Builders
{
get { return _builders.Select(x => new [] { (object)x }); }
}
}
}
}