-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
111 lines (86 loc) · 2.89 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package mongosteps
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
// DatabaseOption sets the database option.
type DatabaseOption interface {
applyDatabaseOption(d *database)
}
type databaseOptionFunc func(d *database)
func (f databaseOptionFunc) applyDatabaseOption(d *database) {
f(d)
}
type database struct {
conn *mongo.Database
cleanUps []string
}
// cleanUp cleans up the collections in the database.
func (d *database) cleanUp(ctx context.Context) error {
for _, collection := range d.cleanUps {
if err := d.truncate(ctx, collection); err != nil {
return err
}
}
return nil
}
// find returns the documents in the collection that match the filter.
func (d *database) find(ctx context.Context, collection string, filter interface{}, opts ...*options.FindOptions) ([]bsoncore.Document, error) {
cursor, err := d.conn.Collection(collection).Find(ctx, filter, opts...)
if err != nil {
return nil, fmt.Errorf("could not find documents in collection %q: %w", collection, err)
}
defer cursor.Close(ctx) // nolint: errcheck
var result []bsoncore.Document
if err := cursor.All(ctx, &result); err != nil {
return nil, fmt.Errorf("could not read documents from collection %q: %w", collection, err)
}
if len(result) == 0 {
return []bsoncore.Document{}, nil
}
return result, nil
}
// truncate deletes all the documents the collection.
func (d *database) truncate(ctx context.Context, collection string) error {
if _, err := d.conn.Collection(collection).DeleteMany(ctx, bson.D{}); err != nil {
return fmt.Errorf("could not truncate collection %q: %w", collection, err)
}
return nil
}
func (d *database) store(ctx context.Context, collection string, docs []bsoncore.Document) error {
documents := make([]interface{}, len(docs))
for i, doc := range docs {
documents[i] = doc
}
if _, err := d.conn.Collection(collection).InsertMany(ctx, documents); err != nil {
return fmt.Errorf("could not insert documents into collection %q: %w", collection, err)
}
return nil
}
func (d *database) count(ctx context.Context, collection string, filter interface{}) (int64, error) {
count, err := d.conn.Collection(collection).CountDocuments(ctx, filter)
if err != nil {
return 0, fmt.Errorf("could not count documents in collection %q: %w", collection, err)
}
return count, nil
}
// newDatabase creates a new database.
func newDatabase(conn *mongo.Database, opts ...DatabaseOption) *database {
d := &database{
conn: conn,
}
for _, opt := range opts {
opt.applyDatabaseOption(d)
}
return d
}
// CleanUpAfterScenario cleans up the collections in the database after the scenario.
func CleanUpAfterScenario(collections ...string) DatabaseOption {
return databaseOptionFunc(func(d *database) {
d.cleanUps = append(d.cleanUps, collections...)
})
}