forked from bluele/gforms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nullbooleanfield_test.go
96 lines (90 loc) · 2.25 KB
/
nullbooleanfield_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
package gforms
import (
"net/http"
"net/url"
"strings"
"testing"
)
type testNullBooleanObject struct {
Check bool `gforms:"check"`
}
func TestTrueNullBooleanField(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"check": {"true"}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
v, ok := form.CleanedData["check"]
if !ok {
t.Error(`"check" is required.`)
return
}
_, ok = v.(bool)
if !ok {
t.Error(`"check" should be boolean type.`)
return
}
obj := new(testNullBooleanObject)
form.MapTo(obj)
if obj.Check == false {
t.Error(`"obj.Check" should not be false.`)
}
} else {
t.Error("validation error.")
}
}
func TestFalseNullBooleanField(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"check": {"false"}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
v, ok := form.CleanedData["check"]
if !ok {
t.Error(`"check" is required.`)
return
}
_, ok = v.(bool)
if !ok {
t.Error(`"check" should be boolean type.`)
return
}
obj := new(testNullBooleanObject)
form.MapTo(obj)
if obj.Check == false {
t.Error(`"obj.Check" should be false.`)
}
} else {
t.Error("validation error.")
}
}
func TestFalseNullBooleanFieldEmpty(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
_, ok := form.CleanedData["check"]
if ok {
t.Error(`"check" should not exist.`)
return
}
}
}
func TestTrueNullBooleanFieldJsonRequired(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", Validators{Required()}),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader("{}"))
req.Header.Add("Content-Type", "application/json")
form := Form(req)
if form.IsValid() {
t.Error("Null boolean field should be required.")
}
}