forked from go-gorm/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_test.go
66 lines (51 loc) · 2.03 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
package clickhouse_test
import (
"testing"
"gorm.io/gorm/utils/tests"
)
func TestCreate(t *testing.T) {
var user = User{ID: 1, Name: "create", 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)
}
func TestBatchCreate(t *testing.T) {
var 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) {
var 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)
}