forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_handler_test.go
More file actions
104 lines (92 loc) · 2.16 KB
/
list_handler_test.go
File metadata and controls
104 lines (92 loc) · 2.16 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
99
100
101
102
103
104
package uadmin
import (
"net/http"
"net/http/httptest"
"testing"
)
// TestListHandler is a unit testing function for listHandler() function
func TestListHandler(t *testing.T) {
// Setup
s1 := &Session{
Active: true,
UserID: 1,
}
s1.GenerateKey()
s1.Save()
Preload(s1)
u1 := &User{
Username: "u1",
Password: "u1",
Active: true,
}
u1.Save()
s2 := &Session{
UserID: u1.ID,
Active: true,
}
s2.GenerateKey()
s2.Save()
Preload(s2)
m1 := TestModelB{
Name: "Testing",
ItemCount: 13,
Phone: "+18005551234",
Active: true,
OtherModelID: 0,
ModelAList: []TestModelA{},
ParentID: 0,
Email: "uadmin@example.com",
Greeting: "Hello uAdmin",
Image: "",
File: "",
Secret: "1234",
Description: "<p>This is the description of this fields</p>",
URL: "/",
Code: "Code good code in here",
P1: 50,
P2: 60.0,
P3: 0.5,
P4: 0.2,
P5: 0.8,
P6: 0.4,
Price: 100.0,
List: testList(1),
}
Save(&m1)
var w *httptest.ResponseRecorder
examples := []struct {
r *http.Request
s *Session
code int
f func(*ModelSchema, *User) (string, []interface{})
}{
{httptest.NewRequest("GET", "/testmodelb/", nil), s1, 200, nil},
{httptest.NewRequest("GET", "/testmodelb/", nil), s2, 404, nil},
{httptest.NewRequest("GET", "/invalidmodel/", nil), s1, 404, nil},
{httptest.NewRequest("GET", "/testmodelb/", nil), s1, 200, func(s *ModelSchema, u *User) (string, []interface{}) {
return "", []interface{}{}
}},
}
// Store the ListModifier
f := Schema["testmodelb"].ListModifier
// Run examples
for _, e := range examples {
schema := Schema["testmodelb"]
schema.ListModifier = e.f
Schema["testmodelb"] = schema
w = httptest.NewRecorder()
listHandler(w, e.r, e.s)
if w.Code != e.code {
t.Errorf("listHandler returned wrong code. Expected: %d, got %d", e.code, w.Code)
}
}
// Reset the ListModifier func to nil
schema := Schema["testmodelb"]
schema.ListModifier = f
Schema["testmodelb"] = schema
// Clean up
Delete(s1)
Delete(s2)
Delete(u1)
Delete(m1)
}