@@ -415,6 +415,71 @@ public void TestToDictionaryNestedDocument()
415
415
Assert . AreEqual ( 2 , nested [ "b" ] ) ;
416
416
}
417
417
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
+
418
483
[ Test ]
419
484
public void TestToDictionaryOneInt32 ( )
420
485
{
@@ -435,6 +500,17 @@ public void TestToDictionaryOneInt64()
435
500
Assert . AreEqual ( 1L , dictionary [ "x" ] ) ;
436
501
}
437
502
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
+
438
514
[ Test ]
439
515
public void TestToDictionaryOneString ( )
440
516
{
@@ -445,6 +521,34 @@ public void TestToDictionaryOneString()
445
521
Assert . AreEqual ( "abc" , dictionary [ "x" ] ) ;
446
522
}
447
523
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
+
448
552
[ Test ]
449
553
public void TestToHashtableEmpty ( )
450
554
{
@@ -493,6 +597,71 @@ public void TestToHashtableNestedDocument()
493
597
Assert . AreEqual ( 2 , nested [ "b" ] ) ;
494
598
}
495
599
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
+
496
665
[ Test ]
497
666
public void TestToHashtableOneInt32 ( )
498
667
{
@@ -513,6 +682,17 @@ public void TestToHashtableOneInt64()
513
682
Assert . AreEqual ( 1L , hashtable [ "x" ] ) ;
514
683
}
515
684
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
+
516
696
[ Test ]
517
697
public void TestToHashtableOneString ( )
518
698
{
@@ -523,6 +703,34 @@ public void TestToHashtableOneString()
523
703
Assert . AreEqual ( "abc" , hashtable [ "x" ] ) ;
524
704
}
525
705
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
+
526
734
private void AssertAreEqual ( string expected , byte [ ] actual )
527
735
{
528
736
StringBuilder sb = new StringBuilder ( ) ;
0 commit comments