Reformat / cleanup
This commit is contained in:
@@ -9,7 +9,7 @@ namespace ZB.MOM.WW.CBDD.Tests;
|
||||
public class BsonDocumentAndBufferWriterTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifies BSON document creation and typed retrieval roundtrip.
|
||||
/// Verifies BSON document creation and typed retrieval roundtrip.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonDocument_Create_And_TryGet_RoundTrip()
|
||||
@@ -32,10 +32,10 @@ public class BsonDocumentAndBufferWriterTests
|
||||
|
||||
var wrapped = new BsonDocument(doc.RawData.ToArray(), reverseMap);
|
||||
|
||||
wrapped.TryGetString("name", out var name).ShouldBeTrue();
|
||||
wrapped.TryGetString("name", out string? name).ShouldBeTrue();
|
||||
name.ShouldBe("Alice");
|
||||
|
||||
wrapped.TryGetInt32("age", out var age).ShouldBeTrue();
|
||||
wrapped.TryGetInt32("age", out int age).ShouldBeTrue();
|
||||
age.ShouldBe(32);
|
||||
|
||||
wrapped.TryGetObjectId("_id", out var id).ShouldBeTrue();
|
||||
@@ -46,7 +46,7 @@ public class BsonDocumentAndBufferWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies typed getters return false for missing fields and type mismatches.
|
||||
/// 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()
|
||||
@@ -71,7 +71,7 @@ public class BsonDocumentAndBufferWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the BSON document builder grows its internal buffer for large documents.
|
||||
/// Verifies the BSON document builder grows its internal buffer for large documents.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonDocumentBuilder_Should_Grow_Buffer_When_Document_Is_Large()
|
||||
@@ -86,21 +86,18 @@ public class BsonDocumentAndBufferWriterTests
|
||||
}
|
||||
|
||||
var builder = new BsonDocumentBuilder(keyMap);
|
||||
for (int i = 1; i <= 180; i++)
|
||||
{
|
||||
builder.AddInt32($"k{i}", i);
|
||||
}
|
||||
for (var 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();
|
||||
wrapped.TryGetInt32("k180", out int value).ShouldBeTrue();
|
||||
value.ShouldBe(180);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies BSON buffer writer emits expected nested document and array layout.
|
||||
/// Verifies BSON buffer writer emits expected nested document and array layout.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonBufferWriter_Should_Write_Nested_Document_And_Array()
|
||||
@@ -125,7 +122,7 @@ public class BsonDocumentAndBufferWriterTests
|
||||
writer.EndDocument(rootSizePos);
|
||||
int rootEnd = writer.Position;
|
||||
|
||||
var bytes = output.WrittenSpan.ToArray();
|
||||
byte[] bytes = output.WrittenSpan.ToArray();
|
||||
PatchDocumentSize(bytes, childSizePos, childEnd);
|
||||
PatchDocumentSize(bytes, arraySizePos, arrayEnd);
|
||||
PatchDocumentSize(bytes, rootSizePos, rootEnd);
|
||||
@@ -164,7 +161,7 @@ public class BsonDocumentAndBufferWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies single-byte and C-string span reads operate correctly.
|
||||
/// Verifies single-byte and C-string span reads operate correctly.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BsonSpanReader_ReadByte_And_ReadCStringSpan_Should_Work()
|
||||
@@ -172,10 +169,10 @@ public class BsonDocumentAndBufferWriterTests
|
||||
var singleByteReader = new BsonSpanReader(new byte[] { 0x2A }, new ConcurrentDictionary<ushort, string>());
|
||||
singleByteReader.ReadByte().ShouldBe((byte)0x2A);
|
||||
|
||||
var cstring = Encoding.UTF8.GetBytes("hello\0");
|
||||
byte[] 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);
|
||||
int written = cstringReader.ReadCString(destination);
|
||||
|
||||
new string(destination, 0, written).ShouldBe("hello");
|
||||
}
|
||||
@@ -194,4 +191,4 @@ public class BsonDocumentAndBufferWriterTests
|
||||
{
|
||||
BinaryPrimitives.WriteInt32LittleEndian(output.AsSpan(sizePosition, 4), endPosition - sizePosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,12 @@
|
||||
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.
|
||||
/// Verifies schema generation for a simple entity.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_SimpleEntity()
|
||||
@@ -53,21 +26,8 @@ public class BsonSchemaTests
|
||||
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.
|
||||
/// Verifies schema generation for collection fields.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_Collections()
|
||||
@@ -83,16 +43,8 @@ public class BsonSchemaTests
|
||||
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.
|
||||
/// Verifies schema generation for nested document fields.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_Nested()
|
||||
@@ -105,16 +57,8 @@ public class BsonSchemaTests
|
||||
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.
|
||||
/// Verifies schema generation for collections of complex types.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GenerateSchema_ComplexCollection()
|
||||
@@ -133,4 +77,56 @@ public class BsonSchemaTests
|
||||
items.NestedSchema.ShouldNotBeNull();
|
||||
items.NestedSchema.Fields.ShouldContain(f => f.Name == "_id");
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
public class NestedEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the parent entity.
|
||||
/// </summary>
|
||||
public SimpleEntity Parent { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ComplexCollectionEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets items.
|
||||
/// </summary>
|
||||
public List<SimpleEntity> Items { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
using Xunit;
|
||||
using System.Collections.Concurrent;
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests;
|
||||
|
||||
@@ -10,13 +9,17 @@ public class BsonSpanReaderWriterTests
|
||||
private readonly ConcurrentDictionary<ushort, string> _keys = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BsonSpanReaderWriterTests"/> class.
|
||||
/// 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)
|
||||
string[] initialKeys =
|
||||
[
|
||||
"name", "age", "active", "_id", "val", "dec", "timestamp", "int32", "int64", "double", "data", "child",
|
||||
"value", "0", "1"
|
||||
];
|
||||
foreach (string key in initialKeys)
|
||||
{
|
||||
_keyMap[key] = id;
|
||||
_keys[id] = key;
|
||||
@@ -25,7 +28,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read simple document.
|
||||
/// Tests write and read simple document.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_SimpleDocument()
|
||||
@@ -33,7 +36,7 @@ public class BsonSpanReaderWriterTests
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
int sizePos = writer.BeginDocument();
|
||||
writer.WriteString("name", "John");
|
||||
writer.WriteInt32("age", 30);
|
||||
writer.WriteBoolean("active", true);
|
||||
@@ -42,29 +45,29 @@ public class BsonSpanReaderWriterTests
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
var size = reader.ReadDocumentSize();
|
||||
int size = reader.ReadDocumentSize();
|
||||
|
||||
size.ShouldBe(writer.Position);
|
||||
|
||||
var type1 = reader.ReadBsonType();
|
||||
var name1 = reader.ReadElementHeader();
|
||||
var value1 = reader.ReadString();
|
||||
string name1 = reader.ReadElementHeader();
|
||||
string 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();
|
||||
string name2 = reader.ReadElementHeader();
|
||||
int 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();
|
||||
string name3 = reader.ReadElementHeader();
|
||||
bool value3 = reader.ReadBoolean();
|
||||
|
||||
type3.ShouldBe(BsonType.Boolean);
|
||||
name3.ShouldBe("active");
|
||||
@@ -72,7 +75,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read object id.
|
||||
/// Tests write and read object id.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_ObjectId()
|
||||
@@ -82,7 +85,7 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
var oid = ObjectId.NewObjectId();
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
int sizePos = writer.BeginDocument();
|
||||
writer.WriteObjectId("_id", oid);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
@@ -91,7 +94,7 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
string name = reader.ReadElementHeader();
|
||||
var readOid = reader.ReadObjectId();
|
||||
|
||||
type.ShouldBe(BsonType.ObjectId);
|
||||
@@ -100,7 +103,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests read write double.
|
||||
/// Tests read write double.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ReadWrite_Double()
|
||||
@@ -112,8 +115,8 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
var reader = new BsonSpanReader(buffer, _keys);
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var val = reader.ReadDouble();
|
||||
string name = reader.ReadElementHeader();
|
||||
double val = reader.ReadDouble();
|
||||
|
||||
type.ShouldBe(BsonType.Double);
|
||||
name.ShouldBe("val");
|
||||
@@ -121,7 +124,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests read write decimal128 round trip.
|
||||
/// Tests read write decimal128 round trip.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ReadWrite_Decimal128_RoundTrip()
|
||||
@@ -129,13 +132,13 @@ public class BsonSpanReaderWriterTests
|
||||
var buffer = new byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
decimal original = 123456.789m;
|
||||
var 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();
|
||||
string name = reader.ReadElementHeader();
|
||||
decimal val = reader.ReadDecimal128();
|
||||
|
||||
type.ShouldBe(BsonType.Decimal128);
|
||||
name.ShouldBe("dec");
|
||||
@@ -143,7 +146,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read date time.
|
||||
/// Tests write and read date time.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_DateTime()
|
||||
@@ -156,7 +159,7 @@ public class BsonSpanReaderWriterTests
|
||||
var expectedTime = new DateTime(now.Year, now.Month, now.Day,
|
||||
now.Hour, now.Minute, now.Second, now.Millisecond, DateTimeKind.Utc);
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
int sizePos = writer.BeginDocument();
|
||||
writer.WriteDateTime("timestamp", expectedTime);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
@@ -165,7 +168,7 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
string name = reader.ReadElementHeader();
|
||||
var readTime = reader.ReadDateTime();
|
||||
|
||||
type.ShouldBe(BsonType.DateTime);
|
||||
@@ -174,7 +177,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read numeric types.
|
||||
/// Tests write and read numeric types.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_NumericTypes()
|
||||
@@ -182,7 +185,7 @@ public class BsonSpanReaderWriterTests
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
int sizePos = writer.BeginDocument();
|
||||
writer.WriteInt32("int32", int.MaxValue);
|
||||
writer.WriteInt64("int64", long.MaxValue);
|
||||
writer.WriteDouble("double", 3.14159);
|
||||
@@ -207,7 +210,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read binary.
|
||||
/// Tests write and read binary.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_Binary()
|
||||
@@ -217,7 +220,7 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
byte[] testData = [1, 2, 3, 4, 5];
|
||||
|
||||
var sizePos = writer.BeginDocument();
|
||||
int sizePos = writer.BeginDocument();
|
||||
writer.WriteBinary("data", testData);
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
@@ -226,8 +229,8 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
var type = reader.ReadBsonType();
|
||||
var name = reader.ReadElementHeader();
|
||||
var readData = reader.ReadBinary(out var subtype);
|
||||
string name = reader.ReadElementHeader();
|
||||
var readData = reader.ReadBinary(out byte subtype);
|
||||
|
||||
type.ShouldBe(BsonType.Binary);
|
||||
name.ShouldBe("data");
|
||||
@@ -236,7 +239,7 @@ public class BsonSpanReaderWriterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests write and read nested document.
|
||||
/// Tests write and read nested document.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WriteAndRead_NestedDocument()
|
||||
@@ -244,10 +247,10 @@ public class BsonSpanReaderWriterTests
|
||||
Span<byte> buffer = stackalloc byte[512];
|
||||
var writer = new BsonSpanWriter(buffer, _keyMap);
|
||||
|
||||
var rootSizePos = writer.BeginDocument();
|
||||
int rootSizePos = writer.BeginDocument();
|
||||
writer.WriteString("name", "Parent");
|
||||
|
||||
var childSizePos = writer.BeginDocument("child");
|
||||
int childSizePos = writer.BeginDocument("child");
|
||||
writer.WriteString("name", "Child");
|
||||
writer.WriteInt32("value", 42);
|
||||
writer.EndDocument(childSizePos);
|
||||
@@ -256,7 +259,7 @@ public class BsonSpanReaderWriterTests
|
||||
|
||||
var documentBytes = buffer[..writer.Position];
|
||||
var reader = new BsonSpanReader(documentBytes, _keys);
|
||||
var rootSize = reader.ReadDocumentSize();
|
||||
int rootSize = reader.ReadDocumentSize();
|
||||
|
||||
rootSize.ShouldBe(writer.Position);
|
||||
|
||||
@@ -276,4 +279,4 @@ public class BsonSpanReaderWriterTests
|
||||
reader.ReadElementHeader().ShouldBe("value");
|
||||
reader.ReadInt32().ShouldBe(42);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user