Debian Codegraph @

docstore/mongodocstore/mongo_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package mongodocstore
16
17 // To run these tests against a real MongoDB server, first run ./localmongo.sh.
18 // Then wait a few seconds for the server to be ready.
19
20 import (
21 "context"
22 "errors"
23 "fmt"
24 "testing"
25 "time"
26
27 "github.com/google/go-cmp/cmp"
28 "go.mongodb.org/mongo-driver/bson"
29 "go.mongodb.org/mongo-driver/mongo"
30 "go.mongodb.org/mongo-driver/mongo/options"
31 "gocloud.dev/docstore"
32 "gocloud.dev/docstore/driver"
33 "gocloud.dev/docstore/drivertest"
34 "gocloud.dev/internal/gcerr"
35 "gocloud.dev/internal/testing/setup"
36 )
37
38 const (
39 serverURIV3 = "mongodb://localhost:27020"
40 serverURIV4 = "mongodb://localhost:27017/?directConnection=true"
41 dbName = "docstore-test"
42 collectionName1 = "docstore-test-1"
43 collectionName2 = "docstore-test-2"
44 collectionName3 = "docstore-test-3"
45 )
46
47 type harness struct {
48 db *mongo.Database
49 supportAtomicWrites bool
50 }
51
52 func (h *harness) MakeCollection(ctx context.Context, kind drivertest.CollectionKind) (driver.Collection, error) {
53 var coll *collection
54 var err error
55 switch kind {
56 case drivertest.SingleKey:
57 coll, err = newCollection(h.db.Collection(collectionName1), drivertest.KeyField, nil, nil)
58 case drivertest.TwoKey:
59 coll, err = newCollection(h.db.Collection(collectionName2), "", drivertest.HighScoreKey, nil)
60 case drivertest.AltRev:
61 coll, err = newCollection(h.db.Collection(collectionName1), drivertest.KeyField, nil,
62 &Options{RevisionField: drivertest.AlternateRevisionField})
63 case drivertest.NoRev:
64 coll, err = newCollection(h.db.Collection(collectionName1), drivertest.KeyField, nil,
65 &Options{NoWriteQueryUpdateRevisions: true})
66 default:
67 panic("bad kind")
68 }
69 if err != nil {
70 return nil, err
71 }
72 // It seems that the client doesn't actually connect until the first RPC. So
73 // time out quickly if there's a problem.
74 tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
75 defer cancel()
76 if err := coll.coll.Drop(tctx); err != nil {
77 return nil, err
78 }
79 return coll, nil
80 }
81
82 func (h *harness) SupportsAtomicWrites() bool { return h.supportAtomicWrites }
83
84 func (*harness) BeforeDoTypes() []interface{} {
85 return []interface{}{
86 []mongo.WriteModel{},
87 &options.FindOptions{},
88 }
89 }
90
91 func (*harness) BeforeQueryTypes() []interface{} {
92 return []interface{}{&options.FindOptions{}, bson.D{}}
93 }
94
95 func (*harness) RevisionsEqual(rev1, rev2 interface{}) bool {
96 return rev1 == rev2
97 }
98
99 func (*harness) Close() {}
100
101 type codecTester struct{}
102
103 func (codecTester) UnsupportedTypes() []drivertest.UnsupportedType {
104 return []drivertest.UnsupportedType{drivertest.NanosecondTimes}
105 }
106
107 func (codecTester) DocstoreEncode(x interface{}) (interface{}, error) {
108 m, err := encodeDoc(drivertest.MustDocument(x), true)
109 if err != nil {
110 return nil, err
111 }
112 return bson.Marshal(m)
113 }
114
115 func (codecTester) DocstoreDecode(value, dest interface{}) error {
116 var m map[string]interface{}
117 if err := bson.Unmarshal(value.([]byte), &m); err != nil {
118 return err
119 }
120 return decodeDoc(m, drivertest.MustDocument(dest), mongoIDField, true)
121 }
122
123 func (codecTester) NativeEncode(x interface{}) (interface{}, error) {
124 return bson.Marshal(x)
125 }
126
127 func (codecTester) NativeDecode(value, dest interface{}) error {
128 return bson.Unmarshal(value.([]byte), dest)
129 }
130
131 type verifyAs struct{}
132
133 func (verifyAs) Name() string {
134 return "verify As"
135 }
136
137 func (verifyAs) CollectionCheck(coll *docstore.Collection) error {
138 var mc *mongo.Collection
139 if !coll.As(&mc) {
140 return errors.New("Collection.As failed")
141 }
142 return nil
143 }
144
145 func (verifyAs) QueryCheck(it *docstore.DocumentIterator) error {
146 var c *mongo.Cursor
147 if !it.As(&c) {
148 return errors.New("DocumentIterator.As failed")
149 }
150 return nil
151 }
152
153 func (verifyAs) ErrorCheck(c *docstore.Collection, err error) error {
154 var cmderr mongo.CommandError
155 var bwerr mongo.BulkWriteError
156 var bwexc mongo.BulkWriteException
157 if !c.ErrorAs(err, &cmderr) && !c.ErrorAs(err, &bwerr) && !c.ErrorAs(err, &bwexc) {
158 if e, ok := err.(*gcerr.Error); ok {
159 err = e.Unwrap()
160 }
161 return fmt.Errorf("Collection.ErrorAs failed, got %T", err)
162 }
163 return nil
164 }
165
166 func TestConformance(t *testing.T) {
167 // mongo 3 doesn't support atomic writes
168 t.Run("V3", func(t *testing.T) { testConformance(t, serverURIV3, false) })
169 t.Run("V4", func(t *testing.T) { testConformance(t, serverURIV4, true) })
170 }
171
172 func testConformance(t *testing.T, serverURI string, supportsAtomicWrites bool) {
173 client := newTestClient(t, serverURI)
174 defer client.Disconnect(context.Background())
175
176 newHarness := func(context.Context, *testing.T) (drivertest.Harness, error) {
177 return &harness{client.Database(dbName), supportsAtomicWrites}, nil
178 }
179 drivertest.RunConformanceTests(t, newHarness, codecTester{}, []drivertest.AsTest{verifyAs{}})
180 }
181
182 func newTestClient(t *testing.T, serverURI string) *mongo.Client {
183 if !setup.HasDockerTestEnvironment() {
184 t.Skip("Skipping Mongo tests since the Mongo server is not available")
185 }
186 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
187 defer cancel()
188 client, err := Dial(ctx, serverURI)
189 if err != nil {
190 t.Fatalf("dialing to %s: %v", serverURI, err)
191 }
192 if err := client.Ping(ctx, nil); err != nil {
193 t.Fatalf("connecting to %s: %v", serverURI, err)
194 }
195 return client
196 }
197
198 func BenchmarkConformance(b *testing.B) {
199 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
200 defer cancel()
201
202 // Only run benchmark once (against MongoDB v4).
203 client, err := Dial(ctx, serverURIV4)
204 if err != nil {
205 b.Fatalf("dialing to %s: %v", serverURIV4, err)
206 }
207 if err := client.Ping(ctx, nil); err != nil {
208 b.Fatalf("connecting to %s: %v", serverURIV4, err)
209 }
210 defer func() { client.Disconnect(context.Background()) }()
211
212 db := client.Database(dbName)
213 coll, err := newCollection(db.Collection(collectionName3), drivertest.KeyField, nil, nil)
214 if err != nil {
215 b.Fatal(err)
216 }
217 drivertest.RunBenchmarks(b, docstore.NewCollection(coll))
218 }
219
220 // Mongo-specific tests.
221
222 // Testing the driver lower all fields in lowercaseFields mode to match the
223 // behavior of the official Go MongoDB client library.
224 func TestLowercaseFields(t *testing.T) {
225 // Verify that the LowercaseFields option works in all cases.
226 must := func(err error) {
227 t.Helper()
228 if err != nil {
229 t.Fatal(err)
230 }
231 }
232
233 ctx := context.Background()
234 client := newTestClient(t, serverURIV4)
235 defer func() { client.Disconnect(ctx) }()
236 db := client.Database(dbName)
237 dc, err := newCollection(db.Collection("lowercase-fields"), "id", nil, &Options{LowercaseFields: true})
238 if err != nil {
239 t.Fatal(err)
240 }
241 coll := docstore.NewCollection(dc)
242 defer coll.Close()
243 defer drivertest.ClearCollection(t, coll)
244
245 type S struct {
246 ID, F, G int
247 DocstoreRevision interface{}
248 }
249
250 // driver.Document.GetField is case-insensitive on structs.
251 doc := drivertest.MustDocument(&S{ID: 1, DocstoreRevision: 1})
252 for _, f := range []string{"ID", "Id", "id", "DocstoreRevision", "docstorerevision"} {
253 got, err := doc.GetField(f)
254 if err != nil {
255 t.Errorf("%s: %v", f, err)
256 }
257 if got != 1 {
258 t.Errorf("got %q, want 1", got)
259 }
260 }
261
262 check := func(got, want interface{}) {
263 t.Helper()
264 if !cmp.Equal(got, want) {
265 t.Errorf("\ngot %+v\nwant %+v", got, want)
266 }
267 }
268
269 sdoc := &S{ID: 1, F: 2, G: 3}
270 must(coll.Put(ctx, sdoc))
271 if sdoc.DocstoreRevision == nil {
272 t.Fatal("revision is nil")
273 }
274
275 // Get with a struct.
276 got := S{ID: 1}
277 must(coll.Get(ctx, &got))
278 check(got, S{ID: 1, F: 2, G: 3, DocstoreRevision: sdoc.DocstoreRevision})
279
280 // Get with map.
281 got2 := map[string]interface{}{"id": 1}
282 must(coll.Get(ctx, got2))
283 check(got2, map[string]interface{}{
284 "id": int64(1), "f": int64(2), "g": int64(3),
285 "docstorerevision": sdoc.DocstoreRevision,
286 })
287
288 // Field paths in Get.
289 got3 := S{ID: 1}
290 must(coll.Get(ctx, &got3, "G"))
291 check(got3, S{ID: 1, F: 0, G: 3, DocstoreRevision: sdoc.DocstoreRevision})
292
293 // Field paths in Update.
294 got4 := map[string]interface{}{"id": 1}
295 udoc := &S{ID: 1}
296 must(coll.Actions().Update(udoc, docstore.Mods{"F": 4}).Get(got4).Do(ctx))
297 check(got4, map[string]interface{}{
298 "id": int64(1), "f": int64(4), "g": int64(3),
299 "docstorerevision": udoc.DocstoreRevision,
300 })
301
302 // Query filters.
303 var got5 S
304 must(coll.Query().Where("ID", "=", 1).Where("G", ">", 2).Get(ctx).Next(ctx, &got5))
305 check(got5, S{ID: 1, F: 4, G: 3, DocstoreRevision: udoc.DocstoreRevision})
306
307 // Query orders.
308 sdoc2 := &S{ID: 2, F: 5, G: 6}
309 must(coll.Put(ctx, sdoc2))
310 var got6 S
311 must(coll.Query().OrderBy("G", docstore.Descending).Get(ctx).Next(ctx, &got6))
312 check(got6, *sdoc2)
313
314 // List queries
315 // select F from coll WHERE G IN (50, 51) ORDER BY G DESC
316 // test that F is 99
317 sdoc3 := &S{ID: 3, F: 99, G: 50}
318 sdoc4 := &S{ID: 4, F: 99, G: 51}
319 must(coll.Put(ctx, sdoc3))
320 must(coll.Put(ctx, sdoc4))
321 var got7, got8 S
322 iter := coll.Query().Where("G", "in", []int{50, 51}).OrderBy("G", docstore.Descending).Get(ctx)
323 must(iter.Next(ctx, &got7))
324 must(iter.Next(ctx, &got8))
325 check(got7, *sdoc4)
326 check(got8, *sdoc3)
327
328 // same query with not-in, expect to get sdoc2 back even though G is higher for sdoc3 and sdoc4
329 var got9 S
330 must(coll.Query().Where("G", "not-in", []int{50, 51}).OrderBy("G", docstore.Descending).Get(ctx).Next(ctx, &got9))
331 check(got9, *sdoc2)
332 }