Skip to content

Commit 0a8ed7f

Browse files
author
rstam
committed
Further work on CSHARP-374. Added unit test to confirm fix for DateTime, and new unit tests for Binary, Boolean, Double, Guid and ObjectId.
1 parent b8af606 commit 0a8ed7f

File tree

2 files changed

+215
-1
lines changed

2 files changed

+215
-1
lines changed

Bson/ObjectModel/BsonBinaryData.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,16 @@ public GuidRepresentation GuidRepresentation
132132
/// <summary>
133133
/// Gets the BsonBinaryData as a Guid if the subtype is UuidStandard or UuidLegacy, otherwise null.
134134
/// </summary>
135+
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
135136
public override object RawValue
136137
{
137138
get
138139
{
139-
if (_subType == BsonBinarySubType.UuidStandard || _subType == BsonBinarySubType.UuidLegacy)
140+
if (_subType == BsonBinarySubType.Binary || _subType == BsonBinarySubType.OldBinary)
141+
{
142+
return _bytes;
143+
}
144+
else if (_subType == BsonBinarySubType.UuidStandard || _subType == BsonBinarySubType.UuidLegacy)
140145
{
141146
return ToGuid();
142147
}
@@ -146,6 +151,7 @@ public override object RawValue
146151
}
147152
}
148153
}
154+
#pragma warning restore 618
149155

150156
/// <summary>
151157
/// Gets the binary data subtype.

BsonUnitTests/ObjectModel/BsonDocumentTests.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,71 @@ public void TestToDictionaryNestedDocument()
415415
Assert.AreEqual(2, nested["b"]);
416416
}
417417

418+
[Test]
419+
public void TestToDictionaryOneBinary()
420+
{
421+
var document = new BsonDocument("x", new BsonBinaryData(new byte[] { 1, 2, 3 }, BsonBinarySubType.Binary));
422+
var dictionary = document.ToDictionary();
423+
Assert.AreEqual(1, dictionary.Count);
424+
Assert.IsInstanceOf<byte[]>(dictionary["x"]);
425+
Assert.IsTrue(new byte[] { 1, 2, 3 }.SequenceEqual((byte[])dictionary["x"]));
426+
}
427+
428+
[Test]
429+
public void TestToDictionaryOneBoolean()
430+
{
431+
var document = new BsonDocument("x", true);
432+
var dictionary = document.ToDictionary();
433+
Assert.AreEqual(1, dictionary.Count);
434+
Assert.IsInstanceOf<bool>(dictionary["x"]);
435+
Assert.AreEqual(true, dictionary["x"]);
436+
}
437+
438+
[Test]
439+
public void TestToDictionaryOneDateTime()
440+
{
441+
var utcNow = DateTime.UtcNow;
442+
var utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000));
443+
var document = new BsonDocument("x", utcNow);
444+
var dictionary = document.ToDictionary();
445+
Assert.AreEqual(1, dictionary.Count);
446+
Assert.IsInstanceOf<DateTime>(dictionary["x"]);
447+
Assert.AreEqual(DateTimeKind.Utc, ((DateTime)dictionary["x"]).Kind);
448+
Assert.AreEqual(utcNowTruncated, dictionary["x"]);
449+
}
450+
451+
[Test]
452+
public void TestToDictionaryOneDouble()
453+
{
454+
var document = new BsonDocument("x", 1.0);
455+
var dictionary = document.ToDictionary();
456+
Assert.AreEqual(1, dictionary.Count);
457+
Assert.IsInstanceOf<double>(dictionary["x"]);
458+
Assert.AreEqual(1.0, dictionary["x"]);
459+
}
460+
461+
[Test]
462+
public void TestToDictionaryOneGuidLegacy()
463+
{
464+
var guid = Guid.NewGuid();
465+
var document = new BsonDocument("x", new BsonBinaryData(guid, GuidRepresentation.CSharpLegacy));
466+
var dictionary = document.ToDictionary();
467+
Assert.AreEqual(1, dictionary.Count);
468+
Assert.IsInstanceOf<Guid>(dictionary["x"]);
469+
Assert.AreEqual(guid, dictionary["x"]);
470+
}
471+
472+
[Test]
473+
public void TestToDictionaryOneGuidStandard()
474+
{
475+
var guid = Guid.NewGuid();
476+
var document = new BsonDocument("x", new BsonBinaryData(guid, GuidRepresentation.Standard));
477+
var dictionary = document.ToDictionary();
478+
Assert.AreEqual(1, dictionary.Count);
479+
Assert.IsInstanceOf<Guid>(dictionary["x"]);
480+
Assert.AreEqual(guid, dictionary["x"]);
481+
}
482+
418483
[Test]
419484
public void TestToDictionaryOneInt32()
420485
{
@@ -435,6 +500,17 @@ public void TestToDictionaryOneInt64()
435500
Assert.AreEqual(1L, dictionary["x"]);
436501
}
437502

503+
[Test]
504+
public void TestToDictionaryOneObjectId()
505+
{
506+
var objectId = ObjectId.GenerateNewId();
507+
var hashtable = new BsonDocument("x", objectId);
508+
var dictionary = hashtable.ToDictionary();
509+
Assert.AreEqual(1, dictionary.Count);
510+
Assert.IsInstanceOf<ObjectId>(dictionary["x"]);
511+
Assert.AreEqual(objectId, dictionary["x"]);
512+
}
513+
438514
[Test]
439515
public void TestToDictionaryOneString()
440516
{
@@ -445,6 +521,34 @@ public void TestToDictionaryOneString()
445521
Assert.AreEqual("abc", dictionary["x"]);
446522
}
447523

524+
[Test]
525+
public void TestToDictionaryUnsupportedTypes()
526+
{
527+
var document = new BsonDocument
528+
{
529+
{ "JavaScript", new BsonJavaScript("x = 1") },
530+
{ "JavaScriptWithScope", new BsonJavaScriptWithScope("x = y", new BsonDocument("y", 1)) },
531+
{ "MaxKey", BsonMaxKey.Value },
532+
{ "MinKey", BsonMinKey.Value },
533+
{ "Null", BsonNull.Value },
534+
{ "RegularExpression", new BsonRegularExpression("abc") },
535+
{ "Symbol", BsonSymbol.Create("name") },
536+
{ "Timestamp", new BsonTimestamp(123L) },
537+
{ "Undefined", BsonUndefined.Value },
538+
};
539+
var dictionary = document.ToDictionary();
540+
Assert.AreEqual(9, dictionary.Count);
541+
Assert.IsNull(dictionary["JavaScript"]);
542+
Assert.IsNull(dictionary["JavaScriptWithScope"]);
543+
Assert.IsNull(dictionary["MaxKey"]);
544+
Assert.IsNull(dictionary["MinKey"]);
545+
Assert.IsNull(dictionary["Null"]);
546+
Assert.IsNull(dictionary["RegularExpression"]);
547+
Assert.IsNull(dictionary["Symbol"]);
548+
Assert.IsNull(dictionary["Timestamp"]);
549+
Assert.IsNull(dictionary["Undefined"]);
550+
}
551+
448552
[Test]
449553
public void TestToHashtableEmpty()
450554
{
@@ -493,6 +597,71 @@ public void TestToHashtableNestedDocument()
493597
Assert.AreEqual(2, nested["b"]);
494598
}
495599

600+
[Test]
601+
public void TestToHashtableOneBinary()
602+
{
603+
var document = new BsonDocument("x", new BsonBinaryData(new byte[] { 1, 2, 3 }, BsonBinarySubType.Binary));
604+
var hashtable = document.ToHashtable();
605+
Assert.AreEqual(1, hashtable.Count);
606+
Assert.IsInstanceOf<byte[]>(hashtable["x"]);
607+
Assert.IsTrue(new byte[] { 1, 2, 3 }.SequenceEqual((byte[])hashtable["x"]));
608+
}
609+
610+
[Test]
611+
public void TestToHashtableOneBoolean()
612+
{
613+
var document = new BsonDocument("x", true);
614+
var dictionary = document.ToHashtable();
615+
Assert.AreEqual(1, dictionary.Count);
616+
Assert.IsInstanceOf<bool>(dictionary["x"]);
617+
Assert.AreEqual(true, dictionary["x"]);
618+
}
619+
620+
[Test]
621+
public void TestToHashtableOneDateTime()
622+
{
623+
var utcNow = DateTime.UtcNow;
624+
var utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000));
625+
var hashtable = new BsonDocument("x", utcNow);
626+
var dictionary = hashtable.ToHashtable();
627+
Assert.AreEqual(1, dictionary.Count);
628+
Assert.IsInstanceOf<DateTime>(dictionary["x"]);
629+
Assert.AreEqual(DateTimeKind.Utc, ((DateTime)dictionary["x"]).Kind);
630+
Assert.AreEqual(utcNowTruncated, dictionary["x"]);
631+
}
632+
633+
[Test]
634+
public void TestToHashtableOneDouble()
635+
{
636+
var document = new BsonDocument("x", 1.0);
637+
var hashtable = document.ToHashtable();
638+
Assert.AreEqual(1, hashtable.Count);
639+
Assert.IsInstanceOf<double>(hashtable["x"]);
640+
Assert.AreEqual(1.0, hashtable["x"]);
641+
}
642+
643+
[Test]
644+
public void TestToHashtableOneGuidLegacy()
645+
{
646+
var guid = Guid.NewGuid();
647+
var hashtable = new BsonDocument("x", new BsonBinaryData(guid, GuidRepresentation.CSharpLegacy));
648+
var dictionary = hashtable.ToHashtable();
649+
Assert.AreEqual(1, dictionary.Count);
650+
Assert.IsInstanceOf<Guid>(dictionary["x"]);
651+
Assert.AreEqual(guid, dictionary["x"]);
652+
}
653+
654+
[Test]
655+
public void TestToHashtableOneGuidStandard()
656+
{
657+
var guid = Guid.NewGuid();
658+
var hashtable = new BsonDocument("x", new BsonBinaryData(guid, GuidRepresentation.Standard));
659+
var dictionary = hashtable.ToHashtable();
660+
Assert.AreEqual(1, dictionary.Count);
661+
Assert.IsInstanceOf<Guid>(dictionary["x"]);
662+
Assert.AreEqual(guid, dictionary["x"]);
663+
}
664+
496665
[Test]
497666
public void TestToHashtableOneInt32()
498667
{
@@ -513,6 +682,17 @@ public void TestToHashtableOneInt64()
513682
Assert.AreEqual(1L, hashtable["x"]);
514683
}
515684

685+
[Test]
686+
public void TestToHashtableOneObjectId()
687+
{
688+
var objectId = ObjectId.GenerateNewId();
689+
var hashtable = new BsonDocument("x", objectId);
690+
var dictionary = hashtable.ToHashtable();
691+
Assert.AreEqual(1, dictionary.Count);
692+
Assert.IsInstanceOf<ObjectId>(dictionary["x"]);
693+
Assert.AreEqual(objectId, dictionary["x"]);
694+
}
695+
516696
[Test]
517697
public void TestToHashtableOneString()
518698
{
@@ -523,6 +703,34 @@ public void TestToHashtableOneString()
523703
Assert.AreEqual("abc", hashtable["x"]);
524704
}
525705

706+
[Test]
707+
public void TestToHashtableUnsupportedTypes()
708+
{
709+
var document = new BsonDocument
710+
{
711+
{ "JavaScript", new BsonJavaScript("x = 1") },
712+
{ "JavaScriptWithScope", new BsonJavaScriptWithScope("x = y", new BsonDocument("y", 1)) },
713+
{ "MaxKey", BsonMaxKey.Value },
714+
{ "MinKey", BsonMinKey.Value },
715+
{ "Null", BsonNull.Value },
716+
{ "RegularExpression", new BsonRegularExpression("abc") },
717+
{ "Symbol", BsonSymbol.Create("name") },
718+
{ "Timestamp", new BsonTimestamp(123L) },
719+
{ "Undefined", BsonUndefined.Value },
720+
};
721+
var hashtable = document.ToHashtable();
722+
Assert.AreEqual(9, hashtable.Count);
723+
Assert.IsNull(hashtable["JavaScript"]);
724+
Assert.IsNull(hashtable["JavaScriptWithScope"]);
725+
Assert.IsNull(hashtable["MaxKey"]);
726+
Assert.IsNull(hashtable["MinKey"]);
727+
Assert.IsNull(hashtable["Null"]);
728+
Assert.IsNull(hashtable["RegularExpression"]);
729+
Assert.IsNull(hashtable["Symbol"]);
730+
Assert.IsNull(hashtable["Timestamp"]);
731+
Assert.IsNull(hashtable["Undefined"]);
732+
}
733+
526734
private void AssertAreEqual(string expected, byte[] actual)
527735
{
528736
StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)