forked from go-gorm/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
80 lines (58 loc) · 2.08 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
package clickhouse_test
import (
"testing"
"time"
"gorm.io/gorm/utils/tests"
)
func TestUpdate(t *testing.T) {
var 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)
}
func TestUpdateWithMap(t *testing.T) {
var 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)
}