forked from danielkurniadi/clickhouse
-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
create_test.go
87 lines (69 loc) · 2.56 KB
/
create_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
package clickhouse_test
import (
"slices"
"testing"
"gorm.io/gorm/utils/tests"
)
func TestCreate(t *testing.T) {
user := User{ID: 1, Name: "create", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 8.8888, Attrs: map[string]string{
"a": "a",
"b": "b",
}}
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)
type partialUser struct {
Name string
}
var partialResult partialUser
if err := DB.Raw("select * from users where id = ?", user.ID).Scan(&partialResult).Error; err != nil {
t.Fatalf("failed to query partial, got error %v", err)
}
var names []string
if err := DB.Select("name").Model(&User{}).Find(&names).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
if !slices.Contains(names, user.Name) {
t.Errorf("name should be included in the result")
}
}
func TestBatchCreate(t *testing.T) {
users := []User{
{ID: 11, Name: "batch_create_1", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 6},
{ID: 12, Name: "batch_create_2", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: false, Salary: 6.12},
{ID: 13, Name: "batch_create_3", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 6.1234},
{ID: 14, Name: "batch_create_4", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: false, Salary: 6.123456},
}
if err := DB.Create(&users).Error; err != nil {
t.Fatalf("failed to create users, got error %v", err)
}
var results []User
DB.Find(&results)
for _, u := range users {
var result User
if err := DB.Find(&result, u.ID).Error; err != nil {
t.Fatalf("failed to query user, got error %v", err)
}
tests.AssertEqual(t, result, u)
}
}
func TestCreateWithMap(t *testing.T) {
user := User{ID: 122, Name: "create2", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 6.6666}
if err := DB.Table("users").Create(&map[string]interface{}{
"id": user.ID, "name": user.Name, "first_name": user.FirstName, "last_name": user.LastName, "age": user.Age, "active": user.Active, "salary": user.Salary,
}).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)
}
user.CreatedAt = result.CreatedAt
user.UpdatedAt = result.UpdatedAt
tests.AssertEqual(t, result, user)
}