-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb_mock.go
273 lines (223 loc) · 5.5 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package gitdb
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/bouggo/log"
"github.com/gogitdb/gitdb/v2/internal/db"
)
type mockdb struct {
config Config
data map[string]Model
index map[string]map[string]interface{}
locks map[string]bool
}
type mocktransaction struct {
name string
operations []operation
db *mockdb
}
func (t *mocktransaction) Commit() error {
for _, o := range t.operations {
if err := o(); err != nil {
log.Info("Reverting transaction: " + err.Error())
return err
}
}
return nil
}
func (t *mocktransaction) AddOperation(o operation) {
t.operations = append(t.operations, o)
}
func newMockConnection() *mockdb {
db := &mockdb{
data: make(map[string]Model),
index: make(map[string]map[string]interface{}),
locks: make(map[string]bool),
}
return db
}
func (g *mockdb) Close() error {
g.data = nil
return nil
}
func (g *mockdb) Insert(m Model) error {
g.data[ID(m)] = m
for name, value := range m.GetSchema().indexes {
key := m.GetSchema().dataset + "." + name
if _, ok := g.index[key]; !ok {
g.index[key] = make(map[string]interface{})
}
g.index[key][ID(m)] = value
}
return nil
}
func (g *mockdb) InsertMany(m []Model) error {
for _, model := range m {
g.Insert(model)
}
return nil
}
func (g *mockdb) Get(id string, result Model) error {
if reflect.ValueOf(result).Kind() != reflect.Ptr || reflect.ValueOf(result).IsNil() {
return errors.New("Second argument to Get must be a non-nil pointer")
}
model, exists := g.data[id]
if exists {
resultType := reflect.ValueOf(result).Type()
modelType := reflect.ValueOf(model).Type()
if resultType != modelType {
return fmt.Errorf("Second argument to Get must be of type %v", modelType)
}
b, err := json.Marshal(model)
if err != nil {
return err
}
return json.Unmarshal(b, result)
}
dataset, _, _, _ := ParseID(id)
return fmt.Errorf("Record %s not found in %s", id, dataset)
}
func (g *mockdb) Exists(id string) error {
_, exists := g.data[id]
if !exists {
dataset, _, _, _ := ParseID(id)
return fmt.Errorf("Record %s not found in %s", id, dataset)
}
return nil
}
func (g *mockdb) Fetch(dataset string, blocks ...string) ([]*db.Record, error) {
var result []*db.Record
blockStream := "|" + strings.Join(blocks, "|") + "|"
for id, model := range g.data {
ds, b, _, err := ParseID(id)
if err != nil {
log.Error(err.Error())
continue
}
if len(blocks) > 0 {
if ds == dataset && strings.Contains(blockStream, "|"+b+"|") {
result = append(result, db.ConvertModel(ID(model), model))
}
} else {
if ds == dataset {
result = append(result, db.ConvertModel(ID(model), model))
}
}
}
return result, nil
}
func (g *mockdb) Search(dataset string, searchParams []*SearchParam, searchMode SearchMode) ([]*db.Record, error) {
result := []*db.Record{}
for _, searchParam := range searchParams {
key := dataset + "." + searchParam.Index
queryValue := strings.ToLower(searchParam.Value)
for recordID, value := range g.index[key] {
addResult := false
dbValue := strings.ToLower(value.(string))
switch searchMode {
case SearchEquals:
addResult = dbValue == queryValue
case SearchContains:
addResult = strings.Contains(dbValue, queryValue)
case SearchStartsWith:
addResult = strings.HasPrefix(dbValue, queryValue)
case SearchEndsWith:
addResult = strings.HasSuffix(dbValue, queryValue)
}
if addResult {
result = append(result, db.ConvertModel(recordID, g.data[recordID]))
}
}
}
return result, nil
}
func (g *mockdb) Delete(id string) error {
delete(g.data, id)
return nil
}
func (g *mockdb) DeleteOrFail(id string) error {
_, exists := g.data[id]
if !exists {
return fmt.Errorf("record %s does not exist", id)
}
delete(g.data, id)
return nil
}
func (g *mockdb) Lock(m Model) error {
if _, ok := m.(LockableModel); !ok {
return errors.New("Model is not lockable")
}
for _, l := range m.(LockableModel).GetLockFileNames() {
key := m.GetSchema().dataset + "." + l
if _, ok := g.locks[l]; !ok {
g.locks[key] = true
} else {
return errors.New("Lock file already exist: " + l)
}
}
return nil
}
func (g *mockdb) Unlock(m Model) error {
if _, ok := m.(LockableModel); !ok {
return errors.New("Model is not lockable")
}
for _, l := range m.(LockableModel).GetLockFileNames() {
key := m.GetSchema().dataset + "." + l
delete(g.locks, key)
}
return nil
}
func (g *mockdb) Upload() *Upload {
//todo
return nil
}
func (g *mockdb) GetMails() []*mail {
return []*mail{}
}
func (g *mockdb) StartTransaction(name string) Transaction {
//todo return mock transaction
return &mocktransaction{name: name, db: g}
}
func (g *mockdb) GetLastCommitTime() (time.Time, error) {
return time.Now(), nil
}
func (g *mockdb) SetUser(user *User) error {
g.config.User = user
return nil
}
func (g *mockdb) Migrate(from Model, to Model) error {
migrate := []Model{}
records, err := g.Fetch(from.GetSchema().dataset)
if err != nil {
return err
}
for _, record := range records {
if err := record.Hydrate(to); err != nil {
return err
}
migrate = append(migrate, to)
}
if err := g.InsertMany(migrate); err != nil {
return err
}
return nil
}
func (g *mockdb) Config() Config {
return g.config
}
func (g *mockdb) configure(cfg Config) {
if len(cfg.ConnectionName) == 0 {
cfg.ConnectionName = defaultConnectionName
}
g.config = cfg
}
func (g *mockdb) Sync() error {
return nil
}
func (g *mockdb) RegisterModel(dataset string, m Model) bool {
return true
}