-
Notifications
You must be signed in to change notification settings - Fork 7
/
time_test.go
95 lines (75 loc) · 2.43 KB
/
time_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
package nullable_test
import (
"testing"
"time"
"github.com/Thor-x86/nullable"
"gorm.io/gorm/utils/tests"
)
func TestScanTime(t *testing.T) {
nullableTime := nullable.NewTime(nil)
basicTime := time.Now()
nullableTime.Scan(basicTime.Local())
tests.AssertEqual(t, nullableTime.Get().Unix(), basicTime.Unix())
nullableTime.Scan(basicTime.UTC())
tests.AssertEqual(t, nullableTime.Get().Unix(), basicTime.Unix())
nullableTime.Scan(nil)
tests.AssertEqual(t, nullableTime.Get(), nil)
}
func TestNewTime(t *testing.T) {
basicTime1 := time.Now()
nullableTime1 := nullable.NewTime(&basicTime1)
tests.AssertEqual(t, nullableTime1.Get().Unix(), basicTime1.Unix())
nullableTime2 := nullable.NewTime(nil)
tests.AssertEqual(t, nullableTime2.Get(), nil)
}
func TestSetTime(t *testing.T) {
nullableTime := nullable.NewTime(nil)
tests.AssertEqual(t, nullableTime.Get(), nil)
basicTime1 := time.Now()
nullableTime.Set(&basicTime1)
tests.AssertEqual(t, nullableTime.Get().Unix(), basicTime1.Unix())
nullableTime.Set(nil)
tests.AssertEqual(t, nullableTime.Get(), nil)
}
func TestJSONTime(t *testing.T) {
basicTime := time.Now()
marshalUnmarshalJSON(t, nullable.NewTime(&basicTime))
marshalUnmarshalJSON(t, nullable.NewTime(nil))
}
func TestTime(t *testing.T) {
type TestNullableTime struct {
UserID uint `gorm:"primaryKey"`
BookName string
BorrowedAt time.Time
ReturnedAt nullable.Time
}
DB.Migrator().DropTable(&TestNullableTime{})
if err := DB.Migrator().AutoMigrate(&TestNullableTime{}); err != nil {
t.Errorf("failed to migrate nullable time, got error: %v", err)
}
userReturn1 := time.Now().Add(-7 * 24 * time.Hour)
user1 := TestNullableTime{
UserID: 777,
BookName: "How To Mating like Cats",
BorrowedAt: time.Now().Add(-14 * 24 * time.Hour),
ReturnedAt: nullable.NewTime(&userReturn1),
}
DB.Create(&user1)
user2 := TestNullableTime{
UserID: 234,
BookName: "Return this or DIE",
BorrowedAt: time.Now().Add(-7 * 24 * time.Hour),
ReturnedAt: nullable.NewTime(nil),
}
DB.Create(&user2)
var result1 TestNullableTime
if err := DB.First(&result1, "user_id = ?", 777).Error; err != nil {
t.Errorf("failed to migrate nullable time, got error: %v", err)
}
tests.AssertEqual(t, result1, user1)
var result2 TestNullableTime
if err := DB.First(&result2, "user_id = ?", 234).Error; err != nil {
t.Errorf("failed to migrate nullable time, got error: %v", err)
}
tests.AssertEqual(t, result2, user2)
}