-
Notifications
You must be signed in to change notification settings - Fork 2
/
aranGoDriver_test.go
181 lines (150 loc) · 4.83 KB
/
aranGoDriver_test.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
package aranGoDriver_test
import (
"log"
"testing"
"flag"
"github.com/TobiEiss/aranGoDriver"
"github.com/TobiEiss/aranGoDriver/models"
"github.com/TobiEiss/aranGoDriver/sliceTricks"
)
var (
testDbHost = flag.String("dbhost", "", "run database integration test")
testUsername = flag.String("dbusername", "testUser", "username of test-user")
testPassword = flag.String("dbpassword", "password123", "password for test-user")
testDbName = flag.String("dbtestdbname", "testDB", "database name of test-database")
testCollName = flag.String("dbtestcollname", "testColl", "collection name of test-collection")
)
func TestMain(t *testing.T) {
flag.Parse()
t.Log("Start tests")
var session aranGoDriver.Session
// check the flag database
if *testDbHost != "" {
t.Log("use arangoDriver")
session = aranGoDriver.NewAranGoDriverSession(*testDbHost)
} else {
t.Log("use testDriver")
testSession := aranGoDriver.NewTestSession()
// fake
testDoc := make([]interface{}, 1)
testMap := make(map[string]interface{})
testMap["foo"] = "bar"
testMap["_id"] = "userid"
testDoc[0] = testMap
fake1 := aranGoDriver.AqlFake{
MapResult: testDoc,
}
testSession.AddAqlFake("FOR element in testColl FILTER element.foo == 'bar' RETURN element", fake1)
session = testSession
}
// Connect
session.Connect(*testUsername, *testPassword)
// DropDB
err := session.DropDB(*testDbName)
t.Log(err)
// Check listDB
list, err := session.ListDBs()
t.Log(err)
assertTrue(!sliceTricks.Contains(list, *testDbName))
t.Log(list)
// CreateDB
err = session.CreateDB(*testDbName)
list, err = session.ListDBs()
t.Log(session.ListDBs())
assertTrue(sliceTricks.Contains(list, *testDbName))
// DropDB
err = session.DropDB(*testDbName)
list, err = session.ListDBs()
t.Log(session.ListDBs())
assertTrue(!sliceTricks.Contains(list, *testDbName))
// CreateDB
err = session.CreateDB(*testDbName)
list, err = session.ListDBs()
t.Log(session.ListDBs())
assertTrue(sliceTricks.Contains(list, *testDbName))
// Create collection
err = session.CreateCollection(*testDbName, *testCollName)
// Truncate collection
err = session.TruncateCollection(*testDbName, *testCollName)
// Create Document
testMap := make(map[string]interface{})
testMap["foo"] = "bar"
if *testDbHost == "" {
testMap["_id"] = "userid"
}
_, err = session.CreateDocument(*testDbName, *testCollName, testMap)
failOnError(err, "failed while creating Document")
// session.AqlQuery
query := "FOR element in testColl FILTER element.foo == 'bar' RETURN element"
results := []map[string]interface{}{}
err = session.AqlQuery(&results, *testDbName, query, true, 1)
failOnError(err, "AQL-Query")
assertTrue(len(results) > 0)
t.Log(results)
// search for ID
id, ok := results[0]["_id"].(string)
if !ok {
t.Error("Cant find a key in result")
}
result, err := session.GetCollectionByID(*testDbName, id)
id2, ok := result["_id"].(string)
if !ok || (id != id2) {
t.Error("id's arent the same")
}
// Update document
result["bar"] = "foo"
err = session.UpdateDocument(*testDbName, result["_id"].(string), result)
failOnError(err, "failed while update document")
// Verify
result, err = session.GetCollectionByID(*testDbName, result["_id"].(string))
assertTrue(result["bar"] == "foo")
// Create graph
// Only implemented for real database
if *testDbHost != "" {
graph_test_collection := "gcol"
graph_test_edge_collection := "gcol"
session.CreateCollection(*testDbName, graph_test_collection)
session.CreateEdgeCollection(*testDbName, graph_test_edge_collection)
graphName := "testgraph"
edgeDefinition := models.EdgeDefinition{
Collection: graph_test_edge_collection,
From: []string{graph_test_collection},
To: []string{graph_test_collection}}
edgeDefinitions := []models.EdgeDefinition{edgeDefinition}
err = session.CreateGraph(*testDbName, graphName, edgeDefinitions)
failOnError(err, "failed creating a graph")
_, err = session.ListGraphs(*testDbName)
failOnError(err, "failed listing all graphs")
err = session.DropGraph(*testDbName, graphName)
failOnError(err, "failed dropping a graph")
}
// check migrations
mig1 := aranGoDriver.Migration{
Name: "mig1",
Handle: func(embeddedSession aranGoDriver.Session) {
testMap := make(map[string]interface{})
testMap["foo"] = "foo"
embeddedSession.CreateDocument(*testDbName, *testCollName, testMap)
},
}
// excute migration
session.Migrate(mig1)
// Drop collection
err = session.DropCollection(*testDbName, *testCollName)
failOnError(err, "Cant drop collection")
// DropDB
err = session.DropDB(*testDbName)
list, err = session.ListDBs()
t.Log(session.ListDBs())
assertTrue(!sliceTricks.Contains(list, *testDbName))
}
func assertTrue(test bool) {
if !test {
panic("Assertion failed")
}
}
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}