-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocks.go
64 lines (51 loc) · 1.53 KB
/
mocks.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
package main
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"reflect"
)
// mockClient is a mock implementation of a mongo client
// which satisfies the same interface as the mongo client wrapper.
// mockClient is used in unit tests to avoid dependency on a
// real mongo database.
type mockClient struct{}
func (mc mockClient) Connect(ctx context.Context) error {
return nil
}
func (mc mockClient) Database(dbName string) DatabaseIface {
return mockDatabase{}
}
type mockDatabase struct{}
func (md mockDatabase) Collection(colName string) CollectionIface {
return mockCollection{}
}
type mockCollection struct{}
func (mc mockCollection) InsertOne(ctx context.Context, document interface{}) (interface{}, error) {
return nil, nil
}
func (mc mockCollection) FindOne(ctx context.Context, v interface{}) SingleResultIface {
if val, ok := v.(bson.D); ok {
m := val.Map()
id := m["schema_id"]
if id == "config-schema" {
return mockSingleResult{}
}
}
return mockSingleResultNotFound{}
}
type mockSingleResult struct{}
func (ms mockSingleResult) Decode(v interface{}) error {
mockResult := schemaData{Schema: `{"mock":"schema"}`}
mockVal := reflect.ValueOf(mockResult)
reflect.ValueOf(v).Elem().Set(mockVal)
return nil
}
type mockSingleResultNotFound struct{}
func (ms mockSingleResultNotFound) Decode(v interface{}) error {
return mongo.ErrNoDocuments
}
// newMockDbClient is a helper function that returns a new mock db client
func newMockDbClient() mockClient {
return mockClient{}
}