-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
uuid_test.go
175 lines (158 loc) · 4.59 KB
/
uuid_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package datatypes_test
import (
"database/sql/driver"
"testing"
"github.com/google/uuid"
"gorm.io/datatypes"
"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)
var _ driver.Valuer = &datatypes.UUID{}
func TestUUID(t *testing.T) {
if SupportedDriver("sqlite", "mysql", "postgres", "sqlserver") {
type UserWithUUID struct {
gorm.Model
Name string
UserUUID datatypes.UUID
}
DB.Migrator().DropTable(&UserWithUUID{})
if err := DB.Migrator().AutoMigrate(&UserWithUUID{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
users := []UserWithUUID{{
Name: "uuid-1",
UserUUID: datatypes.NewUUIDv1(),
}, {
Name: "uuid-2",
UserUUID: datatypes.NewUUIDv1(),
}, {
Name: "uuid-3",
UserUUID: datatypes.NewUUIDv4(),
}, {
Name: "uuid-4",
UserUUID: datatypes.NewUUIDv4(),
}}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
for _, user := range users {
result := UserWithUUID{}
if err := DB.First(
&result, "name = ? AND user_uuid = ?",
user.Name,
user.UserUUID,
).Error; err != nil {
t.Fatalf("failed to find user with uuid, got error: %v", err)
}
AssertEqual(t, !result.UserUUID.IsEmpty(), true)
AssertEqual(t, user.UserUUID.Equals(result.UserUUID), true)
valueUser, err := user.UserUUID.Value()
if err != nil {
t.Fatalf("failed to get user value, got error: %v", err)
}
valueResult, err := result.UserUUID.Value()
if err != nil {
t.Fatalf("failed to get result value, got error: %v", err)
}
AssertEqual(t, valueUser, valueResult)
AssertEqual(t, user.UserUUID.Length(), 36)
}
var tx *gorm.DB
user1 := users[0]
AssertEqual(t, user1.UserUUID.IsNil(), false)
AssertEqual(t, user1.UserUUID.IsEmpty(), false)
tx = DB.Model(&user1).Updates(
map[string]interface{}{"user_uuid": uuid.Nil},
)
AssertEqual(t, tx.Error, nil)
AssertEqual(t, user1.UserUUID.IsNil(), true)
AssertEqual(t, user1.UserUUID.IsEmpty(), true)
user1NewUUID := datatypes.NewUUIDv4()
tx = DB.Model(&user1).Updates(
map[string]interface{}{
"user_uuid": user1NewUUID,
},
)
AssertEqual(t, tx.Error, nil)
AssertEqual(t, user1.UserUUID, user1NewUUID)
user2 := users[1]
AssertEqual(t, user2.UserUUID.IsNil(), false)
AssertEqual(t, user2.UserUUID.IsEmpty(), false)
tx = DB.Model(&user2).Updates(
map[string]interface{}{"user_uuid": nil},
)
AssertEqual(t, tx.Error, nil)
AssertEqual(t, user2.UserUUID.IsNil(), true)
AssertEqual(t, user2.UserUUID.IsEmpty(), true)
user2NewUUID := datatypes.NewUUIDv4()
tx = DB.Model(&user2).Updates(
map[string]interface{}{
"user_uuid": user2NewUUID,
},
)
AssertEqual(t, tx.Error, nil)
AssertEqual(t, user2.UserUUID, user2NewUUID)
}
}
func TestUUIDPtr(t *testing.T) {
if SupportedDriver("sqlite", "mysql", "postgres", "sqlserver") {
type UserWithUUIDPtr struct {
gorm.Model
Name string
UserUUID *datatypes.UUID
}
DB.Migrator().DropTable(&UserWithUUIDPtr{})
if err := DB.Migrator().AutoMigrate(&UserWithUUIDPtr{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
uuid1 := datatypes.NewUUIDv1()
uuid2 := datatypes.NewUUIDv1()
uuid3 := datatypes.NewUUIDv4()
uuid4 := datatypes.NewUUIDv4()
users := []UserWithUUIDPtr{{
Name: "uuid-1",
UserUUID: &uuid1,
}, {
Name: "uuid-2",
UserUUID: &uuid2,
}, {
Name: "uuid-3",
UserUUID: &uuid3,
}, {
Name: "uuid-4",
UserUUID: &uuid4,
}}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
for _, user := range users {
result := UserWithUUIDPtr{}
if err := DB.First(
&result, "name = ? AND user_uuid = ?",
user.Name,
*user.UserUUID,
).Error; err != nil {
t.Fatalf("failed to find user with uuid, got error: %v", err)
}
AssertEqual(t, !result.UserUUID.IsEmpty(), true)
AssertEqual(t, user.UserUUID, result.UserUUID)
valueUser, err := user.UserUUID.Value()
if err != nil {
t.Fatalf("failed to get user value, got error: %v", err)
}
valueResult, err := result.UserUUID.Value()
if err != nil {
t.Fatalf("failed to get result value, got error: %v", err)
}
AssertEqual(t, valueUser, valueResult)
AssertEqual(t, user.UserUUID.Length(), 36)
}
user1 := users[0]
AssertEqual(t, user1.UserUUID.IsNilPtr(), false)
AssertEqual(t, user1.UserUUID.IsEmptyPtr(), false)
tx := DB.Model(&user1).Updates(map[string]interface{}{"user_uuid": nil})
AssertEqual(t, tx.Error, nil)
AssertEqual(t, user1.UserUUID.IsNilPtr(), true)
AssertEqual(t, user1.UserUUID.IsEmptyPtr(), true)
}
}