-
Notifications
You must be signed in to change notification settings - Fork 6
/
types_test.go
108 lines (86 loc) · 2.05 KB
/
types_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
102
103
104
105
106
107
108
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package orm_test
import (
"testing"
"time"
"github.com/issue9/assert/v4"
"github.com/issue9/orm/v6"
"github.com/issue9/orm/v6/internal/test"
)
var (
_ orm.Engine = &orm.DB{}
_ orm.Engine = &orm.Tx{}
)
type beforeObject1 struct {
ID int64 `orm:"name(id);ai"`
Name string `orm:"name(name);len(24)"`
}
type beforeObject2 struct {
ID int64 `orm:"name(id);ai"`
Name string `orm:"name(name);len(24)"`
}
var (
_ orm.BeforeInserter = &beforeObject1{}
_ orm.BeforeUpdater = &beforeObject1{}
)
func (o *beforeObject1) TableName() string { return "objects1" }
func (o *beforeObject1) BeforeInsert() error {
o.Name = "insert-" + o.Name
return nil
}
func (o *beforeObject1) BeforeUpdate() error {
o.Name = "update-" + o.Name
return nil
}
var (
_ orm.BeforeInserter = &beforeObject1{}
_ orm.BeforeUpdater = &beforeObject1{}
)
func (o *beforeObject2) TableName() string { return "objects2" }
func (o *beforeObject2) BeforeInsert() error {
o.Name = "insert-" + o.Name
return nil
}
func (o *beforeObject2) BeforeUpdate() error {
o.Name = "update-" + o.Name
return nil
}
func TestBeforeCreateUpdate(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
// create
t.NotError(t.DB.Create(&beforeObject1{}))
defer func() {
t.NotError(t.DB.Drop(&beforeObject1{}))
}()
// insert
o := &beforeObject1{Name: "name1"}
_, err := t.DB.Insert(o)
t.NotError(err)
o = &beforeObject1{ID: 1}
found, err := t.DB.Select(o)
t.NotError(err).True(found)
t.Equal(o.Name, "insert-name1")
// update
o = &beforeObject1{ID: 1, Name: "name11"}
_, err = t.DB.Update(o)
t.NotError(err)
o = &beforeObject1{ID: 1}
found, err = t.DB.Select(o)
t.NotError(err).True(found)
t.Equal(o.Name, "update-name11")
})
}
func TestNow(t *testing.T) {
a := assert.New(t, false)
now := time.Now()
n1 := orm.NowUnix()
a.True(n1.Time.After(now)).
False(n1.Valid)
n2 := orm.NowNullTime()
a.True(n2.Time.After(now)).
True(n2.Valid)
}