-
Notifications
You must be signed in to change notification settings - Fork 4
/
common_test.go
112 lines (86 loc) · 1.89 KB
/
common_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
package goal_test
import (
"flag"
"fmt"
"net/http/httptest"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/sessions"
"github.com/jinzhu/gorm"
"github.com/thomasdao/goal"
)
var server *httptest.Server
type testuser struct {
ID uint `gorm:"primary_key"`
Username string
Password string
Name string
Age int
Rev int64
}
type article struct {
ID uint `gorm:"primary_key"`
Author *testuser
Title string
goal.Permission
}
var db *gorm.DB
var (
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
)
func setup() {
var err error
db, err = gorm.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
db.SingularTable(true)
// Setup database
goal.InitGormDb(db)
// Setup redis
pool := redis.NewPool(func() (redis.Conn, error) {
c, err := redis.Dial("tcp", *redisAddress)
if err != nil {
return nil, err
}
return c, err
}, *maxConnections)
redisCache := &goal.RedisCache{}
err = redisCache.InitRedisPool(pool)
if err == nil {
goal.RegisterCacher(redisCache)
}
// Initialize API
api := goal.NewAPI()
// Initialize resource
models := []interface{}{&testuser{}, &article{}}
// Add default path
for _, model := range models {
goal.RegisterModel(model)
}
user := &testuser{}
goal.SetUserModel(user)
api.AddDefaultAuthPaths(user)
store := sessions.NewCookieStore([]byte("something-very-secret"))
goal.InitSessionStore(store)
// Setup testing server
server = httptest.NewServer(api.Mux())
}
func tearDown() {
if server != nil {
server.Close()
}
if goal.DB() != nil {
db.Close()
}
if goal.Pool() != nil {
goal.RedisClearAll()
goal.Pool().Close()
}
}
func userURL() string {
return fmt.Sprint(server.URL, "/testuser")
}
func idURL(id interface{}) string {
return fmt.Sprint(server.URL, "/testuser/", id)
}