/
db_mock.go
141 lines (125 loc) · 3.29 KB
/
db_mock.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package db
import (
"fmt"
"reflect"
"go.mongodb.org/mongo-driver/bson"
mg "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MockDB struct {
Data map[string]map[interface{}]interface{}
}
func (db *MockDB) AllCollections() []string {
return []string{MatchCollection, QuestionCollection, AnswerCollection, SnapshotCollection, TeamCollection, PlayerCollection, IndexCollection, SubscriberCollection}
}
func NewMockDb(collectionsAdded bool) *MockDB {
mockDb := &MockDB{}
mockDb.DropCollections()
if collectionsAdded {
mockDb.CreateCollections()
}
return mockDb
}
func (db *MockDB) CreateCollection(collection string) error {
if _, ok := db.Data[collection]; !ok {
db.Data[collection] = make(map[interface{}]interface{})
}
return nil
}
func (db *MockDB) CreateCollections() (err error) {
db.DropCollections()
for _, collection := range db.AllCollections() {
db.CreateCollection(collection)
}
return
}
func (db *MockDB) DropCollections() (err error) {
db.Data = make(map[string]map[interface{}]interface{})
return
}
func (db *MockDB) Create(request interface{}, collection string) (err error) {
doc, err := Document(request)
if err != nil {
return
}
db.Data[collection][doc["_id"]] = doc
return
}
func (db *MockDB) CreateMany(request []interface{}, collection string) (err error) {
for _, v := range request {
db.Create(v, collection)
}
return
}
func Document(request interface{}) (doc bson.M, err error) {
data, err := bson.Marshal(request)
if err != nil {
return
}
err = bson.Unmarshal(data, &doc)
return
}
func (db *MockDB) FindOne(collection string, filters bson.M, findOptions *options.FindOneOptions) (result bson.Raw, err error) {
for _, entry := range db.Data[collection] {
if db.isFilterMatch(entry, filters) {
result, _ = bson.Marshal(entry)
return
}
}
err = fmt.Errorf("no document found")
return
}
func (db *MockDB) Find(collection string, filters bson.M, findOptions *options.FindOptions) (results []bson.Raw, err error) {
results = make([]bson.Raw, 0)
for _, entry := range db.Data[collection] {
if db.isFilterMatch(entry, filters) {
result, _ := bson.Marshal(entry)
results = append(results, result)
}
}
return
}
func (db *MockDB) Delete(collection string, identifier bson.M) (result *mg.DeleteResult, err error) {
for key, entry := range db.Data[collection] {
if db.isFilterMatch(entry, identifier) {
delete(db.Data[collection], key)
return
}
}
err = fmt.Errorf("no document found")
return
}
func (db *MockDB) Update(collection string, identifier bson.M, v interface{}) (result *mg.UpdateResult, err error) {
for _, entry := range db.Data[collection] {
if db.isFilterMatch(entry, identifier) {
db.Create(v, collection)
return
}
}
err = fmt.Errorf("no document found")
return
}
func (db *MockDB) isFilterMatch(entry interface{}, filters bson.M) (bool bool) {
var x = 0
for k, filter := range filters {
val := reflect.ValueOf(filter)
if val.Kind() == reflect.Map {
for kind, values := range val.MapKeys() {
strct := val.MapIndex(values)
if kind == 0 {
for _, v := range strct.Interface().([]string) {
if entry.(bson.M)[k] == v {
x++
break
}
}
}
}
} else {
if entry.(bson.M)[k] == filter {
x++
}
}
}
return x == len(filters)
}