-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
date_test.go
71 lines (57 loc) · 1.57 KB
/
date_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
package datatypes_test
import (
"bytes"
"encoding/gob"
"encoding/json"
"testing"
"time"
"github.com/jinzhu/now"
"gorm.io/datatypes"
. "gorm.io/gorm/utils/tests"
)
func TestDate(t *testing.T) {
type UserWithDate struct {
ID uint
Name string
Date datatypes.Date
}
DB.Migrator().DropTable(&UserWithDate{})
if err := DB.Migrator().AutoMigrate(&UserWithDate{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
curTime := time.Now().UTC()
beginningOfDay := now.New(curTime).BeginningOfDay()
user := UserWithDate{Name: "jinzhu", Date: datatypes.Date(curTime)}
DB.Create(&user)
result := UserWithDate{}
if err := DB.First(&result, "name = ? AND date = ?", "jinzhu", datatypes.Date(curTime)).Error; err != nil {
t.Fatalf("Failed to find record with date")
}
AssertEqual(t, result.Date, beginningOfDay)
}
func TestGobEncoding(t *testing.T) {
date := datatypes.Date(time.Now())
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(date); err != nil {
t.Fatalf("failed to encode datatypes.Date: %v", err)
}
dec := gob.NewDecoder(&buf)
var got datatypes.Date
if err := dec.Decode(&got); err != nil {
t.Fatalf("failed to decode to datatypes.Date: %v", err)
}
AssertEqual(t, date, got)
}
func TestJSONEncoding(t *testing.T) {
date := datatypes.Date(time.Now())
b, err := json.Marshal(date)
if err != nil {
t.Fatalf("failed to encode datatypes.Date: %v", err)
}
var got datatypes.Date
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("failed to decode to datatypes.Date: %v", err)
}
AssertEqual(t, date, got)
}