...
1
2
3
4
5
6
7 package unified
8
9 import (
10 "bytes"
11 "context"
12 "fmt"
13
14 "go.mongodb.org/mongo-driver/bson"
15 "go.mongodb.org/mongo-driver/internal/bsonutil"
16 "go.mongodb.org/mongo-driver/mongo/integration/mtest"
17 "go.mongodb.org/mongo-driver/mongo/options"
18 "go.mongodb.org/mongo-driver/mongo/readconcern"
19 "go.mongodb.org/mongo-driver/mongo/readpref"
20 )
21
22 type collectionData struct {
23 DatabaseName string `bson:"databaseName"`
24 CollectionName string `bson:"collectionName"`
25 Documents []bson.Raw `bson:"documents"`
26 Options *createOptions `bson:"createOptions"`
27 }
28
29 type createOptions struct {
30 Capped *bool `bson:"capped"`
31 SizeInBytes *int64 `bson:"size"`
32 }
33
34
35
36 func (c *collectionData) createCollection(ctx context.Context) error {
37 db := mtest.GlobalClient().Database(c.DatabaseName, options.Database().SetWriteConcern(mtest.MajorityWc))
38 coll := db.Collection(c.CollectionName)
39 if err := coll.Drop(ctx); err != nil {
40 return fmt.Errorf("error dropping collection: %w", err)
41 }
42
43
44 if c.Options != nil {
45 createOpts := options.CreateCollection()
46 if c.Options.Capped != nil {
47 createOpts = createOpts.SetCapped(*c.Options.Capped)
48 }
49 if c.Options.SizeInBytes != nil {
50 createOpts = createOpts.SetSizeInBytes(*c.Options.SizeInBytes)
51 }
52
53 if err := db.CreateCollection(ctx, c.CollectionName, createOpts); err != nil {
54 return fmt.Errorf("error creating collection: %w", err)
55 }
56 }
57
58
59 if len(c.Documents) == 0 && c.Options == nil {
60
61
62 create := bson.D{
63 {"create", coll.Name()},
64 {"writeConcern", bson.D{
65 {"w", "majority"},
66 }},
67 }
68 if err := db.RunCommand(ctx, create).Err(); err != nil {
69 return fmt.Errorf("error creating collection: %w", err)
70 }
71 return nil
72 }
73
74 docs := bsonutil.RawToInterfaces(c.Documents...)
75 if _, err := coll.InsertMany(ctx, docs); err != nil {
76 return fmt.Errorf("error inserting data: %w", err)
77 }
78 return nil
79 }
80
81
82
83 func (c *collectionData) verifyContents(ctx context.Context) error {
84 collOpts := options.Collection().
85 SetReadPreference(readpref.Primary()).
86 SetReadConcern(readconcern.Local())
87 coll := mtest.GlobalClient().Database(c.DatabaseName).Collection(c.CollectionName, collOpts)
88
89 cursor, err := coll.Find(ctx, bson.D{}, options.Find().SetSort(bson.M{"_id": 1}))
90 if err != nil {
91 return fmt.Errorf("Find error: %w", err)
92 }
93 defer cursor.Close(ctx)
94
95 var docs []bson.Raw
96 if err := cursor.All(ctx, &docs); err != nil {
97 return fmt.Errorf("cursor iteration error: %w", err)
98 }
99
100
101
102 if len(c.Documents) != len(docs) {
103 return fmt.Errorf("expected %d documents but found %d: %v", len(c.Documents), len(docs), docs)
104 }
105
106
107
108
109 for idx, expected := range c.Documents {
110 expected = sortDocument(expected)
111 actual := sortDocument(docs[idx])
112
113 if !bytes.Equal(expected, actual) {
114 return fmt.Errorf("document comparison error at index %d: expected %s, got %s", idx, expected, actual)
115 }
116 }
117 return nil
118 }
119
120 func (c *collectionData) namespace() string {
121 return fmt.Sprintf("%s.%s", c.DatabaseName, c.CollectionName)
122 }
123
View as plain text