-
Notifications
You must be signed in to change notification settings - Fork 19
/
field_test.go
85 lines (82 loc) · 1.75 KB
/
field_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
package genmai
import (
"reflect"
"testing"
"time"
)
func TestTimeStamp_BeforeInsert(t *testing.T) {
createdAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 22:36:56")
if err != nil {
t.Fatal(err)
}
updatedAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 23:51:26")
if err != nil {
t.Fatal(err)
}
n, err := time.Parse("2006-01-02 15:04:05", "2000-02-02 16:57:38")
if err != nil {
t.Fatal(err)
}
baknow := now
now = func() time.Time {
return n
}
defer func() {
now = baknow
}()
tm := &TimeStamp{
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
if err := tm.BeforeInsert(); err != nil {
t.Fatal(err)
}
actual := tm.CreatedAt
expected := n
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
actual = tm.UpdatedAt
expected = n
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}
func TestTimeStamp_BeforeUpdate(t *testing.T) {
createdAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 22:36:56")
if err != nil {
t.Fatal(err)
}
updatedAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 23:51:26")
if err != nil {
t.Fatal(err)
}
n, err := time.Parse("2006-01-02 15:04:05", "2000-02-02 16:57:38")
if err != nil {
t.Fatal(err)
}
baknow := now
now = func() time.Time {
return n
}
defer func() {
now = baknow
}()
tm := &TimeStamp{
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
if err := tm.BeforeUpdate(); err != nil {
t.Fatal(err)
}
actual := tm.CreatedAt
expected := createdAt
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
actual = tm.UpdatedAt
expected = n
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}