/
find.go
111 lines (96 loc) 路 2.76 KB
/
find.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 kmongo
import (
"context"
"errors"
internal "github.com/keploy/go-sdk/pkg/keploy"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// FindOne method creates and returns pointer of SingleResult which containes mongo.SingleResult
// in order to mock its method. It mocks Collection.FindOne method explained above in integrations.NewCollections.
//
// See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection.FindOne for information about Collection.FindOne.
func (c *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *SingleResult {
derivedOpts := []options.FindOneOptions{}
for _, j := range opts {
derivedOpts = append(derivedOpts, *j)
}
var singleResult = &SingleResult{
filter: filter,
opts: derivedOpts,
log: c.log,
ctx: ctx,
}
if internal.GetModeFromContext(ctx) == internal.MODE_OFF {
sr := c.Collection.FindOne(ctx, filter, opts...)
if sr != nil {
singleResult.SingleResult = *sr
}
return singleResult
}
kctx, err := internal.GetState(ctx)
if err != nil {
return singleResult
}
mode := kctx.Mode
var sr *mongo.SingleResult
switch mode {
case internal.MODE_TEST:
return singleResult
case internal.MODE_RECORD:
sr = c.Collection.FindOne(ctx, filter, opts...)
if sr != nil {
singleResult.SingleResult = *sr
}
default:
return singleResult
}
return singleResult
}
// Find creates and returns the instance of pointer to keploy Cursor struct which have overridden methods of mongo.Cursor.
// Actual Collection.Find is called only in keploy.MODE_RECORD or "off" mode.
//
// For information about Collection.Find, See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection.Find.
func (c *Collection) Find(ctx context.Context, filter interface{},
opts ...*options.FindOptions) (*Cursor, error) {
derivedOpts := []options.FindOptions{}
for _, j := range opts {
derivedOpts = append(derivedOpts, *j)
}
var result = &Cursor{
filter: filter,
findOpts: derivedOpts,
log: c.log,
ctx: ctx,
}
if internal.GetModeFromContext(ctx) == internal.MODE_OFF {
cursor, err := c.Collection.Find(ctx, filter, opts...)
if cursor != nil {
result.Cursor = *cursor
}
return result, err
}
kctx, er := internal.GetState(ctx)
if er != nil {
return result, er
}
mode := kctx.Mode
var (
cursor *mongo.Cursor
err error
)
switch mode {
case internal.MODE_TEST:
//don't call method in test mode
return result, err
case internal.MODE_RECORD:
cursor, err = c.Collection.Find(ctx, filter, opts...)
if cursor != nil {
result.Cursor = *cursor
}
return result, err
default:
// c.log.Error("integrations: Not in a valid sdk mode")
return result, errors.New("integrations: Not in a valid sdk mode")
}
}