Implement checkpoint modes with docs/tests and reorganize project file layout
This commit is contained in:
197
tests/CBDD.Tests/Bson/BsonDocumentAndBufferWriterTests.cs
Normal file
197
tests/CBDD.Tests/Bson/BsonDocumentAndBufferWriterTests.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
using System.Buffers;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests;
|
||||
|
||||
public class BsonDocumentAndBufferWriterTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifies BSON document creation and typed retrieval roundtrip.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonDocument_Create_And_TryGet_RoundTrip()
|
||||
{
|
||||
var keyMap = new ConcurrentDictionary<string, ushort>(StringComparer.OrdinalIgnoreCase);
|
||||
var reverseMap = new ConcurrentDictionary<ushort, string>();
|
||||
|
||||
RegisterKey(keyMap, reverseMap, 1, "name");
|
||||
RegisterKey(keyMap, reverseMap, 2, "age");
|
||||
RegisterKey(keyMap, reverseMap, 3, "_id");
|
||||
|
||||
var expectedId = ObjectId.NewObjectId();
|
||||
|
||||
var doc = BsonDocument.Create(keyMap, b =>
|
||||
{
|
||||
b.AddString("name", "Alice");
|
||||
b.AddInt32("age", 32);
|
||||
b.AddObjectId("_id", expectedId);
|
||||
});
|
||||
|
||||
var wrapped = new BsonDocument(doc.RawData.ToArray(), reverseMap);
|
||||
|
||||
wrapped.TryGetString("name", out var name).ShouldBeTrue();
|
||||
name.ShouldBe("Alice");
|
||||
|
||||
wrapped.TryGetInt32("age", out var age).ShouldBeTrue();
|
||||
age.ShouldBe(32);
|
||||
|
||||
wrapped.TryGetObjectId("_id", out var id).ShouldBeTrue();
|
||||
id.ShouldBe(expectedId);
|
||||
|
||||
var reader = wrapped.GetReader();
|
||||
reader.ReadDocumentSize().ShouldBeGreaterThan(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies typed getters return false for missing fields and type mismatches.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonDocument_TryGet_Should_Return_False_For_Missing_Or_Wrong_Type()
|
||||
{
|
||||
var keyMap = new ConcurrentDictionary<string, ushort>(StringComparer.OrdinalIgnoreCase);
|
||||
var reverseMap = new ConcurrentDictionary<ushort, string>();
|
||||
|
||||
RegisterKey(keyMap, reverseMap, 1, "name");
|
||||
RegisterKey(keyMap, reverseMap, 2, "age");
|
||||
|
||||
var doc = BsonDocument.Create(keyMap, b =>
|
||||
{
|
||||
b.AddString("name", "Bob");
|
||||
b.AddInt32("age", 28);
|
||||
});
|
||||
|
||||
var wrapped = new BsonDocument(doc.RawData.ToArray(), reverseMap);
|
||||
|
||||
wrapped.TryGetInt32("name", out _).ShouldBeFalse();
|
||||
wrapped.TryGetString("missing", out _).ShouldBeFalse();
|
||||
wrapped.TryGetObjectId("age", out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the BSON document builder grows its internal buffer for large documents.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonDocumentBuilder_Should_Grow_Buffer_When_Document_Is_Large()
|
||||
{
|
||||
var keyMap = new ConcurrentDictionary<string, ushort>(StringComparer.OrdinalIgnoreCase);
|
||||
var reverseMap = new ConcurrentDictionary<ushort, string>();
|
||||
|
||||
for (ushort i = 1; i <= 180; i++)
|
||||
{
|
||||
var key = $"k{i}";
|
||||
RegisterKey(keyMap, reverseMap, i, key);
|
||||
}
|
||||
|
||||
var builder = new BsonDocumentBuilder(keyMap);
|
||||
for (int i = 1; i <= 180; i++)
|
||||
{
|
||||
builder.AddInt32($"k{i}", i);
|
||||
}
|
||||
|
||||
var doc = builder.Build();
|
||||
doc.Size.ShouldBeGreaterThan(1024);
|
||||
|
||||
var wrapped = new BsonDocument(doc.RawData.ToArray(), reverseMap);
|
||||
wrapped.TryGetInt32("k180", out var value).ShouldBeTrue();
|
||||
value.ShouldBe(180);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies BSON buffer writer emits expected nested document and array layout.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonBufferWriter_Should_Write_Nested_Document_And_Array()
|
||||
{
|
||||
var output = new ArrayBufferWriter<byte>();
|
||||
var writer = new BsonBufferWriter(output);
|
||||
|
||||
int rootSizePos = writer.BeginDocument();
|
||||
|
||||
int childSizePos = writer.BeginDocument("child");
|
||||
writer.WriteString("name", "nested");
|
||||
writer.WriteBoolean("active", true);
|
||||
writer.EndDocument(childSizePos);
|
||||
int childEnd = writer.Position;
|
||||
|
||||
int arraySizePos = writer.BeginArray("nums");
|
||||
writer.WriteInt32("0", 1);
|
||||
writer.WriteInt32("1", 2);
|
||||
writer.EndArray(arraySizePos);
|
||||
int arrayEnd = writer.Position;
|
||||
|
||||
writer.EndDocument(rootSizePos);
|
||||
int rootEnd = writer.Position;
|
||||
|
||||
var bytes = output.WrittenSpan.ToArray();
|
||||
PatchDocumentSize(bytes, childSizePos, childEnd);
|
||||
PatchDocumentSize(bytes, arraySizePos, arrayEnd);
|
||||
PatchDocumentSize(bytes, rootSizePos, rootEnd);
|
||||
|
||||
var reader = new BsonSpanReader(bytes, new ConcurrentDictionary<ushort, string>());
|
||||
|
||||
reader.ReadDocumentSize().ShouldBe(bytes.Length);
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.Document);
|
||||
reader.ReadCString().ShouldBe("child");
|
||||
reader.ReadDocumentSize().ShouldBeGreaterThan(8);
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.String);
|
||||
reader.ReadCString().ShouldBe("name");
|
||||
reader.ReadString().ShouldBe("nested");
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.Boolean);
|
||||
reader.ReadCString().ShouldBe("active");
|
||||
reader.ReadBoolean().ShouldBeTrue();
|
||||
reader.ReadBsonType().ShouldBe(BsonType.EndOfDocument);
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.Array);
|
||||
reader.ReadCString().ShouldBe("nums");
|
||||
reader.ReadDocumentSize().ShouldBeGreaterThan(8);
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.Int32);
|
||||
reader.ReadCString().ShouldBe("0");
|
||||
reader.ReadInt32().ShouldBe(1);
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.Int32);
|
||||
reader.ReadCString().ShouldBe("1");
|
||||
reader.ReadInt32().ShouldBe(2);
|
||||
reader.ReadBsonType().ShouldBe(BsonType.EndOfDocument);
|
||||
|
||||
reader.ReadBsonType().ShouldBe(BsonType.EndOfDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies single-byte and C-string span reads operate correctly.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonSpanReader_ReadByte_And_ReadCStringSpan_Should_Work()
|
||||
{
|
||||
var singleByteReader = new BsonSpanReader(new byte[] { 0x2A }, new ConcurrentDictionary<ushort, string>());
|
||||
singleByteReader.ReadByte().ShouldBe((byte)0x2A);
|
||||
|
||||
var cstring = Encoding.UTF8.GetBytes("hello\0");
|
||||
var cstringReader = new BsonSpanReader(cstring, new ConcurrentDictionary<ushort, string>());
|
||||
var destination = new char[16];
|
||||
var written = cstringReader.ReadCString(destination);
|
||||
|
||||
new string(destination, 0, written).ShouldBe("hello");
|
||||
}
|
||||
|
||||
private static void RegisterKey(
|
||||
ConcurrentDictionary<string, ushort> keyMap,
|
||||
ConcurrentDictionary<ushort, string> reverseMap,
|
||||
ushort id,
|
||||
string key)
|
||||
{
|
||||
keyMap[key] = id;
|
||||
reverseMap[id] = key;
|
||||
}
|
||||
|
||||
private static void PatchDocumentSize(byte[] output, int sizePosition, int endPosition)
|
||||
{
|
||||
BinaryPrimitives.WriteInt32LittleEndian(output.AsSpan(sizePosition, 4), endPosition - sizePosition);
|
||||
}
|
||||
}
|
||||
136
tests/CBDD.Tests/Bson/BsonSchemaTests.cs
Executable file
136
tests/CBDD.Tests/Bson/BsonSchemaTests.cs
Executable file
@@ -0,0 +1,136 @@
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
using ZB.MOM.WW.CBDD.Core.Collections;
|
||||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests;
|
||||
|
||||
public class BsonSchemaTests
|
||||
{
|
||||
public class SimpleEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the identifier.
|
||||
/// </summary>
|
||||
public ObjectId Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name.
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the age.
|
||||
/// </summary>
|
||||
public int Age { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the entity is active.
|
||||
/// </summary>
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies schema generation for a simple entity.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_SimpleEntity()
|
||||
{
|
||||
var schema = BsonSchemaGenerator.FromType<SimpleEntity>();
|
||||
|
||||
schema.Title.ShouldBe("SimpleEntity");
|
||||
schema.Fields.Count.ShouldBe(4);
|
||||
|
||||
var idField = schema.Fields.First(f => f.Name == "_id");
|
||||
idField.Type.ShouldBe(BsonType.ObjectId);
|
||||
|
||||
var nameField = schema.Fields.First(f => f.Name == "name");
|
||||
nameField.Type.ShouldBe(BsonType.String);
|
||||
|
||||
var ageField = schema.Fields.First(f => f.Name == "age");
|
||||
ageField.Type.ShouldBe(BsonType.Int32);
|
||||
}
|
||||
|
||||
public class CollectionEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets tags.
|
||||
/// </summary>
|
||||
public List<string> Tags { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets scores.
|
||||
/// </summary>
|
||||
public int[] Scores { get; set; } = Array.Empty<int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies schema generation for collection fields.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_Collections()
|
||||
{
|
||||
var schema = BsonSchemaGenerator.FromType<CollectionEntity>();
|
||||
|
||||
var tags = schema.Fields.First(f => f.Name == "tags");
|
||||
tags.Type.ShouldBe(BsonType.Array);
|
||||
tags.ArrayItemType.ShouldBe(BsonType.String);
|
||||
|
||||
var scores = schema.Fields.First(f => f.Name == "scores");
|
||||
scores.Type.ShouldBe(BsonType.Array);
|
||||
scores.ArrayItemType.ShouldBe(BsonType.Int32);
|
||||
}
|
||||
|
||||
public class NestedEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the parent entity.
|
||||
/// </summary>
|
||||
public SimpleEntity Parent { get; set; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies schema generation for nested document fields.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_Nested()
|
||||
{
|
||||
var schema = BsonSchemaGenerator.FromType<NestedEntity>();
|
||||
|
||||
var parent = schema.Fields.First(f => f.Name == "parent");
|
||||
parent.Type.ShouldBe(BsonType.Document);
|
||||
parent.NestedSchema.ShouldNotBeNull();
|
||||
parent.NestedSchema.Fields.ShouldContain(f => f.Name == "_id");
|
||||
}
|
||||
|
||||
public class ComplexCollectionEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets items.
|
||||
/// </summary>
|
||||
public List<SimpleEntity> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies schema generation for collections of complex types.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_ComplexCollection()
|
||||
{
|
||||
var schema = BsonSchemaGenerator.FromType<ComplexCollectionEntity>();
|
||||
|
||||
var items = schema.Fields.First(f => f.Name == "items");
|
||||
items.Type.ShouldBe(BsonType.Array);
|
||||
// items.ArrayItemType.ShouldBe(BsonType.Document); // Wait, my generator logic might return Array here? No, item type logic...
|
||||
|
||||
// Let's verify generator logic for complex array item type
|
||||
// In generator: (BsonType.Array, itemNested, itemBsonType)
|
||||
// itemBsonType for SimpleEntity should be Document
|
||||
|
||||
items.ArrayItemType.ShouldBe(BsonType.Document);
|
||||
items.NestedSchema.ShouldNotBeNull();
|
||||
items.NestedSchema.Fields.ShouldContain(f => f.Name == "_id");
|
||||
}
|
||||
}
|
||||
279
tests/CBDD.Tests/Bson/BsonSpanReaderWriterTests.cs
Executable file
279
tests/CBDD.Tests/Bson/BsonSpanReaderWriterTests.cs
Executable file
@@ -0,0 +1,279 @@
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
using Xunit;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests;
|
||||
|
||||
public class BsonSpanReaderWriterTests
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, ushort> _keyMap = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly ConcurrentDictionary<ushort, string> _keys = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BsonSpanReaderWriterTests"/> class.
|
||||
/// </summary>
|
||||
public BsonSpanReaderWriterTests()
|
||||
{
|
||||
ushort id = 1;
|
||||
string[] initialKeys = ["name", "age", "active", "_id", "val", "dec", "timestamp", "int32", "int64", "double", "data", "child", "value", "0", "1"];
|
||||
foreach (var key in initialKeys)
|
||||
{
|
||||
_keyMap[key] = id;
|
||||
_keys[id] = key;
|
||||
id++;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read simple document.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_SimpleDocument()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
writer.WriteString("name", "John");
|
||||
writer.WriteInt32("age", 30);
|
||||
writer.WriteBoolean("active", true);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
var size = reader.ReadDocumentSize();
|
||||
|
||||
size.ShouldBe(writer.Position);
|
||||
|
||||
var type1 = reader.ReadBsonType();
|
||||
var name1 = reader.ReadElementHeader();
|
||||
var value1 = reader.ReadString();
|
||||
|
||||
type1.ShouldBe(BsonType.String);
|
||||
name1.ShouldBe("name");
|
||||
value1.ShouldBe("John");
|
||||
|
||||
var type2 = reader.ReadBsonType();
|
||||
var name2 = reader.ReadElementHeader();
|
||||
var value2 = reader.ReadInt32();
|
||||
|
||||
type2.ShouldBe(BsonType.Int32);
|
||||
name2.ShouldBe("age");
|
||||
value2.ShouldBe(30);
|
||||
|
||||
var type3 = reader.ReadBsonType();
|
||||
var name3 = reader.ReadElementHeader();
|
||||
var value3 = reader.ReadBoolean();
|
||||
|
||||
type3.ShouldBe(BsonType.Boolean);
|
||||
name3.ShouldBe("active");
|
||||
value3.ShouldBeTrue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read object id.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_ObjectId()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var oid = ObjectId.NewObjectId();
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
writer.WriteObjectId("_id", oid);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var readOid = reader.ReadObjectId();
|
||||
|
||||
type.ShouldBe(BsonType.ObjectId);
|
||||
name.ShouldBe("_id");
|
||||
readOid.ShouldBe(oid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests read write double.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ReadWrite_Double()
|
||||
{
|
||||
var buffer = new byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
writer.WriteDouble("val", 123.456);
|
||||
|
||||
var reader = new BsonSpanReader(buffer, _keys);
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var val = reader.ReadDouble();
|
||||
|
||||
type.ShouldBe(BsonType.Double);
|
||||
name.ShouldBe("val");
|
||||
val.ShouldBe(123.456);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests read write decimal128 round trip.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ReadWrite_Decimal128_RoundTrip()
|
||||
{
|
||||
var buffer = new byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
decimal original = 123456.789m;
|
||||
writer.WriteDecimal128("dec", original);
|
||||
|
||||
var reader = new BsonSpanReader(buffer, _keys);
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var val = reader.ReadDecimal128();
|
||||
|
||||
type.ShouldBe(BsonType.Decimal128);
|
||||
name.ShouldBe("dec");
|
||||
val.ShouldBe(original);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read date time.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_DateTime()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
// Round to milliseconds as BSON only stores millisecond precision
|
||||
var expectedTime = new DateTime(now.Year, now.Month, now.Day,
|
||||
now.Hour, now.Minute, now.Second, now.Millisecond, DateTimeKind.Utc);
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
writer.WriteDateTime("timestamp", expectedTime);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var readTime = reader.ReadDateTime();
|
||||
|
||||
type.ShouldBe(BsonType.DateTime);
|
||||
name.ShouldBe("timestamp");
|
||||
readTime.ShouldBe(expectedTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read numeric types.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_NumericTypes()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
writer.WriteInt32("int32", int.MaxValue);
|
||||
writer.WriteInt64("int64", long.MaxValue);
|
||||
writer.WriteDouble("double", 3.14159);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
|
||||
reader.ReadBsonType();
|
||||
reader.ReadElementHeader();
|
||||
reader.ReadInt32().ShouldBe(int.MaxValue);
|
||||
|
||||
reader.ReadBsonType();
|
||||
reader.ReadElementHeader();
|
||||
reader.ReadInt64().ShouldBe(long.MaxValue);
|
||||
|
||||
reader.ReadBsonType();
|
||||
reader.ReadElementHeader();
|
||||
Math.Round(reader.ReadDouble(), 5).ShouldBe(Math.Round(3.14159, 5));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read binary.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_Binary()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
byte[] testData = [1, 2, 3, 4, 5];
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
writer.WriteBinary("data", testData);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var readData = reader.ReadBinary(out var subtype);
|
||||
|
||||
type.ShouldBe(BsonType.Binary);
|
||||
name.ShouldBe("data");
|
||||
subtype.ShouldBe((byte)0);
|
||||
testData.AsSpan().SequenceEqual(readData).ShouldBeTrue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read nested document.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_NestedDocument()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[512];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var rootSizePos = writer.BeginDocument();
|
||||
writer.WriteString("name", "Parent");
|
||||
|
||||
var childSizePos = writer.BeginDocument("child");
|
||||
writer.WriteString("name", "Child");
|
||||
writer.WriteInt32("value", 42);
|
||||
writer.EndDocument(childSizePos);
|
||||
|
||||
writer.EndDocument(rootSizePos);
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
var rootSize = reader.ReadDocumentSize();
|
||||
|
||||
rootSize.ShouldBe(writer.Position);
|
||||
|
||||
reader.ReadBsonType(); // String
|
||||
reader.ReadElementHeader().ShouldBe("name");
|
||||
reader.ReadString().ShouldBe("Parent");
|
||||
|
||||
reader.ReadBsonType(); // Document
|
||||
reader.ReadElementHeader().ShouldBe("child");
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
reader.ReadBsonType(); // String
|
||||
reader.ReadElementHeader().ShouldBe("name");
|
||||
reader.ReadString().ShouldBe("Child");
|
||||
|
||||
reader.ReadBsonType(); // Int32
|
||||
reader.ReadElementHeader().ShouldBe("value");
|
||||
reader.ReadInt32().ShouldBe(42);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user