@@ -12,42 +12,28 @@ import (
12
12
"testing"
13
13
14
14
"github.com/stretchr/testify/require"
15
- "go.mongodb.org/mongo-driver/bson"
16
- "go.mongodb.org/mongo-driver/internal/testutil/helpers"
17
15
"go.mongodb.org/mongo-driver/mongo/writeconcern"
18
- "go.mongodb.org/mongo-driver/x/bsonx"
16
+ "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
17
+ "go.mongodb.org/mongo-driver/x/mongo/driver"
19
18
"go.mongodb.org/mongo-driver/x/mongo/driver/description"
20
- "go.mongodb.org/mongo-driver/x/mongo/driver/session "
19
+ "go.mongodb.org/mongo-driver/x/mongo/driver/operation "
21
20
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
22
- "go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
23
- "go.mongodb.org/mongo-driver/x/mongo/driverlegacy"
24
- "go.mongodb.org/mongo-driver/x/network/command"
25
21
)
26
22
27
23
// AutoCreateIndexes creates an index in the test cluster.
28
24
func AutoCreateIndexes (t * testing.T , keys []string ) {
29
- indexes := bsonx. Doc {}
25
+ var elems [][] byte
30
26
for _ , k := range keys {
31
- indexes = append (indexes , bsonx. Elem { k , bsonx . Int32 ( 1 )} )
27
+ elems = append (elems , bsoncore . AppendInt32Element ( nil , k , 1 ) )
32
28
}
33
29
name := strings .Join (keys , "_" )
34
- indexes = bsonx.Doc {
35
- {"key" , bsonx .Document (indexes )},
36
- {"name" , bsonx .String (name )},
37
- }
38
- cmd := command.CreateIndexes {
39
- NS : command .NewNamespace (DBName (t ), ColName (t )),
40
- Indexes : bsonx.Arr {bsonx .Document (indexes )},
41
- }
42
- id , _ := uuid .New ()
43
- _ , err := driverlegacy .CreateIndexes (
44
- context .Background (),
45
- cmd ,
46
- Topology (t ),
47
- description .WriteSelector (),
48
- id ,
49
- & session.Pool {},
30
+ indexes := bsoncore .BuildDocumentFromElements (nil ,
31
+ bsoncore .AppendDocumentElement (nil , "key" , bsoncore .BuildDocumentFromElements (nil ,
32
+ elems ... )),
33
+ bsoncore .AppendStringElement (nil , "name" , name ),
50
34
)
35
+ err := operation .NewCreateIndexes (indexes ).Collection (ColName (t )).Database (DBName (t )).
36
+ Deployment (Topology (t )).ServerSelector (description .WriteSelector ()).Execute (context .Background ())
51
37
require .NoError (t , err )
52
38
}
53
39
@@ -58,100 +44,60 @@ func AutoDropCollection(t *testing.T) {
58
44
59
45
// DropCollection drops the collection in the test cluster.
60
46
func DropCollection (t * testing.T , dbname , colname string ) {
61
- cmd := command.Write {DB : dbname , Command : bsonx.Doc {{"drop" , bsonx .String (colname )}}}
62
- id , _ := uuid .New ()
63
- _ , err := driverlegacy .Write (
64
- context .Background (),
65
- cmd ,
66
- Topology (t ),
67
- description .WriteSelector (),
68
- id ,
69
- & session.Pool {},
70
- )
71
- if err != nil && ! command .IsNotFound (err ) {
47
+ err := operation .NewCommand (bsoncore .BuildDocument (nil , bsoncore .AppendStringElement (nil , "drop" , colname ))).
48
+ Database (dbname ).ServerSelector (description .WriteSelector ()).Deployment (Topology (t )).
49
+ Execute (context .Background ())
50
+ if de , ok := err .(driver.Error ); err != nil && ! (ok && de .NamespaceNotFound ()) {
72
51
require .NoError (t , err )
73
52
}
74
53
}
75
54
76
55
func autoDropDB (t * testing.T , topo * topology.Topology ) {
77
- cmd := command.Write {DB : DBName (t ), Command : bsonx.Doc {{"dropDatabase" , bsonx .Int32 (1 )}}}
78
- id , _ := uuid .New ()
79
- _ , err := driverlegacy .Write (
80
- context .Background (),
81
- cmd ,
82
- topo ,
83
- description .WriteSelector (),
84
- id ,
85
- & session.Pool {},
86
- )
56
+ err := operation .NewCommand (bsoncore .BuildDocument (nil , bsoncore .AppendInt32Element (nil , "dropDatabase" , 1 ))).
57
+ Database (DBName (t )).ServerSelector (description .WriteSelector ()).Deployment (topo ).
58
+ Execute (context .Background ())
87
59
require .NoError (t , err )
88
60
}
89
61
90
62
// AutoInsertDocs inserts the docs into the test cluster.
91
- func AutoInsertDocs (t * testing.T , writeConcern * writeconcern.WriteConcern , docs ... bsonx. Doc ) {
63
+ func AutoInsertDocs (t * testing.T , writeConcern * writeconcern.WriteConcern , docs ... bsoncore. Document ) {
92
64
InsertDocs (t , DBName (t ), ColName (t ), writeConcern , docs ... )
93
65
}
94
66
95
67
// InsertDocs inserts the docs into the test cluster.
96
- func InsertDocs (t * testing.T , dbname , colname string , writeConcern * writeconcern.WriteConcern , docs ... bsonx.Doc ) {
97
- cmd := command.Insert {NS : command .NewNamespace (dbname , colname ), Docs : docs }
98
-
99
- topo := Topology (t )
100
- id , _ := uuid .New ()
101
- _ , err := driverlegacy .Insert (
102
- context .Background (),
103
- cmd ,
104
- topo ,
105
- description .WriteSelector (),
106
- id ,
107
- & session.Pool {},
108
- false ,
109
- )
68
+ func InsertDocs (t * testing.T , dbname , colname string , writeConcern * writeconcern.WriteConcern , docs ... bsoncore.Document ) {
69
+ err := operation .NewInsert (docs ... ).Collection (colname ).Database (dbname ).
70
+ Deployment (Topology (t )).ServerSelector (description .WriteSelector ()).Execute (context .Background ())
110
71
require .NoError (t , err )
111
72
}
112
73
113
74
// EnableMaxTimeFailPoint turns on the max time fail point in the test cluster.
114
75
func EnableMaxTimeFailPoint (t * testing.T , s * topology.Server ) error {
115
- cmd := command.Write {
116
- DB : "admin" ,
117
- Command : bsonx.Doc {
118
- {"configureFailPoint" , bsonx .String ("maxTimeAlwaysTimeOut" )},
119
- {"mode" , bsonx .String ("alwaysOn" )},
120
- },
121
- }
122
- conn , err := s .ConnectionLegacy (context .Background ())
123
- require .NoError (t , err )
124
- defer testhelpers .RequireNoErrorOnClose (t , conn )
125
- _ , err = cmd .RoundTrip (context .Background (), s .SelectedDescription (), conn )
126
- return err
76
+ cmd := bsoncore .BuildDocumentFromElements (nil ,
77
+ bsoncore .AppendStringElement (nil , "configureFailPoint" , "maxTimeAlwaysTimeOut" ),
78
+ bsoncore .AppendStringElement (nil , "mode" , "alwaysOn" ),
79
+ )
80
+ return operation .NewCommand (cmd ).
81
+ Database ("admin" ).Deployment (driver.SingleServerDeployment {Server : s }).
82
+ Execute (context .Background ())
127
83
}
128
84
129
85
// DisableMaxTimeFailPoint turns off the max time fail point in the test cluster.
130
86
func DisableMaxTimeFailPoint (t * testing.T , s * topology.Server ) {
131
- cmd := command.Write {
132
- DB : "admin" ,
133
- Command : bsonx.Doc {
134
- {"configureFailPoint" , bsonx .String ("maxTimeAlwaysTimeOut" )},
135
- {"mode" , bsonx .String ("off" )},
136
- },
137
- }
138
- conn , err := s .ConnectionLegacy (context .Background ())
139
- require .NoError (t , err )
140
- defer testhelpers .RequireNoErrorOnClose (t , conn )
141
- _ , err = cmd .RoundTrip (context .Background (), s .SelectedDescription (), conn )
142
- require .NoError (t , err )
87
+ cmd := bsoncore .BuildDocumentFromElements (nil ,
88
+ bsoncore .AppendStringElement (nil , "configureFailPoint" , "maxTimeAlwaysTimeOut" ),
89
+ bsoncore .AppendStringElement (nil , "mode" , "off" ),
90
+ )
91
+ _ = operation .NewCommand (cmd ).
92
+ Database ("admin" ).Deployment (driver.SingleServerDeployment {Server : s }).
93
+ Execute (context .Background ())
143
94
}
144
95
145
96
// RunCommand runs an arbitrary command on a given database of target server
146
- func RunCommand (t * testing.T , s * topology.Server , db string , b bsonx.Doc ) (bson.Raw , error ) {
147
- conn , err := s .ConnectionLegacy (context .Background ())
148
- if err != nil {
149
- return nil , err
150
- }
151
- defer testhelpers .RequireNoErrorOnClose (t , conn )
152
- cmd := command.Read {
153
- DB : db ,
154
- Command : b ,
155
- }
156
- return cmd .RoundTrip (context .Background (), s .SelectedDescription (), conn )
97
+ func RunCommand (t * testing.T , s * topology.Server , db string , cmd bsoncore.Document ) (bsoncore.Document , error ) {
98
+ op := operation .NewCommand (cmd ).
99
+ Database (db ).Deployment (driver.SingleServerDeployment {Server : s })
100
+ err := op .Execute (context .Background ())
101
+ res := op .Result ()
102
+ return res , err
157
103
}
0 commit comments