forked from danielkurniadi/clickhouse
-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
update_test.go
130 lines (103 loc) · 4.76 KB
/
update_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
package clickhouse_test
import (
"regexp"
"testing"
"time"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
"gorm.io/gorm/utils/tests"
)
func TestUpdateLocalTable(t *testing.T) {
updateLocalTable := clickhouse.UpdateLocalTable{Suffix: "_local"}
for k, v := range map[string]string{
"alter table hello_world.hello_world2 update id=1": "alter table hello_world.hello_world2_local update id=1",
"Alter table `hello_world`.hello_world2 update id=1": "Alter table `hello_world`.hello_world2_local update id=1",
"ALTER TABLE hello_world.`hello_world2` update id=1": "ALTER TABLE hello_world.`hello_world2_local` update id=1",
"alter TABLE `hello_world`.`hello_world2` update id=1": "alter TABLE `hello_world`.`hello_world2_local` update id=1",
"ALTER TABLE `users` UPDATE `name`=?,`updated_at`=? WHERE `id` = ?": "ALTER TABLE `users_local` UPDATE `name`=?,`updated_at`=? WHERE `id` = ?",
} {
if updateLocalTable.ModifySQL(k) != v {
t.Errorf("failed to update sql, expect: %v, got %v", v, updateLocalTable.ModifySQL(k))
}
}
updateLocalTable = clickhouse.UpdateLocalTable{Prefix: "local_"}
for k, v := range map[string]string{
"alter table hello_world.hello_world2 update id=1": "alter table hello_world.local_hello_world2 update id=1",
"alter table `hello_world`.hello_world2 update id=1": "alter table `hello_world`.local_hello_world2 update id=1",
"alter table hello_world.`hello_world2` update id=1": "alter table hello_world.`local_hello_world2` update id=1",
"alter table `hello_world`.`hello_world2` update id=1": "alter table `hello_world`.`local_hello_world2` update id=1",
} {
if updateLocalTable.ModifySQL(k) != v {
t.Errorf("failed to update sql, expect: %v, got %v", v, updateLocalTable.ModifySQL(k))
}
}
updateLocalTable = clickhouse.UpdateLocalTable{Table: "local_table"}
for k, v := range map[string]string{
"alter table hello_world.hello_world2 update id=1": "alter table hello_world.local_table update id=1",
"ALTER table `hello_world`.hello_world2 update id=1": "ALTER table `hello_world`.local_table update id=1",
"alter table hello_world.`hello_world2` update id=1": "alter table hello_world.`local_table` update id=1",
"ALTER TABLE `hello_world`.`hello_world2` update id=1": "ALTER TABLE `hello_world`.`local_table` update id=1",
} {
if updateLocalTable.ModifySQL(k) != v {
t.Errorf("failed to update sql, expect: %v, got %v", v, updateLocalTable.ModifySQL(k))
}
}
}
func TestUpdate(t *testing.T) {
user := User{ID: 3, Name: "update", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 8.8888}
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("failed to create user, got error %v", err)
}
var result User
if err := DB.Find(&result, user.ID).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
tests.AssertEqual(t, result, user)
if err := DB.Model(&result).Update("name", "update-1").Error; err != nil {
t.Fatalf("failed to update user, got error %v", err)
}
time.Sleep(200 * time.Millisecond)
var result2 User
if err := DB.First(&result2, user.ID).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
user.Name = "update-1"
tests.AssertEqual(t, result2, user)
sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Clauses(clickhouse.UpdateLocalTable{Suffix: "_local"}).Model(&result).Update("name", "update-1")
})
if !regexp.MustCompile("`users_local`").MatchString(sql) {
t.Errorf("Table with namer, got %v", sql)
}
}
func TestUpdateWithMap(t *testing.T) {
user := User{ID: 33, Name: "update2", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 8.8888}
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("failed to create user, got error %v", err)
}
var result User
if err := DB.Find(&result, user.ID).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
tests.AssertEqual(t, result, user)
if err := DB.Table("users").Where("id = ?", user.ID).Update("name", "update-2").Error; err != nil {
t.Fatalf("failed to update user, got error %v", err)
}
time.Sleep(200 * time.Millisecond)
var result2 User
if err := DB.First(&result2, user.ID).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
user.Name = "update-2"
tests.AssertEqual(t, result2, user)
if err := DB.Table("users").Where("id = ?", user.ID).Updates(map[string]interface{}{"name": "update-3"}).Error; err != nil {
t.Fatalf("failed to update user, got error %v", err)
}
time.Sleep(200 * time.Millisecond)
var result3 User
if err := DB.First(&result3, user.ID).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
user.Name = "update-3"
tests.AssertEqual(t, result3, user)
}