This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdatetimefield_test.go
101 lines (89 loc) · 2.54 KB
/
datetimefield_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
package gforms
import (
"net/http"
"net/url"
"strings"
"testing"
"time"
)
type testDateTimeObject struct {
Date time.Time `gforms:"date"`
}
func TestInputValidDateFormat(t *testing.T) {
Form := DefineForm(NewFields(
NewDateTimeField("date", DefaultDateFormat, nil),
))
reqDate := "2014-06-18"
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"date": {reqDate}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if !form.IsValid() {
t.Error("validation error.")
return
}
v, ok := form.CleanedData["date"]
if !ok {
t.Error(`"date" is required.`)
}
_, ok = v.(time.Time)
if !ok {
t.Error(`"date" should be time.Time type.`)
return
}
obj := new(testDateTimeObject)
form.MapTo(obj)
if obj.Date.Year() != 2014 || obj.Date.Month() != 6 || obj.Date.Day() != 18 {
t.Error("exptected: " + reqDate)
}
}
func TestInputInValidDateFormat(t *testing.T) {
Form := DefineForm(NewFields(
NewDateTimeField("date", DefaultDateFormat, nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"date": {"20140618"}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
t.Error("expected: validation error.")
return
}
}
func TestCustomDateFormat(t *testing.T) {
Form := DefineForm(NewFields(
NewDateTimeField("date", "2006/01/02", nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"date": {"2014/06/18"}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if !form.IsValid() {
t.Error("not expected: validation error.")
return
}
}
func TestInputValidDateTimeFormat(t *testing.T) {
Form := DefineForm(NewFields(
NewDateTimeField("date", DefaultDateTimeFormat, nil),
))
reqDate := "2014-06-18 05:14:16"
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"date": {reqDate}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if !form.IsValid() {
t.Error("validation error.")
return
}
v, ok := form.CleanedData["date"]
if !ok {
t.Error(`"date" is required.`)
}
_, ok = v.(time.Time)
if !ok {
t.Error(`"date" should be time.Time type.`)
return
}
obj := new(testDateTimeObject)
form.MapTo(obj)
if obj.Date.Year() != 2014 || obj.Date.Month() != 6 || obj.Date.Day() != 18 || obj.Date.Hour() != 5 || obj.Date.Minute() != 14 || obj.Date.Second() != 16 {
t.Error("given: " + obj.Date.String() + ", exptected: " + DefaultDateTimeFormat)
}
}