@@ -33,6 +33,8 @@ import (
3333 "sync"
3434 "sync/atomic"
3535 "testing"
36+
37+ "go.mongodb.org/mongo-driver/bson"
3638)
3739
3840const N = 1000
@@ -683,3 +685,81 @@ func Test_DeadlockOnEachCallbackWhenPanic(t *testing.T) {
683685 t .Errorf ("Expected widgets to have 5 elements, but has %d" , card )
684686 }
685687}
688+
689+ func Test_UnmarshalBSONValue (t * testing.T ) {
690+ tp , s , initErr := bson .MarshalValue (
691+ bson.A {"1" , "2" , "3" , "test" },
692+ )
693+
694+ if initErr != nil {
695+ t .Errorf ("Init Error should be nil: %v" , initErr )
696+
697+ return
698+ }
699+
700+ if tp != bson .TypeArray {
701+ t .Errorf ("Encoded Type should be bson.Array, got: %v" , tp )
702+
703+ return
704+ }
705+
706+ expected := NewSet ("1" , "2" , "3" , "test" )
707+ actual := NewSet [string ]()
708+ err := bson .UnmarshalValue (bson .TypeArray , s , actual )
709+ if err != nil {
710+ t .Errorf ("Error should be nil: %v" , err )
711+ }
712+
713+ if ! expected .Equal (actual ) {
714+ t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
715+ }
716+ }
717+
718+ func TestThreadUnsafeSet_UnmarshalBSONValue (t * testing.T ) {
719+ tp , s , initErr := bson .MarshalValue (
720+ bson.A {int64 (1 ), int64 (2 ), int64 (3 )},
721+ )
722+
723+ if initErr != nil {
724+ t .Errorf ("Init Error should be nil: %v" , initErr )
725+
726+ return
727+ }
728+
729+ if tp != bson .TypeArray {
730+ t .Errorf ("Encoded Type should be bson.Array, got: %v" , tp )
731+
732+ return
733+ }
734+
735+ expected := NewThreadUnsafeSet [int64 ](1 , 2 , 3 )
736+ actual := NewThreadUnsafeSet [int64 ]()
737+ err := actual .UnmarshalBSONValue (bson .TypeArray , []byte (s ))
738+ if err != nil {
739+ t .Errorf ("Error should be nil: %v" , err )
740+ }
741+ if ! expected .Equal (actual ) {
742+ t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
743+ }
744+ }
745+
746+ func Test_MarshalBSONValue (t * testing.T ) {
747+ expected := NewSet ("1" , "test" )
748+
749+ _ , b , err := bson .MarshalValue (
750+ NewSet ("1" , "test" ),
751+ )
752+ if err != nil {
753+ t .Errorf ("Error should be nil: %v" , err )
754+ }
755+
756+ actual := NewSet [string ]()
757+ err = bson .UnmarshalValue (bson .TypeArray , b , actual )
758+ if err != nil {
759+ t .Errorf ("Error should be nil: %v" , err )
760+ }
761+
762+ if ! expected .Equal (actual ) {
763+ t .Errorf ("Expected no difference, got: %v" , expected .Difference (actual ))
764+ }
765+ }
0 commit comments