Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed May 31, 2020
1 parent befef0c commit a02cb39
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
// db.Find(&users).Pluck("age", &ages)
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.AddClause(clause.Select{Columns: []clause.Column{{Name: column}}})
tx.Statement.AddClauseIfNotExists(clause.Select{Columns: []clause.Column{{Name: column}}})
tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx)
return
Expand Down
43 changes: 41 additions & 2 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package tests_test

import (
"reflect"
"sort"
"strconv"
"testing"
"time"

. "github.com/jinzhu/gorm/tests"
)
Expand Down Expand Up @@ -81,6 +83,24 @@ func TestFind(t *testing.T) {
}
}

func TestFillSmallerStruct(t *testing.T) {
user := User{Name: "SmallerUser", Age: 100}
DB.Save(&user)
type SimpleUser struct {
Name string
ID int64
UpdatedAt time.Time
CreatedAt time.Time
}

var simpleUser SimpleUser
if err := DB.Table("users").Where("name = ?", user.Name).First(&simpleUser).Error; err != nil {
t.Fatalf("Failed to query smaller user, got error %v", err)
}

AssertObjEqual(t, user, simpleUser, "Name", "ID", "UpdatedAt", "CreatedAt")
}

func TestPluck(t *testing.T) {
users := []*User{
GetUser("pluck-user1", Config{}),
Expand All @@ -92,12 +112,12 @@ func TestPluck(t *testing.T) {

var names []string
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name").Pluck("name", &names).Error; err != nil {
t.Errorf("Raise error when pluck name, got %v", err)
t.Errorf("got error when pluck name: %v", err)
}

var ids []int
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name").Pluck("id", &ids).Error; err != nil {
t.Errorf("Raise error when pluck id, got %v", err)
t.Errorf("got error when pluck id: %v", err)
}

for idx, name := range names {
Expand All @@ -112,3 +132,22 @@ func TestPluck(t *testing.T) {
}
}
}

func TestPluckWithSelect(t *testing.T) {
users := []User{
{Name: "pluck_with_select_1", Age: 25},
{Name: "pluck_with_select_2", Age: 26},
}

DB.Create(&users)

var userAges []int
err := DB.Model(&User{}).Where("name like ?", "pluck_with_select%").Select("age + 1 as user_age").Pluck("user_age", &userAges).Error
if err != nil {
t.Fatalf("got error when pluck user_age: %v", err)
}

sort.Ints(userAges)

AssertEqual(t, userAges, []int{26, 27})
}

0 comments on commit a02cb39

Please sign in to comment.