forked from istreamdata/orientgo
/
main_test.go
82 lines (66 loc) · 1.81 KB
/
main_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
package main
import (
"os"
"testing"
"github.com/quux00/ogonori/constants"
"github.com/quux00/ogonori/obinary"
)
var DocumentDBSeeds = []string{
"CREATE CLASS Animal",
"CREATE property Animal.name string",
"CREATE property Animal.age integer",
"CREATE CLASS Cat extends Animal",
"CREATE property Cat.caretaker string",
"INSERT INTO Cat (name, age, caretaker) VALUES ('Linus', 15, 'Michael'), ('Keiko', 10, 'Anna')",
}
func SetUp(db *obinary.DBClient) {
if err := obinary.CreateDatabase(db, dbDocumentName, constants.DocumentDB, constants.Persistent); err != nil {
panic(err)
}
if err := obinary.CreateDatabase(db, dbGraphName, constants.GraphDB, constants.Persistent); err != nil {
panic(err)
}
}
func TearDown(db *obinary.DBClient) {
obinary.DropDatabase(db, dbDocumentName, constants.DocumentDB)
obinary.DropDatabase(db, dbGraphName, constants.GraphDB)
}
func Seed() {
db := ConnectToDocumentDatabase()
defer db.Close()
var err error
for _, seed := range DocumentDBSeeds {
if _, _, err = obinary.SQLCommand(db, seed); err != nil {
panic(err) // Programming error
}
}
}
func CleanupSeed() {
documentDB := ConnectToDocumentDatabase()
defer documentDB.Close()
obinary.SQLCommand(documentDB, "TRUNCATE Animal")
obinary.SQLCommand(documentDB, "TRUNCATE Cat")
graphDB := ConnectToGraphDatabase()
defer graphDB.Close()
obinary.SQLCommand(graphDB, "TRUNCATE Person")
obinary.SQLCommand(graphDB, "TRUNCATE Friend")
}
// Runs before all tests
func TestMain(m *testing.M) {
db, _ := NewDB()
defer db.Close()
// Test DB connection
if err := obinary.ConnectToServer(db, dbUser, dbPass); err != nil {
panic(err)
}
// Clean up tests database
TearDown(db)
// Set up test graph & document databases
SetUp(db)
Seed()
// Run Tests
ret := m.Run()
// Clean up tests database
TearDown(db)
os.Exit(ret)
}