Skip to content

Commit

Permalink
test scan into slice
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 20, 2024
1 parent 065c557 commit 0f1c09d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
16 changes: 13 additions & 3 deletions create_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package clickhouse_test

import (
"slices"
"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}
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)
Expand All @@ -27,10 +28,19 @@ func TestCreate(t *testing.T) {
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) {
var users = []User{
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},
Expand All @@ -55,7 +65,7 @@ func TestBatchCreate(t *testing.T) {
}

func TestCreateWithMap(t *testing.T) {
var user = User{ID: 122, Name: "create2", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 6.6666}
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,
Expand Down
4 changes: 2 additions & 2 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestDelete(t *testing.T) {
var user = User{ID: 2, Name: "delete", FirstName: "zhang", LastName: "jinzhu", Age: 18, Active: true, Salary: 8.8888}
user := User{ID: 2, Name: "delete", 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)
Expand All @@ -25,7 +25,7 @@ func TestDelete(t *testing.T) {
t.Fatalf("failed to delete user, got error %v", err)
}

time.Sleep(200 * time.Millisecond)
time.Sleep(500 * time.Millisecond)
if err := DB.First(&result, user.ID).Error; err == nil {
t.Fatalf("should raise ErrRecordNotFound, got error %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ services:
- CLICKHOUSE_USER=gorm
- CLICKHOUSE_PASSWORD=gorm
ports:
- "9941:8123"
- "9942:9000"
- "9943:9009"
- 9941:8123
- 9942:9000
- 9943:9009

0 comments on commit 0f1c09d

Please sign in to comment.