Reformat / cleanup
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
using ZB.MOM.WW.CBDD.Core.Storage;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests;
|
||||
|
||||
public class StorageEngineDictionaryTests
|
||||
{
|
||||
private string GetTempDbPath() => Path.Combine(Path.GetTempPath(), $"test_storage_dict_{Guid.NewGuid()}.db");
|
||||
private string GetTempDbPath()
|
||||
{
|
||||
return Path.Combine(Path.GetTempPath(), $"test_storage_dict_{Guid.NewGuid()}.db");
|
||||
}
|
||||
|
||||
private void Cleanup(string path)
|
||||
{
|
||||
@@ -14,34 +16,37 @@ public class StorageEngineDictionaryTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies dictionary pages are initialized and return normalized keys.
|
||||
/// Verifies dictionary pages are initialized and return normalized keys.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void StorageEngine_ShouldInitializeDictionary()
|
||||
{
|
||||
var path = GetTempDbPath();
|
||||
string path = GetTempDbPath();
|
||||
try
|
||||
{
|
||||
using (var storage = new StorageEngine(path, PageFileConfig.Default))
|
||||
{
|
||||
// Should generate ID > 100
|
||||
var id = storage.GetOrAddDictionaryEntry("TestKey");
|
||||
ushort id = storage.GetOrAddDictionaryEntry("TestKey");
|
||||
(id > DictionaryPage.ReservedValuesEnd).ShouldBeTrue();
|
||||
|
||||
var key = storage.GetDictionaryKey(id);
|
||||
string? key = storage.GetDictionaryKey(id);
|
||||
key.ShouldBe("testkey");
|
||||
}
|
||||
}
|
||||
finally { Cleanup(path); }
|
||||
finally
|
||||
{
|
||||
Cleanup(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies dictionary entries persist across reopen.
|
||||
/// Verifies dictionary entries persist across reopen.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void StorageEngine_ShouldPersistDictionary()
|
||||
{
|
||||
var path = GetTempDbPath();
|
||||
string path = GetTempDbPath();
|
||||
try
|
||||
{
|
||||
ushort id1, id2;
|
||||
@@ -54,8 +59,8 @@ public class StorageEngineDictionaryTests
|
||||
// Reopen
|
||||
using (var storage = new StorageEngine(path, PageFileConfig.Default))
|
||||
{
|
||||
var val1 = storage.GetOrAddDictionaryEntry("Key1");
|
||||
var val2 = storage.GetOrAddDictionaryEntry("Key2");
|
||||
ushort val1 = storage.GetOrAddDictionaryEntry("Key1");
|
||||
ushort val2 = storage.GetOrAddDictionaryEntry("Key2");
|
||||
|
||||
val1.ShouldBe(id1);
|
||||
val2.ShouldBe(id2);
|
||||
@@ -64,16 +69,19 @@ public class StorageEngineDictionaryTests
|
||||
storage.GetDictionaryKey(val2).ShouldBe("key2");
|
||||
}
|
||||
}
|
||||
finally { Cleanup(path); }
|
||||
finally
|
||||
{
|
||||
Cleanup(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies dictionary handling scales to many keys and remains durable.
|
||||
/// Verifies dictionary handling scales to many keys and remains durable.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void StorageEngine_ShouldHandleManyKeys()
|
||||
{
|
||||
var path = GetTempDbPath();
|
||||
string path = GetTempDbPath();
|
||||
try
|
||||
{
|
||||
const int keyCount = 3000;
|
||||
@@ -81,10 +89,10 @@ public class StorageEngineDictionaryTests
|
||||
|
||||
using (var storage = new StorageEngine(path, PageFileConfig.Default))
|
||||
{
|
||||
for (int i = 0; i < keyCount; i++)
|
||||
for (var i = 0; i < keyCount; i++)
|
||||
{
|
||||
var key = $"Key_{i}";
|
||||
var id = storage.GetOrAddDictionaryEntry(key);
|
||||
ushort id = storage.GetOrAddDictionaryEntry(key);
|
||||
expectedIds[key] = id;
|
||||
}
|
||||
}
|
||||
@@ -92,22 +100,25 @@ public class StorageEngineDictionaryTests
|
||||
// Reopen and Verify
|
||||
using (var storage = new StorageEngine(path, PageFileConfig.Default))
|
||||
{
|
||||
for (int i = 0; i < keyCount; i++)
|
||||
for (var i = 0; i < keyCount; i++)
|
||||
{
|
||||
var key = $"Key_{i}";
|
||||
var id = storage.GetOrAddDictionaryEntry(key); // Should get existing
|
||||
ushort id = storage.GetOrAddDictionaryEntry(key); // Should get existing
|
||||
id.ShouldBe(expectedIds[key]);
|
||||
|
||||
var loadedKey = storage.GetDictionaryKey(id);
|
||||
string? loadedKey = storage.GetDictionaryKey(id);
|
||||
loadedKey.ShouldBe(key.ToLowerInvariant());
|
||||
}
|
||||
|
||||
// Add new one
|
||||
var newId = storage.GetOrAddDictionaryEntry("NewKeyAfterReopen");
|
||||
ushort newId = storage.GetOrAddDictionaryEntry("NewKeyAfterReopen");
|
||||
(newId > 0).ShouldBeTrue();
|
||||
expectedIds.ContainsValue(newId).ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
finally { Cleanup(path); }
|
||||
finally
|
||||
{
|
||||
Cleanup(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user