@@ -32,6 +32,8 @@ import (
3232 "sync"
3333 "sync/atomic"
3434 "testing"
35+
36+ "go.mongodb.org/mongo-driver/bson"
3537)
3638
3739const N = 1000
@@ -625,3 +627,79 @@ func Test_MarshalJSON(t *testing.T) {
625627 t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
626628 }
627629}
630+
631+ func Test_UnmarshalBSON (t * testing.T ) {
632+ tp , s , initErr := bson .MarshalValue (
633+ bson.A {"1" , "2" , "3" , "test" },
634+ )
635+
636+ if initErr != nil {
637+ t .Errorf ("Init Error should be nil: %v" , initErr )
638+
639+ return
640+ }
641+
642+ if tp != bson .TypeArray {
643+ t .Errorf ("Encoded Type should be bson.Array, got: %v" , tp )
644+
645+ return
646+ }
647+
648+ expected := NewSet ("1" , "2" , "3" , "test" )
649+ actual := NewSet [string ]()
650+ err := bson .UnmarshalValue (bson .TypeArray , s , actual )
651+ if err != nil {
652+ t .Errorf ("Error should be nil: %v" , err )
653+ }
654+
655+ if ! expected .Equal (actual ) {
656+ t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
657+ }
658+ }
659+ func TestThreadUnsafeSet_UnmarshalBSON (t * testing.T ) {
660+ tp , s , initErr := bson .MarshalValue (
661+ bson.A {int64 (1 ), int64 (2 ), int64 (3 )},
662+ )
663+
664+ if initErr != nil {
665+ t .Errorf ("Init Error should be nil: %v" , initErr )
666+
667+ return
668+ }
669+
670+ if tp != bson .TypeArray {
671+ t .Errorf ("Encoded Type should be bson.Array, got: %v" , tp )
672+
673+ return
674+ }
675+
676+ expected := NewThreadUnsafeSet [int64 ](1 , 2 , 3 )
677+ actual := NewThreadUnsafeSet [int64 ]()
678+ err := actual .UnmarshalBSON ([]byte (s ))
679+ if err != nil {
680+ t .Errorf ("Error should be nil: %v" , err )
681+ }
682+ if ! expected .Equal (actual ) {
683+ t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
684+ }
685+ }
686+ func Test_MarshalBSON (t * testing.T ) {
687+ expected := NewSet ("1" , "test" )
688+
689+ _ , b , err := bson .MarshalValue (
690+ NewSet ("1" , "test" ),
691+ )
692+ if err != nil {
693+ t .Errorf ("Error should be nil: %v" , err )
694+ }
695+
696+ actual := NewSet [string ]()
697+ err = bson .UnmarshalValue (bson .TypeArray , b , actual )
698+ if err != nil {
699+ t .Errorf ("Error should be nil: %v" , err )
700+ }
701+
702+ if ! expected .Equal (actual ) {
703+ t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
704+ }
705+ }
0 commit comments