-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNanoDBTests.cs
283 lines (228 loc) · 11.7 KB
/
NanoDBTests.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System.Reflection;
using AutoFixture;
using AutoFixture.Kernel;
using FluentAssertions;
using Nanoforge.Editor;
using Nanoforge.Render.Resources;
using Nanoforge.Rfg;
namespace Nanoforge.Tests;
public class NanoDBTests
{
private static List<Type> _editorObjectTypes => AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof(EditorObject).IsAssignableFrom(type)).ToList();
public static IEnumerable<object[]> TestEditorObjects;
static NanoDBTests()
{
var typeList = _editorObjectTypes;
Fixture fixture = new();
//Make it so the fixture doesn't complain about circular references on objects. All ZoneObjects can have this issue due to the Parent and Children fields
fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
//Don't set RenderObject fields. AutoFixture throws exceptions on some unsafe types. For the moment its fine to exclude that type from the tests.
fixture.Customizations.Add(new PropertyTypeOmitter(typeof(RenderObjectBase)));
TestEditorObjects = _editorObjectTypes.Select(type => new object[] { new SpecimenContext(fixture).Resolve(type) });
var list = TestEditorObjects.ToList();
}
//Runs once per class that is derived from EditorObject. Tests serialization and deserialization of a single object.
[Theory, MemberData(nameof(TestEditorObjects))]
public void AllEditorObjectSaveLoad(EditorObject originalObject)
{
string testObjectName = "TestObject";
originalObject.Name = testObjectName;
//TODO: Change to write to temporary directory in project dir that gets git ignored
string projectDirectory = "./AllEditorObjectSaveLoadTest/";
string projectFilePath = "";
NanoDB.NewProject(projectDirectory, "AllEditorObjectSaveLoadTest", "UnitTestRunner", "", false);
projectFilePath = NanoDB.CurrentProject.FilePath;
NanoDB.AddObject(originalObject);
NanoDB.Save();
NanoDB.Load(projectFilePath);
EditorObject? deserializedObject = NanoDB.Find(testObjectName);
Assert.NotNull(deserializedObject);
deserializedObject.Should().BeEquivalentTo(originalObject);
}
//Tests serialization & deserialization on several ZoneObjects with parent-child relationships set.
[Fact]
public void ObjectSaveLoadWithHierarchy()
{
string projectDirectory = "./ObjectSaveLoadWithHierarchyTest/";
string projectFilePath = "";
NanoDB.NewProject(projectDirectory, "ObjectSaveLoadWithHierarchyTest", "UnitTestRunner", "", false);
projectFilePath = NanoDB.CurrentProject.FilePath;
Fixture fixture = new();
fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
fixture.Customizations.Add(new PropertyTypeOmitter(typeof(RenderObjectBase)));
//Note: The object types were picked at random to have some variety. This is not representative of a typical RFG map.
var objectA = fixture.Create<RfgMover>();
objectA.Name = "objectA";
objectA.Parent = null;
objectA.Children.Clear();
var objectB = fixture.Create<PlayerStart>();
objectB.Name = "objectB";
objectB.Parent = null;
objectB.Children.Clear();
var objectC = fixture.Create<PlayerStart>();
objectC.Name = "objectC";
objectC.Parent = null;
objectC.Children.Clear();
var objectD = fixture.Create<Weapon>();
objectD.Name = "objectD";
objectD.Parent = null;
objectD.Children.Clear();
objectA.AddChild(objectB);
objectA.AddChild(objectC);
objectC.AddChild(objectD);
NanoDB.AddObject(objectA);
NanoDB.AddObject(objectB);
NanoDB.AddObject(objectC);
NanoDB.AddObject(objectD);
NanoDB.Save();
NanoDB.Load(projectFilePath);
ZoneObject? objectA2 = (ZoneObject?)NanoDB.Find(objectA.Name);
ZoneObject? objectB2 = (ZoneObject?)NanoDB.Find(objectB.Name);
ZoneObject? objectC2 = (ZoneObject?)NanoDB.Find(objectC.Name);
ZoneObject? objectD2 = (ZoneObject?)NanoDB.Find(objectD.Name);
Assert.NotNull(objectA2);
Assert.NotNull(objectB2);
Assert.NotNull(objectC2);
Assert.NotNull(objectD2);
objectA2.Should().BeEquivalentTo(objectA, options => options.IgnoringCyclicReferences());
objectB2.Should().BeEquivalentTo(objectB, options => options.IgnoringCyclicReferences());
objectC2.Should().BeEquivalentTo(objectC, options => options.IgnoringCyclicReferences());
objectD2.Should().BeEquivalentTo(objectD, options => options.IgnoringCyclicReferences());
//TODO: Figure out a better way of handling this
//Manually check parent/child fields since I couldn't figure out how to do it with FluentAssertions
Assert.True(ReferenceEquals(objectA2, objectB2.Parent));
Assert.True(ReferenceEquals(objectA2, objectC2.Parent));
Assert.True(objectA2.Children.Count == 2);
Assert.Contains(objectB2, objectA2.Children);
Assert.Contains(objectC2, objectA2.Children);
Assert.True(objectB2.Children.Count == 0);
Assert.True(ReferenceEquals(objectC2, objectD2.Parent));
Assert.True(objectC2.Children.Count == 1);
Assert.Contains(objectD2, objectC2.Children);
Assert.True(objectD2.Children.Count == 0);
}
//Tests save/load for global objects. These are just like normal editor objects, except they aren't tied to a project. Used for persistent data like editor settings.
[Fact]
public void GlobalObjectSaveLoad()
{
Fixture fixture = new();
fixture.Customizations.Add(new PropertyTypeOmitter(typeof(RenderObjectBase)));
GeneralSettings objA = fixture.Create<GeneralSettings>();
objA.Name = "objA-Global";
NanoDB.AddGlobalObject(objA);
NanoDB.SaveGlobals();
NanoDB.LoadGlobals();
GeneralSettings? objA2 = (GeneralSettings?)NanoDB.Find(objA.Name);
Assert.NotNull(objA2);
objA.Should().BeEquivalentTo(objA2, options => options.IgnoringCyclicReferences());
//Cleanup globals so there's a clean state for any tests that follow
NanoDB.ClearGlobals();
NanoDB.SaveGlobals();
}
//Test basic buffer save/load
[Fact]
public void BufferSaveLoad()
{
string projectDirectory = "./BufferSaveLoadTest/";
NanoDB.NewProject(projectDirectory, "BufferSaveLoadTest", "UnitTestRunner", "", false);
Fixture fixture = new();
byte[] bufferBytes = fixture.CreateMany<byte>(1024).ToArray();
Assert.NotNull(bufferBytes);
ProjectBuffer? buffer = NanoDB.CreateBuffer(bufferBytes, "TestProjectBuffer");
Assert.NotNull(buffer);
byte[] bufferBytesLoaded = buffer.Load();
Assert.NotNull(bufferBytesLoaded);
Assert.Equal(bufferBytes, bufferBytesLoaded);
}
//Test that buffer metadata is preserved across project save/load
[Fact]
public void BufferProjectSaveLoad()
{
string projectDirectory = "./BufferProjectSaveLoadTest/";
string projectFilePath = "";
NanoDB.NewProject(projectDirectory, "BufferProjectSaveLoadTest", "UnitTestRunner", "", false);
projectFilePath = NanoDB.CurrentProject.FilePath;
Fixture fixture = new();
byte[] bufferBytes = fixture.CreateMany<byte>(1024).ToArray();
Assert.NotNull(bufferBytes);
ProjectBuffer? buffer = NanoDB.CreateBuffer(bufferBytes, "TestProjectBuffer");
Assert.NotNull(buffer);
NanoDB.Save();
NanoDB.Load(projectFilePath);
ProjectBuffer? bufferLoaded = NanoDB.Find<ProjectBuffer>(buffer.Name);
Assert.NotNull(bufferLoaded);
//Load the buffer from the drive again and make sure it still works
byte[] bufferBytesLoaded = bufferLoaded.Load();
Assert.NotNull(bufferBytesLoaded);
Assert.Equal(bufferBytes, bufferBytesLoaded);
}
//Test writing a smaller byte array to a buffer than it originally had
[Fact]
public void BufferShrink()
{
string projectDirectory = "./BufferShrinkTest/";
NanoDB.NewProject(projectDirectory, "BufferShrinkTest", "UnitTestRunner", "", false);
Fixture fixture = new();
byte[] bufferBytes = fixture.CreateMany<byte>(1024).ToArray();
Assert.NotNull(bufferBytes);
ProjectBuffer? buffer = NanoDB.CreateBuffer(bufferBytes, "TestProjectBuffer");
Assert.NotNull(buffer);
byte[] bufferBytes2 = fixture.CreateMany<byte>(10).ToArray();
Assert.NotNull(bufferBytes2);
Assert.True(buffer.Save(bufferBytes2));
byte[] bufferBytes2Loaded = buffer.Load();
Assert.NotNull(bufferBytes2Loaded);
Assert.Equal(bufferBytes2, bufferBytes2Loaded);
Assert.NotEqual(bufferBytes, bufferBytes2Loaded);
}
//Test writing a larger byte array to a buffer than it originally had
[Fact]
public void BufferGrow()
{
string projectDirectory = "./BufferGrowTest/";
NanoDB.NewProject(projectDirectory, "BufferGrowTest", "UnitTestRunner", "", false);
Fixture fixture = new();
byte[] bufferBytes = fixture.CreateMany<byte>(1024).ToArray();
Assert.NotNull(bufferBytes);
ProjectBuffer? buffer = NanoDB.CreateBuffer(bufferBytes, "TestProjectBuffer");
Assert.NotNull(buffer);
byte[] bufferBytes2 = fixture.CreateMany<byte>(2048).ToArray();
Assert.NotNull(bufferBytes2);
Assert.True(buffer.Save(bufferBytes2));
byte[] bufferBytes2Loaded = buffer.Load();
Assert.NotNull(bufferBytes2Loaded);
Assert.Equal(bufferBytes2, bufferBytes2Loaded);
Assert.NotEqual(bufferBytes, bufferBytes2Loaded);
}
[Fact]
public void ReferencedBufferSaveLoad()
{
string projectDirectory = "./ReferencedBufferSaveLoad/";
string projectFilePath = "";
NanoDB.NewProject(projectDirectory, "ReferencedBufferSaveLoad", "UnitTestRunner", "", false);
projectFilePath = NanoDB.CurrentProject.FilePath;
Fixture fixture = new();
byte[] bufferBytes = fixture.CreateMany<byte>(1024).ToArray();
ProjectBuffer? buffer = NanoDB.CreateBuffer(bufferBytes, "ReferencedProjectBuffer");
Assert.NotNull(buffer);
ProjectTexture referencingObject = new(buffer);
referencingObject.Name = "ObjectThatReferencesBuffer";
NanoDB.AddObject(referencingObject);
NanoDB.Save();
NanoDB.Load(projectFilePath);
ProjectTexture? referencingObjectLoaded = NanoDB.Find<ProjectTexture>(referencingObject.Name);
Assert.NotNull(referencingObjectLoaded);
ProjectBuffer? bufferLoadedFromReference = referencingObjectLoaded.Data;
Assert.NotNull(bufferLoadedFromReference);
ProjectBuffer? bufferLoadedFromSearch = NanoDB.Find<ProjectBuffer>(buffer.Name);
Assert.NotNull(bufferLoadedFromSearch);
//The object should be referencing the same ProjectBuffer instance that NanoDB tracks
Assert.True(ReferenceEquals(bufferLoadedFromReference, bufferLoadedFromSearch));
//The original buffer and the instance created while loading the project should have the same name and size
Assert.Equivalent(buffer, bufferLoadedFromSearch);
}
}