forked from mirror/gorm
Add more tests
This commit is contained in:
parent
befef0c9a9
commit
a02cb39a45
|
@ -273,7 +273,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
|
||||||
// db.Find(&users).Pluck("age", &ages)
|
// db.Find(&users).Pluck("age", &ages)
|
||||||
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
||||||
tx = db.getInstance()
|
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.Statement.Dest = dest
|
||||||
tx.callbacks.Query().Execute(tx)
|
tx.callbacks.Query().Execute(tx)
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,8 +2,10 @@ package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
. "github.com/jinzhu/gorm/tests"
|
. "github.com/jinzhu/gorm/tests"
|
||||||
)
|
)
|
||||||
|
@ -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) {
|
func TestPluck(t *testing.T) {
|
||||||
users := []*User{
|
users := []*User{
|
||||||
GetUser("pluck-user1", Config{}),
|
GetUser("pluck-user1", Config{}),
|
||||||
|
@ -92,12 +112,12 @@ func TestPluck(t *testing.T) {
|
||||||
|
|
||||||
var names []string
|
var names []string
|
||||||
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name").Pluck("name", &names).Error; err != nil {
|
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
|
var ids []int
|
||||||
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name").Pluck("id", &ids).Error; err != nil {
|
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 {
|
for idx, name := range names {
|
||||||
|
@ -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})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue