-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_test.go
More file actions
98 lines (81 loc) · 1.75 KB
/
Copy pathsetup_test.go
File metadata and controls
98 lines (81 loc) · 1.75 KB
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
package admin_test
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/media_library"
"github.com/qor/qor"
"github.com/qor/qor/test/utils"
_ "github.com/mattn/go-sqlite3"
)
type CreditCard struct {
gorm.Model
UserID uint
Number string
Issuer string
}
type Address struct {
gorm.Model
UserID uint
Address1 string
Address2 string
}
type Language struct {
gorm.Model
Name string
}
type User struct {
gorm.Model
Name string
Age uint
Role string
Active bool
RegisteredAt time.Time
Avatar media_library.FileSystem
CreditCard CreditCard
Addresses []Address
Languages []Language `gorm:"many2many:user_languages;"`
Profile Profile
}
type Profile struct {
gorm.Model
UserID uint
Name string
Sex string
Phone Phone
}
type Phone struct {
gorm.Model
ProfileID uint64
Num string
}
var (
server *httptest.Server
db *gorm.DB
Admin *admin.Admin
)
func init() {
mux := http.NewServeMux()
db = utils.TestDB()
models := []interface{}{&User{}, &CreditCard{}, &Address{}, &Language{}, &Profile{}, &Phone{}}
for _, value := range models {
db.DropTableIfExists(value)
db.AutoMigrate(value)
}
Admin = admin.New(&qor.Config{DB: db})
user := Admin.AddResource(&User{})
user.Meta(&admin.Meta{Name: "Languages", Type: "select_many",
Collection: func(resource interface{}, context *qor.Context) (results [][]string) {
if languages := []Language{}; !context.GetDB().Find(&languages).RecordNotFound() {
for _, language := range languages {
results = append(results, []string{fmt.Sprint(language.ID), language.Name})
}
}
return
}})
Admin.MountTo("/admin", mux)
server = httptest.NewServer(mux)
}