-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestSession.go
242 lines (198 loc) · 6.56 KB
/
testSession.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
package aranGoDriver
import (
"fmt"
"strconv"
"time"
"github.com/TobiEiss/aranGoDriver/models"
"errors"
"math/rand"
"encoding/json"
"github.com/TobiEiss/aranGoDriver/sliceTricks"
"github.com/fatih/structs"
)
type TestSession struct {
database map[string]map[string][]map[string]interface{}
aqlFakes map[string][]byte
}
type AqlFake struct {
MapResult []interface{}
}
func NewTestSession() *TestSession {
// database - collection - list of document (key, value)
testSession := &TestSession{make(map[string]map[string][]map[string]interface{}), make(map[string][]byte)}
testSession.database[systemDB] = make(map[string][]map[string]interface{})
return testSession
}
func findByParam(session *TestSession, dbname string, keyName string, valueV string) *map[string]interface{} {
for _, collection := range session.database[dbname] {
for _, entry := range collection {
for key, value := range entry {
if key == keyName && value == valueV {
return &entry
}
}
}
}
return nil
}
func objectToMap(object interface{}) map[string]interface{} {
var entryAsMap map[string]interface{}
switch item := object.(type) {
case map[string]interface{}:
entryAsMap = item
default:
entryAsMap = structs.Map(object)
}
return entryAsMap
}
// Connect test
func (session TestSession) Connect(username string, password string) error {
fmt.Println("Connect to DB")
return nil
}
func (session *TestSession) Query(typ interface{}, methode string, route string, body interface{}) error {
return errors.New("not implemented..")
}
// Version returns hard-codeed the testDB as Version
func (session *TestSession) Version() (Version, error) {
return Version{License: "testDB", Server: "testDB"}, nil
}
func (session *TestSession) CreateUser(username string, password string) error {
return errors.New("not implemented..")
}
func (session *TestSession) DropUser(username string) error {
return errors.New("not implemented..")
}
func (session *TestSession) GrantDB(dbname string, username string, level string) error {
return errors.New("not implemented..")
}
func (session *TestSession) GrantCollection(dbname string, collectionName string, username string, level string) error {
return errors.New("not implemented..")
}
func (session *TestSession) ListDBs() ([]string, error) {
databases := []string{}
for key := range session.database {
databases = append(databases, key)
}
return databases, nil
}
func (session *TestSession) ListCollections(dbname string) ([]string, error) {
//TODO
return nil, nil
}
// CreateDB test create a db
func (session *TestSession) CreateDB(dbname string) error {
_, ok := session.database[dbname]
if ok {
return errors.New("DB already exists")
}
session.database[dbname] = make(map[string][]map[string]interface{})
return nil
}
func (session *TestSession) DropDB(dbname string) error {
delete(session.database, dbname)
return nil
}
func (session *TestSession) CreateCollection(dbname string, collectionName string) error {
_, ok := session.database[dbname]
if !ok {
return errors.New("DB doesnt")
}
session.database[dbname][collectionName] = make([]map[string]interface{}, 10)
return nil
}
func (session *TestSession) CreateEdgeCollection(dbname string, edgeName string) error {
return errors.New("not implemented..")
}
func (session *TestSession) CreateEdgeDocument(dbname string, edgeName string, from string, to string) (models.ArangoID, error) {
return models.ArangoID{}, errors.New("not implemented...")
}
func (session *TestSession) CreateGraph(dbname string, graphName string, edgeDefinitions []models.EdgeDefinition) error {
return errors.New("not implemented..")
}
func (session *TestSession) ListGraphs(dbname string) (interface{}, error) {
return nil, errors.New("not implemented..")
}
func (session *TestSession) DropGraph(dbname string, graphName string) error {
return errors.New("not implemented..")
}
func (session *TestSession) DropCollection(dbname string, collectionName string) error {
_, ok := session.database[dbname]
if !ok {
return errors.New("DB doesnt")
}
delete(session.database[dbname], collectionName)
return nil
}
func (session *TestSession) TruncateCollection(dbname string, collectionName string) error {
session.database[dbname][collectionName] = make([]map[string]interface{}, 10)
return nil
}
func (session *TestSession) CreateDocument(dbname string, collectionName string, object interface{}) (models.ArangoID, error) {
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
arangoID := models.ArangoID{
ID: timestamp,
Key: strconv.FormatInt(rand.Int63(), 10),
Rev: "",
}
// create entry
entry := structs.Map(arangoID)
for key, value := range objectToMap(object) {
entry[key] = value
}
// "persist"
session.database[dbname][collectionName] = append(session.database[dbname][collectionName], entry)
return arangoID, nil
}
func (session *TestSession) AqlQuery(typ interface{}, dbname string, query string, count bool, batchSize int) error {
if session.aqlFakes[query] != nil {
return json.Unmarshal(session.aqlFakes[query], &typ)
}
return errors.New("fakes are empty")
}
func (session *TestSession) AddAqlFake(aql string, fake AqlFake) {
bytes, _ := json.Marshal(fake.MapResult)
session.aqlFakes[aql] = bytes
}
func (session *TestSession) GetCollectionByID(dbname string, id string) (map[string]interface{}, error) {
if entry := findByParam(session, dbname, "_id", id); entry != nil {
_, err := json.Marshal(entry)
return (*entry), err
}
return nil, errors.New("Cant find id")
}
func (session *TestSession) UpdateDocument(dbname string, id string, object interface{}) error {
if entry := findByParam(session, dbname, "_id", id); entry != nil {
for key, value := range objectToMap(object) {
(*entry)[key] = value
}
}
return nil
}
func (tsession *TestSession) Migrate(migrations ...Migration) error {
if list, _ := tsession.ListDBs(); !sliceTricks.Contains(list, migrationColl) {
tsession.database[systemDB][migrationColl] = make([]map[string]interface{}, 20)
}
// helper function
findMigration := func(name string) map[string]interface{} {
if dbMigrations := tsession.database[systemDB][migrationColl]; len(systemDB) > 0 {
for _, mig := range dbMigrations {
if str, ok := mig["name"]; ok && str == name {
return mig
}
}
}
return nil
}
// transform to session
var session Session = tsession
// iterate all migrations
for _, mig := range migrations {
if findMigration(mig.Name) == nil {
mig.Handle(session)
mig.Status = Finished
tsession.database[systemDB][migrationColl] = append(tsession.database[systemDB][migrationColl], structs.Map(mig))
}
}
return nil
}